通过XML定义Tween(补间动画)指令完成动画

/**
 * Created by zhu on 2018/6/26.
 */

public class BicycleView_New extends View {

    private  Context context;
    private Animation mAnimationAlpha = null;

    private Animation mAnimationScale = null;

    private Animation mAnimationTranslate = null;

    private Animation mAnimationRotate = null;

    private AnimationSet as = null;

    Bitmap mBicycle = null;


    public BicycleView_New(Context context) {
        super(context);
        this.context = context;
        mBicycle = ((BitmapDrawable) getResources().getDrawable(
                R.drawable.qq
        )).getBitmap();
        //
//        mAnimationAlpha = new AlphaAnimation(
//                0.1f, 1.0f
//        );

        mAnimationAlpha = AnimationUtils.loadAnimation(
                context,R.anim.alpha_animation
        );
        mAnimationAlpha.setDuration(1000 * 4);





        mAnimationScale = AnimationUtils.loadAnimation(
                context,R.anim.scale_animation
        );
        mAnimationScale.setDuration(1000 * 4);

       mAnimationTranslate = AnimationUtils.loadAnimation(
                context,R.anim.translate_animation
        );

        mAnimationTranslate.setDuration(1000 * 6);

        mAnimationRotate = AnimationUtils.loadAnimation(
                context,R.anim.rotate_animation
        );

        mAnimationRotate.setDuration(1000 * 4);

//        as = new AnimationSet(true);
//        as.addAnimation(mAnimationAlpha);
//        as.addAnimation(mAnimationScale);
//        as.addAnimation(mAnimationTranslate);
//        as.addAnimation(mAnimationRotate);
//        as.setDuration(1000 * 4);

        as = (AnimationSet) AnimationUtils.loadAnimation(
                context,R.anim.com
        );



    }

    public BicycleView_New(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BicycleView_New(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mBicycle, 0, 0, null);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_UP:
                startAnimation(mAnimationScale);
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                startAnimation(mAnimationTranslate);
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                startAnimation(mAnimationAlpha);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                startAnimation(mAnimationRotate);
                break;
            case KeyEvent.KEYCODE_DPAD_CENTER:
                startAnimation(as);
                break;
        }

        return true;
    }
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="4000"
        android:fromAlpha="0.1"
        android:toAlpha="1.0"></alpha>
    <rotate
        android:duration="4000"
        android:fromDegrees="0"
        android:pivotX="30%"
        android:pivotY="30%"
        android:toDegrees="+360"></rotate>
    <scale
        android:duration="6000"
        android:fillAfter="false"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="70%"
        android:pivotY="70%"
        android:toXScale="1.0"
        android:toYScale="1.0"></scale>
    <translate
        android:duration="6000"
        android:fromXDelta="5"
        android:fromYDelta="4"
        android:toXDelta="200"
        android:toYDelta="150"></translate>
</set>


相关文章:

  • 2022-12-23
  • 2021-07-02
  • 2022-12-23
  • 2022-01-31
  • 2022-12-23
  • 2021-08-22
  • 2022-12-23
猜你喜欢
  • 2022-01-15
  • 2021-12-15
  • 2021-06-22
  • 2021-06-15
  • 2021-08-12
  • 2022-12-23
  • 2021-08-20
相关资源
相似解决方案