網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
本文實(shí)例為大家分享了Android實(shí)現(xiàn)毛玻璃效果彈出菜單動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下
仿ios上屏幕下方向上滑出來(lái)的一個(gè)模糊菜單,效果如下
原理很簡(jiǎn)單,頁(yè)面上原來(lái)有一個(gè)gone的framelayout,調(diào)用方法讓它彈出的時(shí)候加了一個(gè)位移動(dòng)畫,讓它從底部出來(lái),出來(lái)的時(shí)候給這個(gè)framelayout里的一個(gè)imageView設(shè)置一個(gè)模糊后的截屏圖片,并且這個(gè)圖片也加一個(gè)相同時(shí)間的反方向位移動(dòng)畫,這個(gè)demo里用到的Blur模糊類和自定義imageView可以去我前兩篇博客上找到.
這里面用的控件的大小等全部是動(dòng)態(tài)計(jì)算的,不必?fù)?dān)心屏幕適配的問(wèn)題
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){
?? ? //對(duì)當(dāng)前頁(yè)面進(jìn)行截屏
?? ? ?? ??? ?layout.setDrawingCacheEnabled(true); ?
?? ? ?? ??? ?layout.buildDrawingCache(); ?//啟用DrawingCache并創(chuàng)建位圖 ?
?? ? ?? ??? ?Bitmap screen = Bitmap.createBitmap(layout.getDrawingCache()); //創(chuàng)建一個(gè)DrawingCache的拷貝,因?yàn)镈rawingCache得到的位圖在禁用后會(huì)被回收 ?
?? ? ?? ??? ?layout.setDrawingCacheEnabled(false); ?//禁用DrawingCahce否則會(huì)影響性能 ?
?? ? ? ? ?? ?
?? ? ?? ??? ?//將截屏進(jìn)行模糊
?? ? ?? ??? ?screen = Blur.fastblur(this, screen, 10);
?? ? ?? ??? ?bottomBack.setoriginalImage(screen);
?? ? ?? ??? ?
?? ? ?? ??? ?
?? ? ? bottomMenu.setAlpha(0);//在動(dòng)畫開啟之后再可見,否則會(huì)有殘影
?? ? ? 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){
?? ??? ? //對(duì)當(dāng)前頁(yè)面進(jìn)行截屏
?? ??? ? ?? ??? ?layout.setDrawingCacheEnabled(true); ?
?? ??? ? ?? ??? ?layout.buildDrawingCache(); ?//啟用DrawingCache并創(chuàng)建位圖 ?
?? ??? ? ?? ??? ?Bitmap screen = Bitmap.createBitmap(layout.getDrawingCache()); //創(chuàng)建一個(gè)DrawingCache的拷貝,因?yàn)镈rawingCache得到的位圖在禁用后會(huì)被回收 ?
?? ??? ? ?? ??? ?layout.setDrawingCacheEnabled(false); ?//禁用DrawingCahce否則會(huì)影響性能 ?
?? ??? ? ? ? ?? ?
?? ??? ? ?? ??? ?//將截屏進(jìn)行模糊
?? ??? ? ?? ??? ?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
相關(guān)推薦
- 2022-06-22 C語(yǔ)言詳解判斷相同樹案例分析_C 語(yǔ)言
- 2022-09-15 Python淺析匿名函數(shù)lambda的用法_python
- 2022-04-28 WPF使用DockPanel停靠面板布局_實(shí)用技巧
- 2022-10-28 Python利用Rows快速操作csv文件_python
- 2022-03-19 MongoDB數(shù)據(jù)庫(kù)授權(quán)認(rèn)證的實(shí)現(xiàn)_MongoDB
- 2022-05-28 python中數(shù)組和列表的簡(jiǎn)單實(shí)例_python
- 2022-06-12 GO語(yǔ)言中常見的排序算法使用示例_Golang
- 2022-12-31 C++?Boost?Uuid超詳細(xì)講解_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支