在讓用戶自定義頭像或皮膚的時候 可能會採取照相或選取相冊等方法來實現用戶自定義,這裏我們就簡單實現一下~
在這裏先貼出佈局-----僅供參考
activity_main://一張圖片而已 很簡單
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/img_true"
/>
activity_select_photo://選擇圖片 其中有一些圖片資源和動畫,在文末分享給大家
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<LinearLayout
android:id="@+id/dialog_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginBottom="10dp"
android:gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/select_photo_up_bg"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<Button
android:id="@+id/btn_take_photo"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:background="@drawable/select_photo_bg"
android:text="@string/paizhaoxuanqu"
android:textStyle="bold" />
<View
android:layout_width="fill_parent"
android:layout_height="0.5px"
android:background="#828282" />
<Button
android:id="@+id/btn_pick_photo"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_marginTop="0dip"
android:background="@drawable/select_photo_bg"
android:text="@string/xiangcexuanqu"
android:textStyle="bold" />
</LinearLayout>
<Button
android:id="@+id/btn_cancel"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_marginTop="20dip"
android:background="@drawable/select_photo_bg"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="@string/exit"
android:textColor="#ffff0000"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
-----------MainActivity-----------
圖片監聽事件 點擊跳轉選擇頁面使用回調
/** 選擇文件 */
public static final int TO_SELECT_PHOTO = 1;
//圖片選擇事件
imgtrue.setOnClickListener(new OnClickListener() {
Intent intent;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent(this, SelectPhotoActivity.class);
startActivityForResult(intent, TO_SELECT_PHOTO);
intent = null;
}
});
回調方法 接收圖片地址 設置給控件
@SuppressWarnings("deprecation")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == TO_SELECT_PHOTO) {
picPath = data.getStringExtra(SelectPhotoActivity.KEY_PHOTO_PATH);
Bitmap bm = BitmapFactory.decodeFile(picPath);
zoomBitmap = zoomBitmap(bm, 300, 300);
imgtrue.setBackgroundDrawable(new BitmapDrawable(bm));
}
super.onActivityResult(requestCode, resultCode, data);
}
將獲取的圖片按寬高進行縮放
/**
* 將原圖按照指定的寬高進行縮放
*
* @param oldBitmap
* @param newWidth
* @param newHeight
* @return
*/
private Bitmap zoomBitmap(Bitmap oldBitmap, int newWidth, int newHeight) {
int width = oldBitmap.getWidth();
int height = oldBitmap.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newBitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(newBitmap, matrix, null);
return newBitmap;
}
------------------SelectPhotoActivity------------------
//圖片選擇
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SelectPhotoActivity extends Activity implements OnClickListener {
/** 使用照相機拍照獲取圖片 */
public static final int SELECT_PIC_BY_TACK_PHOTO = 1;
/** 使用相冊中的圖片 */
public static final int SELECT_PIC_BY_PICK_PHOTO = 2;
/** 開啓相機 */
private Button btn_take_photo;
/** 開啓圖冊 */
private Button btn_pick_photo;
/** 取消 */
private Button btn_cancel;
/** 獲取到的圖片路徑 */
private String picPath;
private Intent lastIntent;
private Uri photoUri;
/** 從Intent獲取圖片路徑的KEY */
public static final String KEY_PHOTO_PATH = "photo_path";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_photo);
btn_take_photo = (Button) findViewById(R.id.btn_take_photo);
btn_pick_photo = (Button) findViewById(R.id.btn_pick_photo);
btn_cancel = (Button) findViewById(R.id.btn_cancel);
lastIntent = getIntent();
btn_take_photo.setOnClickListener(this);
btn_pick_photo.setOnClickListener(this);
btn_cancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_take_photo : // 開啓相機
takePhoto();
break;
case R.id.btn_pick_photo : // 開啓圖冊
pickPhoto();
break;
case R.id.btn_cancel : // 取消操作
this.finish();
break;
default :
break;
}
}
private void takePhoto() {
// 執行拍照前,應該先判斷SD卡是否存在
String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
/***
* 需要說明一下,以下操作使用照相機拍照,拍照後的圖片會存放在相冊中的 這裏使用的這種方式有一個好處就是獲取的圖片是拍照後的原圖
* 如果不實用ContentValues存放照片路徑的話,拍照後獲取的圖片爲縮略圖不清晰
*/
ContentValues values = new ContentValues();
photoUri = this.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
} else {
Toast.makeText(getApplicationContext(), "內存卡不存在",
Toast.LENGTH_SHORT).show();
}
}
/***
* 從相冊中取圖片
*/
private void pickPhoto() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
finish();
return super.onTouchEvent(event);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
doPhoto(requestCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 選擇圖片後,獲取圖片的路徑
*/
private void doPhoto(int requestCode, Intent data) {
if (requestCode == SELECT_PIC_BY_PICK_PHOTO) {// 從相冊取圖片,有些手機有異常情況,請注意
if (data == null) {
Toast.makeText(getApplicationContext(), "選擇圖片文件出錯",
Toast.LENGTH_SHORT).show();
return;
}
photoUri = data.getData();
if (photoUri == null) {
Toast.makeText(getApplicationContext(), "選擇圖片文件出錯",
Toast.LENGTH_SHORT).show();
return;
}
}
String[] pojo = {MediaStore.Images.Media.DATA};
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(photoUri, pojo, null, null, null);
if (cursor != null) {
int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
cursor.close();
}
if (picPath != null
&& (picPath.endsWith(".png") || picPath.endsWith(".PNG")
|| picPath.endsWith(".jpg") || picPath.endsWith(".JPG"))) {
lastIntent.putExtra(KEY_PHOTO_PATH, picPath);
setResult(Activity.RESULT_OK, lastIntent);
finish();
} else {
Toast.makeText(getApplicationContext(), "選擇圖片文件不正確",
Toast.LENGTH_SHORT).show();
}
}
}
其中會使用到intent的跳轉,我們可以在跳轉期間添加動畫以及背景透明:
<activity
android:name="com.seven.activity.SelectPhotoActivity"
android:screenOrientation="portrait"
android:theme="@style/DialogStyleBottom" >
</activity>
資源分享:
動畫anim
push_bottom_in.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="200"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>
push_bottom_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑出式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="200"
android:fromYDelta="0"
android:toYDelta="50%p" />
</set>
strings.xml資源
<string name="paizhaoxuanqu">拍照選取</string>
<string name="xiangcexuanqu">相冊選取</string>
<string name="exit">取消</string>
stype.xml資源
<!-- 選取照片的Activity的樣式風格,採取對話框的風格 -->
<style name="AnimBottom" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
<item name="android:windowExitAnimation">@anim/push_bottom_out</item>
</style>
<style name="DialogStyleBottom" parent="android:Theme.Dialog">
<item name="android:windowAnimationStyle">@style/AnimBottom</item>
<item name="android:windowFrame">@null</item>
<!-- 邊框 -->
<item name="android:windowIsFloating">false</item>
<!-- 是否浮現在activity之上 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 半透明 -->
<item name="android:windowNoTitle">true</item>
<!-- 無標題 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 背景透明 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 模糊 -->
</style>
圖片資源: