Android圆形的ImageView

本文介绍了如何在Android中自定义ImageView实现圆形图片,并解决了使用默认方法导致的图片边缘模糊、无抗锯齿的问题。通过提供一个改进的自定义RoundImageView类的代码,展示了如何在onDraw()方法中应用 PorterDuffXfermode 模式来实现清晰的圆形图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很对不住大家 此代码有问题,这样做出来的原形imageview没有抗锯齿,到这图片边缘模糊 效果如下:

 

由于项目需要图片要做成圆形的,只能自定义ImageView,下边自定义的ImageView效果如上图,边缘不抗锯齿,模糊

public class RoundImageView extends ImageView {



public RoundImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}


public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}


public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {
Path clipPath = new Path();
int w = this.getWidth();
int h = this.getHeight();
clipPath.addCircle(w / 2, h / 2, w / 2, Path.Direction.CW);
canvas.clipPath(clipPath);
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG
| Paint.FILTER_BITMAP_FLAG));
super.onDraw(canvas);
}


}

所以不建议用上边的代码了:更改起自定义ImageView的代码,实现圆形ImageView的边缘抗锯齿:效果图如下:


我想大家看到效果了,圆形ImageView边缘已经不在模糊了;其自定义的ImageView的代码如下:

public class RoundImageView extends ImageView {


public RoundImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}


public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}


public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}


@Override
protected void onDraw(Canvas canvas) {


Drawable drawable = getDrawable();


if (drawable == null) {
return;
}


if (getWidth() == 0 || getHeight() == 0) {
return;
}


Bitmap b = ((BitmapDrawable) drawable).getBitmap();
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);


int w = getWidth(), h = getHeight();


Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0, 0, null);


}


public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius)
sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
else
sbmp = bmp;
Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(output);


final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());


paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f,
sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);


return output;
}


}

布局layout如下:将自定义的Imageview添加到layout里边

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"
    android:orientation="horizontal" >


    <com.yidaoxing.aaf.view.RoundImageView
        android:id="@+id/dashen_more_list_img"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="6dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="6dp"
        android:background="@drawable/dashen_more_img_bj"
        android:scaleType="fitXY" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:orientation="vertical" >


            <TextView
                android:id="@+id/dashen_more_list_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#4B5558"
                android:textSize="18dp" />


            <TextView
                android:id="@+id/dashen_more_list_dec"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#A5AAAB"
                android:textSize="13dp" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:layout_weight="5"
            android:orientation="horizontal" >


            <ImageView
                android:id="@+id/dashen_more_list_right"
                android:layout_width="10dp"
                android:layout_height="15dp"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="20dp"
                android:src="@drawable/right"
                android:scaleType="fitXY" />
        </LinearLayout>
    </LinearLayout>


</LinearLayout>

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值