很对不住大家 此代码有问题,这样做出来的原形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里边

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



更多相关文章

  1. android EditText设置不可写
  2. android 使用html5作布局文件: webview跟javascript交互
  3. android studio调试c/c++代码
  4. IM-A820L限制GSM,WCDMA上网的原理(其他泛泰机型可参考)7.13
  5. 锁屏界面
  6. android(NDK+JNI)---Eclipse+CDT+gdb调试android ndk程序
  7. Android(安卓)version and Linux Kernel version
  8. Android(安卓)闹钟管理类的使用
  9. Android学习篇之Menu的使用

随机推荐

  1. java实现电脑开关机
  2. Android(安卓)水波纹效果的探究
  3. 太火了!MyBatis Plus 为啥这么牛?
  4. HBase 底层原理详解(深度好文,建议收藏)
  5. 循环结构的继续学习
  6. 工作中常用到的Linux命令
  7. 太简单了!PHP获取文件扩展名的7中方法
  8. php中使用fsockopen实现异步请求(代码示例
  9. php实现将表单内容提交到数据库
  10. 流行的php rpc框架详解