Android基于Matrix绘制PaintDrawable设置BitmapShader,以手指触点为中心显示原图像圆图,Kotlin(3)

Android基于Matrix绘制PaintDrawable设置BitmapShader,以手指触点为中心显示原图像圆图,Kotlin(3)

Android基于Matrix绘制PaintDrawable设置BitmapShader,以手指触点为中心显示原图像圆图,Kotlin(2)-CSDN博客文章浏览阅读313次,点赞4次,收藏3次。【代码】Android基于Matrix绘制PaintDrawable设置BitmapShader,以手指触点为中心显示原图像圆图,Kotlin(2)https://blog.csdn.net/zhangphil/article/details/135508123基础上,增加一个功能,手指在上面的图中移动时,绘制红色移动轨迹(路线)同时,下面图中对应的小图中显示手指与屏幕的触点,这样可以"实时"指示当前手指在上面大图中移动的准确、精细位置。下图中用红色圆圈图指示手指在上面图中的触点。

Kotlin 复制代码
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapShader
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.graphics.Shader.TileMode
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.PaintDrawable
import android.os.Bundle
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatImageView


class MainActivity : AppCompatActivity() {
    private var iv: MyImageView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        iv = findViewById(R.id.iv)

        val r = findViewById<ImageView>(R.id.result)
        iv?.setTestImageView(r)
    }
}

class MyImageView : AppCompatImageView {
    private var mCurX = 0
    private var mCurY = 0

    private val mPath = Path()
    private val mPath2 = Path()
    private val mPathPaint = Paint()

    private var mNewBmp: Bitmap? = null
    private var mSrcBmp: Bitmap? = null
    private var mIsDraw = false
    private val mRadius = 300f

    private var mDrawable: PaintDrawable? = null

    private var testIV: ImageView? = null

    constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) {
        mSrcBmp = (drawable as BitmapDrawable).bitmap

        mPathPaint.style = Paint.Style.STROKE
        mPathPaint.strokeWidth = 15f
        mPathPaint.isAntiAlias = true
        mPathPaint.color = Color.RED
    }

    fun setTestImageView(iv: ImageView?) {
        testIV = iv
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        mCurX = event.x.toInt()
        mCurY = event.y.toInt()

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                Log.d("fly", "开始绘制")

                mPath.moveTo(event.x, event.y)
                //mPath2.moveTo(event.x, event.y)

                mIsDraw = true
            }

            MotionEvent.ACTION_MOVE -> {
                mPath.lineTo(event.x, event.y)
                //mPath2.lineTo(event.x, event.y)
            }

            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                Log.d("fly", "不需绘制")
                mIsDraw = false

                //抬手后,清除手指轨迹。
                myClear()
            }
        }

        invalidate()

        return true
    }

    private fun myClear() {
        //清除历史轨迹。
        mPath.reset()
        mPath2.reset()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        if (mIsDraw) {
            myDraw()

            canvas.drawPath(mPath, mPathPaint)
        }
    }

    private fun myDraw() {
        val shader = BitmapShader(Bitmap.createScaledBitmap(mSrcBmp!!, this.width, this.height, true), TileMode.DECAL, TileMode.DECAL)
        mDrawable = PaintDrawable(Color.DKGRAY)
        mDrawable!!.setCornerRadius(mRadius / 2) //圆角矩形,如果不除2即是圆形框图。
        mDrawable!!.paint.shader = shader
        mDrawable!!.setBounds(0, 0, (mRadius * 2).toInt(), (mRadius * 2).toInt())

        mNewBmp = Bitmap.createBitmap(this.width, this.height, Bitmap.Config.ARGB_8888)
        val c = Canvas(mNewBmp!!)
        c.drawColor(Color.LTGRAY) //画满底色。

        val matrix = Matrix()
        matrix.setTranslate(-mCurX + mRadius, -mCurY + mRadius)
        mDrawable!!.paint.shader.setLocalMatrix(matrix)

        mDrawable!!.draw(c)

        //绘制下面图中的手指触点
        val rectF = RectF()
        matrix.mapRect(rectF)

        val cx = mCurX + rectF.left
        val cy = mCurY + rectF.top
        c.drawCircle(cx, cy, 30f, mPathPaint)


        testIV?.setImageBitmap(mNewBmp)
    }
}
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.pkg.MyImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@drawable/ic_launcher_background"
        android:scaleType="fitCenter"
        android:src="@mipmap/mypic" />

    <ImageView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@drawable/ic_launcher_background"
        android:src="@drawable/ic_launcher_foreground" />

</LinearLayout>

Android基于Matrix绘制PaintDrawable设置BitmapShader,以手指触点为中心显示原图像圆图,Kotlin(2)-CSDN博客文章浏览阅读313次,点赞4次,收藏3次。【代码】Android基于Matrix绘制PaintDrawable设置BitmapShader,以手指触点为中心显示原图像圆图,Kotlin(2)https://blog.csdn.net/zhangphil/article/details/135508123

相关推荐
望佑14 分钟前
Jetpack Compose 入门:从默认工程到实战开发
android
雨白16 分钟前
高阶函数的应用:简化 SharedPreferences 与 ContentValues 操作
kotlin
BoomHe18 分钟前
Android 搭建模块化项目流程及建议
android·架构·gradle
移动开发者1号1 小时前
Retrofit动态URL与Path参数处理
android·kotlin
移动开发者1号2 小时前
Android 中 OkHttp 的自定义 Interceptor 实现统一请求头添加
android·kotlin
进击的CJR2 小时前
MySQL 8.0 OCP 英文题库解析(十八)
android·mysql·开闭原则
进击的CJR2 小时前
MySQL 8.0 OCP 英文题库解析(十四)
android·mysql·开闭原则
奔跑吧 android3 小时前
【android bluetooth 框架分析 04】【bt-framework 层详解 5】【AbstractionLayer介绍】
android·framework·bluetooth·bt·gd·aosp13
Sca_杰3 小时前
android过渡动画
android·运维·nginx
fie88895 小时前
MySQL:Prepared Statement 预处理语句
android·数据库·mysql