关闭

RoundImageView

标签: imageview继承class
109人阅读 评论(0) 收藏 举报

1、创建自定义ImageView继承ImageView

public class MyImageView extends ImageView {
     private Paint paint;
     //重写构造方法
        public MyImageView(Context context, AttributeSet attrs, int defStyleAttr,
            int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

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

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    //绘制自定义图形
    @Override
    protected void onDraw(Canvas canvas) {

      Drawable drawable = getDrawable();

      if (drawable == null) {
        return;
      }

      if (getWidth() == 0 || getHeight() == 0) {
        return; 
      }
      Bitmap b = null;
      if(drawable instanceof BitmapDrawable){
        b =  ((BitmapDrawable)drawable).getBitmap() ;
      }else if(drawable instanceof AsyncDrawable){
        b = Bitmap 
            .createBitmap( 
            drawable.getIntrinsicWidth(), 
            drawable.getIntrinsicHeight(), 
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 
            : Bitmap.Config.RGB_565); 
            //创建画布
            Canvas canvas1 = new Canvas(b); 
            // canvas.setBitmap(bitmap); 
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), 
            drawable.getIntrinsicHeight()); 
            drawable.draw(canvas1); 
      }
      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 int color = 0xffa19774;
      //创建画笔
      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;
    }
}

2、xml布局

  <com.bawei.view.MyImageView
        android:id="@+id/myimg"
        android:layout_height="80dp"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        />

3、☆☆☆自定义RoundImageView加载url时必须要ImageLoader实现,XUtils不好使。

    //找控件
MyImageView myimg = (MyImageView) convertView.findViewById(R.id.myimg);
    //设置数据  
ImageLoader.getInstance().displayImage(list.get(position).getPortrait(), myimg);
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2839次
    • 积分:172
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章存档
    最新评论