Andoroid實(shí)現(xiàn)底部圖片選擇Dialog效果
1.效果圖如下
點(diǎn)擊選擇照相后,彈出如下選擇對(duì)話框:

2. Dialog實(shí)現(xiàn)
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/abroad_takephoto" android:layout_width="match_parent" android:layout_height="@dimen/abroad_dialog_item_hight" android:background="@drawable/abroad_dialogitem_selector" android:gravity="center" android:text="@string/abroad_photo" android:textColor="@color/abroad_dialog_textcolor" android:textSize="@dimen/abroad_dialog_textsize" /> <View android:layout_width="match_parent" android:layout_height="@dimen/abroad_dialog_view_hight" android:background="@color/abroad_dialog_view_bg" /> <TextView android:id="@+id/abroad_choosephoto" android:layout_width="match_parent" android:layout_height="@dimen/abroad_dialog_item_hight" android:background="@drawable/abroad_dialogitem_selector" android:gravity="center" android:text="@string/abroad_choosephotp" android:textColor="@color/abroad_dialog_textcolor" android:textSize="@dimen/abroad_dialog_textsize" /> <TextView android:id="@+id/abroad_choose_cancel" android:layout_width="match_parent" android:layout_height="@dimen/abroad_dialog_item_hight" android:layout_marginTop="@dimen/abroad_feedback_top" android:background="@drawable/abroad_dialogitem_selector" android:gravity="center" android:text="@string/abroad_cancel" android:textColor="@color/abroad_dialog_textcolor" android:textSize="@dimen/abroad_dialog_textsize" /> </LinearLayout>
上面的高度和顏色,文字:
<color name="abroad_dialog_item">#ffffff</color> <color name="abroad_dialog_item_press">#dfdfdf</color> <color name="abroad_dialog_textcolor">#222222</color> <color name="abroad_dialog_view_bg">#cccccc</color> <dimen name="abroad_dialog_item_hight">45dp</dimen> <dimen name="abroad_feedback_top">8dp</dimen> <dimen name="abroad_dialog_textsize">18sp</dimen> <string name="abroad_photo">拍照</string> <string name="abroad_choosephotp">從相冊(cè)選擇</string> <string name="abroad_cancel">取消</string>
控件selector
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/abroad_dialog_item_press" android:state_pressed="true" /> <item android:drawable="@color/abroad_dialog_item" /> </selector>
Dialog 創(chuàng)建
在style文件里面添加主題及dialog彈出動(dòng)畫
<style name="ActionSheetDialogStyle" parent="@android:style/Theme.Dialog"> <!-- 背景透明 --> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowContentOverlay">@null</item> <!-- 浮于Activity之上 --> <item name="android:windowIsFloating">true</item> <!-- 邊框 --> <item name="android:windowFrame">@null</item> <!-- Dialog以外的區(qū)域模糊效果 --> <item name="android:backgroundDimEnabled">true</item> <!-- 無(wú)標(biāo)題 --> <item name="android:windowNoTitle">true</item> <!-- 半透明 --> <item name="android:windowIsTranslucent">true</item> <!-- Dialog進(jìn)入及退出動(dòng)畫 --> <item name="android:windowAnimationStyle">@style/style_inner_map_dialog_animation</item> <style name="style_inner_map_dialog_animation"> <!--dialog的進(jìn)出動(dòng)畫--> <item name="android:windowEnterAnimation">@anim/scale_alpha_to_enter</item> <item name="android:windowExitAnimation">@anim/scale_alpha_to_exit</item> </style>
dialog創(chuàng)建
private TextView cancel;
private TextView takePhoto;
private TextView choosePhoto;
private Dialog dialog;
public void chosePhotoDialog() {
dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
inflate = LayoutInflater.from(this).inflate(R.layout.view_abroad_choosephoto_dialog, null);
choosePhoto = (TextView) inflate.findViewById(R.id.abroad_choosephoto);
takePhoto = (TextView) inflate.findViewById(R.id.abroad_takephoto);
cancel = (TextView) inflate.findViewById(R.id.abroad_choose_cancel);
choosePhoto.setOnClickListener(this);
takePhoto.setOnClickListener(this);
cancel.setOnClickListener(this);
dialog.setContentView(inflate);
Window window = dialog.getWindow();
if (dialog != null && window != null) {
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams attr = window.getAttributes();
if (attr != null) {
attr.height = ViewGroup.LayoutParams.WRAP_CONTENT;
attr.width = ViewGroup.LayoutParams.MATCH_PARENT;
attr.gravity = Gravity.BOTTOM;//設(shè)置dialog 在布局中的位置
window.setAttributes(attr);
}
}
dialog.show();
}
Dialig 點(diǎn)擊事件
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.abroad_choosephoto:
pickAlbum();
break;
case R.id.abroad_takephoto:
takePhotos();
break;
case R.id.abroad_choose_cancel:
dialog.dismiss();
}
dialog.dismiss();
}
3. 選擇圖片
定義事件類型
private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照 private static final int PHOTO_REQUEST_GALLERY = 2;// 從相冊(cè)中選擇 private static final int PHOTO_REQUEST_CUT = 3;// 結(jié)果
從相冊(cè)選取圖片
/***
* 進(jìn)入系統(tǒng)相冊(cè)界面
*/
private void pickAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
手機(jī)拍照后選取圖片
protected void takePhotos() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PHOTO_REQUEST_CAREMA);
}
圖片選擇后,最終都會(huì)把數(shù)據(jù)返回到onActivityResult()方法里面,所以我們需要在activity里面重寫此方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PHOTO_REQUEST_GALLERY:
if (data != null) {
Uri uri = handleImage(data);
cropPhoto(uri);
}
break;
case PHOTO_REQUEST_CAREMA:
if (resultCode == RESULT_CANCELED) {
return;
}
if (data != null) {
Bitmap photo = data.getParcelableExtra("data");
//將Bitmap轉(zhuǎn)化為uri
Uri uri = saveBitmap(photo, "temp");
//啟動(dòng)圖像裁剪
cropPhoto(uri);
}
break;
case PHOTO_REQUEST_CUT:
LogUtil.d("abroadUseActivity2", "裁剪");
// 從剪切圖片返回的數(shù)據(jù)
if (data == null) {
return;
}
bitmap = data.getParcelableExtra("data");
if (bitmap == null) {//
return;
}
// TODO 此處我們便獲得了bitmap對(duì)象,做其他操作
bitmap.recycle();
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
裁剪的方法
private void cropPhoto(Uri uri) {
// 裁剪圖片意圖
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// 裁剪框的比例,1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 裁剪后輸出圖片的尺寸大小
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
intent.putExtra("outputFormat", "JPEG");// 圖片格式
intent.putExtra("noFaceDetection", true);// 取消人臉識(shí)別
intent.putExtra("return-data", true);
// 開啟一個(gè)帶有返回值的Activity,請(qǐng)求碼為PHOTO_REQUEST_CUT
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
拍照后需要先把數(shù)據(jù)保存一個(gè)臨時(shí)的文件,然后再獲取文件,才能裁剪
/**
* 把bitmap保存到本地
*
* @param bm bitmap
* @param dirPath 路徑
* @return 文件的uri
*/
private Uri saveBitmap(Bitmap bm, String dirPath) {
//新建文件夾用于存放裁剪后的圖片
File tmpDir = new File(Environment.getExternalStorageDirectory() + "/" + dirPath);
if (!tmpDir.exists()) {
tmpDir.mkdir();
}
//新建文件存儲(chǔ)裁剪后的圖片
File img = new File(tmpDir.getAbsolutePath() + "/feedback.png");
try {
//打開文件輸出流
FileOutputStream fos = new FileOutputStream(img);
//將bitmap壓縮后寫入輸出流(參數(shù)依次為圖片格式、圖片質(zhì)量和輸出流)
bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
//返回File類型的Uri
return Uri.fromFile(img);
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
4.注意事項(xiàng)
本來(lái)選擇后不打算裁剪,但是在小米6等手機(jī)上,不裁剪容易崩潰,而裁剪的另一個(gè)好處就是壓縮圖片
在我們獲取bitmap后,可以在那里做一些業(yè)務(wù)操作,但是一定要記得把bitmap文件回收,不然容易導(dǎo)致內(nèi)存泄漏
總結(jié)
以上所述是小編給大家介紹的Andoroid實(shí)現(xiàn)底部圖片選擇Dialog效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android實(shí)現(xiàn)本地圖片選擇及預(yù)覽縮放效果
- Android選擇圖片或拍照?qǐng)D片上傳到服務(wù)器
- Android拍照或從圖庫(kù)選擇圖片并裁剪
- Android 中自定義Dialog樣式的Activity點(diǎn)擊空白處隱藏軟鍵盤功能(dialog不消失)
- Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對(duì)話框的功能
- Android實(shí)現(xiàn)簡(jiǎn)潔的APP更新dialog數(shù)字進(jìn)度條
- Android常用的AlertDialog對(duì)話框及自定義對(duì)話框
- Android中ProgressDialog的dismiss()與cancel()方法的區(qū)別
相關(guān)文章
Android登陸界面實(shí)現(xiàn)清除輸入框內(nèi)容和震動(dòng)效果
這篇文章主要介紹了Android登陸界面實(shí)現(xiàn)清除輸入框內(nèi)容和震動(dòng)效果,感興趣的小伙伴們可以參考一下2015-12-12
Android UI實(shí)現(xiàn)底部切換標(biāo)簽fragment
這篇文章主要為大家詳細(xì)介紹了Android UI實(shí)現(xiàn)底部切換標(biāo)簽的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android開發(fā)之圖形圖像與動(dòng)畫(一)Paint和Canvas類學(xué)習(xí)
Paint類代表畫筆,用來(lái)描述圖形的顏色和風(fēng)格,如線寬,顏色,透明度和填充效果等信息;Canvas類代表畫布,通過該類提供的構(gòu)造方法,可以繪制各種圖形;感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助2013-01-01
Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例
這篇文章主要介紹了Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android實(shí)現(xiàn)淘寶倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)淘寶倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Android實(shí)現(xiàn)背景圖滑動(dòng)變大松開回彈效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)背景圖滑動(dòng)變大松開回彈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android 兩個(gè)Service的相互監(jiān)視實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 兩個(gè)Service的相互監(jiān)視實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10

