补间动画的实现

来源:互联网 发布:花千骨天裳进阶数据 编辑:程序博客网 时间:2024/04/30 12:34
public class MainActivity extends Activity {

    private ImageView img_view;
    private AlphaAnimation animation;
    private AlphaAnimation alpha;
    private ScaleAnimation scaleAnimation;
    private TranslateAnimation transalte;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img_view = (ImageView) findViewById(R.id.img_view);
        // codeAlpha();
        // initAlpha();

        // 监听
        img_view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // scaleAnimtion();
                // codeScaleAnim();
                // transalteAnim();
                // codeTransaleAnim();
                // rotateAnim();
                // codeRotateAnim();
                // groupAnim();
                codeGroupAnim();
            }
        });
    }

    // 1.利用xml实现渐变的补间动画
    private void initAlpha() {
        alpha = (AlphaAnimation) AnimationUtils.loadAnimation
                (MainActivity.this, R.anim.lapha_two);
        img_view.startAnimation(alpha);
        
        /*  
         * <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="5000"
        xmlns:android="http://schemas.android.com/apk/res/android">
       
         </alpha>*/
        
    }

    // 1.用代码实现渐变的补间动画
    private void codeAlpha() {
        animation = new AlphaAnimation(0.0f, 1.0f);
        // 持续的时间
        animation.setDuration(5000);
        //
        img_view.startAnimation(animation);
    }

    // 2.用xml实现缩放
    private void scaleAnimtion() {
        scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(
                MainActivity.this, R.anim.scale_anim);
        img_view.startAnimation(scaleAnimation);
        
        /* XML实现缩放
         * <scale
        android:fromXScale="0.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:pivotX="0.5"
        android:pivotY="0.5"
        android:duration="1000"
        xmlns:android="http://schemas.android.com/apk/res/android">
        </scale>*/
        
    }

    // 2.用代码实现的缩放
    private void codeScaleAnim() {
        ScaleAnimation scale = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        scale.setDuration(1000);
        img_view.startAnimation(scale);
        
        
    }

    // 3.用xml实现平移
    private void transalteAnim() {
        transalte = (TranslateAnimation) AnimationUtils.loadAnimation(
                MainActivity.this, R.anim.translate_anim);
        img_view.startAnimation(transalte);
        
        /* XML实现的平移
         * <translate    
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXDelta="0.0"
        android:toXDelta="0.0"
        android:fromYDelta="0.0"
        android:toYDelta="50%"
        android:duration="1000"
        android:repeatCount="-1"
        android:repeatMode="reverse"
        >        
       </translate>*/
        
    }

    // 3.用代码实现平移
    private void codeTransaleAnim() {
        float toYValue;
        TranslateAnimation transalte2 = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0f);
        // 无限循环
        transalte2.setRepeatCount(-1);
        // REVERSE往返循环
        transalte2.setRepeatMode(Animation.REVERSE);
        transalte2.setDuration(1000);
        img_view.startAnimation(transalte2);
    }

    // 4.用xml实现旋转
    private void rotateAnim() {
        RotateAnimation rotate = (RotateAnimation) AnimationUtils
                .loadAnimation(MainActivity.this, R.anim.rotate_anim);
        img_view.startAnimation(rotate);
        
        /*  Xml定义
         * <rotate     
        xmlns:android="http://schemas.android.com/apk/res/android"
         android:fromDegrees="0.0"
         android:toDegrees="90"
         android:pivotX="50%"
         android:pivotY="50%"
         android:duration="2000"
        >        
    </rotate>*/
        
    }

    // 4.用代码实现旋转
    private void codeRotateAnim() {
        RotateAnimation rotate2 = new RotateAnimation(0f, 360f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        rotate2.setDuration(2000);
        img_view.startAnimation(rotate2);
    }

    // 5.用xml实现的组合状态
    private void groupAnim() {
        AnimationSet group = (AnimationSet) AnimationUtils.loadAnimation(
                MainActivity.this, R.anim.group_anim);
        img_view.startAnimation(group);

        /*
         * anima包下的 <set
         <set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:fromXScale="0.0"
        android:toXScale="1.5"
        android:fromYScale="0.0"
        android:toYScale="1.5"
        android:duration="2000"
        />
    <rotate
        android:fromDegrees="0.0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="3000"
        />

     </set>
         */

    }

    // 5.用代码实现的组合状态
    private void codeGroupAnim() {
        AnimationSet animationSet = new AnimationSet(false);
        // 旋转
        RotateAnimation rotates = new RotateAnimation(0f, 360f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        rotates.setDuration(2000);
        // 缩放
        ScaleAnimation scales = new ScaleAnimation(0f, 1.5f, 0f, 1.5f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        scales.setDuration(3000);
        // 将动画添加到动画集合中
        animationSet.addAnimation(rotates);
        animationSet.addAnimation(scales);
        // 开启
        img_view.startAnimation(animationSet);

    }

}

0 0
原创粉丝点击