http://blog.youkuaiyun.com/xiaanming/article/details/18730223看了这篇博客,整理出自己需要的代码写了下面这个自定义头像这么一个demo.
效果图:
关键代码:
/***
* 头像控件
* @author LanYan
*
*/
@SuppressLint({ "ClickableViewAccessibility", "DrawAllocation" })
public class PhotoImageView extends View implements OnClickListener{
private Paint mPaint;
private Bitmap mBitmap;
private int newValue;
private int mWidth;
private int mHeight;
private boolean isClickable=true;
public Bitmap getmBitmap() {
return mBitmap;
}
public void setmBitmap(Bitmap mBitmap) {
this.mBitmap = mBitmap;
}
@SuppressWarnings("deprecation")
public PhotoImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
int [] arr=ScreenUtil.initialScreen(context);
mScreenHeight=arr[1];
mScreenWidth=arr[0];
Drawable mDrawable=getBackground();
if(mDrawable!=null){
setmBitmap(ImageUtils.drawableToBitmap(mDrawable));
setBackgroundDrawable(null);
}
mPaint=new Paint();
setOnClickListener(this);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mHeight=mWidth=mScreenWidth<mScreenHeight?mScreenWidth/3:mScreenHeight/3;
}
public PhotoImageView(Context context, AttributeSet attrs) {
this(context, attrs,0);
// TODO Auto-generated constructor stub
}
public PhotoImageView(Context context) {
this(context,null);