Android圆形的ImageView

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

Android圆形的ImageView_第1张图片 

由于项目需要图片要做成圆形的,只能自定义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的边缘抗锯齿:效果图如下:

Android圆形的ImageView_第2张图片

我想大家看到效果了,圆形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里边

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"
    android:orientation="horizontal" >


            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" />


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


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


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


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



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


                            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" />
       
   



你可能感兴趣的:(Android,Android,imageview)