Skip to content

Commit

Permalink
add option scale bitmap when drawing to canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
kineita committed Apr 28, 2020
1 parent 308b50e commit ef0172d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 35 deletions.
21 changes: 11 additions & 10 deletions opengl/src/main/java/jp/eita/canvasgl/CanvasGL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package jp.eita.canvasgl

import android.graphics.*
import android.opengl.GLES20
import androidx.annotation.FloatRange
import androidx.annotation.IntRange
import jp.eita.canvasgl.glcanvas.*
import jp.eita.canvasgl.glcanvas.GLCanvas.ICustomMVPMatrix
Expand All @@ -28,6 +29,7 @@ import jp.eita.canvasgl.textureFilter.BasicTextureFilter
import jp.eita.canvasgl.textureFilter.FilterGroup
import jp.eita.canvasgl.textureFilter.TextureFilter
import java.util.*
import kotlin.math.roundToInt

/**
* All the depth of textures are the same. So the texture drawn after will cover the texture drawn before.
Expand Down Expand Up @@ -181,6 +183,13 @@ class CanvasGL constructor(override val glCanvas: GLCanvas = GLES20Canvas()) : I
glCanvas.drawTexture(basicTexture, left, top, bitmap.width, bitmap.height, textureFilter, null)
}

override fun drawBitmap(bitmap: Bitmap, @FloatRange(from = 0.1, to = 2.0) scaleRatioBitmap: Float, left: Int, top: Int, textureFilter: TextureFilter) {
val basicTexture = getTexture(bitmap, textureFilter)
val newBitmapWidth: Int = (bitmap.width * scaleRatioBitmap).roundToInt()
val newBitmapHeight: Int = (bitmap.height * scaleRatioBitmap).roundToInt()
glCanvas.drawTexture(basicTexture, left, top, newBitmapWidth, newBitmapHeight, textureFilter, null)
}

override fun drawBitmap(bitmap: Bitmap, src: Rect?, dst: Rect?) {
drawBitmap(bitmap, src, RectF(dst))
}
Expand Down Expand Up @@ -323,18 +332,10 @@ class CanvasGL constructor(override val glCanvas: GLCanvas = GLES20Canvas()) : I
glCanvas.setSize(width, height)
}

// override fun setSize(scaleRatioSize: Float) {
//// this.width = (width * scaleRatioSize).toInt()
//// this.height = (height * scaleRatioSize).toInt()
//// glCanvas.setSize(this.width, this.height)
// }

override fun resume() {}
override fun resume() { }

override fun pause() {
if (currentTextureFilter != null) {
currentTextureFilter!!.destroy()
}
currentTextureFilter?.destroy()
}

override fun setAlpha(@IntRange(from = 0, to = 255) alpha: Int) {
Expand Down
4 changes: 4 additions & 0 deletions opengl/src/main/java/jp/eita/canvasgl/ICanvasGL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import android.graphics.Bitmap
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.SurfaceTexture
import androidx.annotation.FloatRange
import androidx.annotation.IntRange
import jp.eita.canvasgl.glcanvas.*
import jp.eita.canvasgl.matrix.IBitmapMatrix
Expand Down Expand Up @@ -56,6 +57,8 @@ interface ICanvasGL {

fun drawBitmap(bitmap: Bitmap, left: Int, top: Int, textureFilter: TextureFilter)

fun drawBitmap(bitmap: Bitmap, @FloatRange(from = 0.1, to = 2.0) scaleRatioBitmap: Float, left: Int, top: Int, textureFilter: TextureFilter)

fun drawBitmap(bitmap: Bitmap, src: Rect?, dst: Rect?)

fun drawBitmap(bitmap: Bitmap, src: RectF?, dst: RectF?, textureFilter: TextureFilter)
Expand Down Expand Up @@ -86,6 +89,7 @@ interface ICanvasGL {

fun rotate(degrees: Float, px: Float, py: Float)

// This function will scale size of canvas, not Object on Canvas.
fun scale(sx: Float, sy: Float)

fun scale(sx: Float, sy: Float, px: Float, py: Float)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ import jp.eita.canvasgl.ICanvasGL
import jp.eita.canvasgl.OpenGLUtil.setFloat
import jp.eita.canvasgl.glcanvas.BasicTexture

class PixelationFilter(@param:FloatRange(from = 1.0, to = 100.0) private var mPixel: Float) : BasicTextureFilter(), OneValueFilter {
class PixelationFilter(@param:FloatRange(from = 1.0, to = 100.0) private var pixel: Float) : BasicTextureFilter(), OneValueFilter {

private var mImageWidthFactorLocation = 0
private var imageWidthFactorLocation = 0

private var mImageHeightFactorLocation = 0
private var imageHeightFactorLocation = 0

private var mPixelLocation = 0
private var pixelLocation = 0

override val fragmentShader: String
get() = PIXELATION_FRAGMENT_SHADER

override fun onPreDraw(program: Int, texture: BasicTexture, canvas: ICanvasGL) {
super.onPreDraw(program, texture, canvas)
mImageWidthFactorLocation = GLES20.glGetUniformLocation(program, UNIFORM_IMAGE_WIDTH_FACTOR)
mImageHeightFactorLocation = GLES20.glGetUniformLocation(program, UNIFORM_IMAGE_HEIGHT_FACTOR)
mPixelLocation = GLES20.glGetUniformLocation(program, UNIFORM_PIXEL)
setFloat(mImageWidthFactorLocation, 1.0f / texture.width)
setFloat(mImageHeightFactorLocation, 1.0f / texture.height)
setFloat(mPixelLocation, mPixel)
imageWidthFactorLocation = GLES20.glGetUniformLocation(program, UNIFORM_IMAGE_WIDTH_FACTOR)
imageHeightFactorLocation = GLES20.glGetUniformLocation(program, UNIFORM_IMAGE_HEIGHT_FACTOR)
pixelLocation = GLES20.glGetUniformLocation(program, UNIFORM_PIXEL)
setFloat(imageWidthFactorLocation, 1.0f / texture.width)
setFloat(imageHeightFactorLocation, 1.0f / texture.height)
setFloat(pixelLocation, pixel)
}

override fun setValue(@FloatRange(from = 1.0, to = 100.0) value: Float) {
mPixel = value
pixel = value
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import jp.eita.canvasgl.BitmapUtils
import jp.eita.canvasgl.OpenGLUtil
import jp.eita.canvasgl.textureFilter.*
import jp.eita.example.R
import jp.eita.example.structure.ScaleRatioList
import jp.eita.example.bubble.model.Bubble
import jp.eita.example.structure.AlphaList
import jp.eita.example.structure.ScaleRatioList
import kotlinx.android.synthetic.main.activity_opengl.*
import java.util.*
import kotlin.collections.ArrayList
Expand All @@ -48,7 +48,7 @@ class OpenGLActivity : AppCompatActivity() {
}

init {
Handler().postDelayed(runnable, 150)
// Handler().postDelayed(runnable, 150)
}

private fun loopChangingPropertiesBubbles() {
Expand Down Expand Up @@ -112,11 +112,12 @@ class OpenGLActivity : AppCompatActivity() {
}

private fun initFilterList(filterList: MutableList<TextureFilter>) {
filterList.add(BasicTextureFilter())
filterList.add(ContrastFilter(1.6f))
filterList.add(SaturationFilter(1.6f))
filterList.add(PixelationFilter(12F))
filterList.add(HueFilter(100F))
filterList.add(PixelationFilter(1f))
// filterList.add(ContrastFilter(1.6f))
// filterList.add(SaturationFilter(1.6f))
// filterList.add(PixelationFilter(12F))
// filterList.add(HueFilter(100F))
// filterList.add(CropFilter(120.0f, 120.0f, 120.0f, 120.0f))
}

private fun setUpButtonLike() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ class Bubble : MovableCollisionObject {
val left = (point.x - bitmap.width / 2f).toInt()
val top = (point.y - bitmap.height / 2f).toInt()
canvas.rotate(rotateDegree, point.x, point.y)
canvas.scale(scaleSizeRatio, scaleSizeRatio, scaleSizeRatio, scaleSizeRatio)
canvas.setAlpha(alpha)
canvas.drawBitmap(bitmap, left, top, textureFilter!!)
canvas.drawBitmap(bitmap, scaleSizeRatio, left, top, textureFilter!!)
canvas.restore()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ class AlphaList(initialCapacity: Int) : ArrayList<Int>(initialCapacity) {
class Detail(value: Int) {

private var rangeAlpha = intArrayOf(
value,
(value * 0.8).toInt(),
(value * 0.7).toInt(),
(value * 0.4).toInt(),
(value * 0.2).toInt(),
(value * 0.4).toInt(),
(value * 0.6).toInt(),
(value * 0.7).toInt(),
(value * 0.8).toInt(),
value,
(value * 0.8).toInt(),
(value * 0.6).toInt(),
(value * 0.4).toInt(),
(value * 0.2).toInt()
value
)

private var crawlerAlpha: Int = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ class ScaleRatioList(initialCapacity: Int) : ArrayList<Float>(initialCapacity) {

private var rangeScaleRatio: FloatArray = floatArrayOf(
value * 1.0f,
value * 0.9f,
value * 0.8f,
value * 0.7f,
value * 0.6f,
value * 0.5f,
value * 0.4f,
value * 0.4f,
value * 0.5f,
value * 0.6f,
value * 0.7f,
value * 0.8f,
value * 1.0f
)
Expand Down

0 comments on commit ef0172d

Please sign in to comment.