Android 设置圆角图片

在开发过程中有时需要将图片显示成圆角图片,一般我们可以通过在xml中设置drawable shape即可,但今天我给出另一种方法,用java代码动态去设置圆角,顺便做个简单的笔记。
主要原理是使用系统自带api:

RoundedBitmapDrawableFactory

先上效果图:

这里写图片描述

由于比较简单,直接给出实现方式:

public class MainActivity extends AppCompatActivity {

    private ImageView mImgRectRound;
    private ImageView mImgRound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImgRectRound = (ImageView) findViewById(R.id.img_rect_rounded);
        mImgRound = (ImageView) findViewById(R.id.img_rounded);
        rectRoundBitmap();
        roundBitmap();
    }

    private void rectRoundBitmap(){
        //得到资源文件的BitMap
        Bitmap image= BitmapFactory.decodeResource(getResources(),R.drawable.dog);
        //创建RoundedBitmapDrawable对象
        RoundedBitmapDrawable roundImg =RoundedBitmapDrawableFactory.create(getResources(),image);
        //抗锯齿
        roundImg.setAntiAlias(true);
        //设置圆角半径
        roundImg.setCornerRadius(30);
        //设置显示图片
        mImgRectRound.setImageDrawable(roundImg);
    }

    private void roundBitmap(){
        //如果是圆的时候,我们应该把bitmap图片进行剪切成正方形, 然后再设置圆角半径为正方形边长的一半即可
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
        Bitmap bitmap = null;
        //将长方形图片裁剪成正方形图片
        if (image.getWidth() == image.getHeight()) {
            bitmap = Bitmap.createBitmap(image, image.getWidth() / 2 - image.getHeight() / 2, 0, image.getHeight(), image.getHeight());
        } else {
            bitmap = Bitmap.createBitmap(image, 0, image.getHeight() / 2 - image.getWidth() / 2, image.getWidth(), image.getWidth());
        }
        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
        //圆角半径为正方形边长的一半
        roundedBitmapDrawable.setCornerRadius(bitmap.getWidth() / 2);
        //抗锯齿
        roundedBitmapDrawable.setAntiAlias(true);
        mImgRound.setImageDrawable(roundedBitmapDrawable);
    }
}

布局文件:

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.cjl.roundedbitmap.MainActivity">

    <ImageView
        android:id="@+id/img_rect_rounded"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"/>

    <ImageView
        android:id="@+id/img_rounded"
        android:layout_marginTop="20dp"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

如有问题,欢迎指正,谢谢

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值