http://104zz.iteye.com/blog/1685389

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

本实例弹出窗口主要是继承PopupWindow类来实现的弹出窗体,布局可以根据自己定义设计。弹出效果主要使用了translate和alpha样式实现,具体实习如下:

第一步:设计弹出窗口xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="wrap_content"
  6. android:gravity="center_horizontal"
  7. android:orientation="vertical"
  8. >
  9. <LinearLayout
  10. android:id="@+id/pop_layout"
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:gravity="center_horizontal"
  14. android:orientation="vertical"
  15. android:layout_alignParentBottom="true"
  16. android:background="@drawable/btn_style_alert_dialog_background"
  17. >
  18. <Button
  19. android:id="@+id/btn_take_photo"
  20. android:layout_marginLeft="20dip"
  21. android:layout_marginRight="20dip"
  22. android:layout_marginTop="20dip"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:text="拍照"
  26. android:background="@drawable/btn_style_alert_dialog_button"
  27. android:textStyle="bold"
  28. />
  29. <Button
  30. android:id="@+id/btn_pick_photo"
  31. android:layout_marginLeft="20dip"
  32. android:layout_marginRight="20dip"
  33. android:layout_marginTop="5dip"
  34. android:layout_width="fill_parent"
  35. android:layout_height="wrap_content"
  36. android:text="从相册选择"
  37. android:background="@drawable/btn_style_alert_dialog_button"
  38. android:textStyle="bold"
  39. />
  40. <Button
  41. android:id="@+id/btn_cancel"
  42. android:layout_marginLeft="20dip"
  43. android:layout_marginRight="20dip"
  44. android:layout_marginTop="15dip"
  45. android:layout_marginBottom="15dip"
  46. android:layout_width="fill_parent"
  47. android:layout_height="wrap_content"
  48. android:text="取消"
  49. android:background="@drawable/btn_style_alert_dialog_cancel"
  50. android:textColor="#ffffff"
  51. android:textStyle="bold"
  52. />
  53. </LinearLayout>
  54. </RelativeLayout>

第二步:创建SelectPicPopupWindow类继承PopupWindow:

  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.graphics.drawable.ColorDrawable;
  4. import android.view.LayoutInflater;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.View.OnTouchListener;
  9. import android.view.ViewGroup.LayoutParams;
  10. import android.widget.Button;
  11. import android.widget.PopupWindow;
  12. public class SelectPicPopupWindow extends PopupWindow {
  13. private Button btn_take_photo, btn_pick_photo, btn_cancel;
  14. private View mMenuView;
  15. public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
  16. super(context);
  17. LayoutInflater inflater = (LayoutInflater) context
  18. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  19. mMenuView = inflater.inflate(R.layout.alert_dialog, null);
  20. btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
  21. btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
  22. btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
  23. //取消按钮
  24. btn_cancel.setOnClickListener(new OnClickListener() {
  25. public void onClick(View v) {
  26. //销毁弹出框
  27. dismiss();
  28. }
  29. });
  30. //设置按钮监听
  31. btn_pick_photo.setOnClickListener(itemsOnClick);
  32. btn_take_photo.setOnClickListener(itemsOnClick);
  33. //设置SelectPicPopupWindow的View
  34. this.setContentView(mMenuView);
  35. //设置SelectPicPopupWindow弹出窗体的宽
  36. this.setWidth(LayoutParams.FILL_PARENT);
  37. //设置SelectPicPopupWindow弹出窗体的高
  38. this.setHeight(LayoutParams.WRAP_CONTENT);
  39. //设置SelectPicPopupWindow弹出窗体可点击
  40. this.setFocusable(true);
  41. //设置SelectPicPopupWindow弹出窗体动画效果
  42. this.setAnimationStyle(R.style.AnimBottom);
  43. //实例化一个ColorDrawable颜色为半透明
  44. ColorDrawable dw = new ColorDrawable(0xb0000000);
  45. //设置SelectPicPopupWindow弹出窗体的背景
  46. this.setBackgroundDrawable(dw);
  47. //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
  48. mMenuView.setOnTouchListener(new OnTouchListener() {
  49. public boolean onTouch(View v, MotionEvent event) {
  50. int height = mMenuView.findViewById(R.id.pop_layout).getTop();
  51. int y=(int) event.getY();
  52. if(event.getAction()==MotionEvent.ACTION_UP){
  53. if(y<height){
  54. dismiss();
  55. }
  56. }
  57. return true;
  58. }
  59. });
  60. }
  61. }

