本文实例为大家分享了自定义Drawable实现圆形和圆角的具体代码,供大家参考,具体内容如下
圆形
package com.customvIEw.Widget;import androID.graphics.Bitmap;import androID.graphics.BitmapShader;import androID.graphics.Canvas;import androID.graphics.colorFilter;import androID.graphics.Paint;import androID.graphics.PixelFormat;import androID.graphics.Shader;import androID.graphics.drawable.Drawable;import androID.support.annotation.IntRange;import androID.support.annotation.NonNull;import androID.support.annotation.Nullable;/** * 自定义Drawable 实现圆形图片 * Created by hust_twj on 2017/9/26. */public class CircleDrawable extends Drawable { private Paint mPaint; private int mWIDth; //宽/高,直径 private Bitmap mBitmap; public CircleDrawable(Bitmap bitmap) { this.mBitmap = bitmap; BitmapShader bitmapShader = new BitmapShader(mBitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);//着色器 水平和竖直都需要填充满 mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setShader(bitmapShader); mWIDth = Math.min(mBitmap.getWIDth(),mBitmap.getHeight()); //宽高的最小值作为直径 } @OverrIDe public voID draw(@NonNull Canvas canvas) { canvas.drawCircle(mWIDth/2,mWIDth/2,mPaint); //绘制圆形 } @OverrIDe public voID setAlpha(@IntRange(from = 0,to = 255) int i) { mPaint.setAlpha(i); } @OverrIDe public voID setcolorFilter(@Nullable colorFilter colorFilter) { mPaint.setcolorFilter(colorFilter); } @OverrIDe public int getopacity() { return PixelFormat.TRANSLUCENT; //设置系统默认,让drawable支持和窗口一样的透明度 } //还需要从重写以下2个方法,返回drawable实际宽高 @OverrIDe public int getIntrinsicWIDth() { return mWIDth; } @OverrIDe public int getIntrinsicHeight() { return mWIDth; }}
圆角:
package com.customvIEw.Widget;import androID.graphics.Bitmap;import androID.graphics.BitmapShader;import androID.graphics.Canvas;import androID.graphics.colorFilter;import androID.graphics.Paint;import androID.graphics.PixelFormat;import androID.graphics.RectF;import androID.graphics.Shader;import androID.graphics.drawable.Drawable;import androID.support.annotation.IntRange;import androID.support.annotation.NonNull;import androID.support.annotation.Nullable;/** * 自定义Drawable 实现圆角图片 * Created by hust_twj on 2017/9/26. */public class RoundCornerDrawable extends Drawable { private Paint mPaint; private RectF rectF;//定义矩形区域 private Bitmap mBitmap; public RoundCornerDrawable(Bitmap bitmap) { this.mBitmap = bitmap; BitmapShader bitmapShader = new BitmapShader(mBitmap,Shader.TileMode.CLAMP);//着色器 水平和竖直都需要填充满 mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setShader(bitmapShader); //rectF = new RectF(0,bitmap.getWIDth(),bitmap.getHeight()); } @OverrIDe public voID draw(@NonNull Canvas canvas) { //RectF:圆角矩形区域 canvas.drawRoundRect(new RectF(0,mBitmap.getWIDth(),mBitmap.getHeight()),100,mPaint);//rx:x方向上的圆角半径; ry:y方向上的圆角半径。 }/* @OverrIDe public voID setBounds(int left,int top,int right,int bottom) { super.setBounds(left,top,right,bottom); rectF = new RectF(left,bottom); }*/ @OverrIDe public voID setAlpha(@IntRange(from = 0,to = 255) int i) { mPaint.setAlpha(i); } @OverrIDe public voID setcolorFilter(@Nullable colorFilter colorFilter) { mPaint.setcolorFilter(colorFilter); } @OverrIDe public int getopacity() { return PixelFormat.TRANSLUCENT; //设置系统默认,让drawable支持和窗口一样的透明度 } //还需要从重写以下2个方法,返回drawable实际宽高 @OverrIDe public int getIntrinsicWIDth() { return mBitmap.getWIDth(); } @OverrIDe public int getIntrinsicHeight() { return mBitmap.getHeight(); }}
MainActivity中:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.XXX);imageVIEw.setimageDrawable(new RoundCornerDrawable(bitmap));
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android自定义Drawable实现圆形和圆角全部内容,希望文章能够帮你解决Android自定义Drawable实现圆形和圆角所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)