欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义带圆角的ImageView

程序员文章站 2022-09-06 13:26:08
最近有一个实现一个带有圆角的imageview的需求,在网上找了找三方,虽然demo都是正确的,但是移植过来就不可以了,因为请求链接的时候用的是xutils中bitmap来...

最近有一个实现一个带有圆角的imageview的需求,在网上找了找三方,虽然demo都是正确的,但是移植过来就不可以了,因为请求链接的时候用的是xutils中bitmap来进行解析的,这样就总是会报类型转换异常的错误。

就这样只能自己定义一个了.

demo:

package com.yizooo.yizooo.ui;
 
 
import android.content.context;
import android.graphics.bitmap;
import android.graphics.bitmap.config;
import android.graphics.canvas;
import android.graphics.paint;
import android.graphics.pixelformat;
import android.graphics.rectf;
import android.graphics.porterduff.mode;
import android.graphics.porterduffxfermode;
import android.graphics.rect;
import android.graphics.drawable.bitmapdrawable;
import android.graphics.drawable.drawable;
import android.util.attributeset;
import android.widget.imageview;
 
import com.lidroid.xutils.bitmap.core.asyncdrawable;
 
 
/**
 * created by 雪宝宝 on 2016/3/27.
 * 自定义圆角工具
 */
public class roundimageview extends imageview {
  private paint paint;
 
  public roundimageview(context context) {
    this(context,null);
  }
 
  public roundimageview(context context, attributeset attrs) {
    this(context, attrs,0);
  }
 
  public roundimageview(context context, attributeset attrs, int defstyle) {
    super(context, attrs, defstyle);
    paint = new paint();
  }
 
  /**
   * 绘制圆角矩形图片
   */
  @override
  protected void ondraw(canvas canvas) {
    drawable drawable = getdrawable();
    bitmap bitmap = null;
    if (null != drawable && drawable instanceof bitmapdrawable ) {
      bitmapdrawable bitmapdrawable = (bitmapdrawable) drawable;
       bitmap = bitmapdrawable.getbitmap();
      //bitmap bitmap =( (bitmapdrawable)drawable).getbitmap();
      bitmap b = getroundbitmap(bitmap, 10);
      final rect rectsrc = new rect(0, 0, b.getwidth(), b.getheight());
      final rect rectdest = new rect(0,0,getwidth(),getheight());
      paint.reset();
      canvas.drawbitmap(b, rectsrc, rectdest, paint);
 
    }//防止出现类型转换异常
    else if(this.getdrawable() instanceof asyncdrawable){
      bitmap = bitmap
          .createbitmap(
              getwidth(),
              getheight(),
              drawable.getopacity() != pixelformat.opaque ? bitmap.config.argb_8888
                  : bitmap.config.rgb_565);
      canvas canvas1 = new canvas(bitmap);
      // canvas.setbitmap(bitmap);
      drawable.setbounds(0, 0, getwidth(),
          getheight());
      drawable.draw(canvas1);
    }
    else {
      super.ondraw(canvas);
    }
  }
 
  /**
   * 获取圆角矩形图片方法
   * @param bitmap
   * @param roundpx,一般设置成14
   * @return bitmap
   * @author caizhiming
   */
  private bitmap getroundbitmap(bitmap bitmap, int roundpx) {
    bitmap output = bitmap.createbitmap(bitmap.getwidth(),
        bitmap.getheight(), config.argb_8888);
    canvas canvas = new canvas(output);
 
    final int color = 0xff424242;
 
    final rect rect = new rect(0, 0, bitmap.getwidth(), bitmap.getheight());
    final rectf rectf = new rectf(rect);
    paint.setantialias(true);
    canvas.drawargb(0, 0, 0, 0);
    paint.setcolor(color);
    int x = bitmap.getwidth();
 
    canvas.drawroundrect(rectf, roundpx, roundpx, paint);
    paint.setxfermode(new porterduffxfermode(mode.src_in));
    canvas.drawbitmap(bitmap, rect, rect, paint);
    return output;
 
 
  }
}
<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/swipelayout"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" >
 
    <com.yizooo.yizooo.ui.roundimageview
      android:id="@+id/item_frag_news_icon"
      android:layout_width="@dimen/dp_47"
      android:layout_height="@dimen/dp_50"
      android:scaletype="fitxy"
      android:src="@mipmap/fuwutongzhi"
      android:layout_margin="@dimen/dp_10"
      />
</relativelayout>

最终的效果图就不发照片了,各位朋友尝试一下就可以看出效果图了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。