Android 簡單動畫
阿新 • • 發佈:2018-12-19
public class MainActivity extends AppCompatActivity {
private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取圖片控制元件 iv = (ImageView) findViewById(R.id.iv); } /** * @param v * 透明動畫 */ public void alpha(View v) { // 建立透明度動畫, 1.0:完全不透明,, 0.0:完全透明 AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f); //設定時長 alphaAnimation.setDuration(3000); //啟動 動畫 iv.startAnimation(alphaAnimation); } /** *Translate:平移動畫 * @param v */ public void trans(View v) { //前兩個引數是控制X軸, 後兩個引數是控制Y軸; TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f); translateAnimation.setDuration(3000); iv.startAnimation(translateAnimation); } /** *rotate :旋轉動畫 * //引數1:從哪個旋轉角度開始 //引數2:轉到什麼角度 //-------(後4個引數用於設定圍繞著旋轉的圓的圓心在哪裡) //引數3:確定x軸座標的型別,有ABSOLUT絕對座標、RELATIVE_TO_SELF相對於自身座標、RELATIVE_TO_PARENT相對於父控制元件的座標 //引數4:x軸的值,0.5f表明是以自身這個控制元件的一半長度為x軸 //引數5:確定y軸座標的型別 //引數6:y軸的值,0.5f表明是以自身這個控制元件的一半長度為x軸 */ public void rotate(View v) { RotateAnimation rotateAnimation = new RotateAnimation( 0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f); rotateAnimation.setDuration(3000); iv.startAnimation(rotateAnimation); } /** * scale:縮放動畫 * @param v */ public void scale(View v) { // X軸從哪到哪, //Y 軸從哪到哪 ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f); RotateAnimation rotateAnimation = new RotateAnimation( 0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //動畫集合 AnimationSet animationSet = new AnimationSet(true); animationSet.setDuration(3000); animationSet.addAnimation(scaleAnimation); animationSet.addAnimation(rotateAnimation); iv.startAnimation(animationSet); // 動畫監聽 animationSet.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } //動畫結束的時候回撥 @Override public void onAnimationEnd(Animation animation) { Toast.makeText(MainActivity.this, "動畫播放結束", Toast.LENGTH_SHORT).show(); } @Override public void onAnimationRepeat(Animation animation) { } }); } /** * 使用佈局來實現動畫 * @param view */ public void goLayout(View view) { startActivity(new Intent(MainActivity.this,Main2Activity.class)); }
}
佈局檔案
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:onClick="alpha" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="透明" /> <Button android:onClick="trans" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="位移" /> <Button android:onClick="rotate" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="旋轉" /> <Button android:onClick="scale" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="縮放" /> </LinearLayout> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_marginTop="100dp" android:layout_marginLeft="100dp" android:layout_height="wrap_content" android:src="@drawable/aa"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentTop="true" android:layout_marginTop="61dp" android:onClick="goLayout" android:text="跳轉到佈局實現" />