Android矩阵Matrix实现Glide图像fitCenter转换为centerCrop,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:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image1"
android:layout_width="500px"
android:layout_height="500px"
android:background="@android:color/holo_green_light" />
<ImageView
android:id="@+id/image2"
android:layout_width="500px"
android:layout_height="500px"
android:background="@android:color/holo_blue_light" />
<ImageView
android:id="@+id/image3"
android:layout_width="500px"
android:layout_height="500px"
android:background="@android:color/holo_red_light" />
<ImageView
android:id="@+id/image4"
android:layout_width="500px"
android:layout_height="500px"
android:background="@android:color/holo_orange_light" />
</LinearLayout>
Kotlin
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.RectF
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
class MainActivity : AppCompatActivity() {
companion object {
const val TAG = "fly"
const val SIZE = 500
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val image1 = findViewById<ImageView>(R.id.image1)
val image2 = findViewById<ImageView>(R.id.image2)
val image3 = findViewById<ImageView>(R.id.image3)
val image4 = findViewById<ImageView>(R.id.image4)
Glide.with(this)
.load(R.mipmap.pic)
.fitCenter()
.override(SIZE)
.into(image1)
Glide.with(this)
.load(R.mipmap.pic)
.centerCrop()
.override(SIZE)
.into(image2)
Glide.with(this)
.asBitmap()
.load(R.mipmap.pic)
.fitCenter()
.addListener(object : RequestListener<Bitmap> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>, isFirstResource: Boolean): Boolean {
return false
}
override fun onResourceReady(
resource: Bitmap,
model: Any,
target: Target<Bitmap>?,
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
val bmp = transformToCenterCrop(resource)
image4.setImageBitmap(bmp)
return false
}
})
.override(SIZE)
.into(image3)
}
fun transformToCenterCrop(bitmap: Bitmap): Bitmap {
val w = bitmap.width
val h = bitmap.height
val bmp = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888)
val c = Canvas(bmp)
c.drawColor(Color.LTGRAY)
val mini = Math.min(w, h)
val left = (w - mini) / 2f
val top = (h - mini) / 2f
val right = (w + mini) / 2f
val bottom = (h + mini) / 2f
val srcRectF = RectF(left, top, right, bottom)
val dstRectF = RectF(0f, 0f, SIZE.toFloat(), SIZE.toFloat())
val mx = Matrix()
mx.setRectToRect(srcRectF, dstRectF, Matrix.ScaleToFit.CENTER)
c.drawBitmap(bitmap, mx, null)
return bmp
}
}
转换为centerCrop耗时约1ms。