最简洁的方式实现圆形ImageView + 呼吸动画

源代码

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值