Android --- Tween动画示例(XML定义的动画)

本文详细介绍了在Android应用中如何利用Tween动画实现图像的动态效果,包括透明度、缩放、移动和旋转等。通过创建自定义的Tween动画XML文件和布局文件,以及在Activity中加载并控制动画的播放与暂停。了解不同类型的Tween动画在实际应用中的具体实现和参数设置,为开发者提供了一种丰富UI交互体验的方法。

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

1、在res/anim目录下新建XML文件:tween_anim.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.2" android:toAlpha="1.0" android:duration="3000" android:repeatMode="reverse" android:repeatCount="10" /> <scale android:fromXScale="0.2" android:toXScale="1.0" android:fromYScale="0.2" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="3000" android:repeatMode="reverse" android:repeatCount="10" /> <translate android:fromXDelta="50" android:toXDelta="100" android:fromYDelta="50" android:toYDelta="100" android:duration="3000" android:repeatMode="restart" android:repeatCount="10" /> <rotate android:fromDegrees="0" android:toDegrees="360" android:duration="3000" android:repeatMode="restart" android:repeatCount="10" /> </set> 2、在res/layout目录下新建XML文件:tween_anim_layout.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imgTween" android:src="@drawable/c01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1.0" /> <Button android:id="@+id/btnControl" android:text="开始" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> 3、Activity里面添加代码:

package com.bison; 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.AnimationUtils; import android.widget.Button; import android.widget.ImageView; public class TweenAnimationDemo extends Activity { // 声明一个开始停止的标识符 private boolean flags = true; private ImageView imgTween; private Button btnCtrl; /** 初始化 */ public void init() { imgTween = (ImageView) findViewById(R.id.imgTween); // 声明Tween动画 final Animation anim = AnimationUtils.loadAnimation(this, R.anim.tween_anim); btnCtrl = (Button) findViewById(R.id.btnControl); btnCtrl.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (flags) { btnCtrl.setText("停止"); imgTween.startAnimation(anim); flags = false; } else { btnCtrl.setText("开始"); imgTween.clearAnimation(); flags = true; } } }); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tween_anim_layout); init(); } }
PS:Tween动画有alpha(透明)、scale(缩放)、translate(移动)、rotate(旋转),每个有不同的定义,仔细研究。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值