第三步:编写MainActivity类实现测试:

  1. import android.app.Activity;
  2. import android.os.Bundle;
  3. import android.view.Gravity;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.TextView;
  7. public class MainActivity extends Activity {
  8. //自定义的弹出框类
  9. SelectPicPopupWindow menuWindow;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. TextView tv = (TextView) this.findViewById(R.id.text);
  15. //把文字控件添加监听,点击弹出自定义窗口
  16. tv.setOnClickListener(new OnClickListener() {
  17. public void onClick(View v) {
  18. //实例化SelectPicPopupWindow
  19. menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
  20. //显示窗口
  21. menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
  22. }
  23. });
  24. }
  25. //为弹出窗口实现监听类
  26. private OnClickListener  itemsOnClick = new OnClickListener(){
  27. public void onClick(View v) {
  28. menuWindow.dismiss();
  29. switch (v.getId()) {
  30. case R.id.btn_take_photo:
  31. break;
  32. case R.id.btn_pick_photo:
  33. break;
  34. default:
  35. break;
  36. }
  37. }
  38. };
  39. }

第四:运行效果如下:


http://104zz.iteye.com/blog/1687662

android 比较靠谱的选择图片以及拍照,保存图片

在开发项目中用到这个功能,之前就按照学过的拍照和选择照片的功能,所以也没在意就写了上去,可是最后发现在有些机子里面获取到的数据时空的,所以会导致程序崩溃的情况出现,网上找了很多例子大多都是一样的,还是有问题,后来就查看跟踪了拍照后返回的数据写了下面的代码相对大多数机子是可行的,经测试还是比较靠谱的,包括拍照后图片翻转了90度问题都没问题。直接看代码:

