Android开发之Animations(二)

Animations的第二种使用方法:(在xml文件中进行设置)

使用该方法的好处:提高程序的可重用性和可维护性。


使用该方法的步骤:

1.在res文件夹下新建一个名为anim的文件夹

2.创建Animation resource file文件,并首先加入set标签

3.在该标签当中加入rotate、alpha、scale或者translate标签并设置属性

4.在代码当中使用AnimationUtils装载对应的xml文件,并生成Animation对象


android:pivotX的值共有三种设置方法:

1.android:pivotX="50"这种方法使用绝对位置定位

2.android:pivotX="50%"这种方法相对于控件本身定位

3.android:pivotX="50%p"这种方法相对于父控件定位


alpha.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="500"/>
</set>


rotate.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:fromDegrees="0"
        android:toDegrees="+360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"
        android:repeatCount="infinite"
        />
</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="2000"
        android:repeatCount="3"/>

</set>


translate.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0%"
        android:toXDelta="100%p"
        android:fromYDelta="0%"
        android:toYDelta="100%p"
        android:duration="3000"/>
</set>


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mycompany.animations2.MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:src="@drawable/littledog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/alpha"
            android:text="淡入淡出动画效果"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/scale"
            android:text="缩放动画效果"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/rotate"
            android:text="旋转动画效果"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/translate"
            android:text="移动效果"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

MainActivity.java:

package com.mycompany.animations2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private Button alphaButton;
    private Button scaleButton;
    private Button rotateButton;
    private Button translateButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.imageView);
        alphaButton = (Button) findViewById(R.id.alpha);
        scaleButton = (Button) findViewById(R.id.scale);
        rotateButton = (Button) findViewById(R.id.rotate);
        translateButton = (Button) findViewById(R.id.translate);

        alphaButton.setOnClickListener(new AlphaButtonListener());
        rotateButton.setOnClickListener(new RotateButtonListener());
        scaleButton.setOnClickListener(new ScaleButtonListener());
        translateButton.setOnClickListener(new TranslateButtonListener());
    }

    class AlphaButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            //  使用AnimationUtils装载动画设置文件
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
            imageView.startAnimation(animation);
        }
    }
    class RotateButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
            imageView.startAnimation(animation);
        }
    }class ScaleButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
            imageView.startAnimation(animation);
        }
    }class TranslateButtonListener 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、付费专栏及课程。

余额充值