安卓動畫(一)

amynn發表於2020-09-24

安卓動畫

檢視動畫

幀動畫(loading介面,上拉重新整理時)

res/drawable/animation-list標籤
在這裡插入圖片描述

在這裡插入圖片描述

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/image1" android:duration="200"></item>
    <item android:drawable="@drawable/image2" android:duration="200"></item>
    <item android:drawable="@drawable/image3" android:duration="200"></item>
</animation-list>
public class Main5Activity extends AppCompatActivity {

    ImageView imageView;
    AnimationDrawable animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        imageView = findViewById(R.id.image_1);
        animation = (AnimationDrawable) imageView.getBackground();
    }


    public void startAnim(View view) {
        animation.start();
    }

    public void stopAnim(View view) {
        animation.stop();
    }
}

補間動畫(頁面切換)

1.位置:res/anim/
2.事件:onAnimationStart、onAnimationEnd、onAnimationRepeat
3.分類:旋轉、平移、透明度、縮放
在這裡插入圖片描述
在這裡插入圖片描述

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360"
    android:duration="2000"></rotate>
 @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_translate:

                TranslateAnimation translateAnimation =
                        (TranslateAnimation) AnimationUtils.loadAnimation(this,R.anim.image_anim);
                //動畫結束後不回到起點
                translateAnimation.setFillAfter(true);
                translateAnimation.setRepeatCount(1);
                //REVERSE 從終點重複   RESTART 回到起點重複
                translateAnimation.setRepeatMode(TranslateAnimation.RESTART);
                imageView.startAnimation(translateAnimation);

                translateAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        Log.d("amy", "onAnimationStart: ");
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        Log.d("amy", "onAnimationEnd: ");
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        //設定了重複數量才會走到這個方法中
                        Log.d("amy", "onAnimationRepeat: ");
                    }
                });
                break;
            case R.id.btn_rotate:
                RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(this,R.anim.image_anim_rotate);
                imageView.startAnimation(rotateAnimation);
                break;
            case R.id.btn_scale:
                ScaleAnimation animation = (ScaleAnimation) AnimationUtils.loadAnimation(this,R.anim.image_anim_scale);
                animation.setDuration(2000);
                imageView.startAnimation(animation);
                break;
            case R.id.btn_alpha:
                AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(this,R.anim.image_anim_alpha);
                alphaAnimation.setDuration(2000);
                imageView.startAnimation(alphaAnimation);
                break;
            case R.id.btn_group:
                AnimationSet set = new AnimationSet(true);
                AlphaAnimation alphaAnimation1 = (AlphaAnimation) AnimationUtils.loadAnimation(this,R.anim.image_anim_alpha);
                alphaAnimation1.setDuration(2000);

                ScaleAnimation scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(this,R.anim.image_anim_scale);
                scaleAnimation.setDuration(2000);
                set.addAnimation(alphaAnimation1);
                set.addAnimation(scaleAnimation);

                imageView.startAnimation(set);
                break;
        }
    }

屬性動畫

在這裡插入圖片描述

case R.id.btn_vAnimTranslate:
                ValueAnimator valueAnimator = ValueAnimator.ofInt(0,100);
                valueAnimator.setDuration(2000);
                valueAnimator.setInterpolator(new LinearInterpolator()); //自由落體插值器
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();
                        layoutParams.leftMargin = (int) animation.getAnimatedValue();
//                        layoutParams.bottomMargin = (int) animation.getAnimatedValue();
                        imageView.setLayoutParams(layoutParams);
                    }
                });
                valueAnimator.start();
                break;

相關文章