源代码
0.最终的效果
1.java代码实现
// 呼吸动画
private void initShotAnim(ImageView iv) {
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator animatorX = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 1.2f, 1f);
ObjectAnimator animatorY = ObjectAnimator.ofFloat(iv, "scaleY", 1f, 1.2f, 1f);
animatorX.setRepeatCount(-1);
animatorY.setRepeatCount(-1);
animatorSet.play(animatorX).with(animatorY);
animatorSet.setDuration(1000).start();
}
// 圆形Img
private void initCircleImg(ImageView view) {
Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.sylm); //获取Bitmap图片
RoundedBitmapDrawable bitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), src); //创建RoundedBitmapDrawable对象
bitmapDrawable.setCircular(true); // 设置圆形
bitmapDrawable.setAntiAlias(true); //设置抗锯齿
view.setImageDrawable(bitmapDrawable); //显示圆角
}
2.kotlin代码实现
/**
* 呼吸动画
*/
private fun initShotAnim(iv: ImageView) {
val animatorSet = AnimatorSet();
val animatorX = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 1.2f, 1f);
val animatorY = ObjectAnimator.ofFloat(iv, "scaleY", 1f, 1.2f, 1f);
animatorX.repeatCount = -1;
animatorY.repeatCount = -1;
animatorSet.play(animatorX).with(animatorY);
animatorSet.setDuration(1000).start();
}
/**
* 设置圆形
*/
private fun initCircleImg(iv: ImageView) {
val src = BitmapFactory.decodeResource(resources, R.drawable.sylm); //获取Bitmap图片
val bitmapDrawable = RoundedBitmapDrawableFactory.create(resources, src); //创建RoundedBitmapDrawable对象
bitmapDrawable.isCircular = true
bitmapDrawable.setAntiAlias(true); //设置抗锯齿
iv.setImageDrawable(bitmapDrawable); //显示圆角
}
3.使用
initCircleImg(iv)
initShotAnim(iv)
4.XML布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:layout_marginTop="50dp"
android:layout_width="200dp"
android:layout_height="200dp"/>
</LinearLayout>
注意:
圆形图片实现方式是最简洁的方式,很多场景不支持,图片文件本身必须是正方形的才可以;
如下图:
参考资料:4- Android四种自定义圆形ImageView的方法_bobo_zai的博客-CSDN博客_圆形imageview