本文实例讲述了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,bottom); } @OverrIDe public voID draw(Canvas canvas) { if (mType == Type.TYPE_ROUND) { canvas.drawRoundRect(mRectF,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程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android开发使用Drawable绘制圆角与圆形图案功能示例全部内容,希望文章能够帮你解决Android开发使用Drawable绘制圆角与圆形图案功能示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)