Android 自定义PopupWindow动画效果

简介:

public class RollActivity extends Activity {
	private View view;
	private Button btn;
	private PopupWindow mPopupWindow;
	private View[] btns;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
//		LinearLayout layout=(LinearLayout) view.findViewById(R.id.layout_main);
//		//设置背景图片旋转180
//		Bitmap mBitmap=setRotate(R.drawable.bg_kuang);
//		BitmapDrawable drawable=new BitmapDrawable(mBitmap);
//		layout.setBackgroundDrawable(drawable);
        
        btn=(Button) this.findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showPopupWindow(btn);
			}
        	
        });
        
        initPopupWindow(R.layout.popwindow);

    }
    
	private void initPopupWindow(int resId){
		LayoutInflater mLayoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
	    view = mLayoutInflater.inflate(resId, null);
	        
		mPopupWindow = new PopupWindow(view, 400,LayoutParams.WRAP_CONTENT);
//		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());//必须设置background才能消失
		mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_frame));
		mPopupWindow.setOutsideTouchable(true);
		
		//自定义动画
//		mPopupWindow.setAnimationStyle(R.style.PopupAnimation);
		//使用系统动画
		mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		mPopupWindow.update();
		mPopupWindow.setTouchable(true);
		mPopupWindow.setFocusable(true);
		
		btns=new View[3];
		btns[0]=view.findViewById(R.id.btn_0);
		btns[1]=view.findViewById(R.id.btn_1);
		btns[2]=view.findViewById(R.id.btn_2);
		btns[0].setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//doSomething
			}
		});
		btns[1].setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//doSomething
			}
		});
		btns[2].setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//doSomething
			}
		});
	}
	private void showPopupWindow(View view) {
		if(!mPopupWindow.isShowing()){
//			mPopupWindow.showAsDropDown(view,0,0);
			mPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
		}
	}
	public Bitmap setRotate(int resId) {
		Matrix mFgMatrix = new Matrix();
		Bitmap mFgBitmap = BitmapFactory.decodeResource(getResources(), resId);
		mFgMatrix.setRotate(180f);
		return mFgBitmap=Bitmap.createBitmap(mFgBitmap, 0, 0, 
				mFgBitmap.getWidth(), mFgBitmap.getHeight(), mFgMatrix, true);
	}
}

PopupWindow的布局popwindow.xml 
注意3个LinearLayout里必须设置clickable和background,这样当点击上去的时候才会有点击效果。 
android:clickable="true" 
android:background="@drawable/state_btn_pressed"
 

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
 		xmlns:android="http://schemas.android.com/apk/res/android"
 		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:orientation="horizontal"
		android:id="@+id/layout_main"
		>
		<LinearLayout android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:orientation="vertical"
			android:gravity="center_horizontal"
			android:clickable="true"
			android:background="@drawable/state_btn_pressed"
			android:layout_weight="1"
			android:id="@+id/btn_0"
			>
			<ImageView android:layout_width="wrap_content"
				android:layout_height="wrap_content" 
				android:scaleType="fitCenter"
				android:src="@drawable/ic_call"
				>
			</ImageView>
			<TextView android:layout_width="wrap_content"
				android:layout_height="wrap_content" 
				android:textColor="#000000"
				android:textSize="18px"
				android:text="电话">
			</TextView>
		</LinearLayout>
		<LinearLayout android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:orientation="vertical"
			android:gravity="center_horizontal"
			android:clickable="true"
			android:background="@drawable/state_btn_pressed"
			android:layout_weight="1"
			android:id="@+id/btn_1"
			>
			<ImageView android:layout_width="wrap_content"
				android:layout_height="wrap_content" 
				android:scaleType="fitCenter"
				android:src="@drawable/ic_home"
				>
			</ImageView>
			<TextView android:layout_width="wrap_content"
				android:layout_height="wrap_content" 
				android:textColor="#000"
				android:textSize="18px"
				android:text="空间">
			</TextView>
		</LinearLayout>
		
		<LinearLayout android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:orientation="vertical"
			android:gravity="center_horizontal"
			android:clickable="true"
			android:background="@drawable/state_btn_pressed"
			android:layout_weight="1"
			android:id="@+id/btn_2"
			>
			<ImageView android:layout_width="wrap_content"
				android:layout_height="wrap_content" 
				android:scaleType="fitCenter"
				android:src="@drawable/ic_sms"
				>
			</ImageView>
			<TextView android:layout_width="wrap_content"
				android:layout_height="wrap_content" 
				android:textColor="#000"
				android:textSize="18px"
				android:text="短信"
				>
			</TextView>
		</LinearLayout>
</LinearLayout>

state_btn_pressed.xml,点击的效果: 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/bg_btn_pressed"
        android:padding="0dp"/>
</selector>

Android 模仿迅雷的 PopupWindow 出现/消失动画  
出现:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<scale android:fromXScale="0.6" android:toXScale="1.1"
		android:fromYScale="0.6" android:toYScale="1.1" android:pivotX="50%"
		android:pivotY="50%" android:duration="200" />
	<scale android:fromXScale="1.0" android:toXScale="0.91"
		android:fromYScale="1.0" android:toYScale="0.91" android:pivotX="50%"
		android:pivotY="50%" android:duration="400" android:delay="200" />
	<alpha android:interpolator="@android:anim/linear_interpolator"
		android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="400" />
</set>

