編輯:關於 Android 開發
本文整理自公開教學,示範如何製作一個可從本機相簿選圖或直接拍照、並進行裁剪的頭像設定功能。
功能簡介
開發 Android 應用時,使用者註冊功能幾乎是必備,往往還需要頭像編輯能力。設定流程如下圖:
介面設計
建立一個縮圖 ImageView,點擊時彈出設定頭像的對話框,設定完成後刷新縮圖:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F3F1DA"
android:orientation="vertical" >
<!-- title -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/title_bg"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/title_bar_txt"
android:textColor="@android:color/white" />
</LinearLayout>
<!-- image switch -->
<RelativeLayout
android:id="@+id/switch_face_rl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dip"
android:background="@drawable/item_edit_bg"
android:clickable="true"
android:padding="5dip" >
<ImageView
android:id="@+id/face"
android:layout_width="42dip"
android:layout_height="42dip"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dip"
android:src="@drawable/mini_avatar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:layout_toRightOf="@id/face"
android:layout_gravity="center"
android:text="點擊設置頭像"
android:textColor="@android:color/black" />
</RelativeLayout>
</LinearLayout>
Activity 設計
MainActivity.java:
public class MainActivity extends Activity {
private RelativeLayout switchAvatar;
private ImageView faceImage;
private String[] items = new String[] { "選擇本地圖片", "拍照" };
private static final String IMAGE_FILE_NAME = "faceImage.jpg";
private static final int IMAGE_REQUEST_CODE = 0;
private static final int CAMERA_REQUEST_CODE = 1;
private static final int RESULT_REQUEST_CODE = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
switchAvatar = (RelativeLayout) findViewById(R.id.switch_face_rl);
faceImage = (ImageView) findViewById(R.id.face);
switchAvatar.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
};
/** 顯示選擇對話框 */
private void showDialog() {
new AlertDialog.Builder(this)
.setTitle("設置頭像")
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Intent intentFromGallery = new Intent();
intentFromGallery.setType("image/*");
intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intentFromGallery, IMAGE_REQUEST_CODE);
break;
case 1:
Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Tools.hasSdcard()) {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File file = new File(path, IMAGE_FILE_NAME);
intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
}
startActivityForResult(intentFromCapture, CAMERA_REQUEST_CODE);
break;
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED) {
switch (requestCode) {
case IMAGE_REQUEST_CODE:
startPhotoZoom(data.getData());
break;
case CAMERA_REQUEST_CODE:
if (Tools.hasSdcard()) {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File tempFile = new File(path, IMAGE_FILE_NAME);
startPhotoZoom(Uri.fromFile(tempFile));
} else {
Toast.makeText(MainActivity.this, "未找到存儲卡,無法存儲照片!", Toast.LENGTH_LONG).show();
}
break;
case RESULT_REQUEST_CODE:
if (data != null) {
getImageToView(data);
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/** 裁剪圖片 */
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 340);
intent.putExtra("outputY", 340);
intent.putExtra("return-data", true);
startActivityForResult(intent, RESULT_REQUEST_CODE);
}
/** 保存並顯示裁剪後的圖片 */
private void getImageToView(Intent data) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
Drawable drawable = new BitmapDrawable(this.getResources(), photo);
faceImage.setImageDrawable(drawable);
}
}
}
Tools.java:
public class Tools {
/** 檢查是否存在 SDCard */
public static boolean hasSdcard() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
}
整體流程是:點擊頭像區塊彈出選擇框,使用者可選相簿或拍照,之後透過 com.android.camera.action.CROP 進行正方形裁剪,最後把結果顯示回 ImageView。此外,Tools.hasSdcard() 用來確認外置儲存是否可用,避免拍照後無法存檔。
ViewFlipper 實現動畫切換屏幕與手勢翻頁
ViewFlipper 是一個專門用於多視圖切換的容器,配合動畫資源和手勢判斷,可以做出類似幻燈片或引導頁的效果。本文記錄完整的實現過程。 效果預覽: 項目結構: 一
Android 定位與地圖導航:自訂公交路線圖層的完整實作
以百度地圖 API 打造自訂公交換乘路線圖 一、問題描述 本範例的目標,是基於百度地圖 API,檢索指定城市、指定公交流線的交通路線,並將路線與關鍵節點以自訂樣式呈現在地
Android TextView 跑馬燈效果實作
TextView 跑馬燈效果 當文字內容過長且不希望換行時,可以使用跑馬燈效果讓文字水平滾動顯示。這在標題、公告或地點名稱等场景中都很實用。 基本原理 Android
Java 檔案大小轉換工具類:B、KB、MB、GB、TB、PB 一鍵換算
Java 檔案大小轉換工具類(B、KB、MB、GB、TB、PB 之間的換算) 當我們需要呈現類似下圖所示的檔案大小效果時,就得先把原始位元組數轉換成人類易讀的單位,再進