Android挖取原图中心区域RectF(并框线标记)放大到ImageView宽高,Kotlin

本文主要是介绍Android挖取原图中心区域RectF(并框线标记)放大到ImageView宽高,Kotlin,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android挖取原图中心区域RectF(并框线标记)放大到ImageView宽高,Kotlin

 

ea989779d6ab486380f290492e1b1938.png

红色线框区域即为选中的原图中心区域,放大后放到等宽高的ImageView里面。

 

 

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
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.os.Bundle
import android.util.AttributeSet
import android.util.Log
import android.util.SizeF
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import androidx.core.graphics.toRectclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val miv = findViewById<MyImageView>(R.id.iv)val iv = findViewById<ImageView>(R.id.result)miv.setResultImage(iv)}
}class MyImageView : AppCompatImageView {private var W = 0private var H = 0private val mSizeF = SizeF(400f, 200f)private var mOriginBmp: Bitmap? = nullprivate var mPaint = Paint()private var result: ImageView? = nullconstructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) {mPaint.style = Paint.Style.STROKEmPaint.strokeWidth = 5fmPaint.isAntiAlias = truemPaint.color = Color.REDmOriginBmp = getOriginalBitmap(ctx, R.mipmap.image)Log.d("fly", "origin bmp w=${mOriginBmp!!.width} h=${mOriginBmp!!.height}")}private fun getOriginalBitmap(ctx: Context, resId: Int): Bitmap {val options = BitmapFactory.Options()options.inJustDecodeBounds = true //只解析原始图片的宽高,不decode原始文件装载到内存的Bitmap。BitmapFactory.decodeResource(resources, resId, options)//这一阶段,最关键的是获取原始图的真实宽高。val srcBmpWidth = options.outWidthval srcBmpHeight = options.outHeightval d = ContextCompat.getDrawable(ctx, resId)//根据原始图片的宽高创建一个空的Bitmapval bitmap = Bitmap.createBitmap(srcBmpWidth, srcBmpHeight, Bitmap.Config.ARGB_8888)val canvas = Canvas(bitmap)d?.setBounds(0, 0, srcBmpWidth, srcBmpHeight)d?.draw(canvas) //至此,bitmap即为原始图片。return bitmap}fun setResultImage(iv: ImageView) {result = iv}override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {super.onSizeChanged(w, h, oldw, oldh)W = wH = hLog.d("fly", "W=$W H=$H")}//从原始的Bitmap图中抠出一块SizeF大小的图。private fun getCenterBmp(): Bitmap {val bmp = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888)val c = Canvas(bmp)val dstRectF = RectF(0f, 0f, bmp.width.toFloat(), bmp.height.toFloat())val cx = mOriginBmp!!.width / 2fval cy = mOriginBmp!!.height / 2fval centerRectF = RectF(cx - mSizeF.width / 2f, cy - mSizeF.height / 2f, cx + mSizeF.width / 2f, cy + mSizeF.height / 2f)c.drawBitmap(mOriginBmp!!, centerRectF.toRect(), dstRectF, null)return bmp}override fun onDraw(canvas: Canvas) {super.onDraw(canvas)//绘制中心圆圈。mPaint.color = Color.YELLOWcanvas.drawCircle(W / 2f, H / 2f, 40f, mPaint)drawRoundRectLine(canvas)result?.setImageBitmap(getCenterBmp())}private fun drawRoundRectLine(canvas: Canvas) {val lineRectF = RectF(0f,0f,mSizeF.width,mSizeF.height)//原始图被Android系统拉伸放到屏幕上,所以lineRectF也需要进行相同的拉伸。val originBmpSizeMapMatrix = Matrix()originBmpSizeMapMatrix.setScale(getOriginBmpScaleToImageViewFactor(), getOriginBmpScaleToImageViewFactor())originBmpSizeMapMatrix.mapRect(lineRectF)//注意移动到中心位置,ImageView的中心位置。lineRectF.offset(W / 2f - lineRectF.width() / 2f, H / 2f - lineRectF.height() / 2f)//绘制红色的lineRectF线框。val path = Path()path.addRoundRect(lineRectF, 20f, 20f, Path.Direction.CW)mPaint.color = Color.REDcanvas.drawPath(path, mPaint)}private fun getOriginBmpScaleToImageViewFactor(): Float {return (W.toFloat()) / (mOriginBmp!!.width.toFloat())}
}

 

 

<?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:scaleType="fitCenter"android:src="@mipmap/image" /><ImageViewandroid:id="@+id/result"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>

 

 

 

Android矩阵setRectToRect裁剪Bitmap原图Matrix放大,mapRect标记中心区域,Kotlin-CSDN博客文章浏览阅读419次,点赞3次,收藏6次。【代码】Android矩阵setRectToRect裁剪Bitmap原图Matrix放大,mapRect标记中心区域,Kotlin。https://blog.csdn.net/zhangphil/article/details/135960921

Android BitmapFactory.decodeResource读取原始图片装载成原始宽高Bitmap,Kotlin_bitmapfactory解码宽高-CSDN博客文章浏览阅读853次。文章浏览阅读1.8k次。/*Java代码 将Drawable转化为Bitmap */ Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth();Android Drawable 转化成 Bitmap-CSDN博客。_bitmapfactory解码宽高https://blog.csdn.net/zhangphil/article/details/134449577

 

这篇关于Android挖取原图中心区域RectF(并框线标记)放大到ImageView宽高,Kotlin的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/730254

相关文章

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos

Nacos注册中心和配置中心的底层原理全面解读

《Nacos注册中心和配置中心的底层原理全面解读》:本文主要介绍Nacos注册中心和配置中心的底层原理的全面解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录临时实例和永久实例为什么 Nacos 要将服务实例分为临时实例和永久实例?1.x 版本和2.x版本的区别

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

SpringCloud使用Nacos 配置中心实现配置自动刷新功能使用

《SpringCloud使用Nacos配置中心实现配置自动刷新功能使用》SpringCloud项目中使用Nacos作为配置中心可以方便开发及运维人员随时查看配置信息,及配置共享,并且Nacos支持配... 目录前言一、Nacos中集中配置方式?二、使用步骤1.使用$Value 注解2.使用@Configur