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

2023-05-16

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

       首先,第一步我们来编写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>
        第二步,新建java类,命名为SelectPicPopupWindow,继承Activity类并实现OnClickListener接口,这个接口可以不用实现,具体代码如下所示:  
package com.h8.imageroundcorner;


import android.app.Activity;
import android.os.Bundle;
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;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alert_dialog);
		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;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_take_photo:
			break;
		case R.id.btn_pick_photo:				
			break;
		case R.id.btn_cancel:				
			break;
		default:
			break;
		}
		finish();
	}
	
}
       第三步,编写MainActivity类,点击头像的时候,让她从底部滑出一定弹出框,代码如下所示:   
package com.h8.imageroundcorner;



import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class TestActivity extends Activity {
	ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
		imageView = (ImageView) findViewById(R.id.imageView2);
		Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.my);

		imageView.setImageBitmap(toRoundCorner(b,200));
		
	      //把文字控件添加监听,点击弹出自定义窗口
		imageView.setOnClickListener(new OnClickListener() {			
				public void onClick(View v) {
					startActivity(new Intent(TestActivity.this,SelectPicPopupWindow.class));
				}
			});
	}

	public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;

		final Paint paint = new Paint();

		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

		final RectF rectF = new RectF(rect);

		final float roundPx = pixels;

		paint.setAntiAlias(true);

		canvas.drawARGB(0, 0, 0, 0);

		paint.setColor(color);

		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;

	}

}
       第四步,设置属性样式实现我们所需要的效果,代码如下所示:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
       <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>
 <!-- 边框 -->
        <item name="android:windowIsFloating">true</item>
 <!-- 是否浮现在activity之上 -->
        <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>
        第五步,在点击头像的时候,我们需要她是上下滑入式的效果,点击取消的时候需要她是上下滑出式的效果,怎么实现nie,具体代码如下图所示,首先是滑入式的效果:

<?xml version="1.0" encoding="utf-8"?>  
<!-- 上下滑入式 -->  
<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"?>  
<!-- 上下滑出式 -->  
<set xmlns:android="http://schemas.android.com/apk/res/android" >  
  
      
    <translate  
        android:duration="200"  
        android:fromYDelta="0"  
        android:toYDelta="50%p" />  
