Andoroid实现底部图片选择Dialog效果

Andoroid实现底部图片选择Dialog效果,第1张

概述1.效果图如下点击选择照相后,d出如下选择对话框:2.Dialog实现布局<LinearLayoutxmlns:android=\"http://schemas.android.com/apk/res/android\"

1.效果图如下

点击选择照相后,d出如下选择对话框:

2. Dialog实现

布局

<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">从相册选择</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 创建

在style文件里面添加主题及dialogd出动画

<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以外的区域模糊效果 --> <item name="androID:backgroundDimEnabled">true</item> <!-- 无标题 --> <item name="androID:windowNoTitle">true</item> <!-- 半透明 --> <item name="androID:windowIsTranslucent">true</item> <!-- Dialog进入及退出动画 --> <item name="androID:windowAnimationStyle">@style/style_inner_map_dialog_animation</item><style name="style_inner_map_dialog_animation"> <!--dialog的进出动画--> <item name="androID:windowEnteranimation">@anim/scale_Alpha_to_enter</item> <item name="androID:windowExitAnimation">@anim/scale_Alpha_to_exit</item></style>

dialog创建

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);  WindowManager.LayoutParams attr = window.getAttributes();  if (attr != null) {   attr.height = VIEwGroup.LayoutParams.WRAP_CONTENT;   attr.wIDth = VIEwGroup.LayoutParams.MATCH_PARENT;   attr.gravity = Gravity.BottOM;//设置dialog 在布局中的位置   window.setAttributes(attr);  } } dialog.show();}

Dialig 点击事件

 @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;// 从相册中选择 private static final int PHOTO_REQUEST_CUT = 3;// 结果

从相册选取图片

 /***  * 进入系统相册界面  */ 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); }

手机拍照后选取图片

 protected voID takePhotos() {  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  startActivityForResult(intent,PHOTO_REQUEST_CAREMA); }

图片选择后,最终都会把数据返回到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转化为uri     Uri uri = saveBitmap(photo,"temp");     //启动图像裁剪     cropPhoto(uri);    }    break;   case PHOTO_REQUEST_CUT:    LogUtil.d("abroadUseActivity2","裁剪");    // 从剪切图片返回的数据    if (data == null) {     return;    }    bitmap = data.getParcelableExtra("data");    if (bitmap == null) {//     return;    }    // Todo 此处我们便获得了bitmap对象,做其他 *** 作    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);// 取消人脸识别  intent.putExtra("return-data",true);  // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT  startActivityForResult(intent,PHOTO_REQUEST_CUT); }

拍照后需要先把数据保存一个临时的文件,然后再获取文件,才能裁剪

/**  * 把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();  }  //新建文件存储裁剪后的图片  file img = new file(tmpDir.getabsolutePath() + "/Feedback.png");  try {   //打开文件输出流   fileOutputStream fos = new fileOutputStream(img);   //将bitmap压缩后写入输出流(参数依次为图片格式、图片质量和输出流)   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.注意事项

本来选择后不打算裁剪,但是在小米6等手机上,不裁剪容易崩溃,而裁剪的另一个好处就是压缩图片
在我们获取bitmap后,可以在那里做一些业务 *** 作,但是一定要记得把bitmap文件回收,不然容易导致内存泄漏

总结

以上所述是小编给大家介绍的AndoroID实现底部图片选择Dialog效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Andoroid实现底部图片选择Dialog效果全部内容,希望文章能够帮你解决Andoroid实现底部图片选择Dialog效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1144157.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存