Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何有效重画位图? 我画了一个视图,画出一个正方形,中间有一个“洞”来显示它下面的东西。还需要添加更改此孔大小的动画。以下是我的实现: class RoundMaskView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr), SizeChangeListener { companion object { private const val SHIFT_COEF = 0.3f } init { setLayerType(LAYER_TYPE_SOFTWARE, null) } private val squarePaint = Paint() private val squareBitmapPaint = Paint().apply { isAntiAlias = true style = Paint.Style.FILL color = ContextCompat.getColor(context, R.color.intro_color_background) } private val holeBitmapPaint = Paint().apply { isAntiAlias = true style = Paint.Style.FILL } private val holePaint = Paint().apply { xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT) } private var shift = 1f private val holeSquareSize = context.resources.getDimensionPixelSize(R.dimen.item_intro_size) get() = (field * shift).toInt() private var squareBitmap: Bitmap? = null private var holeBitmap: Bitmap? = null override fun onSizeChange(progress: Float) { shift = 1f - SHIFT_COEF * progress resizeHoleBitmap() invalidate() } override fun onDraw(canvas: Canvas) { if (squareBitmap == null) { initBitmaps() } canvas.drawBitmap(squareBitmap!!, 0f, 0f, squarePaint) canvas.drawBitmap( holeBitmap!!, (width / 2f) - holeSquareSize / 2f, (height / 2f) - holeSquareSize / 2f, holePaint ) } private fun initBitmaps() { squareBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).apply { Canvas(this).drawRect(Rect(0, 0, width, height), squareBitmapPaint) } holeBitmap = Bitmap.createBitmap(holeSquareSize, holeSquareSize, Bitmap.Config.ARGB_8888).apply { Canvas(this).drawCircle(holeSquareSize / 2f, holeSquareSize / 2f, holeSquareSize / 2f, holeBitmapPaint) } } private fun resizeHoleBitmap() { holeBitmap = Bitmap.createBitmap(holeSquareSize, holeSquareSize, Bitmap.Config.ARGB_8888).apply { Canvas(this).drawCircle(holeSquareSize / 2f, holeSquareSize / 2f, holeSquareSize / 2f, holeBitmapPaint) } } }_Android_Kotlin_Bitmap_Android Bitmap - Fatal编程技术网

Android 如何有效重画位图? 我画了一个视图,画出一个正方形,中间有一个“洞”来显示它下面的东西。还需要添加更改此孔大小的动画。以下是我的实现: class RoundMaskView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr), SizeChangeListener { companion object { private const val SHIFT_COEF = 0.3f } init { setLayerType(LAYER_TYPE_SOFTWARE, null) } private val squarePaint = Paint() private val squareBitmapPaint = Paint().apply { isAntiAlias = true style = Paint.Style.FILL color = ContextCompat.getColor(context, R.color.intro_color_background) } private val holeBitmapPaint = Paint().apply { isAntiAlias = true style = Paint.Style.FILL } private val holePaint = Paint().apply { xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT) } private var shift = 1f private val holeSquareSize = context.resources.getDimensionPixelSize(R.dimen.item_intro_size) get() = (field * shift).toInt() private var squareBitmap: Bitmap? = null private var holeBitmap: Bitmap? = null override fun onSizeChange(progress: Float) { shift = 1f - SHIFT_COEF * progress resizeHoleBitmap() invalidate() } override fun onDraw(canvas: Canvas) { if (squareBitmap == null) { initBitmaps() } canvas.drawBitmap(squareBitmap!!, 0f, 0f, squarePaint) canvas.drawBitmap( holeBitmap!!, (width / 2f) - holeSquareSize / 2f, (height / 2f) - holeSquareSize / 2f, holePaint ) } private fun initBitmaps() { squareBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).apply { Canvas(this).drawRect(Rect(0, 0, width, height), squareBitmapPaint) } holeBitmap = Bitmap.createBitmap(holeSquareSize, holeSquareSize, Bitmap.Config.ARGB_8888).apply { Canvas(this).drawCircle(holeSquareSize / 2f, holeSquareSize / 2f, holeSquareSize / 2f, holeBitmapPaint) } } private fun resizeHoleBitmap() { holeBitmap = Bitmap.createBitmap(holeSquareSize, holeSquareSize, Bitmap.Config.ARGB_8888).apply { Canvas(this).drawCircle(holeSquareSize / 2f, holeSquareSize / 2f, holeSquareSize / 2f, holeBitmapPaint) } } }

