• Android 属性动画实现一个简单的PopupWindow


    1.今天看到一个PopupWindow的效果如图:

    2.其实就是属性动画的一个简单实用就ObjectAnimator就可以的,想实现更多,更灵活的可以用ValueAnimator

    3.直接上代码:

      

      1 public class MainActivity extends Activity implements OnClickListener {
      2 
      3     private LinearLayout layout1, layout2;
      4     private Button btnShow, btnHint;
      5 
      6     private int screenWidth, screenHeight;
      7 
      8     @Override
      9     protected void onCreate(Bundle savedInstanceState) {
     10         super.onCreate(savedInstanceState);
     11         setContentView(R.layout.activity_main);
     12         initView();
     13         addListener();
     14 
     15     }
     16 
     17     private void addListener() {
     18         btnShow.setOnClickListener(this);
     19         btnHint.setOnClickListener(this);
     20     }
     21 
     22     private void initView() {
     23         screenWidth = getWindowWidth(MainActivity.this);
     24         screenHeight = getWindowHeigh(MainActivity.this);
     25         layout1 = (LinearLayout) findViewById(R.id.layout1);
     26         btnShow = (Button) findViewById(R.id.layout_button);
     27         layout2 = (LinearLayout) findViewById(R.id.layout2);
     28         btnHint = (Button) findViewById(R.id.btnhint);
     29     }
     30 
     31     @SuppressLint("NewApi")
     32     private void initShowMeanuAnimation() {
     33 
     34         System.out.println("执行没有");
     35         ObjectAnimator animator1 = ObjectAnimator.ofFloat(layout1, "scaleX",
     36                 1.0f, 0.8f);
     37         animator1.setDuration(350);
     38         ObjectAnimator animator2 = ObjectAnimator.ofFloat(layout1, "scaleY",
     39                 1.0f, 0.8f);
     40         animator2.setDuration(350);
     41         ObjectAnimator animator3 = ObjectAnimator.ofFloat(layout1, "alpha",
     42                 1.0f, 0.5f);
     43         animator3.setDuration(350);
     44         ObjectAnimator animator4 = ObjectAnimator.ofFloat(layout1, "rotationX",
     45                 0f, 12f);
     46         animator4.setDuration(200);
     47         ObjectAnimator animator5 = ObjectAnimator.ofFloat(layout1, "rotationX",
     48                 12f, 0f);
     49         animator5.setDuration(300);
     50         animator5.setStartDelay(250);
     51         ObjectAnimator animator6 = ObjectAnimator.ofFloat(layout1,
     52                 "translationY", 0, -screenHeight * 0.1f);
     53         animator6.setDuration(350);
     54 
     55         ObjectAnimator animator7 = ObjectAnimator.ofFloat(layout2,
     56                 "translationY", 300, 0);
     57         animator7.setDuration(350);
     58 
     59         animator7.addListener(new AnimatorListenerAdapter() {
     60 
     61             @Override
     62             public void onAnimationStart(Animator animation) {
     63                 super.onAnimationStart(animation);
     64 
     65                 layout2.setVisibility(View.VISIBLE);
     66 
     67             }
     68 
     69         });
     70 
     71         AnimatorSet set = new AnimatorSet();
     72         set.playTogether(animator1, animator2, animator3, animator4, animator5,
     73                 animator6, animator7);
     74 
     75         set.start();
     76     }
     77 
     78     /**
     79      * 隐藏PopupWindow
     80      */
     81     @SuppressLint("NewApi")
     82     public void initHideMeanuAnimation() {
     83 
     84         ObjectAnimator animator1 = ObjectAnimator.ofFloat(layout1, "scaleX",
     85                 0.8f, 1.0f);
     86         animator1.setDuration(350);
     87         ObjectAnimator animator2 = ObjectAnimator.ofFloat(layout1, "scaleY",
     88                 0.8f, 1.0f);
     89         animator2.setDuration(350);
     90         ObjectAnimator animator3 = ObjectAnimator.ofFloat(layout1, "alpha",
     91                 0.5f, 1.0f);
     92         animator3.setDuration(350);
     93         ObjectAnimator animator4 = ObjectAnimator.ofFloat(layout1, "rotationX",
     94                 0f, 12f);
     95         animator4.setDuration(200);
     96         ObjectAnimator animator5 = ObjectAnimator.ofFloat(layout1, "rotationX",
     97                 12f, 0f);
     98         animator5.setDuration(300);
     99         animator5.setStartDelay(250);
    100         ObjectAnimator animator6 = ObjectAnimator.ofFloat(layout1,
    101                 "translationY", -screenHeight * 0.1f, 0);
    102         animator6.setDuration(350);
    103 
    104         ObjectAnimator animator7 = ObjectAnimator.ofFloat(layout2,
    105                 "translationY", 0, 300);
    106         animator7.setDuration(350);
    107 
    108         animator7.addListener(new AnimatorListenerAdapter() {
    109 
    110             @Override
    111             public void onAnimationEnd(Animator animation) {
    112                 super.onAnimationEnd(animation);
    113                 layout2.setVisibility(View.INVISIBLE);
    114 
    115             }
    116 
    117         });
    118 
    119         AnimatorSet set1 = new AnimatorSet();
    120         set1.playTogether(animator1, animator2, animator3, animator4,
    121                 animator5, animator6, animator7);
    122 
    123         set1.start();
    124 
    125     }
    126 
    127     @Override
    128     public void onClick(View v) {
    129         switch (v.getId()) {
    130         case R.id.layout_button:
    131             initShowMeanuAnimation();
    132             break;
    133         case R.id.btnhint:
    134             initHideMeanuAnimation();
    135             break;
    136 
    137         }
    138 
    139     }
    140 
    141     public static int getWindowWidth(Context context) {
    142         // 获取屏幕分辨率
    143         WindowManager wm = (WindowManager) (context
    144                 .getSystemService(Context.WINDOW_SERVICE));
    145         DisplayMetrics dm = new DisplayMetrics();
    146         wm.getDefaultDisplay().getMetrics(dm);
    147         int mScreenWidth = dm.widthPixels;
    148         return mScreenWidth;
    149 
    150     }
    151 
    152     public static int getWindowHeigh(Context context) {
    153         // 获取屏幕分辨率
    154         WindowManager wm = (WindowManager) (context
    155                 .getSystemService(Context.WINDOW_SERVICE));
    156         DisplayMetrics dm = new DisplayMetrics();
    157         wm.getDefaultDisplay().getMetrics(dm);
    158         int mScreenHeigh = dm.heightPixels;
    159         return mScreenHeigh;
    160     }
    161 }

    代码有重复的地方就没有合并了。

    源码下载:下载

  • 相关阅读:
    四则运算
    读书计划
    典型用户和场景分析
    课堂练习--电梯调度
    重力解锁--用户需求调研
    书籍促销活动优惠问题
    小组开发项目--NABC分析
    梦断代码读后感之终结篇
    结对开发-求环状二维数组最大子数组
    结对开发之大数溢出
  • 原文地址:https://www.cnblogs.com/liangstudyhome/p/4467265.html
Copyright © 2020-2023  润新知