</set>
        最后,我们来看一下运行的效果,如下图所示:

        
       小编寄语:该博文小编主要简单的介绍了如何从底部滑出弹出框,希望可以帮助到需要的小伙伴们,每次实现一个简单的效果,小编都会特别开心,伴着实习的脚步,小编慢慢长大`(*∩_∩*)′,这就是生命的意义,还是那句话对于小编来说,既是挑战更是机遇,因为知识都是相通的,再者来说,在小编的程序人生中,留下最珍贵的记忆,虽然以后小编不一定从事安卓这个行业,代码世界里,很多种事,有的甜蜜,有的温馨,有的婉转成歌,有的绵延不息,在这些故事里,我们唯一的共通之处就是,某年,某月,某个波澜不惊的日子里,曾经很爱很爱你!爱你--这段实习的日子里,安卓带给小编的种种的惊喜。
    

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Demo---实现从底部弹出窗口 的相关文章

随机推荐

  • 三大框架之hibernate入门学习教程增删改查

    好久没更新分享了 xff01 现在发下三大框架的hibernate便于初学者学习 xff01 另外struts2的那些配置文件代码可以找我要 xff0c 里面包括如何自定义拦截器等等 开始hibernate的学习吧 xff01 首先不多说先
  • jquery ajax无刷新请求Struts2验证用户名密码数据库是否存在

    通过ajax请求验证后台数据是否存在 首先导入struts2的核心包 后台Action代码 import com opensymphony xwork2 ActionSupport public class CodeCheckAction
  • 手把手教你们通过jquery ajax调用查询java struts2后端数据+js拼接字符串

    1 首先新建一个web项目 xff0c 创建一个User实体 package com qm entity public class User private String id private String name private Str
  • python检查URL是否能正常访问

    今天 xff0c 项目经理问我一个问题 xff0c 问我这里有2000个URL要检查是否能正常打开 xff0c 其实我是拒绝的 xff0c 我知道因为要写代码了 xff0c 正好学了点python xff0c 一想 xff0c python
  • 小甲鱼第十一课:列表:一个“打了激素”的数组2总结反思

    2 如果你每次想从列表的末尾取出一个元素 xff0c 并将这个元素插入到列表的最前边 xff0c 你会怎么做 xff1f member span class token operator 61 span span class token p
  • 找鞍点

    import java util public class Test4 找出一个二位数组中的鞍点 xff0c 即该位置上的元素在该行上最大 xff0c 在该列上最小 xff0c 也可能没有鞍点 public static void main
  • js自己写脚本自动操作注册插件,基于chrome浏览器

    大家好 xff01 又到了一周的福利时间 xff0c 今天给大家一个福利 xff0c 以后抢票不需要手动刷新页面了 xff0c 直接用你自己写的插件来控制 xff0c 事先声明 xff0c 本人是js菜鸟 xff0c 所以今天带来的例子都是
  • VMware Workstation Proa安装mac镜像

    首先你得有一个VMware 然后下载好mac镜像文件还有for OS X插件补丁 我这里都已经下载好了 xff0c 又需要的可以在评论里留下邮箱地址 xff0c 我分享给你 现在该有的文件都有了 xff0c 那么我们开始 首先VMware镜
  • Spring事务管理的四种方式(以银行转账为例)

    文章转自 https blog csdn net daijin888888 article details 51822257 本文配套示例代码下载地址 xff08 完整可运行 xff0c 含sql文件 xff0c 下载后请修改数据库配置 x
  • redis秒杀系统数据同步(保证不多卖)

    原文链接 http www cnblogs com shihaiming p 6062663 html 东西不多卖 秒杀系统需要保证东西不多卖 xff0c 关键是在多个客户端对库存进行减操作时 xff0c 必须加锁 Redis中的Watch
  • csdn过滤广告谷歌浏览器插件

    首先要知道浏览器插件的原理 通过访问网站 xff0c 加载我们写的js脚本 这样我们就可以对你所要操作的网站进行操作啦 xff01 首先看看谷歌的广告的代码块 如果换成你在开发这个网站 xff0c 肯定直接隐藏这个class 为 csdn
  • android_AlertDialog_点击屏幕不消失

    Android系统默认AlertDialog是点击屏幕就消失的 根据业务需求 点击屏幕不消失的方法 AlertDialog dialog 61 new AlertDialog Build this setView view create d
  • MLlib分类算法实战演练--Spark学习(机器学习)

    因为自身原因最近再学习spark MLlib xff0c 看的教材是 spark机器学习 xff0c 感觉这本书偏入门并且有很多实操 xff0c 非常适合新手 下面就是我在学习到第五章关于分类算法的一些要点 xff0c 最要是通过代码实操
  • Android Camera 照相机屏幕旋转问题

    大多数的相机程序都使用横向拍照 xff0c 这也是摄像头传感器的自然方向 但是这并不影响您在竖屏的时候拍照 xff0c 设备的方向信息会存储到图片的EXIF信息中 可以通过函数 setCameraDisplayOrientation 来改变
  • 信息系统开发与管理

    信息化是这个时代的主旋律 xff0c 如何执她之手 xff0c 跟上她的节拍 xff0c 不掉队 xff0c 我相信 xff0c 聪明的读者 xff0c 你的答案一定跃然于心底 一本 信息系统开发与管理 xff0c 结合学生信息管理系统 x
  • 在与SQL Server建立连接时出现与网络相关的或特定于实例的错误

    向往前一样 xff0c 学习牛腩新闻发布系统的视频 xff0c 敲代码 xff0c 打开数据库 xff0c 出现一个框框 xff0c 详细内容如下 xff1a 数据库连接不上 xff0c 所有的工作都要歇班 xff0c 捣鼓了会儿 xff0
  • 只要活着,我愿意一辈子都做程序员

    前不久 xff0c 我看过一个有意思的帖子 xff0c 标题是 35岁是程序员的终点 作者列举了35岁的年龄已经不适合继续做程序员的种种原因 xff0c 试图说服在这个年龄段的程序员做出改变 xff0c 初一看 xff0c 我自己也觉得很有
  • Sql Server服务远程过程调用失败

    由于开发系统 xff0c 需要vs版本统一 xff0c 于是经过了昨天一整天艰苦卓绝的斗争 xff0c 小编终于成功的写在了13版本的vs xff0c 重新装上了12版本的vs xff0c 本来想着 xff0c 12版本的vs搭建成功了 x
  • Android仿淘宝购物车demo

    夏的热情渐渐退去 xff0c 秋如期而至 xff0c 丰收的季节 xff0c 小编继续着实习之路 xff0c 走着走着 xff0c 就走到了购物车 xff0c 逛过淘宝或者是京东的小伙伴都知道购物车里面的宝贝可不止一件 xff0c 对于爱购
  • Android Demo---实现从底部弹出窗口

    在前面的博文中 xff0c 小编简单的介绍了如何制作圆角的按钮以及圆角的图片 xff0c 伴着键盘和手指之间的舞步 xff0c 迎来新的问题 xff0c 不知道小伙伴有没有这样的经历 xff0c 以App为例 xff0c 点击头像的时候 x