Android Glide transform圆形图CircleCrop动态代码描边绘制外框线并rotateImage旋转,Kotlin
XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="2px"
android:background="@drawable/ic_launcher_background"
android:src="@drawable/ic_launcher_foreground" />
</FrameLayout>
注意,要用padding而不是margin,padding的值2px和kotlin代码里面的描边线宽度stroke一直。
Kotlin
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.TransformationUtils
class MainActivity : AppCompatActivity() {
companion object {
const val SIZE = 200
const val STROKE_WIDTH = 2F //描边宽度为2px
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val image = findViewById<ImageView>(R.id.image)
GlideApp.with(this)
.load(R.mipmap.pic4)
.transform(CenterCrop(), MyCircleCrop())
.error(android.R.drawable.stat_notify_error)
.override(SIZE)
.into(image)
}
class MyCircleCrop : CircleCrop() {
override fun transform(
pool: BitmapPool,
toTransform: Bitmap,
outWidth: Int,
outHeight: Int
): Bitmap {
val bmp = TransformationUtils.circleCrop(pool, toTransform, outWidth, outHeight)
val p = Paint()
p.isAntiAlias = true
p.style = Paint.Style.STROKE //描边
p.color = Color.RED
p.strokeWidth = STROKE_WIDTH
val cv = Canvas(bmp)
cv.drawCircle(
(outWidth / 2).toFloat(),
(outHeight / 2).toFloat(),
//半径取宽和高最小的。同时减去描边占掉的宽度,否则会把边画到外面。
minOf(outWidth, outHeight) / 2 - STROKE_WIDTH,
p
)
return TransformationUtils.rotateImage(bmp, -90) //逆时针旋转90度。
}
}
}
Android transform旋转rotate圆角矩形图roundedCorners,Kotlin-CSDN博客文章浏览阅读63次。文章浏览阅读369次。文章浏览阅读1.2w次。引入: implementation 'com.github.bumptech.glide:glide:4.9.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'Android标准Glide加载圆形图和圆角矩形图_android glide加载圆角-CSDN博客。Glide transform CircleCrop()圆图,Kotlin。https://blog.csdn.net/zhangphil/article/details/134149777Glide transform CircleCrop()圆图,Kotlin-CSDN博客文章浏览阅读383次。文章浏览阅读1.2w次。引入: implementation 'com.github.bumptech.glide:glide:4.9.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'Android标准Glide加载圆形图和圆角矩形图_android glide加载圆角-CSDN博客。Glide transform CircleCrop()圆图,Kotlin。https://blog.csdn.net/zhangphil/article/details/134147774