码迷,mamicode.com
首页 > 移动开发 > 详细

android 头像修改

时间:2015-06-15 14:44:03      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:拍照   系统相册   头像修改   

http://blog.csdn.net/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);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		initDraw(canvas);
	}


	private void initDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		newValue=mScreenWidth<=mScreenHeight?mScreenWidth/3/3*2:mScreenHeight/3/3*2;
		if(getmBitmap()==null){
			Bitmap mNewBitmap=Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
			setmBitmap(mNewBitmap);
		}
		mBitmap=ImageUtils.zipImage(getmBitmap(),newValue, newValue);		
		mBitmap=ImageUtils.getRoundedCornerBitmap(mBitmap, mBitmap.getWidth()/2);
		mPaint.setAntiAlias(true);
		canvas.drawBitmap(getmBitmap(),mWidth/6,mHeight/6, mPaint);
		mPaint.setColor(Color.parseColor("#CDC7C1"));
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(10);
		mPaint.setAntiAlias(true);
		canvas.drawCircle(mWidth/6+getmBitmap().getWidth()/2,mHeight/6+mBitmap.getHeight()/2, getmBitmap().getWidth()/2+5, mPaint);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(isClickable){
			if(listener!=null){
				listener.onClick();
			}
		}
	}
	
	public void setBitmap(Bitmap mBitmap){
		newValue=mScreenWidth<=mScreenHeight?mScreenWidth/3/3*2:mScreenHeight/3/3*2;
		Bitmap mb=ImageUtils.zipImage(mBitmap,newValue, newValue);
		mPaint.reset();		
		mPaint.setAntiAlias(true);
		setmBitmap(mb);
		postInvalidate();
	}

	public void setBackground(int id){
		newValue=mScreenWidth<=mScreenHeight?mScreenWidth/3/3*2:mScreenHeight/3/3*2;
		Drawable mdDrawable=getResources().getDrawable(id);
		Bitmap bitmap=ImageUtils.drawableToBitmap(mdDrawable);
		Bitmap mb=ImageUtils.zipImage(bitmap,newValue, newValue);
		mPaint.reset();		
		mPaint.setAntiAlias(true);
		setmBitmap(mb);
		postInvalidate();
	}
	public interface OnPhotoListener{
		void onOpenCamera();
		void onOpenPictures();
		void onClick();
		void onCancel();
	}
	private OnPhotoListener listener;
	private int mScreenWidth;
	private int mScreenHeight;

	public OnPhotoListener getListener() {
		return listener;
	}

	public void setListener(OnPhotoListener listener) {
		this.listener = listener;
	}

}

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.widget.ImageView;
/**
 * 图片处理工具类
 * @author LanYan
 *
 */
public class ImageUtils {
	/***
	 * 获得圆角图片的方法      
	 * @param bitmap
	 * @param roundPx
	 * @return
	 */
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){      

		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);      
		Canvas canvas = new Canvas(output);      

		final int color = 0xff424242;      
		final Paint paint = new Paint();      
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());      
		final RectF rectF = new RectF(rect);      

		paint.setAntiAlias(true);      
		canvas.drawARGB(0, 0, 0, 0);      
		paint.setColor(color);      
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);      
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));      
		canvas.drawBitmap(bitmap, rect, rect, paint);      

		return output;      
	}   
	/**
	 * 获得带倒影的图片方法      
	 * @param bitmap
	 * @return
	 */
	public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){      
		final int reflectionGap = 4;      
		int width = bitmap.getWidth();      
		int height = bitmap.getHeight();      

		Matrix matrix = new Matrix();      
		matrix.preScale(1, -1);      

		Bitmap reflectionImage = Bitmap.createBitmap(bitmap,0, height/2, width, height/2, matrix, false);      

		Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888);      

		Canvas canvas = new Canvas(bitmapWithReflection);      
		canvas.drawBitmap(bitmap, 0, 0, null);      
		Paint deafalutPaint = new Paint();      
		canvas.drawRect(0, height,width,height + reflectionGap,      
				deafalutPaint);      

		canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);      

		Paint paint = new Paint();      
		LinearGradient shader = new LinearGradient(0,      
				bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);      
		paint.setShader(shader);      
		// Set the Transfer mode to be porter duff and destination in      
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));      
		// Draw a rectangle using the paint with our linear gradient      
		canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);      

		return bitmapWithReflection;      
	}    
	/**
	 * Drawable 转Bitmap
	 * @param drawable
	 * @return
	 */
	public static Bitmap drawableToBitmap(Drawable drawable){
		BitmapDrawable bd = (BitmapDrawable) drawable;
		return bd.getBitmap();
	}
	/**
	 * Bitmap 转Drawable
	 * @param drawable
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Drawable bitmapToDrawable(Bitmap bitmap) {
		BitmapDrawable bd = new BitmapDrawable(bitmap);
		return bd;
	}	


	/***
	 * 图片的缩放方法
	 * 
	 * @param bgimage
	 *            :源图片资源
	 * @param newWidth
	 *            :缩放后宽度
	 * @param newHeight
	 *            :缩放后高度
	 * @return
	 */
	public static Bitmap zipImage(Bitmap bgimage, double newWidth,
			double newHeight) {
		// 获取这个图片的宽和高
		float width = bgimage.getWidth();
		float height = bgimage.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();
		// 计算宽高缩放率
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
				(int) height, matrix, true);
		return bitmap;
	}
	
	/** 
	 * @author sunglasses 
	 * @category 图片加载效果 
	 * @param imageView 
	 * @param bitmap 
	 */  
	public static void fadeInDisplay(ImageView imageView, Bitmap bitmap) {//目前流行的渐变效果  
		final TransitionDrawable transitionDrawable = new TransitionDrawable(  
				new Drawable[] { TRANSPARENT_DRAWABLE,  
						new BitmapDrawable(imageView.getResources(), bitmap) });  
		imageView.setImageDrawable(transitionDrawable);  
		transitionDrawable.startTransition(500);  
	}  
	private static final ColorDrawable TRANSPARENT_DRAWABLE = new ColorDrawable(  
			android.R.color.transparent); 

}

ImageUtil工具包类有个别方法测试调用过,实际demo没用到

源码下载地址:http://download.csdn.net/detail/anddroid_lanyan/8807533

android 头像修改

标签:拍照   系统相册   头像修改   

原文地址:http://blog.csdn.net/anddroid_lanyan/article/details/46502405

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!