栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

1.效果图如下

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

2. Dialog实现

布局


 
 
 
 

上面的高度和颜色,文字:

 #ffffff
 #dfdfdf
 #222222
 #cccccc
 45dp
 8dp
 18sp
 拍照
 从相册选择
 取消

控件selector



 
 

Dialog 创建

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


 
 @android:color/transparent
 @null
 
 true
 
 @null
 
 true
 
 true
 
 true
 
 @style/style_inner_map_dialog_animation


 
 @anim/scale_alpha_to_enter
 @anim/scale_alpha_to_exit

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
 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效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/157662.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号