拍照选择图片(Activity底部弹出)

效果图如下:

第一步 : 显示出的布局文件:alert_dialog.xml

<?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"
    android:orientation="vertical"
  >
<LinearLayout
    android:id="@+id/pop_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"  //包裹内容
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:layout_alignParentBottom="true"    //底部
     android:background="@drawable/btn_style_alert_dialog_background"  //黑背景
     >

    <Button
        android:id="@+id/btn_take_photo"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="20dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="拍照"
        android:background="@drawable/btn_style_alert_dialog_button"  //选择器
        android:textStyle="bold"
         />

    <Button
        android:id="@+id/btn_pick_photo"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="5dip"
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="从相册选择"
         android:background="@drawable/btn_style_alert_dialog_button"  //选择器
         android:textStyle="bold"
         />

    <Button
        android:id="@+id/btn_cancel"
       android:layout_marginLeft="20dip"
       android:layout_marginRight="20dip"
       android:layout_marginTop="15dip"
       android:layout_marginBottom="15dip"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="取消"
       android:background="@drawable/btn_style_alert_dialog_cancel"  //选择器
       android:textColor="#ffffff"
       android:textStyle="bold"

        />
</LinearLayout>
</RelativeLayout>

选择器:btn_style_alert_dialog_button.xml

<?xml version="1.0" encoding="utf-8"?>  //btn_style_alert_dialog_button.xml
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/btn_style_alert_dialog_button_pressed" />
    <item android:drawable="@drawable/btn_style_alert_dialog_button_normal" />
</selector>

选择器:btn_style_alert_dialog_cancel.xml

<?xml version="1.0" encoding="utf-8"?> //btn_style_alert_dialog_cancel.xml
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/btn_style_alert_dialog_button_pressed" />
    <item android:drawable="@drawable/btn_style_alert_dialog_cancel_normal" />
</selector>

第二部 : 底部弹出的Activity 代码

public class SelectPicPopupWindow extends Activity implements OnClickListener {

    private Button btn_take_photo, btn_pick_photo, btn_cancel;
    private LinearLayout layout;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_dialog);
        intent = getIntent();
        btn_take_photo = (Button) this.findViewById(R.id.btn_take_photo); //拍照
        btn_pick_photo = (Button) this.findViewById(R.id.btn_pick_photo);  //从相册选择
        btn_cancel = (Button) this.findViewById(R.id.btn_cancel);   //取消

        layout = (LinearLayout) findViewById(R.id.pop_layout);

        // 添加选择窗口范围监听可以优先获取触点,即不再执行onTouchEvent()函数,点击其他地方时执行onTouchEvent()函数销毁Activity
        layout.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!",
                        Toast.LENGTH_SHORT).show();
            }
        });
        // 添加按钮监听
        btn_cancel.setOnClickListener(this);
        btn_pick_photo.setOnClickListener(this);
        btn_take_photo.setOnClickListener(this);
    }

    // 实现onTouchEvent触屏函数但点击屏幕时销毁本Activity
    @Override
    public boolean onTouchEvent(MotionEvent event) {  //点击外面退出这activity
        finish();
        return true;
    }

    @Override  //startActivityResult()后调用的这下面方法
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {   // 选择完, 拍照,或者选择图片后调用的方法
        if (resultCode != RESULT_OK) {
            return;
        }
        //选择完或者拍完照后会在这里处理,然后我们继续使用setResult返回Intent以便可以传递数据和调用
        if (data.getExtras() != null)
            intent.putExtras(data.getExtras());   //拍照得到的图片
        if (data.getData()!= null)
            intent.setData(data.getData());   //选择图片得到的数据, 里面有uri
        setResult(1, intent);     // 返回到下面的, MainActivity
        finish();

    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_take_photo:     //拍照
            try {
                //拍照我们用Action为MediaStore.ACTION_IMAGE_CAPTURE,
                //有些人使用其他的Action但我发现在有些机子中会出问题,所以优先选择这个
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        case R.id.btn_pick_photo:    // 选择图片
            try {
                //选择照片的时候也一样,我们用Action为Intent.ACTION_GET_CONTENT,
                //有些人使用其他的Action但我发现在有些机子中会出问题,所以优先选择这个
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent, 2);
            } catch (ActivityNotFoundException e) {

            }
            break;
        case R.id.btn_cancel:
            finish();
            break;
        default:
            break;
        }

    }

}

第二步  主界面的 MainActivity,:

public class MainActivity extends Activity {
    private ImageView photo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        photo = (ImageView) this.findViewById(R.id.text);
        // 把文字控件添加监听,点击弹出自定义窗口
        photo.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //使用startActivityForResult启动SelectPicPopupWindow当返回到此Activity的时候就会调用onActivityResult函数
                startActivityForResult(new Intent(MainActivity.this,
                        SelectPicPopupWindow.class), 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (resultCode) {
        case 1:
            if (data != null) {
                //取得返回的Uri,基本上选择照片的时候返回的是以Uri形式,但是在拍照中有得机子呢Uri是空的,所以要特别注意
                Uri mImageCaptureUri = data.getData();  //选择图片有uri
                //返回的Uri不为空时,那么图片信息数据都会在Uri中获得。如果为空,那么我们就进行下面的方式获取
                if (mImageCaptureUri != null) {
                    Bitmap image;
                    try {
                        //这个方法是根据Uri获取Bitmap图片的静态方法
                        image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageCaptureUri);
                        if (image != null) {
                            photo.setImageBitmap(image);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    Bundle extras = data.getExtras(); //拍照没有uri
                    if (extras != null) {
                        //这里是有些拍照后的图片是直接存放到Bundle中的所以我们可以从这里面获取Bitmap图片
                        Bitmap image = extras.getParcelable("data");
                        if (image != null) {
                            photo.setImageBitmap(image);
                        }
                    }
                }

            }
            break;
        default:
            break;

        }
    }

}

第三步奏 : 清单文件 AndroidManifest.xml

