当前位置: 首页 > news >正文

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

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

 

在 https://zhangphil.blog.csdn.net/article/details/135374279 基础上,增加一个功能,当手指在上面的图片上滑动时候,显示滑动轨迹:

1deb0cfc742e4016acae8e98cc819466.png

 

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.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.AppCompatImageViewclass MainActivity : AppCompatActivity() {private var iv: MyImageView? = nulloverride 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 = 0private var mCurY = 0private val mPath = Path()private val mPathPaint = Paint()private var mNewBmp: Bitmap? = nullprivate var mSrcBmp: Bitmap? = nullprivate var mIsDraw = falseprivate val mRadius = 300fprivate var mDrawable: PaintDrawable? = nullprivate var testIV: ImageView? = nullconstructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) {mSrcBmp = (drawable as BitmapDrawable).bitmapmPathPaint.style = Paint.Style.STROKEmPathPaint.strokeWidth = 15fmPathPaint.isAntiAlias = truemPathPaint.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)mIsDraw = true}MotionEvent.ACTION_MOVE -> {mPath.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()}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 = shadermDrawable!!.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)testIV?.setImageBitmap(mNewBmp)}
}

 

 

 

<?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.MyImageViewandroid: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" /><ImageViewandroid: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>

 

 

 

https://zhangphil.blog.csdn.net/article/details/135374279https://zhangphil.blog.csdn.net/article/details/135374279

 

http://www.lryc.cn/news/279125.html

相关文章:

  • FlinkOnYarn 监控 flink任务
  • C++内存管理机制(侯捷)笔记1
  • 【论文阅读】Non-blocking Lazy Schema Changes in Multi-Version
  • Rust 最新版1.75.0升级记
  • 使用 KubeSphere 与极狐GitLab 打造云原生持续交付系统
  • EasyExcel的追加写入(新增POI、CSV)
  • JetBrains 开发工具——免费教育许可申请流程
  • 打造高性价比小程序,轻松降低成本
  • mysql 索引优化查询
  • 跟着cherno手搓游戏引擎【4】窗口抽象、GLFW配置
  • Tomcat基础升华学习
  • 一种具有轨迹优化的无人驾驶车实时运动规划器 论文阅读
  • GPDB - 高可用 - 流复制状态
  • 最佳解决方案:如何在网络爬虫中解决验证码
  • 在线项目实习分享:股票价格形态聚类与收益分析
  • c# vb.net检测字符串是否匹配一组相似度数组input Like
  • DEJA_VU3D - Cesium功能集 之 113-获取圆节点(2)
  • spring-boot项目启动类错误: 找不到或无法加载主类 com.**Application
  • 搭建大数据开发环境【AutoDL容器】
  • 写一个简单的Java的Gui文本输入窗口,JFrame的简单使用
  • Unity中URP下抓屏的 开启 和 使用
  • 业务题day01
  • DEJA_VU3D - Cesium功能集 之 114-雷达效果(基础效果)
  • 【Leetcode】2696. 删除子串后的字符串最小长度
  • 利用gulp工具对常规web项目进行压缩打包
  • 面试经典题---68.文本左右对齐
  • 完整的模型验证套路
  • 内 存 取 证
  • 【PHP】价格区间字段验证,如4万-5万
  • 安徽省暨合肥市“希望工程·梦想计划”小盖茨机器人捐赠启动仪式举行