王学岗补间动画(一)——补间动画的初步应用

一、布局

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:layout_marginTop="200dp"
        android:id="@+id/iv_zhangxin"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_launcher" />

    <Button
         android:layout_marginTop="200dp"
        android:id="@+id/bt_alpha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="渐变动画" />

    <Button
        android:id="@+id/bt_scale"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="缩放动画" />

    <Button
        android:id="@+id/bt_rotation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="旋转动画" />

    <Button
        android:id="@+id/bt_translation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="平移动画" />

</LinearLayout>

二:代码

package com.example.bujiandonghua;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

    private ImageView iv_zhangxin;
    private Button bt_alpha, bt_translation, bt_scale, bt_rotation;
    private Animation translationAnimation;
    private boolean isRunning;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_zhangxin = (ImageView) findViewById(R.id.iv_zhangxin);
        bt_alpha = (Button) findViewById(R.id.bt_alpha);
        bt_rotation = (Button) findViewById(R.id.bt_rotation);
        bt_scale = (Button) findViewById(R.id.bt_scale);
        bt_translation = (Button) findViewById(R.id.bt_translation);
        bt_alpha.setOnClickListener(this);
        bt_rotation.setOnClickListener(this);
        bt_scale.setOnClickListener(this);
        bt_translation.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_alpha:
            alphaAnimation();
            break;
        case R.id.bt_rotation:
            rotationAnimation();
            break;
        case R.id.bt_translation:
            translationAnimation();
            break;
        case R.id.bt_scale:
            scaleAnimation();
            break;

        default:
            break;
        }

    }

    private void rotationAnimation() {
        Animation rotationAnimation=
        AnimationUtils.loadAnimation(this, R.anim.tween_rotation_animation);
        iv_zhangxin.startAnimation(rotationAnimation);
    }

    private void translationAnimation() {

        translationAnimation = AnimationUtils.loadAnimation(this,
                R.anim.tween_translate_animation);
            iv_zhangxin.startAnimation(translationAnimation);
    }

    private void scaleAnimation() {
        Animation scaleAnimation = AnimationUtils.loadAnimation(this,
                R.anim.tween_scale_animation);
        iv_zhangxin.startAnimation(scaleAnimation);
    }

    private void alphaAnimation() {
        Animation alphaAnimation = AnimationUtils.loadAnimation(this,
                R.anim.tween_alpha_animation);
        iv_zhangxin.startAnimation(alphaAnimation);
    }
}

资源

补间动画的资源需要定义在res/anim文件中

  1. alpha资源
<?xml version="1.0" encoding="utf-8"?>
<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1"
    android:toAlpha="0"
    android:duration="2000"/>

  1. translation资源
<?xml version="1.0" encoding="utf-8"?>
<!--
fromXDelta:动画起使时的x坐标上的位置
toXDelta:动画结束时的x坐标上的位置
fromYDelta:动画起使时的y坐标上的位置
toYDelta:动画结束时的y坐标上的位置
主意:没有指定fromXDelta fromYDelta toXDelta
toYDelta的时候,默认的是以自己为参照物
duration:动画执行时间s              

-->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:fromXDelta="0%"
    android:fromYDelta="0%"
    android:toXDelta="100%"
    android:toYDelta="100%" >

</translate>
  1. scale资源
<?xml version="1.0" encoding="utf-8"?>
  <!-- interpolator:加速器
  //加速在减速
   accelerate_decelerate_interpolator
   //加速
   accelerate_interpolator
   //减速
   decelerate_interpolator

   pivotX:动画相对于物件的x坐标的开始位置
   pivotY:动画相对于物件的y坐标的开始位置
         注:以上两个属性从0%-100%中取值50%取中心点
    -->
<scale
     xmlns:android="http://schemas.android.com/apk/res/android"
      android:fromXScale="0.0"
      android:fromYScale="0.0"
      android:toXScale="3.0"
      android:toYScale="3.0"
      android:fillAfter="true"
      android:duration="5000"
      android:pivotX="50%"
      android:pivotY="50%"
      android:interpolator="@android:anim/accelerate_decelerate_interpolator"
      />


  1. rotation资源
<?xml version="1.0" encoding="utf-8"?>
<!-- 
RotateAniamtion(旋转动画)
fromDegrees:动画起使时物体的角度
toDegrees:动画结束时物体的角度(可以大于360)
说明:当我们的设置的角度为负数--表示逆时针旋转
当我们的设置的角度为正数--表示顺时针旋转
    负数from — to正数 ---顺时针旋转)
    (负数from — to负数 ---逆时针旋转)
    正数from — to正数 ---顺时针旋转)
    (正数from — to负数 ---逆时针旋转)
    总结:凡是to为正数,那么物体就是顺时针旋转
 -->
<rotate
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="5000"
     android:fromDegrees="0"
     android:toDegrees="350"
     android:pivotX="50%"
     android:pivotY="50%">
</rotate>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值