   <activity android:name=".SelectPicPopupWindow" android:theme="@style/MyDialogStyleBottom" /> 
 styles.xml的配置
<resources>
    <style name="AppTheme" parent="android:Theme.Light" />
    <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="MyDialogStyleBottom" parent="android:Theme.Dialog">
        <item name="android:windowAnimationStyle">@style/AnimBottom</item>
            <!-- 边框 -->
        <item name="android:windowFrame">@null</item>
            <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsFloating">true</item>
            <!-- 半透明 -->
        <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>
</resources>
anim文件夹下  push_bottom_in.xml , push_bottom_out.xml
<?xml version="1.0" encoding="utf-8"?>  //push_bottom_in.xml
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
          android:duration="200"
          android:fromYDelta="100%p"
          android:toYDelta="0" />
</set>
<?xml version="1.0" encoding="utf-8"?> //push_bottom_out.xml
<!-- 上下滑出式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="200"
        android:fromYDelta="0"
        android:toYDelta="50%p" />
</set>
 
       
 
				
时间: 2024-08-24 13:55:30

拍照选择图片(Activity底部弹出)的相关文章

Android 底部弹出提示框的解决办法(使用Activity以及PopupWindow)

本片文章主要谈探讨了如何实现在底部弹出提示框背景为半透明效果的实现.想要实现此种效果一般有两种方式一个是使用Activity设置Theme另一种方式就是使用PopupWindow设置样式实现效果. 一,使用Activity 首先是此activity的布局文件: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.andro

转 android 从底部弹出一个popuwindow,渐入渐出效果。我这里是用在购物车需要选择购买选项的操作。

最近要改客户端,需要实现一个从底部弹出的popuwindow,像我这种渣渣android技术,能整出popuwindow但是整不出动画,百度之,记录一下. 从下面这个地址转的 http://blog.csdn.net/yxhuang2008/article/details/42617805 最近因为要用到PopupWindow,所以,就在网上搜索了一下,发现挺多关于这样的文章,现在我把它们整理了一下. 1.Android PopupWindow 的使用技巧,http://www.cnblogs.

【Android】android PopupWindow实现从底部弹出或滑出选择菜单或窗口

转载自:android PopupWindow实现从底部弹出或滑出选择菜单或窗口 Android PopupWindow的使用和分析 Popupwindow的使用 PopupWindow用法

Android Demo---实现从底部弹出窗口

在前面的博文中,小编简单的介绍了如何制作圆角的按钮以及圆角的图片,伴着键盘和手指之间的舞步,迎来新的问题,不知道小伙伴有没有这样的经历,以App为例,点击头像的时候,会从底部弹出一个窗口,有从相册中选择.拍照.取消的字样,点击相应的按钮,完成相应的操作,在小编做项目的过程中遇到类似的问题,小编经过一番捣鼓,终于搞定了ing,今天这篇博文博文,小编简单的介绍一下,如何点击头像,实现从底部弹出窗口的故事,这个故事实现的是弹出滑动窗口,主要是使用了一些设置Activity的样式来实现弹出效果和滑动效果

Android-实现底部弹出PopupWindow并让背景逐渐变暗

Android-实现底部弹出PopupWindow并让背景逐渐变暗 在android开发中,经常需要通过点击某个按钮弹出对话框或者选择框,通过Dialog或者PopupMenu.PopupWindow都能实现. 这里主要介绍后两者:PopupMenu.PopupWindow的实现. 先看两个效果图左边PopupMenu,右边PopupWindow: Android-实现底部弹出PopupWindow并让背景逐渐变暗 一PopupMenu实现 二PopupWindow实现 一.PopupMenu实

Android底部弹出iOS7风格对话选项框

<Android底部弹出iOS7风格对话选项框> 效果图如下: 网上流传的Android底部弹出iOS7风格的对话选项框开源代码,原作者不详.我在网上流传的代码基础上改进了一些地方,把原来作为Application发布的代码整理成一个Android的Library,如果在未来的Android项目中需要这样的对话选项框样式,则只需要下载我上次到CSDN的完整库项目(完整的Android库项目代码我已经上传到CSDN,下载地址:http://download.csdn.net/download/z

通用的popupwindow底部弹出框

前段时间做项目的时候,有几个底部弹出框,当时因为忙着赶进度所有就单独写了好几个popupwindow.后来就想着怎么实现一个通用的PopupWindow工具类 就是在要用到的时候创建该工具类的对象,并传入相应的框体布局,就可以实现了. 先看看效果,下面的两是调用同一个PopupWindowUtils创建的:       ok,先看看popupWindow的具体实现 import android.app.Activity; import android.graphics.drawable.Colo

UIActionSheet底部弹出框

<底部弹出框来提示用户信息>    1.遵循代理方法<UIActionSheetDelete>    2.调用放法 [UIActionSheet *sheet=[UIActionSheet alloc]initWithTitle:@“通关了!” delegate:self cancelButtonTitle:@“取消” destructiveButtonTitle:@“购买” otherButtonTitles:@“购买1”,@“购买2”,nil]    [sheet showIn

Android编程:底部弹出的对话框

本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN10 开发环境:Android Studio 2.2 Preview 3 说明: 两种方法实现底部弹出的对话框: Dialog DialogFragment 推荐用DialogFragment 效果图: 布局文件dialog_select_call.xml: <?xml version="1.0" encoding="utf-8"?> &l