Android之圓形頭像(實現相機拍攝+相簿選擇+圖片裁剪功能)
阿新 • • 發佈:2019-02-11
功能實現:點選圓形頭像之後可以實現相簿上傳或者開啟相機,然後把得到的圖片經過剪裁,把剪裁過的圖片設定為頭像的背景圖
步驟:第一步:自定義一個類,繼承ImageView,重寫draw方法,實現外觀為圓形
第二步:在xml檔案中引用該控制元件
第三步:實現圓形頭像的點選事件,點選後顯示對話方塊介面,詢問你是開啟相簿還是相機(自動省略顯示對話方塊的程式碼)
第四步:根據使用者選擇情況,開啟相簿或者相機
第五步:將拍攝的圖片或者相簿選中的圖片進行剪裁,將結果儲存在指定記憶體區域
第六步:更新頭像圖片
具體實現:第一步:自定義一個類,繼承ImageView,重寫draw方法,實現外觀為圓形
//圓形頭像類
public classMyRoundPhotoextendsImageView{
private Paint p;
private Bitmap bitmap;
private Context context;
private int wAndHeight[]=new int[2];
private File file;
publicMyRoundPhoto(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
//獲得控制元件長寬(px)
wAndHeight = getWidthAndHeight(context,attrs);
this.context = context;
//初始化控制元件
init();
}
publicMyRoundPhoto(Context context){
super(context);
// TODO Auto-generated constructor stub
//獲得控制元件長寬(px)
wAndHeight=getWidthAndHeight(context,attrs);
this .context = context;
init();
}
publicMyRoundPhoto(Context context, AttributeSet attrs){
super(context, attrs);
//獲得控制元件長寬(px)
wAndHeight=getWidthAndHeight(context,attrs);
// TODO Auto-generated constructor stub
this.context = context;
init();
}
@Override
protectedvoidonDraw(Canvas canvas){
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(bitmap, new Matrix(), p);
}
privatevoidinit(){
//從手機儲存區域獲取圖片檔案(該位置為手機相簿選中的圖片經過剪裁後的圖片的儲存路徑)
file = new File(Environment.getExternalStorageDirectory(),Info.PHOTO_NAME);
//如果圖片檔案存在,則顯示,否則則建立並顯示
if(file.exists()){
Log.v("檔案存在", "是");
this.bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
}
else{
Log.v("檔案不存在", "是");
//生成預設圖片的檔案
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();
}
}
//將方形的點陣圖轉換為圓形的點陣圖
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)
//建立畫布
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;
}
//使當前檢視無效,從而使系統重新繪製檢視
publicvoidmyValidate(){
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
bitmap=toRoundBitmap(bitmap);
invalidate();
}
//將dp轉換為px
privatestaticintconvertDIP2PX(Context context, int dip){
float scale = context.getResources().getDisplayMetrics().density;
return (int)(dip*scale + 0.5f*(dip>=0?1:-1));
}
//根據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.包名.MyRoundPhotoandroid:id="@+id/myRoundPhoto"android:layout_width="100dp"android:layout_height="100dp" >
</com.包名.MyRoundPhoto>
第三步:實現圓形頭像的點選事件,點選後顯示對話方塊介面,詢問你是開啟相簿還是相機(自動省略顯示對話方塊的程式碼)
publicvoidonClick(View v){
// TODO Auto-generated method stub
//點選頭像
if(v.getId()==R.id.myRoundPhoto){
//開啟DialogActivity,詢問開啟照相機還是相簿
Intent intent = new Intent(GuideActivity.this,DialogActivity.class);
startActivityForResult(intent, Info.PICK_PHOTO);
}
}
第四步:根據使用者選擇情況,開啟相簿或者相機
image =new File(Environment.getExternalStorageDirectory(),Info.PHOTO_NAME);
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
//開啟相機
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;
}
}
}
第五步:將拍攝的圖片或者相簿選中的圖片進行剪裁,將結果儲存在指定記憶體區域
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
//開啟相機
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(); //使原有檢視無效,從而使系統重新繪製檢視
}
break;
}
default:{
break;
}
}
}
注意:需要新增的許可權
<uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>