android動畫初級入門

   Android中的動畫處理能夠用SurfaceView來處理,研究深的同窗們能夠作一些遊戲什麼的好玩的東西,對於淺淺的咱們也只是平面的樣子了下面直接說一下簡單的動畫的四種變換方式:
ide

旋轉、移動、縮放、淡入淡出,示例以下動畫

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.p_w_picpathView1);
        iv.setOnClickListener(new OnClickListener() {
                         
            @Override
            public void onClick(View v) {
                AnimationSet animationSet = new AnimationSet(true);
                AlphaAnimation alpha = new AlphaAnimation(1, 0);
                alpha.setDuration(4000);
                animationSet.addAnimation(alpha);
                iv.setBackgroundResource(R.drawable.i7);
                iv.startAnimation(animationSet);
            }
        });
    }
                 
}
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.p_w_picpathView1);
        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimationSet animationSet = new AnimationSet(false);
                RotateAnimation rotate = new RotateAnimation(0, 360,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                rotate.setDuration(2000);
                animationSet.addAnimation(rotate);
                iv.startAnimation(animationSet);
            }
        });
    }
}
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.p_w_picpathView1);
        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimationSet animationSet = new AnimationSet(true);
                ScaleAnimation scale = new ScaleAnimation(1, 0.25f, 1, 0.25f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                scale.setDuration(3000);
                animationSet.addAnimation(scale);
//              iv.setBackgroundColor(color.black);
                iv.startAnimation(scale);
            }
        });
    }
}
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.p_w_picpathView1);
        iv.setOnClickListener(new OnClickListener() {
                         
            @Override
            public void onClick(View v) {
                AnimationSet animationSet = new AnimationSet(false);
                TranslateAnimation translate  = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
                translate.setDuration(4000);
                animationSet.addAnimation(translate);
                iv.startAnimation(animationSet);
            }
        });
    }
}
public class MainActivity extends Activity {
    private ImageView iv;
    //淡入淡出
//  private AlphaAnimation a1 = new AlphaAnimation(1, 0);
//  private AlphaAnimation a2 = new AlphaAnimation(0, 1);
    //縮放
//  private ScaleAnimation a1 = new ScaleAnimation(1, 0.25f, 1, 0.25f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//  private ScaleAnimation a2 = new ScaleAnimation(0.25f,1,0.25f,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
    //移動
//  private TranslateAnimation a1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
//  private TranslateAnimation a2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0);
    //旋轉
    private RotateAnimation a1 = new RotateAnimation(0,180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    private RotateAnimation a2 = new RotateAnimation(180,0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.p_w_picpathView1);
        iv.setBackgroundResource(R.drawable.i4);
        a1.setDuration(2000);
        a2.setDuration(2000);
        iv.setOnClickListener(new OnClickListener() {
                         
            @Override
            public void onClick(View v) {
                iv.startAnimation(a1);
                             
            }
        });
        a1.setAnimationListener(new AnimationListener() {
                         
            @Override
            public void onAnimationStart(Animation animation) {
            }
                         
            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
                             
            }
                         
            @Override
            public void onAnimationEnd(Animation animation) {
                iv.startAnimation(a2);
                             
            }
        });
    }
                 
}

效果請在模擬器中觀察哈~遊戲

相關文章
相關標籤/搜索