首先是主要代碼 的實現 ,去調用 各個xml 配置文件:
MainActivity:
public class MainActivity extends Activity { private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取顯示控件ID imageView = (ImageView) findViewById(R.id.imageView1); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void animImpl(View v){ //調用 動畫的配置文件 Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_demo); imageView.startAnimation(animation); } //旋轉動畫 public void rotate(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_demo); imageView.startAnimation(animation); } //縮放動畫 public void scaleImpl(){ Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale_demo); imageView.startAnimation(animation); } //移動效果 public void translateImpl(){ //XML文件 /* Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_demo); animation.setRepeatCount(Animation.INFINITE);*/ /* 第一種 * imageView.setAnimation(animation); animation.start();*/ //第二種 // imageView.startAnimation(animation); //Java代碼 TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 0); translateAnimation.setDuration(2000); imageView.startAnimation(translateAnimation); } //縮放效果 }
<alpha xmlns:android="https://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromAlpha="1.0" android:toAlpha="0.1" android:duration="2000"/> <!-- fromAlpha :起始透明度 toAlpha:結束透明度 1.0表示完全不透明 0.0表示完全透明 -->