自定义view画个圈圈
2016-01-31 17:19
453 查看
public class CircleImageView extends ImageView {
int bordercolor;
int borderwidth;
public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context,attrs);
}
private void init(Context context, AttributeSet attrs) {
// TODO Auto-generated method stub
TypedArray t=context.obtainStyledAttributes(attrs, R.styleable.CircleImageView);
bordercolor=t.getColor(R.styleable.CircleImageView_border_color, Color.RED);
borderwidth=t.getDimensionPixelSize(R.styleable.CircleImageView_border_width, 2);
t.recycle();
}
public CircleImageView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public CircleImageView(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
//将作为参数传入的方形的bitmap对象变成圆形的bitmap对象 然后画到组件上 传入的默认方形图片
public void setCircleImageBitmap(Bitmap bitmap){
int width=getWidth();
int height=getHeight();
if(width==0||height==0){
//手动指定width 和 height对应的大小
width=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
height=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
}
bitmap=Bitmap.createScaledBitmap(bitmap, width, height, true);
Bitmap bm=Bitmap.createBitmap(width, height, bitmap.getConfig());
int r=Math.min(width, height)/2;
Canvas c=new Canvas(bm);
Paint p=new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(Color.GREEN);
Shader shader=new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
p.setShader(shader);
//p.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
//c.drawBitmap(bitmap, 0, 0, p);
c.drawCircle(width/2, height/2, r-borderwidth , p);
p.reset();
p.setStyle(Style.STROKE);
p.setAntiAlias(true);
p.setColor(bordercolor);
p.setStrokeWidth(borderwidth);
c.drawCircle(width/2, height/2, r-borderwidth, p);
setScaleType(ScaleType.CENTER);
super.setImageBitmap(bm);
}
}
<resources>
<declare-styleable name="CircleImageView">
<attr name="border_color" format="color"/>
<attr name="border_width" format="dimension"/>
</declare-styleable>
</resources>
int bordercolor;
int borderwidth;
public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context,attrs);
}
private void init(Context context, AttributeSet attrs) {
// TODO Auto-generated method stub
TypedArray t=context.obtainStyledAttributes(attrs, R.styleable.CircleImageView);
bordercolor=t.getColor(R.styleable.CircleImageView_border_color, Color.RED);
borderwidth=t.getDimensionPixelSize(R.styleable.CircleImageView_border_width, 2);
t.recycle();
}
public CircleImageView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public CircleImageView(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
//将作为参数传入的方形的bitmap对象变成圆形的bitmap对象 然后画到组件上 传入的默认方形图片
public void setCircleImageBitmap(Bitmap bitmap){
int width=getWidth();
int height=getHeight();
if(width==0||height==0){
//手动指定width 和 height对应的大小
width=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
height=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
}
bitmap=Bitmap.createScaledBitmap(bitmap, width, height, true);
Bitmap bm=Bitmap.createBitmap(width, height, bitmap.getConfig());
int r=Math.min(width, height)/2;
Canvas c=new Canvas(bm);
Paint p=new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(Color.GREEN);
Shader shader=new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
p.setShader(shader);
//p.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
//c.drawBitmap(bitmap, 0, 0, p);
c.drawCircle(width/2, height/2, r-borderwidth , p);
p.reset();
p.setStyle(Style.STROKE);
p.setAntiAlias(true);
p.setColor(bordercolor);
p.setStrokeWidth(borderwidth);
c.drawCircle(width/2, height/2, r-borderwidth, p);
setScaleType(ScaleType.CENTER);
super.setImageBitmap(bm);
}
}
<resources>
<declare-styleable name="CircleImageView">
<attr name="border_color" format="color"/>
<attr name="border_width" format="dimension"/>
</declare-styleable>
</resources>
相关文章推荐
- IOS-网络(数据安全:MD5加密)
- Java值传递
- lightoj1349 - Aladdin and the Optimal Invitation【求中位数】
- AFNetworking3.0使用
- LeetCode290 pattern string 解题报告
- 面向对象六大基本原则的理解
- 防止网站内容被复制的方法
- iOS开发中CALayer的详细使用说明
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) 618C Constellation(计算几何+stl)
- php 循环列找关联字段问题
- lightoj 1403 - Air Raid
- git常用命令学习笔记
- 【CSS】盒子模型 之 IE 与W3C的盒子模型对比
- Linux下Mysql简单操作
- Android AttributeSet
- CentOS6.5下Git服务器搭建
- git命令大全
- Git命令参数及用法详解(转)(二)
- Git命令参数及用法详解(转)(一)
- linux下删除指定文件之外的其他文…