导航:首页 > 编程语言 > android代码添加动画

android代码添加动画

发布时间:2025-03-31 15:16:55

A. 如何实现Rotate旋转动画的android源代码

Android平台提供了丰富的动画效果,其中Rotate动画可以实现对象的旋转。Rotate动画通过设置起始角度和结束角度来控制旋转方向与旋转范围。例如,使用android:fromDegrees="+360"与android:toDegrees="0",可以实现从正方向逆时针旋转360度的效果。

在XML布局文件中,可以这样定义一个加速旋转的动画:
<rotate android:interpolator="@android:anim/accelerate_interpolator" android:fromDegrees="+360" android:toDegrees="0" android:pivotX="50%" android:pivotY="50%" android:ration="2000" />
其中,android:interpolator属性定义了动画的加速曲线,android:pivotX和android:pivotY属性设定了动画的旋转中心,android:ration属性则设定了动画的执行时间。

在Activity中实现动画效果,可以如下编写代码:
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 RotateActivity extends Activity {
/**向左旋转动画按钮**/
Button mButton0 = null;
/**向右旋转动画按钮**/
Button mButton1 = null;
/**显示动画的ImageView**/
ImageView mImageView = null;
/**向左旋转动画**/
Animation mLeftAnimation = null;
/**向右旋转动画**/
Animation mRightAnimation = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.retate);
/**拿到ImageView对象**/
mImageView = (ImageView)findViewById(R.id.imageView);
/**加载向左与向右旋转动画**/
mLeftAnimation = AnimationUtils.loadAnimation(this, R.anim.retateleft);
mRightAnimation = AnimationUtils.loadAnimation(this, R.anim.retateright);
mButton0 = (Button)findViewById(R.id.button0);
mButton0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/**播放向左旋转动画**/
mImageView.startAnimation(mLeftAnimation);
}
});
mButton1 = (Button)findViewById(R.id.button1);
mButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/**播放向右旋转动画**/
mImageView.startAnimation(mRightAnimation);
}
});
}
}

除了基本的旋转动画,Android还提供了其他多种动画类型,如Alpha、Scale、Translate等,它们分别对应着透明度、缩放和移动动画。通过组合使用这些动画,开发者可以实现丰富多彩的视觉效果。

了解更多Android源代码知识,可以参考官方文档和相关教程,或通过阅读开源项目源码进行学习。

阅读全文

与android代码添加动画相关的资料

热点内容
c图片写入数据库 浏览:466
c串口程序 浏览:500
中农金融注册app有哪些 浏览:629
回收站文件清理不在电脑上有痕迹 浏览:886
dx版本更新 浏览:738
主机集群教程 浏览:939
苹果6英版好不好 浏览:959
nodejs抓取网站音频 浏览:772
app上下载的软件在哪里 浏览:36
起凡保存的照片在哪个文件夹 浏览:354
数学建模如何把模型编程 浏览:176
ug找不到指定的许可文件 浏览:850
数控编程g01表示什么 浏览:700
java实用类 浏览:190
去年做哪个网站能致富 浏览:727
多少的cad版本能打开pdf格式文件 浏览:540
win10文件比率是什么 浏览:652
msdb数据库置疑 浏览:210
移动花卡免流app为什么要10元 浏览:147
xamppphp配置文件 浏览:268

友情链接