android Animations 动画效果(二)

Animations的第二种使用方法
1在res文件夹下新建一个anim文件夹
2.创建xml文件,并首先加入set标签,改标签如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
</set>
3.在该标签中加入rotate,alpha,scale或translate标签

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


下面是代码

MainActivity.java

package com.yx.animations01;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private Button scaleButton=null;
    private Button rotateButton=null;
    private Button alphaButton=null;
    private Button translateButton=null;
    private ImageView imageView = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        scaleButton = (Button) findViewById(R.id.scaleButton);
        scaleButton.setOnClickListener(new scaleButtonListener());
        
        rotateButton = (Button) findViewById(R.id.rotateButton);
        rotateButton.setOnClickListener(new rotateButtonListener());
        
        alphaButton = (Button) findViewById(R.id.alphaButton);
        alphaButton.setOnClickListener(new alphaButtonListener());
        
        translateButton = (Button) findViewById(R.id.translateButton);
        translateButton.setOnClickListener(new translateButtonListener());
        
        imageView = (ImageView) findViewById(R.id.imageViewId);
    }

    class scaleButtonListener implements OnClickListener{
        @Override
        public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
            imageView.startAnimation(animation);
        }
    }
    
    class rotateButtonListener implements OnClickListener{
        @Override
        public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
            imageView.startAnimation(animation);
        }
    }
    
    //淡入淡出
    class alphaButtonListener implements OnClickListener{
        @Override
        public void onClick(View v) {
            //使用AnimationUtils装载动画设置文件
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
            imageView.startAnimation(animation);
        }
    }
    
    class translateButtonListener implements OnClickListener{
        @Override
        public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
            imageView.startAnimation(animation);
        }
    }
    
}

下面分别是四个xml

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"></alpha>
</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="+350"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="3000"></rotate>
    <!--
        android:pivotX的值有三种设置方法
        android:pivotX="50"这种方法使用绝对定位
        android:pivotX="50%"这种方法为相对控件本身的定位
        android:pivotX="50%p"这种方法相对于控件的父控件定位
     -->

</set>

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">
    <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"
        ></scale>
</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="0%"
        android:toYDelta="100%"
        android:duration="2000"
        />
</set>
注意xml文件的放置位置


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值