- package com.cc.rxandroid
-
- import android.content.Context
- import android.graphics.*
- import android.util.AttributeSet
- import android.view.View
-
-
- class MyView(c: Context, a: AttributeSet): View(c, a) {
-
- val paint = Paint()
-
- override fun onDraw(canvas: Canvas) {
- super.onDraw(canvas)
- setLayerType(LAYER_TYPE_SOFTWARE, null)
-
- paint.color = -0x33bc
- canvas.drawBitmap(makeDst(width, height), 0f, 0f, paint)
-
- paint.color = -0x995501
- paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
- canvas.drawBitmap(makeSrc(width, height), 0f, 0f, paint)
-
-
- paint.xfermode = null
- }
-
- fun makeDst(w: Int, h: Int): Bitmap {
- val bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
- val canvas = Canvas(bm)
- val p = Paint(Paint.ANTI_ALIAS_FLAG)
- p.color = -0x33bc
- canvas.drawOval(RectF(0f, 0f, w * 3f / 4, h * 3f / 4), p)
- return bm
- }
-
- fun makeSrc(w: Int, h: Int): Bitmap {
- val bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
- val canvas = Canvas(bm)
- val p = Paint(Paint.ANTI_ALIAS_FLAG)
- p.color = -0x995501
- canvas.drawRect(
- (w / 3).toFloat(), (h / 3).toFloat(), (w * 19 / 20).toFloat(),
- (h * 19 / 20).toFloat(), p
- )
- return bm
- }
- }
运行效果:
不使用xfermode
使用xfermode.scr_in后
官方效果示例:
有几个使用的点要注意下:
1.绘制的源图和新图必须是bitmap,才能有官方例子的效果。
2.要关闭硬件加速。