第一:拍照选择界面:

  1. import android.app.Activity;
  2. import android.content.ActivityNotFoundException;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.provider.MediaStore;
  6. import android.view.MotionEvent;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.LinearLayout;
  11. import android.widget.Toast;
  12. public class SelectPicPopupWindow extends Activity implements OnClickListener {
  13. private Button btn_take_photo, btn_pick_photo, btn_cancel;
  14. private LinearLayout layout;
  15. private Intent intent;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.alert_dialog);
  20. intent = getIntent();
  21. btn_take_photo = (Button) this.findViewById(R.id.btn_take_photo);
  22. btn_pick_photo = (Button) this.findViewById(R.id.btn_pick_photo);
  23. btn_cancel = (Button) this.findViewById(R.id.btn_cancel);
  24. layout = (LinearLayout) findViewById(R.id.pop_layout);
  25. // 添加选择窗口范围监听可以优先获取触点,即不再执行onTouchEvent()函数,点击其他地方时执行onTouchEvent()函数销毁Activity
  26. layout.setOnClickListener(new OnClickListener() {
  27. public void onClick(View v) {
  28. // TODO Auto-generated method stub
  29. Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!",
  30. Toast.LENGTH_SHORT).show();
  31. }
  32. });
  33. // 添加按钮监听
  34. btn_cancel.setOnClickListener(this);
  35. btn_pick_photo.setOnClickListener(this);
  36. btn_take_photo.setOnClickListener(this);
  37. }
  38. // 实现onTouchEvent触屏函数但点击屏幕时销毁本Activity
  39. @Override
  40. public boolean onTouchEvent(MotionEvent event) {
  41. finish();
  42. return true;
  43. }
  44. @Override
  45. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  46. if (resultCode != RESULT_OK) {
  47. return;
  48. }
  49. //选择完或者拍完照后会在这里处理,然后我们继续使用setResult返回Intent以便可以传递数据和调用
  50. if (data.getExtras() != null)
  51. intent.putExtras(data.getExtras());
  52. if (data.getData()!= null)
  53. intent.setData(data.getData());
  54. setResult(1, intent);
  55. finish();
  56. }
  57. public void onClick(View v) {
  58. switch (v.getId()) {
  59. case R.id.btn_take_photo:
  60. try {
  61. //拍照我们用Action为MediaStore.ACTION_IMAGE_CAPTURE,
  62. //有些人使用其他的Action但我发现在有些机子中会出问题,所以优先选择这个
  63. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  64. intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
  65. startActivityForResult(intent, 1);
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. break;
  70. case R.id.btn_pick_photo:
  71. try {
  72. //选择照片的时候也一样,我们用Action为Intent.ACTION_GET_CONTENT,
  73. //有些人使用其他的Action但我发现在有些机子中会出问题,所以优先选择这个
  74. Intent intent = new Intent();
  75. intent.setType("image/*");
  76. intent.setAction(Intent.ACTION_GET_CONTENT);
  77. startActivityForResult(intent, 2);
  78. } catch (ActivityNotFoundException e) {
  79. }
  80. break;
  81. case R.id.btn_cancel:
  82. finish();
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. }

第二:显示照片界面:

  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.graphics.Bitmap;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.provider.MediaStore;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.ImageView;
  10. public class MainActivity extends Activity {
  11. private ImageView photo;
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. photo = (ImageView) this.findViewById(R.id.text);
  17. // 把文字控件添加监听,点击弹出自定义窗口
  18. photo.setOnClickListener(new OnClickListener() {
  19. public void onClick(View v) {
  20. //使用startActivityForResult启动SelectPicPopupWindow当返回到此Activity的时候就会调用onActivityResult函数
  21. startActivityForResult(new Intent(MainActivity.this,
  22. SelectPicPopupWindow.class), 1);
  23. }
  24. });
  25. }
  26. @Override
  27. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  28. switch (resultCode) {
  29. case 1:
  30. if (data != null) {
  31. //取得返回的Uri,基本上选择照片的时候返回的是以Uri形式,但是在拍照中有得机子呢Uri是空的,所以要特别注意
  32. Uri mImageCaptureUri = data.getData();
  33. //返回的Uri不为空时,那么图片信息数据都会在Uri中获得。如果为空,那么我们就进行下面的方式获取
  34. if (mImageCaptureUri != null) {
  35. Bitmap image;
  36. try {
  37. //这个方法是根据Uri获取Bitmap图片的静态方法
  38. image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageCaptureUri);
  39. if (image != null) {
  40. photo.setImageBitmap(image);
  41. }
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. } else {
  46. Bundle extras = data.getExtras();
  47. if (extras != null) {
  48. //这里是有些拍照后的图片是直接存放到Bundle中的所以我们可以从这里面获取Bitmap图片
  49. Bitmap image = extras.getParcelable("data");
  50. if (image != null) {
  51. photo.setImageBitmap(image);
  52. }
  53. }
  54. }
  55. }
  56. break;
  57. default:
  58. break;
  59. }
  60. }
  61. }

第三:如果需要保存图片到SD卡或者上传图片可以用下面代码:

  1. public static String savePicToSdcard(Bitmap bitmap, String path,
  2. String fileName) {
  3. String filePath = "";
  4. if (bitmap == null) {
  5. return filePath;
  6. } else {
  7. filePath=path+ fileName;
  8. File destFile = new File(filePath);
  9. OutputStream os = null;
  10. try {
  11. os = new FileOutputStream(destFile);
  12. bitmap.compress(CompressFormat.JPEG, 100, os);
  13. os.flush();
  14. os.close();
  15. } catch (IOException e) {
  16. filePath = "";
  17. }
  18. }
  19. return filePath;
  20. }

