栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android自定义View圆形图片控件代码详解

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android自定义View圆形图片控件代码详解

前言

在日常开发中,圆形的图片效果还是很常见的。可以通过给Paint设置Xfermode来实现,这里简单记录如下。

实现

实现圆形效果的核心是PorterDuffXfermode,对于PorterDuffXfermode,这里不展开,可以查询相关资料。

核心代码

//绘制背景
canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2, mPaint);
//设置模式为:显示背景层和上层的交集,且显示上层图像
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//绘制要显示的图像
canvas.drawBitmap(mSrcBitmap, 0, 0, mPaint);
//重置Xfermode
mPaint.setXfermode(null);

自定义属性



  
    
    
    
    
      
      
      
      
    
  

自定义控件

public class CircleView extends View {

  private static final int DEFAULT_SIZE = 200;

  private static final int DEFAULT_RADIUS = 20;

  private static final int TYPE_ROUND = 1;

  private static final int TYPE_RECT = 2;

  private int mSize;

  private int mResourceId;

  private int mType;

  private Paint mPaint;

  private Bitmap mSrcBitmap;

  public CircleView(Context context) {
    this(context, null);
  }

  public CircleView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleView);
    mResourceId = ta.getResourceId(R.styleable.CircleView_src, R.mipmap.ic_launcher);
    mType = ta.getInt(R.styleable.CircleView_type, TYPE_ROUND);
    ta.recycle();
    init();
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = getMeasureSize(widthMeasureSpec);
    int height = getMeasureSize(heightMeasureSpec);
    mSize = Math.min(width, height);
    setMeasuredDimension(mSize, mSize);
  }

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //绘制背景
    if (mSrcBitmap == null) {
      mSrcBitmap = getScaleBitmap();
    }
    if (mType == TYPE_ROUND) {
      canvas.drawCircle(mSize / 2, mSize / 2, mSize / 2, mPaint);
    } else if (mType == TYPE_RECT) {
      canvas.drawRoundRect(0, 0, mSize, mSize, DEFAULT_RADIUS, DEFAULT_RADIUS, mPaint);
    }
    //设置模式为:显示背景层和上层的交集,且显示上层图像
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    //绘制要显示的图像
    canvas.drawBitmap(mSrcBitmap, 0, 0, mPaint);
    //重置Xfermode
    mPaint.setXfermode(null);
  }

  private void init() {
    //禁用硬件加速,否则可能无法绘制圆形
    setLayerType(LAYER_TYPE_HARDWARE, null);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setStyle(Paint.Style.FILL);
  }

  private int getMeasureSize(int measureSpec) {
    int mode = MeasureSpec.getMode(measureSpec);
    int size = MeasureSpec.getSize(measureSpec);
    return mode == MeasureSpec.EXACTLY ? size : DEFAULT_SIZE;
  }

  
  private Bitmap getScaleBitmap() {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), mResourceId, options);
    options.inSampleSize = calcSampleSize(options, mSize, mSize);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(getResources(), mResourceId, options);
  }

  
  private int calcSampleSize(BitmapFactory.Options option, int width, int height) {
    int originWidth = option.outWidth;
    int originHeight = option.outHeight;
    int sampleSize = 1;
    while ((originWidth = originWidth >> 1) > width && (originHeight = originHeight >> 1) > height) {
      sampleSize = sampleSize << 1;
    }
    return sampleSize;
  }
}

注意:如果没有圆形的效果,那么可能需要禁用硬件加速:setLayerType(LAYER_TYPE_HARDWARE, null)

布局




  

  

  

效果

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153676.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号