qq_35312940 2016-06-14 10:04 采纳率: 100%
浏览 2069
已采纳

安卓开发问题怎么写淡入淡出的动画

怎么写淡入淡出的动画?怎么写淡入淡出的动画?怎么写淡入淡出的动画

  • 写回答

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();
    }
    

    }这是我敲的一个小例子

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集