怎么写淡入淡出的动画?怎么写淡入淡出的动画?怎么写淡入淡出的动画
6条回答 默认 最新
- qq_27578023 2016-06-14 14:41关注
public class MainActivity extends Activity implements View.OnClickListener{
private TextView[] tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.objectanimatortest);
tv = new TextView[]{
(TextView) findViewById(R.id.tv01),
(TextView) findViewById(R.id.tv02),
(TextView) findViewById(R.id.tv03),
(TextView) findViewById(R.id.tv04),(TextView) findViewById(R.id.tv11), (TextView) findViewById(R.id.tv12), (TextView) findViewById(R.id.tv13), (TextView) findViewById(R.id.tv14) }; initTextViews(); } private void initTextViews(){ for(TextView textView :tv){//颜色随机变化代码 使用ValueAnimator类给每个视图加上北京颜色随时间变化的动画 int color1 = Color.rgb((new Random()).nextInt(255), (new Random()).nextInt(255), (new Random()).nextInt(255)) ; int color2 = Color.rgb((new Random()).nextInt(255), (new Random()).nextInt(255), (new Random()).nextInt(255)) ; ValueAnimator animator = ObjectAnimator.ofInt(textView, "backgroundColor", color1,color2); animator.setDuration(3000); animator.setEvaluator(new ArgbEvaluator());//设置数值计算器,保证动画变化过程中的数值正确 animator.setRepeatCount(ValueAnimator.INFINITE); animator.setRepeatMode(ValueAnimator.REVERSE); animator.start(); textView.setOnClickListener(this); } } @Override public boolean onCreateOptionsMenu(Menu menu) { //getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onClick(final View view) { final ValueAnimator animator1 = ObjectAnimator.ofFloat(view, "alpha",1,0);//淡出效果 animator1.setDuration(1000); animator1.setInterpolator(new AccelerateInterpolator()); ValueAnimator animator2 = ObjectAnimator.ofFloat(view,"x",view.getX(),(view.getX()-view.getWidth()));//向左移动效果 animator2.setDuration(1000); animator2.setInterpolator(new DecelerateInterpolator()); AnimatorSet animatorSet = new AnimatorSet();//合起来就是左淡出效果 animatorSet.play(animator2).before(animator1);
// animatorSet.start();
final ValueAnimator animator3 = ObjectAnimator.ofFloat(view, "alpha",0,1);//淡入效果 animator3.setDuration(1000); animator3.setInterpolator(new AccelerateInterpolator()); ValueAnimator animator4 = ObjectAnimator.ofFloat(view,"x",view.getX()+2*view.getWidth(),view.getX());//从右边向左移动 animator4.setDuration(1000); animator4.setInterpolator(new DecelerateInterpolator()); animator4.addListener(new AnimatorListenerAdapter() {//当动画播放完,我们做什么 @Override public void onAnimationEnd(Animator animation) {//向下移动淡出,然后向上移动淡入 super.onAnimationEnd(animation); final ValueAnimator animatorY = ObjectAnimator.ofFloat(view, "y", view.getY(),view.getY()+view.getHeight()); animatorY.setDuration(1000); final ValueAnimator alphaY = animator1.clone(); ValueAnimator rotate = ObjectAnimator.ofFloat(view,"rotationY",0,90); rotate.setDuration(2000);
// rotate.start();
animatorY.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); animatorY.reverse(); animator3.clone().start(); } }); AnimatorSet set = new AnimatorSet(); set.play(animatorY).with(alphaY); set.start(); } });
// animatorSet.play(animator3).after(animator1);//合起来就是左淡出,右淡入效果
// animatorSet.play(animator3).with(animator4);
// animatorSet.start();AnimatorSet animatorSet1 = new AnimatorSet(); animatorSet1.play(animator3).with(animator4); AnimatorSet animatorSet2 = new AnimatorSet(); animatorSet2.play(animator2).with(animator1); AnimatorSet set = new AnimatorSet(); set.playSequentially(animatorSet2,animatorSet1);//使用playSequentially方法测试效果 set.start(); }
}这是我敲的一个小例子
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 如何在scanpy上做差异基因和通路富集?
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配
- ¥15 Power query添加列问题
- ¥50 Kubernetes&Fission&Eleasticsearch
- ¥15 報錯:Person is not mapped,如何解決?
- ¥15 c++头文件不能识别CDialog