程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> android-安卓開發問題怎麼寫淡入淡出的動畫

android-安卓開發問題怎麼寫淡入淡出的動畫

編輯:編程解疑
安卓開發問題怎麼寫淡入淡出的動畫

怎麼寫淡入淡出的動畫?怎麼寫淡入淡出的動畫?怎麼寫淡入淡出的動畫

最佳回答:


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

}這是我敲的一個小例子

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved