Animation动画效果一

本文提供了一个详细的Android动画实现案例,包括平移、透明度变化、旋转和缩放等常见动画效果的实现方式。通过实例代码展示了如何使用AnimationSet组合多种动画,并设置动画的持续时间、重复次数等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

重要方法例子
AnimationSet as = new AnimationSet(true);
AlphaAnimation als = new AlphaAnimation(1, 0);
als.setDuration(1000);
//设置为true, 动画执行后, 控件停留在执行结束后的状态
als.setFillAfter(true);
//设置为true, 动画执行后, 控件返回执行前得状态
als.setFillBefore(false);
//设置1000ms, 表示1000ms后再执行动画
als.setStartOffset(1000);
//设置执行动画5次
als.setRepeatCount(5);
as.addAnimation(als);
iv.startAnimation(as);


完整例子:
package com.cn;

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 AnimationActivity extends Activity {
/** Called when the activity is first created. */
ImageView iv = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView)findViewById(R.id.imageView1);
}
public void translate(View v){
AnimationSet as = new AnimationSet(true);
/**
* 参数:
* 1. X轴坐标类型
* 2. X轴开始位置
* 3. X轴坐标类型
* 4. X轴移动后坐标位置. 0.5f为X轴的一半
* 5. Y轴坐标类型
* 6. Y轴开始位置
* 7. Y轴坐标类型
* 8. Y轴移动后坐标位置. 1f为Y轴底部
*/
TranslateAnimation als = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f
);
als.setDuration(1000);
as.addAnimation(als);
iv.startAnimation(as);
}
public void alpha(View v){
AnimationSet as = new AnimationSet(true);
AlphaAnimation als = new AlphaAnimation(1, 0);
als.setDuration(1000);
//设置为true, 动画执行后, 控件停留在执行结束后的状态
als.setFillAfter(true);
//设置为true, 动画执行后, 控件返回执行前得状态
als.setFillBefore(false);
//设置1000ms, 表示1000ms后再执行动画
als.setStartOffset(1000);
//设置执行动画5次
als.setRepeatCount(5);
as.addAnimation(als);
iv.startAnimation(as);
}
public void rotate(View v){
AnimationSet as = new AnimationSet(true);
/**
* 参数:
* 1. 旋转开始角度
* 2. 旋转结束角度
* 3. X轴坐标类型. Animation.RELATIVE_TO_PARENT相对于父控件的坐标.
* Animation.RELATIVE_TO_SELF相对于自身的坐标
* 4. X轴的坐标比例, 1f表示X轴末端
* 5. Y轴坐标类型
* 6. Y轴坐标比例
*/
RotateAnimation ra = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
ra.setDuration(1000);
as.addAnimation(ra);
iv.startAnimation(as);
}
public void scale(View v){
AnimationSet as = new AnimationSet(true);
/**
* 参数:
* 1. X轴开始比例
* 2. X轴结束比例
* 3. Y轴开始比例
* 4. Y轴结束比例
* 5. X轴坐标类型. Animation.RELATIVE_TO_PARENT相对于父控件的坐标.
* Animation.RELATIVE_TO_SELF相对于自身的坐标
* 6. X轴缩小或者放大最后中心点
* 7. Y轴坐标类型. Animation.RELATIVE_TO_PARENT相对于父控件的坐标.
* Animation.RELATIVE_TO_SELF相对于自身的坐标
* 8. Y轴缩小或者放大最后中心点
*/
ScaleAnimation als = new ScaleAnimation(1, .1f, 1, .1f,
Animation.RELATIVE_TO_SELF,.5f,
Animation.RELATIVE_TO_SELF,.5f);
als.setDuration(1000);
as.addAnimation(als);
iv.startAnimation(as);
}
}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content" android:layout_weight="1">
<ImageView android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_width="wrap_content" android:src="@drawable/icon" ></ImageView>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_width="match_parent" android:orientation="vertical">
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/button1" android:text="@string/translate" android:onClick="translate"></Button>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/button3" android:text="@string/alpha" android:onClick="alpha"></Button>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/button2" android:text="@string/rotate" android:onClick="rotate"></Button>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/button4" android:text="@string/scale" android:onClick="scale"></Button>
</LinearLayout>
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值