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

时间:2023-03-09 03:57:12
Android 属性动画实现一个简单的PopupWindow

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

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

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

3.直接上代码:

  

 public class MainActivity extends Activity implements OnClickListener {

     private LinearLayout layout1, layout2;
private Button btnShow, btnHint; private int screenWidth, screenHeight; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
addListener(); } private void addListener() {
btnShow.setOnClickListener(this);
btnHint.setOnClickListener(this);
} private void initView() {
screenWidth = getWindowWidth(MainActivity.this);
screenHeight = getWindowHeigh(MainActivity.this);
layout1 = (LinearLayout) findViewById(R.id.layout1);
btnShow = (Button) findViewById(R.id.layout_button);
layout2 = (LinearLayout) findViewById(R.id.layout2);
btnHint = (Button) findViewById(R.id.btnhint);
} @SuppressLint("NewApi")
private void initShowMeanuAnimation() { System.out.println("执行没有");
ObjectAnimator animator1 = ObjectAnimator.ofFloat(layout1, "scaleX",
1.0f, 0.8f);
animator1.setDuration(350);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(layout1, "scaleY",
1.0f, 0.8f);
animator2.setDuration(350);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(layout1, "alpha",
1.0f, 0.5f);
animator3.setDuration(350);
ObjectAnimator animator4 = ObjectAnimator.ofFloat(layout1, "rotationX",
0f, 12f);
animator4.setDuration(200);
ObjectAnimator animator5 = ObjectAnimator.ofFloat(layout1, "rotationX",
12f, 0f);
animator5.setDuration(300);
animator5.setStartDelay(250);
ObjectAnimator animator6 = ObjectAnimator.ofFloat(layout1,
"translationY", 0, -screenHeight * 0.1f);
animator6.setDuration(350); ObjectAnimator animator7 = ObjectAnimator.ofFloat(layout2,
"translationY", 300, 0);
animator7.setDuration(350); animator7.addListener(new AnimatorListenerAdapter() { @Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation); layout2.setVisibility(View.VISIBLE); } }); AnimatorSet set = new AnimatorSet();
set.playTogether(animator1, animator2, animator3, animator4, animator5,
animator6, animator7); set.start();
} /**
* 隐藏PopupWindow
*/
@SuppressLint("NewApi")
public void initHideMeanuAnimation() { ObjectAnimator animator1 = ObjectAnimator.ofFloat(layout1, "scaleX",
0.8f, 1.0f);
animator1.setDuration(350);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(layout1, "scaleY",
0.8f, 1.0f);
animator2.setDuration(350);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(layout1, "alpha",
0.5f, 1.0f);
animator3.setDuration(350);
ObjectAnimator animator4 = ObjectAnimator.ofFloat(layout1, "rotationX",
0f, 12f);
animator4.setDuration(200);
ObjectAnimator animator5 = ObjectAnimator.ofFloat(layout1, "rotationX",
12f, 0f);
animator5.setDuration(300);
animator5.setStartDelay(250);
ObjectAnimator animator6 = ObjectAnimator.ofFloat(layout1,
"translationY", -screenHeight * 0.1f, 0);
animator6.setDuration(350); ObjectAnimator animator7 = ObjectAnimator.ofFloat(layout2,
"translationY", 0, 300);
animator7.setDuration(350); animator7.addListener(new AnimatorListenerAdapter() { @Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
layout2.setVisibility(View.INVISIBLE); } }); AnimatorSet set1 = new AnimatorSet();
set1.playTogether(animator1, animator2, animator3, animator4,
animator5, animator6, animator7); set1.start(); } @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.layout_button:
initShowMeanuAnimation();
break;
case R.id.btnhint:
initHideMeanuAnimation();
break; } } public static int getWindowWidth(Context context) {
// 获取屏幕分辨率
WindowManager wm = (WindowManager) (context
.getSystemService(Context.WINDOW_SERVICE));
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int mScreenWidth = dm.widthPixels;
return mScreenWidth; } public static int getWindowHeigh(Context context) {
// 获取屏幕分辨率
WindowManager wm = (WindowManager) (context
.getSystemService(Context.WINDOW_SERVICE));
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int mScreenHeigh = dm.heightPixels;
return mScreenHeigh;
}
}

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

源码下载:下载