首页 > 系统 > Android > 正文

Android Animation(二)利用xml创建动画

2019-11-06 09:34:12
字体:
来源:转载
供稿:网友

之前创建动画利用的代码实现,实则还可以利用xml文件实现。

首先我们需要在res文件夹下创建一个anim文件夹,然后创建四个xml文件

1.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">    //fromAlpha,toAlpha分别表示为起始和结束时的透明度    //startoffset 启动时间    <alpha        android:fromAlpha="1.0"        android:toAlpha="0.3"        android:startOffset="500"        android:duration="1000"/></set>2.rorate.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator">    //fromDegrees 开始的角度    //toDegrees 结束的角度    //pivotX 旋转时X轴坐标    //1)当值为"50",表示绝对位置定位    //2)当值为"50%",表示相对于控件本身定位    //3)当值为"50%p",表示相对于控件的父控件定位    //pivotY 旋转时Y轴坐标    //duration  动画的持续时间    <rotate        android:fromDegrees="0"        android:toDegrees="360"        android:pivotX="50%"        android:pivotY="50%"        android:duration="1000"/></set>3.scale.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:interpolator="@android:anim/accelerate_interpolator">    //fromXScale 起始X轴坐标    //toXScale   结束X轴坐标    //fromYScale 起始Y轴坐标    //toYScale   结束Y轴坐标    //pivotX    //pivotY    <scale        android:fromXScale="1.0"        android:toXScale="0.3"        android:fromYScale="1.0"        android:toYScale="0.3"        android:pivotX="50%"        android:pivotY="50%"        android:duration="1000"/></set>4.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="0%"        android:toXDelta="100%"        android:fromYDelta="0%"        android:toYDelta="100%"        android:duration="1000"/></set>

剩下的,就是布局文件以及Activity了

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="sc.animationtwo.MainActivity">    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/btn_rotate"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="旋转"/>        <Button            android:id="@+id/btn_scale"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="缩放"/>        <Button            android:id="@+id/btn_alpha"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="淡入淡出"/>        <Button            android:id="@+id/btn_translate"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="移动"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <ImageView            android:id="@+id/animations_image"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@mipmap/ic_launcher"            android:layout_gravity="center"/>    </LinearLayout></LinearLayout>
public class MainActivity extends AppCompatActivity {    PRivate Button btnRotate;    private Button btnScale;    private Button btnAlpha;    private Button btnTranslate;    private ImageView imageView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btnRotate = (Button) findViewById(R.id.btn_rotate);        btnScale = (Button) findViewById(R.id.btn_scale);        btnAlpha = (Button) findViewById(R.id.btn_alpha);        btnTranslate = (Button) findViewById(R.id.btn_translate);        imageView = (ImageView) findViewById(R.id.animations_image);        btnRotate.setOnClickListener(new RotateListener());        btnAlpha.setOnClickListener(new AlphaListener());        btnScale.setOnClickListener(new ScaleListener());        btnTranslate.setOnClickListener(new TranslateListener());    }    class RotateListener implements View.OnClickListener{        @Override        public void onClick(View v) {            Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.rorate);            imageView.startAnimation(animation);        }    }    class AlphaListener implements View.OnClickListener{        @Override        public void onClick(View v) {            Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha);            imageView.startAnimation(animation);        }    }    class ScaleListener implements View.OnClickListener{        @Override        public void onClick(View v) {            Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.scale);            imageView.startAnimation(animation);        }    }    class TranslateListener implements View.OnClickListener{        @Override        public void onClick(View v) {            Animation animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate);            imageView.startAnimation(animation);        }    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表