Android自定义Drawable实现圆形和圆角
程序员文章站
2023-12-14 23:31:16
本文实例为大家分享了自定义drawable实现圆形和圆角的具体代码,供大家参考,具体内容如下
圆形
package com.customview.widget;...
本文实例为大家分享了自定义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, 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, shader.tilemode.clamp);//着色器 水平和竖直都需要填充满 mpaint = new paint(); mpaint.setantialias(true); mpaint.setshader(bitmapshader); //rectf = new rectf(0, 0, bitmap.getwidth(), bitmap.getheight()); } @override public void draw(@nonnull canvas canvas) { //rectf:圆角矩形区域 canvas.drawroundrect(new rectf(0, 0, mbitmap.getwidth(), mbitmap.getheight()),100, 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, top, right, 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实现圆角矩形的方法
-
Android中Glide加载圆形图片和圆角图片实例代码
-
Android实现自定义圆角对话框Dialog的示例代码
-
Android自定义Drawable实现圆角效果
-
Android 实现图片生成卷角和圆角缩略图的方法
-
Android开发使用Drawable绘制圆角与圆形图案功能示例
-
Android自定义Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)
-
Android开发基于Drawable实现圆角矩形的方法
-
Android自定义View圆形和拖动圆跟随手指拖动