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

Android开发使用Drawable绘制圆角与圆形图案功能示例

程序员文章站 2023-12-15 19:18:04
本文实例讲述了android开发使用drawable绘制圆角与圆形图案功能。分享给大家供大家参考,具体如下: 1. 创建类roundcircledrawable继承dra...

本文实例讲述了android开发使用drawable绘制圆角与圆形图案功能。分享给大家供大家参考,具体如下:

1. 创建类roundcircledrawable继承drawable

/**
 * 圆角矩形
 * @project  app_view
 * @package  com.android.view.drawable
 * @author   chenlin
 * @version  1.0
 * @date    2016年4月21日
 * @note    todo
 */
public class roundcircledrawable extends drawable{
  private paint mpaint;//画笔
  private int mwidth;//图片宽与长度的最小值
  private int mradius;//半径
  private int mround;//圆角
  private rectf mrectf;//矩形
  private bitmap mbitmap;//图片
  private type mtype = type.type_round;//默认是矩形
  //设置类型
  enum type{
    type_round, type_cicle;
  }
  public roundcircledrawable(bitmap bitmap){
    this.mbitmap = bitmap;
    //初始化画笔
    mpaint = new paint();
    mpaint.setantialias(true);
    bitmapshader shader = new bitmapshader(mbitmap, tilemode.clamp, tilemode.clamp);
    mpaint.setshader(shader);
    mwidth = math.min(mbitmap.getwidth(), mbitmap.getheight());
    mradius = mwidth / 2;
  }
  /**
   * 向外提供设置图片类型的方法
   * @param type
   */
  public void settype(type type){
    this.mtype = type;
  }
  /**
   * 暴露给外面设置圆角的大小
   * 
   * @param round
   */
  public void setround(int round) {
    this.mround = round;
  }
  @override
  public void setbounds(int left, int top, int right, int bottom) {
    super.setbounds(left, top, right, bottom);
    mrectf = new rectf(left, top, right, bottom);
  }
  @override
  public void draw(canvas canvas) {
    if (mtype == type.type_round) {
      canvas.drawroundrect(mrectf, mround, mround, mpaint);
    }else {
      canvas.drawcircle(mwidth / 2, mwidth / 2, mradius, mpaint);
    }
  }
  @override
  public int getintrinsicwidth() {
    if (mtype == type.type_cicle) {
      return mwidth;
    }else {
      return mbitmap.getwidth();
    }
  }
  @override
  public int getintrinsicheight() {
    if (mtype == type.type_cicle) {
      return mwidth;
    }else {
      return mbitmap.getheight();
    }
  }
  @override
  public void setalpha(int alpha) {
    mpaint.setalpha(alpha);
  }
  @override
  public void setcolorfilter(colorfilter cf) {
    mpaint.setcolorfilter(cf);
  }
  @override
  public int getopacity() {
    return pixelformat.translucent;
  }
}

2. 实现方法

public class roundactivity extends activity {
  private imageview mimageview;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_round_drawable);
    mimageview = (imageview) findviewbyid(r.id.iv_round);
    bitmap bitmap = bitmapfactory.decoderesource(getresources(), r.drawable.aa);
    //roundimagedrawable drawable = new roundimagedrawable(bitmap);
    //drawable.setround(30);
    roundcircledrawable drawable = new roundcircledrawable(bitmap);
    drawable.setround(50);
    mimageview.setimagedrawable(drawable);
  }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: