Android自定义Drawable实现圆形和圆角
作者:hust_twj 时间:2022-05-07 14:59:59
本文实例为大家分享了自定义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));
来源:http://blog.csdn.net/hust_twj/article/details/78109730
标签:Android,Drawable,圆形,圆角
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java Spring的启动原理详解
2022-09-02 04:39:59
![](https://img.aspxhome.com/file/2023/4/66244_0s.jpg)
在WPF中实现平滑滚动的方法详解
2021-06-17 06:34:16
Spring Boot缓存实战 Caffeine示例
2021-11-15 17:39:34
Springboot引入多个yml方法(多种方案)
2023-07-21 21:10:48
![](https://img.aspxhome.com/file/2023/7/88597_0s.png)
spring boot加载第三方jar包的配置文件的方法
2023-03-02 22:45:13
httpwebreqeust读取httponly的cookie方法
2022-04-19 19:47:05
spring boot集成smart-doc自动生成接口文档详解
2023-11-28 23:08:02
![](https://img.aspxhome.com/file/2023/3/60773_0s.png)
maven的pom.xml中profiles的作用详解
2022-07-03 20:40:54
![](https://img.aspxhome.com/file/2023/9/64509_0s.png)
android 横竖屏限制的配置方法
2023-02-06 21:55:45
Java京东面试题之为什么HashMap线程不安全
2022-12-06 07:20:02
![](https://img.aspxhome.com/file/2023/5/65615_0s.png)
SpringCloud实现Redis在各个微服务的Session共享问题
2022-07-14 06:33:19
Java中ArrayList与顺序表的定义与实现方法
2022-06-08 03:27:12
![](https://img.aspxhome.com/file/2023/7/67197_0s.png)
Android 静默安装实现方法
2021-08-04 05:11:25
浅谈SpringBoot中的Bean初始化方法 @PostConstruct
2022-06-11 07:21:17
![](https://img.aspxhome.com/file/2023/1/80251_0s.jpg)
Struts2相关的面试题整理分享
2022-04-06 08:02:20
Android mvvm之LiveData原理案例详解
2023-09-29 11:49:26
![](https://img.aspxhome.com/file/2023/5/92625_0s.png)
C#中Linq的入门教程
2023-12-23 16:20:52
![](https://img.aspxhome.com/file/2023/2/91362_0s.png)
C#中Span相关的性能优化建议
2021-07-05 15:42:26
![](https://img.aspxhome.com/file/2023/7/69817_0s.png)
SpringBoot2整合Drools规则引擎及案例详解
2021-12-30 05:57:10
![](https://img.aspxhome.com/file/2023/3/66703_0s.jpg)
Java如何实现支付宝电脑支付基于servlet版本
2021-12-11 04:39:47
![](https://img.aspxhome.com/file/2023/7/81237_0s.png)