Android矩阵Matrix在1张宽平大Bitmap批量绘制N个小Bitmap,Kotlin(1)
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.RectF
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivityclass MainActivity : AppCompatActivity() {companion object {const val TAG = "fly"const val IMG_SIZE = 200const val IMG_COUNT = 5}override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val image5 = findViewById<ImageView>(R.id.image5)myDrawMultiBitmap(image5)}private fun myDrawMultiBitmap(imageView: ImageView) {imageView.scaleType = ImageView.ScaleType.CENTER_CROPval options = BitmapFactory.Options()options.outWidth = IMG_SIZEoptions.outHeight = IMG_SIZEoptions.inPreferredConfig = Bitmap.Config.RGB_565val bitmaps = ArrayList<Bitmap>()for (i in 0 until IMG_COUNT) {if (i % 2 == 0) {val b = BitmapFactory.decodeResource(resources, R.mipmap.img, options)bitmaps.add(b)} else {val b = BitmapFactory.decodeResource(resources, R.mipmap.pic, options)bitmaps.add(b)}}val resultBitmap = Bitmap.createBitmap(IMG_SIZE * IMG_COUNT, IMG_SIZE, Bitmap.Config.RGB_565)val canvas = Canvas(resultBitmap)canvas.drawColor(Color.LTGRAY)bitmaps.forEachIndexed { index, bitmap ->val w = bitmap.widthval h = bitmap.heightval srcRct = RectF(0f, 0f, w.toFloat(), h.toFloat())val dstRctLeft = index * IMG_SIZE.toFloat()val dstRct = RectF(dstRctLeft, 0f, dstRctLeft + IMG_SIZE, IMG_SIZE.toFloat())val paint = Paint()if (index % 2 == 0) {paint.color = Color.RED} else {paint.color = Color.BLUE}canvas.drawRect(dstRct, paint)val mx = Matrix()mx.setRectToRect(srcRct, dstRct, Matrix.ScaleToFit.CENTER)canvas.drawBitmap(bitmap, mx, null)}imageView.setImageBitmap(resultBitmap)}
}
遗留问题,需要把BitmapFactory解码出来的Bitmap最终以中心缩放格式绘制到大宽平Bitmap上。这里没有实现中心缩放centerCrop。
Android矩阵Matrix实现Glide图像fitCenter转换为centerCrop,Kotlin-CSDN博客文章浏览阅读551次,点赞12次,收藏12次。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。Android拼接合并图片生成长图代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。基础上,把剪切的区域从矩形Rect变为圆形的Path,当手指在上面的ImageView移动时候,下面同等大小对应的坐标区域显示“剪切”出来的圆形图。https://zhangphil.blog.csdn.net/article/details/144183165