仿QQ好友動態添加說說、圖片菜單滑動移進移出效果--在指定控件下面顯示popupwindow動畫不移效果

上一篇:PopupWindow 自定義窗口從屏幕底部上移-動畫

先看一下要做成的樣子和我做成的樣子:

 

功能一樣,但樣子不是太好看,大家微微看看就行了~

好了,我繼續上代碼:

看我的主類:MainActivity

package com.example.animationtest;

import com.example.animationtest.MyPopwindow.MyPopwindowListener;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
	private MyPopwindow popwindow;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViewById(R.id.bt_show_animation).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(popwindow == null){// 如果popupwindow沒顯示
					popwindow = new MyPopwindow(MainActivity.this);
					popwindow.setMyPopwindowListener(new MyPopwindowListener() {
						
						@Override
						public void onBlankspaceClickListener(View v) {
							popwindow.dismiss();
							popwindow = null;
						}
					});
					popwindow.showWindow(v);
				}else{
					if(popwindow.isShowing()){
						popwindow.dismiss();
						popwindow = null;
					}
				}
			}
		});
	}
}

主類裏面的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/bt_show_animation"
        android:layout_width="match_parent"
        android:gravity="center"
        android:background="@android:color/holo_blue_dark"
        android:layout_height="100dp"
        android:textColor="@android:color/white"
        android:text="開始動畫" />

</RelativeLayout>

我的pop類:MyPopwindow

package com.example.animationtest;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.PopupWindow;

public class MyPopwindow {

	private MyWindow window;
	private View animationView;
	private AnimationSet set;
	private AnimationSet set1;
	private MyPopwindowListener myPopwindowListener;
	
	// 構造方法
	public MyPopwindow(Context context) {
		window = new MyWindow(context);// 實例化當前類
	}
<span style="white-space:pre">	</span>// 這是要彈出的popupwindow窗體
	private class MyWindow extends PopupWindow {

		public MyWindow(Context context) {
			super(context);
			View v = LayoutInflater.from(context).inflate(
					R.layout.window_layout, null);// 找到這個窗體
			animationView = v.findViewById(R.id.ll_transView);// 初始化這個窗體裏面的要執行的動畫區域模塊
			v.setOnClickListener(new View.OnClickListener() {// 點擊這個窗體任意地方事件

				@Override
				public void onClick(View arg0) {
					if(myPopwindowListener!=null){
						myPopwindowListener.onBlankspaceClickListener(arg0);
					}
				}
			});
			initEnterAnimation();
			initExitAnimation();
			setWidth(LayoutParams.MATCH_PARENT);
			setHeight(LayoutParams.MATCH_PARENT);
			setContentView(v);
			ColorDrawable dw = new ColorDrawable(0xb0000000);
			setBackgroundDrawable(dw);
			setAnimationStyle(R.style.PopupAnimation);
		}
	}
<span style="white-space:pre">	</span>// 移出
	private void initExitAnimation() {
	<span style="white-space:pre">	</span>int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        <span style="white-space:pre">	</span>int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
       <span style="white-space:pre">	</span> <span style="white-space:pre">	</span>animationView.measure(w, h);
       <span style="white-space:pre">	</span> <span style="white-space:pre">	</span>int width =animationView.getMeasuredWidth();
        <span style="white-space:pre">	</span>int height =animationView.getMeasuredHeight();<span style="white-space:pre">
</span><pre name="code" class="java"><span style="white-space:pre">		</span>//  以上代碼是計算控件長寬
<span style="white-space:pre">		</span>// 以下是設置動畫
set1 = new AnimationSet(true);TranslateAnimation animation = new TranslateAnimation(0, 0, 0, -height);// 設置動畫移動時從哪移動到哪兒,這裏的-height是指要移動的部分的高度set1.setDuration(300);// 動畫時間set1.addAnimation(animation);// 添加動畫set1.setAnimationListener(new AnimationListener() {// 動畫監聽 @Overridepublic void onAnimationStart(Animation arg0) {}@Overridepublic void onAnimationRepeat(Animation arg0) {}@Overridepublic void onAnimationEnd(Animation arg0) {window.dismiss();}});} // 移入-以下代碼和上面一樣,不做解釋private void initEnterAnimation() {int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); animationView.measure(w, h); int width =animationView.getMeasuredWidth(); int height =animationView.getMeasuredHeight();animationView.setVisibility(View.GONE);set = new AnimationSet(true);TranslateAnimation animation = new TranslateAnimation(0, 0, -height, 0);set.setDuration(500);set.addAnimation(animation);set.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation arg0) {animationView.setVisibility(View.VISIBLE);}@Overridepublic void onAnimationRepeat(Animation arg0) {}@Overridepublic void onAnimationEnd(Animation arg0) {}});}public void setMyPopwindowListener(MyPopwindowListener myPopwindowListener){this.myPopwindowListener = myPopwindowListener;}private void startEnterAnimation() {animationView.startAnimation(set);}private void startExitAnimation() {animationView.startAnimation(set1);}public void showWindow(View v) {window.showAsDropDown(v); // 設置將pop顯示在哪個控件下方startEnterAnimation();// 開始動畫-進入} // 開始動畫-進入public boolean isShowing() {return window.isShowing();} // 開始動畫-退出public void dismiss() {startExitAnimation();}// 接口public interface MyPopwindowListener{void onBlankspaceClickListener(View v);}}

要彈出來的popupwindow界面佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/ll_transView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:background="@android:color/white"
            android:padding="20dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="1" />

        <TextView
            android:background="@android:color/white"
            android:padding="20dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="2" />

        <TextView
            android:background="@android:color/white"
            android:padding="20dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="3" />

        <TextView
            android:background="@android:color/white"
            android:padding="20dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="4" />
    </LinearLayout>

</LinearLayout>
在看一下我anim文件夾裏面的代碼:

1-> popup_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
		android:fromAlpha="0.0"
		android:toAlpha="1.0"
		android:duration="500" />
</set>
2-> popup_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
		android:fromAlpha="1.0"
		android:toAlpha="0.0"
		android:duration="200" />
</set>

styles裏面:

<style name="PopupAnimation" parent="android:Animation" mce_bogus="1">        
        <item name="android:windowEnterAnimation">@anim/popup_enter</item>   
        <item name="android:windowExitAnimation">@anim/popup_exit</item>   
    </style>  

其實就這麼簡單:有不清楚的可以回覆我的哦~

這是我寫的demo的下載地址,點此進入哦



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章