消失: 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<scale android:fromXScale="1.0" android:toXScale="1.25"
		android:fromYScale="1.0" android:toYScale="1.25" android:pivotX="50%"
		android:pivotY="50%" android:duration="200" />
	<scale android:fromXScale="1.0" android:toXScale="0.48"
		android:fromYScale="1.0" android:toYScale="0.48" android:pivotX="50%"
		android:pivotY="50%" android:duration="400" android:delay="200" />
	<alpha android:interpolator="@android:anim/linear_interpolator"
		android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="400" />
</set>

最后用下面的 XML 封装: 

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<style name="PopupAnimation" parent="android:Animation"
		mce_bogus="1">
		<item name="android:windowEnterAnimation">@anim/anim_dialog_show</item>
		<item name="android:windowExitAnimation">@anim/anim_dialog_hide</item>
	</style>
</resources>

DraweRoll.rar  (111 KB)
相关文章
|
4月前
|
缓存 前端开发 Android开发
安卓开发中的自定义视图:从零到英雄
【10月更文挑战第42天】 在安卓的世界里,自定义视图是一块画布,让开发者能够绘制出独一无二的界面体验。本文将带你走进自定义视图的大门,通过深入浅出的方式,让你从零基础到能够独立设计并实现复杂的自定义组件。我们将探索自定义视图的核心概念、实现步骤,以及如何优化你的视图以提高性能和兼容性。准备好了吗?让我们开始这段创造性的旅程吧!
74 1
|
5月前
|
Android开发 开发者
安卓应用开发中的自定义视图
【9月更文挑战第37天】在安卓开发的海洋中,自定义视图犹如一座座小岛,等待着勇敢的探索者去发现其独特之处。本文将带领你踏上这段旅程,从浅滩走向深海,逐步揭开自定义视图的神秘面纱。
63 3
|
5月前
|
数据可视化 Android开发 开发者
安卓应用开发中的自定义View组件
【10月更文挑战第5天】在安卓应用开发中,自定义View组件是提升用户交互体验的利器。本篇将深入探讨如何从零开始创建自定义View,包括设计理念、实现步骤以及性能优化技巧,帮助开发者打造流畅且富有创意的用户界面。
176 0
|
4月前
|
搜索推荐 前端开发 Android开发
安卓应用开发中的自定义视图实现
【10月更文挑战第30天】在安卓开发的海洋中,自定义视图是那抹不可或缺的亮色,它为应用界面的个性化和交互体验的提升提供了无限可能。本文将深入探讨如何在安卓平台创建自定义视图,并展示如何通过代码实现这一过程。我们将从基础出发,逐步引导你理解自定义视图的核心概念,然后通过一个实际的代码示例,详细讲解如何将理论应用于实践,最终实现一个美观且具有良好用户体验的自定义控件。无论你是想提高自己的开发技能,还是仅仅出于对安卓开发的兴趣,这篇文章都将为你提供价值。
|
4月前
|
Android开发 开发者 UED
安卓开发中自定义View的实现与性能优化
【10月更文挑战第28天】在安卓开发领域,自定义View是提升应用界面独特性和用户体验的重要手段。本文将深入探讨如何高效地创建和管理自定义View,以及如何通过代码和性能调优来确保流畅的交互体验。我们将一起学习自定义View的生命周期、绘图基础和事件处理,进而探索内存和布局优化技巧,最终实现既美观又高效的安卓界面。
79 5
|
5月前
|
Android开发 UED
Android 中加载 Gif 动画
【10月更文挑战第20天】加载 Gif 动画是 Android 开发中的一项重要技能。通过使用第三方库或自定义实现,可以方便地在应用中展示生动的 Gif 动画。在实际应用中,需要根据具体情况进行合理选择和优化,以确保用户体验和性能的平衡。可以通过不断的实践和探索,进一步掌握在 Android 中加载 Gif 动画的技巧和方法,为开发高质量的 Android 应用提供支持。
|
5月前
|
XML 前端开发 Java
安卓应用开发中的自定义View组件
【10月更文挑战第5天】自定义View是安卓应用开发的一块基石,它为开发者提供了无限的可能。通过掌握其原理和实现方法,可以创造出既美观又实用的用户界面。本文将引导你了解自定义View的创建过程,包括绘制技巧、事件处理以及性能优化等关键步骤。
|
6月前
|
Android开发 开发者
安卓开发中的自定义视图:从入门到精通
【9月更文挑战第19天】在安卓开发的广阔天地中,自定义视图是一块充满魔力的土地。它不仅仅是代码的堆砌,更是艺术与科技的完美结合。通过掌握自定义视图,开发者能够打破常规,创造出独一无二的用户界面。本文将带你走进自定义视图的世界,从基础概念到实战应用,一步步展示如何用代码绘出心中的蓝图。无论你是初学者还是有经验的开发者,这篇文章都将为你打开一扇通往创意和效率的大门。让我们一起探索自定义视图的秘密,将你的应用打造成一件艺术品吧!
99 10
|
6月前
|
XML 编解码 Android开发
安卓开发中的自定义视图控件
【9月更文挑战第14天】在安卓开发中,自定义视图控件是一种高级技巧,它可以让开发者根据项目需求创建出独特的用户界面元素。本文将通过一个简单示例,引导你了解如何在安卓项目中实现自定义视图控件,包括创建自定义控件类、处理绘制逻辑以及响应用户交互。无论你是初学者还是有经验的开发者,这篇文章都会为你提供有价值的见解和技巧。
87 3

热门文章

最新文章