关于popupwindow的两种实现方式
http://104zz.iteye.com/blog/1685389
android PopupWindow实现从底部弹出或滑出选择菜单或窗口
本实例弹出窗口主要是继承PopupWindow类来实现的弹出窗体,布局可以根据自己定义设计。弹出效果主要使用了translate和alpha样式实现,具体实习如下:
第一步:设计弹出窗口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>
第二步:创建SelectPicPopupWindow类继承PopupWindow:
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.drawable.ColorDrawable;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.PopupWindow;
- public class SelectPicPopupWindow extends PopupWindow {
- private Button btn_take_photo, btn_pick_photo, btn_cancel;
- private View mMenuView;
- public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
- super(context);
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mMenuView = inflater.inflate(R.layout.alert_dialog, null);
- btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo);
- btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo);
- btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
- //取消按钮
- btn_cancel.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- //销毁弹出框
- dismiss();
- }
- });
- //设置按钮监听
- btn_pick_photo.setOnClickListener(itemsOnClick);
- btn_take_photo.setOnClickListener(itemsOnClick);
- //设置SelectPicPopupWindow的View
- this.setContentView(mMenuView);
- //设置SelectPicPopupWindow弹出窗体的宽
- this.setWidth(LayoutParams.FILL_PARENT);
- //设置SelectPicPopupWindow弹出窗体的高
- this.setHeight(LayoutParams.WRAP_CONTENT);
- //设置SelectPicPopupWindow弹出窗体可点击
- this.setFocusable(true);
- //设置SelectPicPopupWindow弹出窗体动画效果
- this.setAnimationStyle(R.style.AnimBottom);
- //实例化一个ColorDrawable颜色为半透明
- ColorDrawable dw = new ColorDrawable(0xb0000000);
- //设置SelectPicPopupWindow弹出窗体的背景
- this.setBackgroundDrawable(dw);
- //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
- mMenuView.setOnTouchListener(new OnTouchListener() {
- public boolean onTouch(View v, MotionEvent event) {
- int height = mMenuView.findViewById(R.id.pop_layout).getTop();
- int y=(int) event.getY();
- if(event.getAction()==MotionEvent.ACTION_UP){
- if(y<height){
- dismiss();
- }
- }
- return true;
- }
- });
- }
- }
第三步:编写MainActivity类实现测试:
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- //自定义的弹出框类
- SelectPicPopupWindow menuWindow;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TextView tv = (TextView) this.findViewById(R.id.text);
- //把文字控件添加监听,点击弹出自定义窗口
- tv.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- //实例化SelectPicPopupWindow
- menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
- //显示窗口
- menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
- }
- });
- }
- //为弹出窗口实现监听类
- private OnClickListener itemsOnClick = new OnClickListener(){
- public void onClick(View v) {
- menuWindow.dismiss();
- switch (v.getId()) {
- case R.id.btn_take_photo:
- break;
- case R.id.btn_pick_photo:
- break;
- default:
- break;
- }
- }
- };
- }
第四:运行效果如下:
http://104zz.iteye.com/blog/1687662
android 比较靠谱的选择图片以及拍照,保存图片
在开发项目中用到这个功能,之前就按照学过的拍照和选择照片的功能,所以也没在意就写了上去,可是最后发现在有些机子里面获取到的数据时空的,所以会导致程序崩溃的情况出现,网上找了很多例子大多都是一样的,还是有问题,后来就查看跟踪了拍照后返回的数据写了下面的代码相对大多数机子是可行的,经测试还是比较靠谱的,包括拍照后图片翻转了90度问题都没问题。直接看代码:
第一:拍照选择界面:
- import android.app.Activity;
- import android.content.ActivityNotFoundException;
- import android.content.Intent;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- 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) {
- finish();
- return true;
- }
- @Override
- 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());
- setResult(1, intent);
- 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;
- }
- }
- }
第二:显示照片界面:
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageView;
- 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中获得。如果为空,那么我们就进行下面的方式获取
- 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();
- if (extras != null) {
- //这里是有些拍照后的图片是直接存放到Bundle中的所以我们可以从这里面获取Bitmap图片
- Bitmap image = extras.getParcelable("data");
- if (image != null) {
- photo.setImageBitmap(image);
- }
- }
- }
- }
- break;
- default:
- break;
- }
- }
- }
第三:如果需要保存图片到SD卡或者上传图片可以用下面代码:
- public static String savePicToSdcard(Bitmap bitmap, String path,
- String fileName) {
- String filePath = "";
- if (bitmap == null) {
- return filePath;
- } else {
- filePath=path+ fileName;
- File destFile = new File(filePath);
- OutputStream os = null;
- try {
- os = new FileOutputStream(destFile);
- bitmap.compress(CompressFormat.JPEG, 100, os);
- os.flush();
- os.close();
- } catch (IOException e) {
- filePath = "";
- }
- }
- return filePath;
- }
上传图片的话我们可以获取bitmap的流然后上传,如上面方式获取,如要上传,上传代码自己实现,这个比较简单。
运行效果图:
关于popupwindow的两种实现方式的更多相关文章
- Web APi之认证(Authentication)两种实现方式【二】(十三)
前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再叙述废话. 序言 对于所谓的认证说到底 ...
- Android中BroadcastReceiver的两种注册方式(静态和动态)详解
今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- JavaScript 函数的两种声明方式
1.函数声明的方式 JavaScript声明函数有两种选择:函数声明法,表达式定义法. 函数声明法 function sum (num1 ,num2){ return num1+num2 } 表达式定 ...
- Redis两种持久化方式(RDB&AOF)
爬虫和转载请注明原文地址;博客园蜗牛:http://www.cnblogs.com/tdws/p/5754706.html Redis所需内存 超过可用内存怎么办 Redis修改数据多线程并发—Red ...
- struts2+spring的两种整合方式
也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...
- easyui datagride 两种查询方式
easyui datagride 两种查询方式function doReseach() { //$('#tt').datagrid('load', { // FixedCompany: $('.c_s ...
- 【Visual Lisp】两种出错处理方式
两种出错处理方式:一种是对出错函数进行重定义,一种是对错误进行捕捉处理. ;;============================================================= ...
- 两种include方式及filter中的dispatcher解析
两种include方式 我自己写了一个original.jsp,另外有一个includedPage.jsp,我想在original.jsp中把includedPage.jsp引进来有两种方式: 1.& ...
随机推荐
- perl dbi 测试 mysql wait_timeout
The number of seconds the server waits for activity on a noninteractive connection before closing it ...
- JavaScript IDE 大盘点,让选择不再难
文章来源:http://gcdn.gcpowertools.com.cn/showtopic-24110-1-3.html 阅读本文之前,分享大家一张图片,看图会发现JavaScript开发需求最 ...
- 上海游侠电动汽车团队招募。iOS,Android,产品经理以及 SEVER 端工程师 - V2EX
上海游侠电动汽车团队招募.iOS,Android,产品经理以及 SEVER 端工程师 - V2EX 上海游侠电动汽车团队招募.iOS,Android,产品经理以及 SEVER 端工程师
- [置顶] API相关工作过往的总结之整体介绍
此系列的总结文章,仅仅是我个人工作总结,有考虑不周之处还请各位同行多多指教. API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是 ...
- node.js + express(ejs) + mongodb(mongoose) 增删改实例
MongoDB 安装步骤总结: 1.解压目录到d盘 mongodb 2.安装目录的下新建文件mongo.config文件 ##store data here dbpath=D:\mongodb\dat ...
- Python 文件I/O (转)
Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...
- Droppable(放置)组件
.加载方式 //class 加载方式 <div id="dd" class="easyui-droppable" data-options="a ...
- Oracle 11g XML java连接
在网上找了好多教程 走好好多弯路 现在从头总结下 oralce11g 可以直接用xmltype节点存储xml文件 简单来说就是直接存一个文件进去 首先安装oracle11g 网上教程非常多 然后进Ne ...
- 循序渐进Socket网络编程(多客户端、信息共享、文件传输)
循序渐进Socket网络编程(多客户端.信息共享.文件传输) 前言:在最近一个即将结束的项目中使用到了Socket编程,用于调用另一系统进行处理并返回数据.故把Socket的基础知识总结梳理一遍. 1 ...
- asp.net 的那点事(1、当用户在浏览器地址栏输入了网址后,发生了什么?)
从今天开始我将抽出空闲时间复习asp.net相关知识.此篇博文只是为了记录学习当中的知识点和感觉到比较重要的知识点. 本人才疏学浅,如有遗漏或者错误希望广大博友尽情拍砖.我会在后续中进行更正. 这个问 ...