上传图片的话我们可以获取bitmap的流然后上传,如上面方式获取,如要上传,上传代码自己实现,这个比较简单。

运行效果图:

关于popupwindow的两种实现方式的更多相关文章

  1. Web APi之认证(Authentication)两种实现方式【二】(十三)

    前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再叙述废话. 序言 对于所谓的认证说到底 ...

  2. Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...

  3. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  4. JavaScript 函数的两种声明方式

    1.函数声明的方式 JavaScript声明函数有两种选择:函数声明法,表达式定义法. 函数声明法 function sum (num1 ,num2){ return num1+num2 } 表达式定 ...

  5. Redis两种持久化方式(RDB&AOF)

    爬虫和转载请注明原文地址;博客园蜗牛:http://www.cnblogs.com/tdws/p/5754706.html Redis所需内存 超过可用内存怎么办 Redis修改数据多线程并发—Red ...

  6. struts2+spring的两种整合方式

    也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...

  7. easyui datagride 两种查询方式

    easyui datagride 两种查询方式function doReseach() { //$('#tt').datagrid('load', { // FixedCompany: $('.c_s ...

  8. 【Visual Lisp】两种出错处理方式

    两种出错处理方式:一种是对出错函数进行重定义,一种是对错误进行捕捉处理. ;;============================================================= ...

  9. 两种include方式及filter中的dispatcher解析

    两种include方式 我自己写了一个original.jsp,另外有一个includedPage.jsp,我想在original.jsp中把includedPage.jsp引进来有两种方式: 1.& ...

随机推荐

  1. perl dbi 测试 mysql wait_timeout

    The number of seconds the server waits for activity on a noninteractive connection before closing it ...

  2. JavaScript IDE 大盘点,让选择不再难

      文章来源:http://gcdn.gcpowertools.com.cn/showtopic-24110-1-3.html 阅读本文之前,分享大家一张图片,看图会发现JavaScript开发需求最 ...

  3. 上海游侠电动汽车团队招募。iOS,Android,产品经理以及 SEVER 端工程师 - V2EX

    上海游侠电动汽车团队招募.iOS,Android,产品经理以及 SEVER 端工程师 - V2EX 上海游侠电动汽车团队招募.iOS,Android,产品经理以及 SEVER 端工程师

  4. [置顶] API相关工作过往的总结之整体介绍

    此系列的总结文章,仅仅是我个人工作总结,有考虑不周之处还请各位同行多多指教. API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是 ...

  5. node.js + express(ejs) + mongodb(mongoose) 增删改实例

    MongoDB 安装步骤总结: 1.解压目录到d盘 mongodb 2.安装目录的下新建文件mongo.config文件 ##store data here dbpath=D:\mongodb\dat ...

  6. Python 文件I/O (转)

    Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...

  7. Droppable(放置)组件

    .加载方式 //class 加载方式 <div id="dd" class="easyui-droppable" data-options="a ...

  8. Oracle 11g XML java连接

    在网上找了好多教程 走好好多弯路 现在从头总结下 oralce11g 可以直接用xmltype节点存储xml文件 简单来说就是直接存一个文件进去 首先安装oracle11g 网上教程非常多 然后进Ne ...

  9. 循序渐进Socket网络编程(多客户端、信息共享、文件传输)

    循序渐进Socket网络编程(多客户端.信息共享.文件传输) 前言:在最近一个即将结束的项目中使用到了Socket编程,用于调用另一系统进行处理并返回数据.故把Socket的基础知识总结梳理一遍. 1 ...

  10. asp.net 的那点事(1、当用户在浏览器地址栏输入了网址后,发生了什么?)

    从今天开始我将抽出空闲时间复习asp.net相关知识.此篇博文只是为了记录学习当中的知识点和感觉到比较重要的知识点. 本人才疏学浅,如有遗漏或者错误希望广大博友尽情拍砖.我会在后续中进行更正. 这个问 ...