Android 属性动画实现一个简单的PopupWindow
1.今天看到一个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;
}
}
代码有重复的地方就没有合并了。
源码下载:下载
Android 属性动画实现一个简单的PopupWindow的更多相关文章
- Android属性动画之ValueAnimation
ValueAnimation是ObjectAnimation类的父类,经过前几天的介绍,相信大家对ObjectAnimation有了 一定的认识,今天就为大家最后介绍一下ValueAnimation, ...
- Android属性动画完全解析(下)
转载:http://blog.csdn.net/guolin_blog/article/details/44171115 大家好,欢迎继续回到Android属性动画完全解析.在上一篇文章当中我们学习了 ...
- Android属性动画完全解析(上),初识属性动画的基本用法
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43536355 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系 ...
- Android属性动画完全解析(中)
转载:http://blog.csdn.net/guolin_blog/article/details/43536355 大家好,在上一篇文章当中,我们学习了Android属性动画的基本用法,当然也是 ...
- Android属性动画完全解析(上)
Android属性动画完全解析(上) 转载:http://blog.csdn.net/guolin_blog/article/details/43536355 在手机上去实现一些动画效果算是件比较炫酷 ...
- Android 属性动画(Property Animation) 全然解析 (下)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38092093 上一篇Android 属性动画(Property Animatio ...
- Android 属性动画 源码解析 深入了解其内部实现
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42056859,本文出自:[张鸿洋的博客] 我参加了博客之星评选,如果你喜欢我的博 ...
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/44171115 大家好,欢迎继续回到Android属性动画完全解析.在上一篇文章当中 ...
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43536355 大家好,在上一篇文章当中,我们学习了Android属性动画的基本用法 ...
随机推荐
- Python请求外部POST请求,常见四种请求体
原文http://blog.csdn.net/silencemylove/article/details/50462206 HTTP 协议规定 POST 提交的数据必须放在消息主体(entity-bo ...
- Java HashMap 遍历方式探讨
JDK8之前,可以使用keySet或者entrySet来遍历HashMap,JDK8中引入了map.foreach来进行遍历. keySet其实是遍历了2次,一次是转为Iterator对象,另一次是从 ...
- JS中的this机制
1.this是啥? 简言之,this是JavaScript语言中定义的众多关键字之一,它的特殊在于它自动定义于每一个函数域内,但是this倒地指引啥东西却让很多人张二摸不着头脑.这里我们留个小悬念,希 ...
- 自动执行单元测试maven插件
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-suref ...
- python 阿里云短信群发推送
本篇文章是使用Python的Web框架Django提供发送短信接口供前端调用,Python版本2.7 阿里云入驻.申请短信服务.创建应用和模板等步骤请参考:阿里云短信服务入门 1.下载sdk 阿里云短 ...
- vue组件化开发实践
前言 公司目前制作一个H5活动,特别是有一定统一结构的活动,都要码一个重复的轮子.后来接到一个基于模板的活动设计系统的需求,便有了一下的内容.首先会对使用Vue进行开发的一些前期需要的技术储备进行简单 ...
- centos7 ping www.baidu.com ping 不通。
centos7 ping www.baidu.com ping 不通. 记录下,在搭建NodeJS服务器遇到的坑:centos7 ping www.baidu.com ping 不通. 1. 配置网卡 ...
- C#抽象类与接口的区别【转】
一.抽象类: 抽象类是特殊的类,只是不能被实例化(可以用派生类实例化基类对象):除此以外,具有类的其他特性:重要的是抽象类可以包括抽象方法(当然它可以有普通方法),这是普通类所不能的.抽象方 ...
- write()和print()还有<%= %>的细微区别
1.首先介绍write()和print()方法的区别: (1).write():仅支持输出字符类型数据,字符.字符数组.字符串等 (2).print():可以将各种类型(包括Object)的数据通过默 ...
- jsp滚动框(非滚动条)
<marquee scrollAmount=4 width=300>需要滚动的字</marquee> scrollAmount表示运动速度,值是正整数,默认为6,越大滚动越快 ...