標籤:
Rotate的xml檔案編寫方法
<rotate android:fromDegrees="0" android:toDegrees="+350" android:pivotX="50%" android:pivotY="50%" android:duration="1000"/>
*android:toDegrees="+350"正號代表的是旋轉方向,正號為順時針,負號為逆時針
*android:pivotX的值共有三種設定方法
1.android:pivotX="50"這種方法使用絕對位置定位(螢幕位置座標上50這個點)
2.android:pivotX="50%"這種方法使用相對於控制項本身定位
3.android:pivotX="50%p"這種方法相對於控制項的父控制項定位
其他幾種動畫的寫法與此類似
MainActivity.java
public class MainActivity extends Activity { private ImageView imageView; private Button button_1; private Button button_2; private Button button_3; private Button button_4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView)findViewById(R.id.imageView); button_1 = (Button)findViewById(R.id.button_1); button_2 = (Button)findViewById(R.id.button_2); button_3 = (Button)findViewById(R.id.button_3); button_4 = (Button)findViewById(R.id.button_4); AlphaButtonListener alphaButtonListener = new AlphaButtonListener(); RotateButtonListener rotateButtonListener = new RotateButtonListener(); ScaleButtonListener scaleButtonListener = new ScaleButtonListener(); TranslateButtonListener translateButtonListener = new TranslateButtonListener(); button_1.setOnClickListener(alphaButtonListener); button_2.setOnClickListener(rotateButtonListener); button_3.setOnClickListener(scaleButtonListener); button_4.setOnClickListener(translateButtonListener); } //淡入淡出 private class AlphaButtonListener implements OnClickListener { @Override public void onClick(View v) { //使用AnimationUtils裝載動畫檔案 Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imageView.startAnimation(animation); } } //旋轉 private class RotateButtonListener implements OnClickListener { @Override public void onClick(View v) { Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate); imageView.startAnimation(animation); } } //水平 private class TranslateButtonListener implements OnClickListener { @Override public void onClick(View v) { Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate); imageView.startAnimation(animation); } } //縮放 private class ScaleButtonListener implements OnClickListener { @Override public void onClick(View v) { Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale); imageView.startAnimation(animation); } }}
在res目錄下建立anim檔案夾
alpha.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:startOffset="500" android:duration="500"/></set>
rotate.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="3000"/></set>
scale.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="1.0" android:toXScale="0.0" android:fromYScale="1.0" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="1000"/></set>
translate.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:fromXDelta="50%" android:toXDelta="100%" android:fromYDelta="50%" android:toYDelta="100%" android:duration="1000"/></set>
android通過xml檔案實現Animation動畫