Android实现毛玻璃效果弹出菜单动画
作者:lvshaorong 发布时间:2023-04-22 17:02:12
标签:Android,毛玻璃,菜单
本文实例为大家分享了Android实现毛玻璃效果弹出菜单动画的具体代码,供大家参考,具体内容如下
仿ios上屏幕下方向上滑出来的一个模糊菜单,效果如下
原理很简单,页面上原来有一个gone的framelayout,调用方法让它弹出的时候加了一个位移动画,让它从底部出来,出来的时候给这个framelayout里的一个imageView设置一个模糊后的截屏图片,并且这个图片也加一个相同时间的反方向位移动画,这个demo里用到的Blur模糊类和自定义imageView可以去我前两篇博客上找到.
这里面用的控件的大小等全部是动态计算的,不必担心屏幕适配的问题
activity的布局如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/window"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.npi.blureffect.DialogActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="124dp" />
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ratingBar1"
android:layout_below="@+id/ratingBar1"
android:layout_marginLeft="24dp"
android:layout_marginTop="81dp"
android:text="Switch" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/ratingBar1"
android:layout_below="@+id/ratingBar1"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ratingBar1"
android:layout_alignLeft="@+id/switch1"
android:layout_marginBottom="52dp"
android:text="Button" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/ratingBar1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="49dp"
android:layout_toLeftOf="@+id/button1" />
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/switch1"
android:layout_toRightOf="@+id/switch1" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/progressBar1"
android:layout_alignLeft="@+id/switch1"
android:text="RadioButton" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/progressBar2"
android:layout_below="@+id/progressBar2"
android:text="Button" />
<FrameLayout
android:id="@+id/bottom_menu"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#eeeeee"
android:visibility="gone" >
<com.npi.blureffect.ScrollableImageView
android:id="@+id/bottom_back"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/abs__ab_share_pack_holo_light" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/abs__ic_voice_search" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/abs__ic_voice_search" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/abs__ic_voice_search" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/abs__ic_voice_search" />
</LinearLayout>
</FrameLayout>
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:visibility="gone" />
</RelativeLayout>
activity如下
package com.npi.blureffect;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class DialogActivity extends Activity {
TextView textView1;
RelativeLayout window;
ImageView background;
FrameLayout bottomMenu;
Button button2;
Button button1;
ScrollableImageView bottomBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
textView1 = (TextView) findViewById(R.id.textView1);
window = (RelativeLayout)findViewById(R.id.window);
background = (ImageView) findViewById(R.id.background);
bottomMenu = (FrameLayout) findViewById(R.id.bottom_menu);
button2 = (Button) findViewById(R.id.button2);
bottomBack = (ScrollableImageView) findViewById(R.id.bottom_back);
button1 = (Button) findViewById(R.id.button1);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showBottomMenu(window,500);
}
});
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
hideBottomMenu(window, 500);
}
});
}
public void showBottomMenu(View layout,final int duration){
//对当前页面进行截屏
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache(); //启用DrawingCache并创建位图
Bitmap screen = Bitmap.createBitmap(layout.getDrawingCache()); //创建一个DrawingCache的拷贝,因为DrawingCache得到的位图在禁用后会被回收
layout.setDrawingCacheEnabled(false); //禁用DrawingCahce否则会影响性能
//将截屏进行模糊
screen = Blur.fastblur(this, screen, 10);
bottomBack.setoriginalImage(screen);
bottomMenu.setAlpha(0);//在动画开启之后再可见,否则会有残影
bottomMenu.setVisibility(View.VISIBLE);
bottomMenu.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Animation animation = new TranslateAnimation(0, 0, bottomMenu.getHeight(), 0);//这里弹出框的高度的dp需要事先写死
bottomBack.handleScroll(bottomBack.getOriginalImage().getHeight()-bottomMenu.getHeight(), 0);
Animation backgroundAnimation = new TranslateAnimation(0,0,-bottomBack.getHeight(),0);
backgroundAnimation.setDuration(duration);
bottomBack.startAnimation(backgroundAnimation);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
bottomMenu.setAlpha(255);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
});
animation.setDuration(duration);
bottomMenu.startAnimation(animation);
}
});
}
public void hideBottomMenu(View layout,final int duration){
//对当前页面进行截屏
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache(); //启用DrawingCache并创建位图
Bitmap screen = Bitmap.createBitmap(layout.getDrawingCache()); //创建一个DrawingCache的拷贝,因为DrawingCache得到的位图在禁用后会被回收
layout.setDrawingCacheEnabled(false); //禁用DrawingCahce否则会影响性能
//将截屏进行模糊
screen = Blur.fastblur(this, screen, 10);
bottomBack.setoriginalImage(screen);
bottomMenu.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Animation animation = new TranslateAnimation(0, 0, 0, +bottomMenu.getHeight());//这里弹出框的高度的dp需要事先写死
bottomBack.handleScroll(bottomBack.getOriginalImage().getHeight()-bottomMenu.getHeight(), 0);
Animation backgroundAnimation = new TranslateAnimation(0,0,0,-bottomBack.getHeight());
backgroundAnimation.setDuration(duration);
bottomBack.startAnimation(backgroundAnimation);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
bottomMenu.setAlpha(255);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
bottomMenu.setVisibility(View.GONE);
}
});
animation.setDuration(duration);
bottomMenu.startAnimation(animation);
}
});
}
}
来源:https://blog.csdn.net/lvshaorong/article/details/50396022


