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>
时间: 2024-12-22 19:18:22