ImageView的圆角或圆形使用原生support v4中的RoundedBitmapDrawable实现

前言

之前曾使用Xfermode和BitmapShader实现了ImageView的圆角/圆形的效果。后来无意中在support v4包中,发现了一个类:RoundedBitmapDrawable。原来Google已经提供了直接实现图片的圆角,根本不需要使用第三方的类库,可以直接使用ImageView控件,重要的是还能和各种图片加载库配合使用。看到如此方便的类,心里小小的激动了一下,话不多说,下面就来介绍它的用法吧。

介绍

关于RoundedBitmapDrawable,Google官方文档有一句话介绍:一个包含了bitmap并且能够绘制圆角的Drawable。
能够通过文件路径、输入流、或者Bitmap对象来创建一个RoundedBitmapDrawable。
例如:

RoundedBitmapDrawableFactory.create(Resources res, String filepath);
RoundedBitmapDrawableFactory.create(Resources res, Bitmap bitmap);
RoundedBitmapDrawableFactory.create(Resources res, InputStream is);

还有其他属性可以设置:

setCircular(boolean circular) : 把图片的形状设为圆形;
setCornerRadius(float cornerRadius) : 设置图片的圆角半径。
setAlpha(int alpha) :透明度
setAntiAlias(boolean aa): 抗锯齿

开始使用

Java代码

ImageView roundview = (ImageView) findViewById(R.id.Roundview);
ImageView circleview = (ImageView) findViewById(R.id.Circleview);

RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(getResources(),id2Bitmap(R.drawable.ac_default_icon));
//设置圆角角度        roundedDrawable.setCornerRadius(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,60,getResources().getDisplayMetrics()));
roundview.setImageDrawable(roundedDrawable);

RoundedBitmapDrawable circleDrawable = RoundedBitmapDrawableFactory.create(getResources(),id2Bitmap(R.drawable.ac_default_icon));
//设置为圆形
circleDrawable.setCircular(true);
circleview.setImageDrawable(circleDrawable);

private Bitmap id2Bitmap(int id) {
     return BitmapFactory.decodeResource(getResources(),id);
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/activity_main"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:padding="10dp">

    <ImageView
        android:id="@+id/Roundview"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="20dp"/>

    <ImageView
        android:id="@+id/Circleview"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginTop="20dp"/>

</LinearLayout>

创建一个正圆形带有边框的图片

   /**
     * @param bitmap
     * @param borderWidth 边距
     * @return
     */
    private Bitmap createRoundImageWithBorder(Bitmap bitmap,int borderWidth){

        //原图宽度
        int bitmapWidth = bitmap.getWidth();
        //原图高度
        int bitmapHeight = bitmap.getHeight();
        //圆的半径
        int mRadius = Math.min(bitmapWidth,bitmapHeight);
        //带边框圆的半径
        int newRadiusWidth = mRadius + borderWidth;
        //创建画布,需要加上边框
        Bitmap roundedBitmap = Bitmap.createBitmap(newRadiusWidth,newRadiusWidth,Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(roundedBitmap);
        //在画布上,绘制bitmap,保证在中心上,所以偏移边距的一半
        canvas.drawBitmap(bitmap, borderWidth/2, borderWidth/2, null);

        Paint borderPaint = new Paint();
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(borderWidth);
        borderPaint.setColor(Color.RED);

        canvas.drawCircle(newRadiusWidth/2, newRadiusWidth/2, newRadiusWidth/2, borderPaint);
        return roundedBitmap;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值