Android實(shí)現(xiàn)相機(jī)拍攝、選擇、圖片裁剪功能
最近的一些學(xué)習(xí)心得:
功能實(shí)現(xiàn):點(diǎn)擊圓形頭像之后可以實(shí)現(xiàn)相冊上傳或者開啟相機(jī),然后把得到的圖片經(jīng)過剪裁,把剪裁過的圖片設(shè)置為頭像的背景圖
步驟:第一步:自定義一個類,繼承ImageView,重寫draw方法,實(shí)現(xiàn)外觀為圓形
第二步:在xml文件中引用該控件
第三步:實(shí)現(xiàn)圓形頭像的點(diǎn)擊事件,點(diǎn)擊后顯示對話框界面,詢問你是打開相冊還是相機(jī)(自動省略顯示對話框的代碼)
第四步:根據(jù)用戶選擇情況,打開相冊或者相機(jī)
第五步:將拍攝的圖片或者相冊選中的圖片進(jìn)行剪裁,將結(jié)果保存在指定內(nèi)存區(qū)域
第六步:更新頭像圖片
具體實(shí)現(xiàn):
第一步:自定義一個類,繼承ImageView,重寫draw方法,實(shí)現(xiàn)外觀為圓形
//圓形頭像類 public class MyRoundPhoto extends ImageView{ private Paint p; private Bitmap bitmap; private Context context; private int wAndHeight[]=new int[2]; private File file; public MyRoundPhoto(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub //獲得控件長寬(px) wAndHeight = getWidthAndHeight(context,attrs); this.context = context; //初始化控件 init(); } public MyRoundPhoto(Context context) { super(context); // TODO Auto-generated constructor stub //獲得控件長寬(px) wAndHeight=getWidthAndHeight(context,attrs); this.context = context; init(); } public MyRoundPhoto(Context context, AttributeSet attrs) { super(context, attrs); //獲得控件長寬(px) wAndHeight=getWidthAndHeight(context,attrs); // TODO Auto-generated constructor stub this.context = context; init(); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); canvas.drawBitmap(bitmap, new Matrix(), p); } private void init(){ //從手機(jī)存儲區(qū)域獲取圖片文件(該位置為手機(jī)相冊選中的圖片經(jīng)過剪裁后的圖片的存儲路徑) file = new File(Environment.getExternalStorageDirectory(),Info.PHOTO_NAME); //如果圖片文件存在,則顯示,否則則創(chuàng)建并顯示 if(file.exists()){ Log.v("文件存在", "是"); this.bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); } else{ Log.v("文件不存在", "是"); //生成默認(rèn)圖片的文件 this.bitmap=BitmapFactory.decodeStream(context.getResources().openRawResource(R.drawable.defalut)); //person.setPicture() FileOutputStream fos=null; try { fos = new FileOutputStream(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); //壓縮 try { fos.flush(); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //將方形的位圖轉(zhuǎn)換為圓形的位圖 this.bitmap = toRoundBitmap(this.bitmap); p = new Paint(); } private Bitmap toRoundBitmap(Bitmap map){ //int height = map.getHeight()+100; int height=convertDIP2PX(context,this.wAndHeight[1]); //位圖的高度(px) int width = convertDIP2PX(context,this.wAndHeight[0]);//位圖的寬度(px) //創(chuàng)建畫布 Bitmap bit = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(bit); //畫筆 Paint paint = new Paint(); paint.setAntiAlias(false); int r = (width>height)?height:width; //繪制圓形 RectF rectF = new RectF(0,0,r,r); canvas.drawRoundRect(rectF, r/2, r/2, paint); //畫頭像 //canvas.drawARGB(0, 0, 0, 0); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(map, null,rectF, paint); //返回圓形位圖 return bit; } //使當(dāng)前視圖無效,從而使系統(tǒng)重新繪制視圖 public void myValidate(){ bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); bitmap=toRoundBitmap(bitmap); invalidate(); } //將dp轉(zhuǎn)換為px private static int convertDIP2PX(Context context, int dip) { float scale = context.getResources().getDisplayMetrics().density; return (int)(dip*scale + 0.5f*(dip>=0?1:-1)); } //根據(jù)xml文件中的屬性,返回寬高(px) private static int[] getWidthAndHeight(Context context,AttributeSet attrs){ int height,width; int n = attrs.getAttributeCount(); int wAndH[] = new int[2]; for(int i=0;i<n;i++){ String str = attrs.getAttributeName(i); //獲取寬度 if(str.equals("layout_width")){ //System.out.println(attrs.getAttributeName(0)); String sttr = attrs.getAttributeValue(i); String temp = ""; int j=0; while(sttr.charAt(j)>='0'&&sttr.charAt(j)<='9'){ temp+=sttr.charAt(j); j++; } wAndH[0]=Integer.parseInt(temp); temp=""; continue; } //獲取長度 if(str.equals("layout_height")){ //System.out.println(attrs.getAttributeName(1)); String sttr = attrs.getAttributeValue(i); String temp = ""; int j=0; while(sttr.charAt(j)>='0'&&sttr.charAt(j)<='9'){ temp+=sttr.charAt(j); j++; } //System.out.println("temp"+temp); wAndH[1]=Integer.parseInt(temp); temp=""; continue; } } return wAndH; } }
第二步:在xml文件中引用該控件
<com.包名.MyRoundPhoto android:id="@+id/myRoundPhoto" android:layout_width="100dp" android:layout_height="100dp" > </com.包名.MyRoundPhoto>
第三步:實(shí)現(xiàn)圓形頭像的點(diǎn)擊事件,點(diǎn)擊后顯示對話框界面,詢問你是打開相冊還是相機(jī)(自動省略顯示對話框的代碼)
public void onClick(View v) { // TODO Auto-generated method stub //點(diǎn)擊頭像 if(v.getId()==R.id.myRoundPhoto){ //打開DialogActivity,詢問打開照相機(jī)還是相冊 Intent intent = new Intent(GuideActivity.this,DialogActivity.class); startActivityForResult(intent, Info.PICK_PHOTO); } }
第四步:根據(jù)用戶選擇情況,打開相冊或者相機(jī)
image =new File(Environment.getExternalStorageDirectory(),Info.PHOTO_NAME); public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ //打開相機(jī) case R.id.imageButton1:{ Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image)); startActivityForResult(intent, Info.OPEN_CAMERA); break; } //打開相冊 case R.id.imageButton2:{ Intent intent = new Intent(Intent.ACTION_PICK); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image)); intent.setType("image/*"); startActivityForResult(intent, Info.OPEN_GALLERY); break; } } }
第五步:將拍攝的圖片或者相冊選中的圖片進(jìn)行剪裁,將結(jié)果保存在指定內(nèi)存區(qū)域
protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch(requestCode){ //打開相機(jī) case Info.OPEN_CAMERA:{ if(resultCode==RESULT_OK){ //啟動裁剪activity Log.v("啟動剪裁程序", "是的"); Intent intent1 = new Intent("com.android.camera.action.CROP"); intent1.setDataAndType(Uri.fromFile(image), "image/*"); intent1.putExtra("crop", "true"); intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));// intent1.putExtra("aspectX", 1); intent1.putExtra("aspectY", 1); intent1.putExtra("outputFormat", Bitmap.CompressFormat.JPEG); intent1.putExtra("outputX", 720); intent1.putExtra("outputY", 720); intent1.putExtra("return-data", false); startActivityForResult(intent1, Info.CROP_PHOTO); } break; } //打開相冊 case Info.OPEN_GALLERY:{ if(resultCode==RESULT_OK){ //啟動剪裁程序 Log.v("啟動剪裁程序", "是的"); Intent intent1 = new Intent("com.android.camera.action.CROP"); intent1.setDataAndType(Uri.fromFile(image), "image/*"); intent1.putExtra("crop", "true"); intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));// intent1.putExtra("aspectX", 1); intent1.putExtra("aspectY", 1); intent1.putExtra("outputFormat", Bitmap.CompressFormat.JPEG); intent1.putExtra("outputX", 720); intent1.putExtra("outputY", 720); intent1.putExtra("return-data", false); startActivityForResult(intent1, Info.CROP_PHOTO); } break; } //裁剪圖片 case Info.CROP_PHOTO:{ Intent intent=new Intent(); setResult(this.RESULT_OK, intent); finish(); break; } } }
第六步:更新頭像圖片
protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch(requestCode){ //選擇頭像 case Info.PICK_PHOTO:{ //如果攝取圖片成功 if(resultCode==RESULT_OK){ Log.v("requstCodeGuideOne", "PICK_PHOTO"); btn_choosePhoto.myValidate(); //使原有視圖無效,從而使系統(tǒng)重新繪制視圖 } break; } default:{ break; } } }
注意:需要添加的權(quán)限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android調(diào)用系統(tǒng)圖片裁剪限定尺寸及7.0照相問題的解決方法
- Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
- android調(diào)用原生圖片裁剪后圖片尺寸縮放的解決方法
- Android ImageView實(shí)現(xiàn)圖片裁剪和顯示功能
- Android 7.0中拍照和圖片裁剪適配的問題詳解
- Android圖片裁剪功能實(shí)現(xiàn)代碼
- Android開發(fā)從相機(jī)或相冊獲取圖片裁剪
- 使用Java代碼在Android中實(shí)現(xiàn)圖片裁剪功能
- android實(shí)現(xiàn)圖片裁剪的兩種方法
相關(guān)文章
Android 高版本API方法在低版本系統(tǒng)上的兼容性處理
本文主要介紹Android 高版本API方法在低版本系統(tǒng)上的兼容性處理的問題,這里提供了解決辦法,并附簡單示例,來詳細(xì)說明解決問題步驟,有需要的小伙伴可以參考下2016-09-09Android GZip的使用-開發(fā)中網(wǎng)絡(luò)請求的壓縮實(shí)例詳解
這篇文章主要介紹了Android GZip的使用-開發(fā)中網(wǎng)絡(luò)請求的壓縮實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-11-11Android Activity啟動模式之singleTop實(shí)例詳解
這篇文章主要介紹了Android Activity啟動模式之singleTop,結(jié)合實(shí)例形式較為詳細(xì)的分析了singleTop模式的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01Android自定義實(shí)現(xiàn)轉(zhuǎn)盤菜單
旋轉(zhuǎn)菜單是一種占用空間較大,實(shí)用性稍弱的UI,本文主要為大家詳細(xì)介紹了Android如何自定義實(shí)現(xiàn)轉(zhuǎn)盤菜單,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下2023-12-12基于Android實(shí)現(xiàn)桌面懸浮清內(nèi)存app概述
最近沒有項(xiàng)目做,于是寫了個小程序練練手,android桌面懸浮清內(nèi)存app概述,感興趣的朋友一起學(xué)習(xí)吧2015-12-12