pop菜單

1.mainActivity

 

package com.example.topmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class MainActivity extends Activity {

 private Button btn;
 private PopupWindow pop;
 private View line;
 
 //這是pop種的空間
 private LinearLayout vi;
 private Animation animation;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  btn = (Button) findViewById(R.id.button1);

  //這是頭部的分割線
  line = (View) findViewById(R.id.line);

  animation = new TranslateAnimation(0, 0, -60f, 0);

  animation.setDuration(1000);

  btn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

    getPopupWindow();

    pop.showAsDropDown(line);

   }
  });
 }

 // 獲取popupwindow實例
 private void getPopupWindow() {

  if (null != pop) {
   pop.dismiss();
   return;

  } else {
   initPopWindow();
  }
 }

 public void initPopWindow() {

  View popupWindow_view = getLayoutInflater().inflate(R.layout.pop, null);

  vi = (LinearLayout) popupWindow_view.findViewById(R.id.head);

  int width = getWindowManager().getDefaultDisplay().getWidth();

  int height = getWindowManager().getDefaultDisplay().getHeight();
  // 創建popupwindow實例
  pop = new PopupWindow(popupWindow_view, width, height, true);
  // 設置動畫效果
  pop.setAnimationStyle(R.style.AnimationAlpha);
  //這是在pop中的某個控件添加效果
  vi.startAnimation(animation);
  // 不設置當前pop不能點擊,一定的記住
  pop.setFocusable(true);
  // 點擊其他地方消失
  popupWindow_view.setOnTouchListener(new OnTouchListener() {

   @Override
   public boolean onTouch(View v, MotionEvent event) {
    if (pop != null && pop.isShowing()) {
     pop.dismiss();
     pop = null;
    }
    return false;
   }
  });

 }
}

 

對於上面的動畫,這個可以在anim中定義相關的文件,然後再style中設置相關的效果

<style name="AnimationFade">

        <!-- PopupWindow左右彈出的效果 -->
        <item name="android:windowEnterAnimation">@anim/in_lefttoright</item>
        <item name="android:windowExitAnimation">@anim/in_righttoleft</item>
    </style>

上面的兩個">@anim/in_lefttoright,">@anim/in_lefttoright

1.   <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定義從左向右進入的動畫 -->
    <translate
        android:duration="500"
        android:fromXDelta="-100%"
        android:toXDelta="0" />

</set>

 

2.    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定義從右向左動畫退出動畫 -->
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%" />

</set>

 

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