猜你喜欢
- 累加数累加数 是一个字符串,组成它的数字可以形成累加序列。一个有效的 累加序列 必须 至少 包含 3 个数。除了最开始的两个数以外,序列中的
- Volley简介我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发
- 题目要求思路一:双指针(模拟)Javaclass Solution { public boolean isFlip
- 目录问题案例原因分析源码分析解决方法备注问题案例来个简单点的例子public static void main(String[] args)
- 如果项目中要用到数据库,铁定要用到分页排序。之前在做数据库查询优化的时候,通宵写了以下代码,来拼接分页排序的SQL语句 /// <su
- springboot天生支持使用hibernate validation对参数的优雅校验,如果不使用它,只能对参数挨个进行如下方式的手工校验
- 本文实例讲述了Java矩阵连乘问题(动态规划)算法。分享给大家供大家参考,具体如下:问题描述:给定n个矩阵:A1,A2,...,An,其中A
- 1. 在原有工程目录右键-> new ->Module->:2. 选择library:3. 一路next,最后finish
- @schedule 注解 是springboot 常用的定时任务注解,使用起来简单方便,但是如果定时任务非常多,或者有的任务很耗时
- Spring-Data-Redis项目(简称SDR)对Redis的Key-Value数据存储操作提供了更高层次的抽象,类似于Spring F
- 这篇文章主要介绍了Java线程并发访问代码分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参
- 一、return语句执行顺序finally语句是在return语句执行之后,return语句返回之前执行的package exception
- 一、Thread 类了解如何使用Thread 类实现多线程之后,继续学习Thread 类实现多线程之后的相关功能及方法。1、操作线程名称的方
- 通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了。
- 简介单例指的是只能存在一个实例的类(在C#中,更准确的说法是在每个AppDomain之中只能存在一个实例的类,它是软件工程中使用最多的几种模
- 若要在 C++ 中实现异常处理,你可以使用 try、throw 和 catch 表达式。首先,使用 try 块将可能引发异常的一个或多个语句
- 一、前言二、案例需求1.编写login.html登录页面,username&password两个输入框2.使用Druid数据库连接池
- 复合语句Java的复合语句是以整个区块为单位的语句,由{}以及{}内包含的内容组成对于复合语句来说,复合语句创建了一个局部变量的作用域,该作
- mybatis resulttype 返回值异常在使用mybatis时。resulttype返回自定义的类时,可能返回的类中字段数据存在缺失
- 1. 前言本文主要是介绍一下RocketMQ消息生产者在发送消息的时候发送失败的问题处理?这里有两个点,一个是关于消息的处理,一个是关于br