补间动画和属性动画的用法

补间动画

package com.yztc.tweenanimation;

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

/**
* 补间动画的用法
*/

public class MainActivity extends Activity {

private ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iv = (ImageView) findViewById(R.id.iv);
}

// **透明度动画**
public void alpha(View v) {
    // 从不透明到透明 fromAlpha——>toAlpha:起始透明度到结束透明度
    AlphaAnimation aa = new AlphaAnimation(0.1f, 1.0f);
    // 动画的执行时间
    aa.setDuration(2000);
    // 动画的重复执行次数,一共执行次数+1
    aa.setRepeatCount(2);
    // 重复的模式
    aa.setRepeatMode(Animation.RESTART);// RESTART 重新开始,REVERSE 反转
    // 保存结束时候的状态
    aa.setFillAfter(true);
    // 开始执行动画
    iv.startAnimation(aa);
}

// **位移动画(有点绕)**
/**
 * fromXDelta/fromYDelta:x轴上的起点 /y轴上的起点 
 * toXDelta/toYDelta:x轴上的终点/y轴上的终点
 */
public void translate(View v) {

    // 方法一: 移动的距离为像素(四个参数)
    // TranslateAnimation ta = new TranslateAnimation(0, 300.0f, 0, 200.0f);// x轴偏移量,y轴偏移量
    /**
     * 方法二:(八个参数)
     * 1.fromXType:在x轴上起点进行变化时的类型 2.变化多大 
     * 3.x轴上在终点时变化类型 4.变化多大
     * 5.fromYType:在y轴上起点进行变化时的类型 6.变化多大 
     * 7.y轴上在终点时变化类型 8.变化多大
     * Animation.RELATIVE_TO_SELF+0.0f 相对于本身 控件现在所在的坐标 0* 控件本身的宽度(x轴)/高度(y轴)        
     * 注:0.0f不是固定值 
     * Animation.RELATIVE_TO_PARENT+0.0f 相对于父控件 控件现在所在的坐标 0* 控件的父控件的宽度(x轴)/高度(y轴)
     */

    // 设置为相对于父控件要比本身变化范围大(我这里用的是方法二)
    TranslateAnimation ta = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
            0.5f, Animation.RELATIVE_TO_SELF, 0,
            Animation.RELATIVE_TO_SELF, 0);
    ta.setDuration(2000);
    ta.setRepeatCount(2);
    ta.setRepeatMode(Animation.REVERSE);// 不是所有方法都适用
    iv.startAnimation(ta);
}

// **缩放动画**
public void scale(View v) {
    // ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2);// X轴缩放量,Y轴缩放量
    ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    sa.setDuration(2000);
    sa.setRepeatCount(2);
    iv.startAnimation(sa);
}

// **旋转动画**
public void rotate(View v) {
    // RotateAnimation ra = new RotateAnimation(0, 45);
    RotateAnimation ra = new RotateAnimation(0, 90,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    ra.setDuration(2000);
    ra.setRepeatCount(2);
    iv.startAnimation(ra);
}

// **动画集合**
public void set(View v) {
    // 创建一个集合
    AnimationSet sets = new AnimationSet(true);
    ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    RotateAnimation ra = new RotateAnimation(0, 90,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    sets.addAnimation(sa);
    sets.addAnimation(ra);
    sets.setDuration(3000);
    sets.setRepeatCount(2);
    iv.startAnimation(sets);
    }
}

布局

<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" >

<LinearLayout
    android:id="@+id/l1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <!-- 透明度 -->

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="alpha"
        android:text="Alpha" />
    <!-- 位移 -->

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="translate"
        android:text="Trans" />
    <!-- 缩放 -->

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="scale"
        android:text="Scale" />
    <!-- 旋转 -->

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="rotate"
        android:text="Rotate" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/l1"
    android:orientation="vertical" >

    <!-- 动画集合 -->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="set"
        android:text="集合" />
</LinearLayout>

<ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

属性动画和补间动画其实差别不大,只是作用到谁身上

package com.yztc.propertyanimation;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.yztc.tweenanimation.R;

/**
* 属性动画的用法
*/

public class PropertyAnimationActivity extends Activity {

private ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_property_animation);
    iv = (ImageView) findViewById(R.id.iv);
    iv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(PropertyAnimationActivity.this, "我在这里", Toast.LENGTH_SHORT).show();
        }
    });
}

// 透明度动画
public void alpha(View v) {
    /**
     * target:属性动画作用在谁身上 
     * propertyName:属性名称 
     * value:属性的变化范围值
     */
    ObjectAnimator anim = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.5f);
    anim.setDuration(2000);
    anim.setRepeatCount(2);
    anim.start();
}

// 位移动画
public void translate(View v) {
    ObjectAnimator anim = ObjectAnimator.ofFloat(iv, "translationX", 0,200f);
    anim.setDuration(2000);
    anim.setRepeatCount(2);
    anim.start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值