日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android选择头像弹窗,Android App开发常用功能之用户头像选择-Go语言中文社区

發(fā)布時間:2023/12/10 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android选择头像弹窗,Android App开发常用功能之用户头像选择-Go语言中文社区 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

現(xiàn)在的APP基本都有個人資料的填寫,基本的都有頭像的選擇,支持拍照和從本地相冊選擇,剪切圓形頭像的功能,現(xiàn)在用個小demo實現(xiàn)以下。

下面看一下效果圖

上代碼:

主界面代碼

package com.example.androidpersonal_icon;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.util.Log;

import android.view.Gravity;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ImageView;

import java.io.File;

import android.app.Activity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

public class MainActivity extends Activity {

protected static final int CHOOSE_PICTURE = 0;

protected static final int TAKE_PICTURE = 1;

private static final int CROP_SMALL_PICTURE = 2;

protected static Uri tempUri;

private ImageView iv_personal_icon;

private SelectPicPopupWindow menuWindow;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// 新建一個用來存儲照片的文件夾

File destDir = new File(Environment.getExternalStorageDirectory() + "/AndroidPersonal_icon");

if (!destDir.exists()) {

destDir.mkdirs();

}

iv_personal_icon = (ImageView) findViewById(R.id.iv_personal_icon);

iv_personal_icon.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// 實例化SelectPicPopupWindow

menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);

// 顯示窗口

menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main),

Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 設(shè)置layout在PopupWindow中顯示的位置

}

});

// 讀取上一次剪切的照片

if (destDir.exists() && destDir.isDirectory()) {

if (destDir.list().length > 0) {

Log.d("111111111112", destDir.toString() + "/image_icon.png");

Bitmap bitmap = BitmapFactory.decodeFile(destDir.toString() + "/image_icon.png");

iv_personal_icon.setImageBitmap(bitmap);

} else {

iv_personal_icon.setBackgroundResource(R.drawable.default_personal_image);

}

}

}

// 為彈出窗口實現(xiàn)監(jiān)聽類

private OnClickListener itemsOnClick = new OnClickListener() {

public void onClick(View v) {

menuWindow.dismiss();

switch (v.getId()) {

case R.id.Layout_take_photo:

Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

tempUri = Uri.fromFile(

new File(Environment.getExternalStorageDirectory() + "/AndroidPersonal_icon", "image.jpg"));

Log.d("11111111", tempUri.toString());

// 指定照片保存路徑(SD卡),image.jpg為一個臨時文件,每次拍照后這個圖片都會被替換

openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);

startActivityForResult(openCameraIntent, TAKE_PICTURE);

break;

case R.id.Layout_pick_photo:

Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);

openAlbumIntent.setType("image/*");

startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);

break;

default:

break;

}

}

};

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) { // 如果返回碼是可以用的

switch (requestCode) {

case TAKE_PICTURE:

startPhotoZoom(tempUri); // 開始對圖片進行裁剪處理

break;

case CHOOSE_PICTURE:

startPhotoZoom(data.getData()); // 開始對圖片進行裁剪處理

break;

case CROP_SMALL_PICTURE:

if (data != null) {

setImageToView(data); // 讓剛才選擇裁剪得到的圖片顯示在界面上

}

break;

}

}

}

/**

* 裁剪圖片方法實現(xiàn)

*

* @param uri

*/

protected void startPhotoZoom(Uri uri) {

if (uri == null) {

Log.i("tag", "The uri is not exist.");

}

tempUri = uri;

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setDataAndType(uri, "image/*");

// 設(shè)置裁剪

intent.putExtra("crop", "true");

// aspectX aspectY 是寬高的比例

intent.putExtra("aspectX", 1);

intent.putExtra("aspectY", 1);

// outputX outputY 是裁剪圖片寬高

intent.putExtra("outputX", 150);

intent.putExtra("outputY", 150);

intent.putExtra("return-data", true);

startActivityForResult(intent, CROP_SMALL_PICTURE);

}

/**

* 保存裁剪之后的圖片數(shù)據(jù)

*

* @param

*

* @param picdata

*/

protected void setImageToView(Intent data) {

Bundle extras = data.getExtras();

if (extras != null) {

Bitmap photo = extras.getParcelable("data");

photo = Utils.toRoundBitmap(photo, tempUri); // 這個時候的圖片已經(jīng)被處理成圓形的了

iv_personal_icon.setImageBitmap(photo);

uploadPic(photo);

}

}

