Android Glide加载transform CenterCrop, CircleCrop ShapeableImageView圆形图并描边,Kotlin
Kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.google.android.material.imageview.ShapeableImageView
class MainActivity : AppCompatActivity() {
companion object {
const val SIZE = 500
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val image = findViewById<ShapeableImageView>(R.id.image)
image.rotation = -20F //逆时针旋转20度。
GlideApp.with(this)
.load(R.mipmap.pic4)
.transform(CenterCrop(), CircleCrop())
.error(android.R.drawable.stat_notify_error)
.override(SIZE)
.into(image)
}
}
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">
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="2dp"
app:shapeAppearance="@style/rounded_style"
app:strokeColor="@android:color/holo_red_dark"
app:strokeWidth="2dp" />
</FrameLayout>
注意,此处用padding而不是margin,如果用margin,圆边被切割。另外,padding的值需要与stroke值相同,本例才可以完美的绘制边线。
styles.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<style name="rounded_style">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
</resources>

上图显示的描边效果显然优于该篇文章:
所实现的在Kotlin动态代码中绘制的描边效果。