Android 如何有效重画位图? 我画了一个视图,画出一个正方形,中间有一个“洞”来显示它下面的东西。还需要添加更改此孔大小的动画。以下是我的实现: class RoundMaskView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr), SizeChangeListener { companion object { private const val SHIFT_COEF = 0.3f } init { setLayerType(LAYER_TYPE_SOFTWARE, null) } private val squarePaint = Paint() private val squareBitmapPaint = Paint().apply { isAntiAlias = true style = Paint.Style.FILL color = ContextCompat.getColor(context, R.color.intro_color_background) } private val holeBitmapPaint = Paint().apply { isAntiAlias = true style = Paint.Style.FILL } private val holePaint = Paint().apply { xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT) } private var shift = 1f private val holeSquareSize = context.resources.getDimensionPixelSize(R.dimen.item_intro_size) get() = (field * shift).toInt() private var squareBitmap: Bitmap? = null private var holeBitmap: Bitmap? = null override fun onSizeChange(progress: Float) { shift = 1f - SHIFT_COEF * progress resizeHoleBitmap() invalidate() } override fun onDraw(canvas: Canvas) { if (squareBitmap == null) { initBitmaps() } canvas.drawBitmap(squareBitmap!!, 0f, 0f, squarePaint) canvas.drawBitmap( holeBitmap!!, (width / 2f) - holeSquareSize / 2f, (height / 2f) - holeSquareSize / 2f, holePaint ) } private fun initBitmaps() { squareBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).apply { Canvas(this).drawRect(Rect(0, 0, width, height), squareBitmapPaint) } holeBitmap = Bitmap.createBitmap(holeSquareSize, holeSquareSize, Bitmap.Config.ARGB_8888).apply { Canvas(this).drawCircle(holeSquareSize / 2f, holeSquareSize / 2f, holeSquareSize / 2f, holeBitmapPaint) } } private fun resizeHoleBitmap() { holeBitmap = Bitmap.createBitmap(holeSquareSize, holeSquareSize, Bitmap.Config.ARGB_8888).apply { Canvas(this).drawCircle(holeSquareSize / 2f, holeSquareSize / 2f, holeSquareSize / 2f, holeBitmapPaint) } } },android,kotlin,bitmap,android-bitmap,Android,Kotlin,Bitmap,Android Bitmap,当我调用onSizeChange(进度)更改孔的大小(位图)时,您会看到什么。但每次调用后创建新的位图,我知道执行效果不佳。如何更正每次调用onSizeChange时位图大小的更改?如果您想绘制一个“中间有孔的正方形”,请使用Canvas.drawPath(path,paint)-其中path是一个路径对象-请注意@pskink,您认为我们可以不用“复杂的”xfermode=porterduffermode(porterduff.Mode.DST_OUT)和位图?确定:在middle@pskin

当我调用
onSizeChange(进度)
更改孔的大小(位图)时,您会看到什么。但每次调用后创建新的位图,我知道执行效果不佳。如何更正每次调用
onSizeChange
时位图大小的更改?

如果您想绘制一个“中间有孔的正方形”,请使用
Canvas.drawPath(path,paint)
-其中
path
是一个
路径
对象-请注意@pskink,您认为我们可以不用“复杂的”
xfermode=porterduffermode(porterduff.Mode.DST_OUT)
位图
?确定:在middle@pskink真的。我改变了我的解决方案,让它变得简单。Thxsure,不客气