android 頭像修改

來源:互聯網
上載者:User

標籤:拍照   系統相簿   頭像修改   

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 stubint [] 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);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper.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}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);initDraw(canvas);}private void initDraw(Canvas canvas) {// TODO Auto-generated method stubnewValue=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);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(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 頭像修改

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.