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

之前创建动画利用的代码实现,实则还可以利用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);
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值