private void uploadPic(Bitmap bitmap) {

// 上傳至服務(wù)器

// ... 可以在這里把Bitmap轉(zhuǎn)換成file,然后得到file的url,做文件上傳操作

// 注意這里得到的圖片已經(jīng)是圓形圖片了

// bitmap是沒有做個圓形處理的,但已經(jīng)被裁剪了

String imagePath = Utils.savePhoto(bitmap,

Environment.getExternalStorageDirectory().getAbsolutePath() + "/AndroidPersonal_icon", "image_icon");

Log.d("imagePath", imagePath + "");

if (imagePath != null) {

// 拿著imagePath上傳了

// ...

}

}

}

圓形頭像剪切代碼:

package com.example.androidpersonal_icon;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import android.graphics.Bitmap;

import android.graphics.Bitmap.Config;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.PorterDuff.Mode;

import android.graphics.PorterDuffXfermode;

import android.graphics.Rect;

import android.graphics.RectF;

import android.net.Uri;

import android.util.Log;

public class Utils {

/**

* Save image to the SD card

*

* @param photoBitmap

* @param photoName

* @param path

*/

public static String savePhoto(Bitmap photoBitmap, String path,

String photoName) {

String localPath = null;

if (android.os.Environment.getExternalStorageState().equals(

android.os.Environment.MEDIA_MOUNTED)) {

File dir = new File(path);

if (!dir.exists()) {

dir.mkdirs();

}

File photoFile = new File(path, photoName + ".png");

FileOutputStream fileOutputStream = null;

try {

fileOutputStream = new FileOutputStream(photoFile);

if (photoBitmap != null) {

if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100,

fileOutputStream)) { // 轉(zhuǎn)換完成

localPath = photoFile.getPath();

fileOutputStream.flush();

}

}

} catch (FileNotFoundException e) {

photoFile.delete();

localPath = null;

e.printStackTrace();

} catch (IOException e) {

photoFile.delete();

localPath = null;

e.printStackTrace();

} finally {

try {

if (fileOutputStream != null) {

fileOutputStream.close();

fileOutputStream = null;

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

return localPath;

}

/**

* 轉(zhuǎn)換圖片成圓形

*

* @param bitmap

* 傳入Bitmap對象

* @param tempUri

* @return

*/

public static Bitmap toRoundBitmap(Bitmap bitmap, Uri tempUri) {

int width = bitmap.getWidth();

int height = bitmap.getHeight();

float roundPx;

float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;

if (width <= height) {

roundPx = width / 2;

left = 0;

top = 0;

right = width;

bottom = width;

height = width;

dst_left = 0;

dst_top = 0;

dst_right = width;

dst_bottom = width;

} else {

roundPx = height / 2;

float clip = (width - height) / 2;

left = clip;

right = width - clip;

top = 0;

bottom = height;

width = height;

dst_left = 0;

dst_top = 0;

dst_right = height;

dst_bottom = height;

}

Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);

Canvas canvas = new Canvas(output);

final int color = 0xff424242;

final Paint paint = new Paint();

final Rect src = new Rect((int) left, (int) top, (int) right,

(int) bottom);

final Rect dst = new Rect((int) dst_left, (int) dst_top,

(int) dst_right, (int) dst_bottom);

final RectF rectF = new RectF(dst);

paint.setAntiAlias(true);// 設(shè)置畫筆無鋸齒

canvas.drawARGB(0, 0, 0, 0); // 填充整個Canvas

paint.setColor(color);

// 以下有兩種方法畫圓,drawRounRect和drawCircle

// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);//

// 畫圓角矩形,第一個參數(shù)為圖形顯示區(qū)域,第二個參數(shù)和第三個參數(shù)分別是水平圓角半徑和垂直圓角半徑。

canvas.drawCircle(roundPx, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 設(shè)置兩張圖片相交時的模式,參考http://trylovecatch.iteye.com/blog/1189452

canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合并bitmap和已經(jīng)draw了的Circle

return output;

}

}

ok,大功告成,最后別忘了在清單文件中添加讀寫sd可權(quán)限,不然得不到imagePath

總結(jié)

以上是生活随笔為你收集整理的android选择头像弹窗,Android App开发常用功能之用户头像选择-Go语言中文社区的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。