欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-17 13:53:46
1.效果图如下 点击选择照相后,弹出如下选择对话框: 2. dialog实现 布局

1.效果图如下

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

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

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文件里面添加主题及dialog弹出动画

<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, 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效果,希望对大家有所帮助

上一篇:

下一篇: