Android 自定义圆形Imageview
来源:互联网 发布:点对点网速测试软件 编辑:程序博客网 时间:2024/06/05 00:23
public class RoundImageView extends ImageView {
private float xRadius = 10;
private float yRadius = 10;
private Paint paint = new Paint();
public RoundImageView(Context context) {
super(context);
}
public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public float getxRadius() {
return xRadius;
}
public void setxRadius(float xRadius) {
this.xRadius = xRadius;
}
public float getyRadius() {
return yRadius;
}
public void setyRadius(float yRadius) {
this.yRadius = yRadius;
}
@SuppressWarnings("deprecation")
@Override
protected void onDraw(Canvas canvas) {
// java.lang.ClassCastException: android.graphics.drawable.TransitionDrawable cannot be cast
// to android.graphics.drawable.BitmapDrawable
BitmapShader shader;
if (getDrawable() instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable)getDrawable();
// clip
shader = new BitmapShader(bitmapDrawable.getBitmap(), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
RectF rect = new RectF(0.0f, 0.0f, getWidth(), getHeight());
int width = bitmapDrawable.getBitmap().getWidth();
int height = bitmapDrawable.getBitmap().getHeight();
RectF src = null;
if (((float)width) / height > 1) {
src = new RectF(0.0f, 0.0f, height, height);
} else {
src = new RectF(0.0f, 0.0f, width, width);
}
Matrix matrix = canvas.getMatrix();
matrix.setRectToRect(src, rect, Matrix.ScaleToFit.CENTER);
shader.setLocalMatrix(matrix);
// 抗锯齿
paint.setAntiAlias(true);
paint.setShader(shader);
// draw round circle for HeadImage or other
canvas.drawRoundRect(rect, this.getWidth() / 2, this.getHeight() / 2, paint);
canvas.drawRoundRect(rect, xRadius, yRadius / 2, paint);
}
}
}
private float xRadius = 10;
private float yRadius = 10;
private Paint paint = new Paint();
public RoundImageView(Context context) {
super(context);
}
public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public float getxRadius() {
return xRadius;
}
public void setxRadius(float xRadius) {
this.xRadius = xRadius;
}
public float getyRadius() {
return yRadius;
}
public void setyRadius(float yRadius) {
this.yRadius = yRadius;
}
@SuppressWarnings("deprecation")
@Override
protected void onDraw(Canvas canvas) {
// java.lang.ClassCastException: android.graphics.drawable.TransitionDrawable cannot be cast
// to android.graphics.drawable.BitmapDrawable
BitmapShader shader;
if (getDrawable() instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable)getDrawable();
// clip
shader = new BitmapShader(bitmapDrawable.getBitmap(), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
RectF rect = new RectF(0.0f, 0.0f, getWidth(), getHeight());
int width = bitmapDrawable.getBitmap().getWidth();
int height = bitmapDrawable.getBitmap().getHeight();
RectF src = null;
if (((float)width) / height > 1) {
src = new RectF(0.0f, 0.0f, height, height);
} else {
src = new RectF(0.0f, 0.0f, width, width);
}
Matrix matrix = canvas.getMatrix();
matrix.setRectToRect(src, rect, Matrix.ScaleToFit.CENTER);
shader.setLocalMatrix(matrix);
// 抗锯齿
paint.setAntiAlias(true);
paint.setShader(shader);
// draw round circle for HeadImage or other
canvas.drawRoundRect(rect, this.getWidth() / 2, this.getHeight() / 2, paint);
canvas.drawRoundRect(rect, xRadius, yRadius / 2, paint);
}
}
}
阅读全文
0 0
- 自定义android圆形ImageView
- android自定义圆形imageview
- Android 自定义圆形Imageview
- android 自定义圆形imageview控件
- android 圆形头像 自定义ImageView
- Android简单自定义圆形Imageview
- android 自定义圆形的ImageView
- Android实现自定义圆形ImageView
- Android自定义控件之圆形进度条ImageView
- android自定义圆形ImageView(学习笔记整理)
- android UI自定义设计圆形ImageView
- androidの自定义ImageView圆形头像
- Android 自定义ImageView, 圆形六边形头像
- android自定义ImageView实现圆形图片
- android自定义圆形头像view,继承imageview
- Android自定义控件——圆形ImageView
- Android之自定义ImageView圆形图片
- android中自定义的圆形ImageView
- JAVAMAIL试用google邮箱发送邮件
- 【TCP】流量控制和拥塞控制
- Android开发之Bitmap的获取
- Java数据类型
- c++ 关键字详解
- Android 自定义圆形Imageview
- python入门系列1---基础知识
- 启动VMware虚拟机显示:物理内存不足无法使用的五种解决方法
- tomcat7简单优化
- 找不到或无法加载主类、JAVA_HOME、PATH、CLASSPATH
- 静态
- TimeAndMath
- WebHubBot爬虫分享(每天可抓取500万以上)
- 问题:Objective-C 中 C 类型函数不识别 foundation 中的类