Android BitmapShader setLocalMatrix缩放Bitmap高度重新onMeasure,Kotlin

Android BitmapShader setLocalMatrix缩放Bitmap高度重新onMeasure,Kotlin

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"/>

</LinearLayout>
Kotlin 复制代码
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.BitmapShader
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.Shader
import android.graphics.drawable.PaintDrawable
import android.os.Bundle
import android.util.AttributeSet
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatImageView


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

class MyImageView : AppCompatImageView {
    private var mBitmapShader: BitmapShader? = null
    private var mDrawable: PaintDrawable? = null
    private val mx = Matrix()
    private val mFactor = 3.5f //对于原始图的放大系数。
    private var mBmp: Bitmap? = null

    constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) {
        mBmp = BitmapFactory.decodeResource(resources, R.mipmap.mypic)
        mBitmapShader = BitmapShader(
            mBmp!!,
            Shader.TileMode.DECAL,
            Shader.TileMode.DECAL
        )

        //对于原始图放大。
        mx.setScale(mFactor, mFactor)

        mDrawable = PaintDrawable(Color.BLACK)
        mDrawable!!.paint.shader = mBitmapShader
        mDrawable!!.paint.shader.setLocalMatrix(mx)
        mDrawable!!.setBounds(0, 0, mBmp!!.width * mFactor.toInt(), mBmp!!.height * mFactor.toInt())
    }

    //测量高度。
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)

        var myHeight = 0
        myHeight = if (heightMode == MeasureSpec.EXACTLY) { //在xml精确设置值或者为match_parent
            heightSize
        } else {
            //如果是wrap_content,则需要重新设置高度,否则onDraw不会"draw"显示。
            mBmp!!.height * mFactor.toInt()
        }

        this.setMeasuredDimension(widthMeasureSpec, myHeight)
    }

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

        mDrawable!!.draw(canvas)
    }
}

Android Matrix绘制PaintDrawable设置BitmapShader,手指触点为圆心scale放大原图,Kotlin(二)-CSDN博客文章浏览阅读676次,点赞11次,收藏6次。遗留问题,手指在上图滑动过程中,当滑动到一定区域,下面的切图框中已无太有效的图可以"放大",后续可以填充黑色,表示无效放大。所有的绘制轨迹线,都限定在了绿色的圆角矩形框中,超出区域不予绘制。基础上,限定下面切图的绘制区域,超出绿色区域的轨迹线不再绘制。https://blog.csdn.net/zhangphil/article/details/135601993

Android自定义ViewGroup:onMeasure与onLayout(1)_android viewgroup onmeasure onlayout-CSDN博客文章浏览阅读2.9k次。Android自定义ViewGroup:onMeasure与onLayout(1)Android自定义一个ViewGroup,需要重写ViewGrouo里面的两个最重要的回调函数onMeasure()与onLayout()。如果开发者自己摆脱Android为我们做好的几套布局(如常见的线1性布局、相对布局、帧布局等等),往底层实现view呈现,那么我们就得在ViewGroup中小心计算_android viewgroup onmeasure onlayouthttps://blog.csdn.net/zhangphil/article/details/51191567 Android横竖屏切换View设置不同尺寸或等比例缩放的自定义View的onMeasure解决方案(2)_安卓自定义view,竖屏切到横屏时宽度不变化-CSDN博客文章浏览阅读3.9k次。Android横竖屏切换View设置不同尺寸或等比例缩放的自定义View的onMeasure解决方案(2)附录文章1以xml布局文件方式实现了一个view在横竖屏切换时候的大小尺寸缩放,实现这种需求,也可以使用自定义View的onMeasure方法实现。比如,写一个自定义的ScaleRelativeLayout相对布局:https://blog.csdn.net/zhangphil/article/details/73467857

相关推荐
南宫码农9 小时前
我的电视 - Android原生电视直播软件 完整使用教程
android·开发语言·windows·电视盒子
道亦无名10 小时前
音频数据特征值提取 方法和步骤
android·音视频
Lancker10 小时前
定制侠 一个国产纯血鸿蒙APP的诞生过程
android·华为·智能手机·鸿蒙·国产操作系统·纯血鸿蒙·华为鸿蒙
2601_9498095911 小时前
flutter_for_openharmony家庭相册app实战+通知设置实现
android·javascript·flutter
液态不合群12 小时前
【面试题】MySQL 中 count(*)、count(1) 和 count(字段名) 有什么区别?
android·数据库·mysql
雪球Snowball14 小时前
【Android关键流程】资源加载
android
2501_9159184114 小时前
常见 iOS 抓包工具的使用,从代理抓包、设备抓包到数据流抓包
android·ios·小程序·https·uni-app·iphone·webview
灯火不休ᝰ15 小时前
[kotlin] 从Java到Kotlin:掌握基础语法差异的跃迁指南
java·kotlin·安卓
墨月白15 小时前
[QT]QProcess的相关使用
android·开发语言·qt
enbug16 小时前
编译安卓内核:以坚果R1、魔趣MK100(Android 10)系统为例
android·linux