补间动画

通过代码实现

MainActivity.java

public class MainActivity extends Activity {
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
    }

    /**
     * 透明度渐变的动画
     * 
     * @param view
     */
    public void alpha(View view) {
        AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
        aa.setDuration(2000);
        aa.setRepeatCount(1);
        aa.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(aa);
    }

    /**
     * 位移动画
     * 
     * @param view
     */
    public void trans(View view) {
        TranslateAnimation ta = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, -0.5f,
                Animation.RELATIVE_TO_PARENT, 0.5f,
                Animation.RELATIVE_TO_PARENT, -0.5f,
                Animation.RELATIVE_TO_PARENT, 0.5f);
        ta.setDuration(2000);
        ta.setRepeatCount(1);
        ta.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(ta);
    }

    /**
     * 缩放动画
     * 
     * @param view
     */
    public void scale(View view) {
        ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f);
        sa.setDuration(2000);
        sa.setRepeatCount(1);
        sa.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(sa);
    }

    /**
     * 旋转动画
     * 
     * @param view
     */
    public void rotate(View view) {
        RotateAnimation ra = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF,
                1.0f);
        ra.setDuration(2000);
        ra.setRepeatCount(1);
        ra.setRepeatMode(Animation.REVERSE);
        iv.startAnimation(ra);
    }
    /**
     * 动画组合。
     * @param view
     */
    public void set(View view){
        AnimationSet set = new AnimationSet(false);
        TranslateAnimation ta = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, -0.5f,
                Animation.RELATIVE_TO_PARENT, 0.5f,
                Animation.RELATIVE_TO_PARENT, -0.5f,
                Animation.RELATIVE_TO_PARENT, 0.5f);
        ta.setDuration(2000);
        ta.setRepeatCount(1);
        ta.setRepeatMode(Animation.REVERSE);
        ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f);
        sa.setDuration(2000);
        sa.setRepeatCount(1);
        sa.setRepeatMode(Animation.REVERSE);
        RotateAnimation ra = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF,
                1.0f);
        ra.setDuration(2000);
        ra.setRepeatCount(1);
        ra.setRepeatMode(Animation.REVERSE);
        set.addAnimation(ta);
        set.addAnimation(sa);
        set.addAnimation(ra);
        iv.startAnimation(set);
    }
}

activity_main.xml



    

        

通过XML实现

public class MainActivity extends Activity {
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
    }

    /**
     * 透明度渐变的动画
     * 
     * @param view
     */
    public void alpha(View view) {
        Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha);
        iv.startAnimation(aa);
    }

    /**
     * 位移动画
     * 
     * @param view
     */
    public void trans(View view) {
        Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans);
        iv.startAnimation(ta);
    }

    /**
     * 缩放动画
     * 
     * @param view
     */
    public void scale(View view) {
        Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale);
        iv.startAnimation(sa);
    }

    /**
     * 旋转动画
     * 
     * @param view
     */
    public void rotate(View view) {
        Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate);
        iv.startAnimation(ra);
    }
    /**
     * 动画组合。
     * @param view
     */
    public void set(View view){
        Animation set = AnimationUtils.loadAnimation(this, R.anim.set);
        iv.startAnimation(set);
    }
}

alpha.xml



rotate.xml



scale.xml



set.xml



    
    
    
    
    
    

trans.xml




你可能感兴趣的:(补间动画)