android tween动画平移怎么设置时间,android Tween Animation属性设置方法实例
在Android开发中,Animation是用来给控件制作效果的。大多数的控件都可以用这个类,这个类包含了4种基本动作,分别为移动,旋转,淡入淡出,缩放。在使用Animation时,可以在.java文件中用java代码对其进行设置,这样的优点是可以方便调试程序效果;另外一种方法就是在xml中对控件的属性做设置,好处是代码的重用性比较高,缺点是不方便调试。
一、在java代码中使用Animation在java代码中使用Animation主要分为下面4个步骤。
创建一个AnimationSet类,AnimationSet类是一个Animation集合,里面可以许多Animation,且在AnimationSet中设置的属性适用于里面的所有Animation。
根据我们需要的动态效果创建一个Animation类,主要有4个这样的类,分别为AlphaAnimation,ScaleAnimation,RotateAnimation,TranslateAnimation,分别对应着一种动画效果。
将上面建立好的Animation设置相应的动态属性,然后加入到AnimationSet中。
最后在需要适用这个动态的效果的控件中加载这个AnimationSet。
这里,做了一个简单的实验,分别试了下上面的动态效果。实验室对一个image图标进行动态演示,下面有4个按钮,每个按钮对应一个动态演示的效果。这4中效果分别是:image图标由完全透明到完全不透明变化,持续时间为1s;image图标由原来大小尺寸沿自身尺寸中心逐渐缩放到0,持续时间为1s;image图标以自身中心为圆心旋转360度,持续时间为1s;image图标从自身位置开始同时向右和向下移动了imageView控件的宽和高长度。
界面如下所示(动态效果就不一张一张截图演示了):
在设置Animation属性的时候,有一点需要注意的是,在进行尺度缩放,平移,旋转会涉及到中心点以及移动的距离那些参数,这些参数的值的设置是需要依据它值属性来的,如果值属性为Animation.RELATIVE_TO_SELF,那么就是参考控件自身的坐标,如果是Animation.RELATIVE_TO_PARENT,那么就是参考程序界面的坐标。
Java文件代码如下:
package com.example.anim_1;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
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.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView image = null;
private Button alpha = null;
private Button scale = null;
private Button rotate = null;
private Button translate = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView)findViewById(R.id.image);
alpha = (Button)findViewById(R.id.alpha);
scale = (Button)findViewById(R.id.scale);
rotate = (Button)findViewById(R.id.rotate);
translate = (Button)findViewById(R.id.translate);
alpha.setOnClickListener(new AlphaButtonListener());
scale.setOnClickListener(new ScaleButtonListener());
rotate.setOnClickListener(new RotateButtonListener());
translate.setOnClickListener(new TranslateButtonListener());
}
private class AlphaButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
//这里设置ture参数表示共享interpolator
AnimationSet alpha_animation_set = new AnimationSet(true);
//从完全不透明到完全透明
//这里的数字后面用f难道表示浮点型?
AlphaAnimation alpha_animation = new AlphaAnimation(1.0f, 0.0f);
alpha_animation_set.addAnimation(alpha_animation);
alpha_animation.setDuration(1000);//1s钟
image.startAnimation(alpha_animation_set);
}
}
private class ScaleButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
AnimationSet scale_animation_set = new AnimationSet(true);
//以自身为尺度缩放中心,从原大小尺寸逐渐缩放到0尺寸
ScaleAnimation scale_animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scale_animation_set.addAnimation(scale_animation);
scale_animation.setDuration(1000);//1s钟
image.startAnimation(scale_animation_set);
}
}
private class RotateButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
AnimationSet rotate_animation_set = new AnimationSet(true);
//以自身中心为圆心,旋转360度
RotateAnimation rotate_animation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotate_animation_set.addAnimation(rotate_animation);
rotate_animation.setDuration(1000);//1s钟
image.startAnimation(rotate_animation_set);
}
}
private class TranslateButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
AnimationSet translate_animation_set = new AnimationSet(true);
//以自身为坐标系和长度单位,从(0,0)移动到(1,1)
TranslateAnimation translate_animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f);
translate_animation_set.addAnimation(translate_animation);
translate_animation.setDuration(1000);//1s钟
image.startAnimation(translate_animation_set);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
本次试验的xml布局文件代码如下:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/image"
android:layout_centerHorizontal="true"
android:paddingTop="80px"
android:paddingBottom="80px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/girl"
/>
android:id="@+id/button_group"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:layout_below="@id/image"
>
android:id="@+id/alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"
android:text="@string/alpha"
android:ems="1"
/>
android:id="@+id/scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"
android:text="@string/scale"
android:ems="1"
/>
android:id="@+id/rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"
android:text="@string/rotate"
android:ems="1"
/>
android:id="@+id/translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"
android:text="@string/translate"
android:ems="1"
/>
二、在xml文件中使用animation在xml中使用animation,只需对xml文件中的动画属性进行设置,这样写好的一个xml文件可以被多次利用。
完成该功能步骤大概如下:
首先在res目录下新建anim目录,在anim目录里面建立xml文件,每个xml文件对应里面可以设置各种动画,可以将上面讲到的4种动画都放里面。这些xml文件的属性设置应该都在set标签下面。
在java语句中,只需要对需要动画显示的控件加载一个animation对象,该animation对象采用AnimationUtils.loadAnimation()方法来获得,该方法里面就有一个参数为anim下的一个xml,因此这个控件也就得到了相应xml里面设置的动画效果了。
这次试验的效果和第一种情况的一样,只是图片换了一张。
效果演示界面如下:
在用xml设置动态属性的时候,有些属性比如旋转中心,平移尺寸的设置,有如下规则:
如果android:pivotX=”N”,则表示绝对坐标比例,即屏幕的坐标比例。
如果android:pivotX=”N%”,则表示相对自身的坐标比例。
如果android:pivotX=”N%p”,则表示相对于父控件的坐标比例。
实验代码如下(附录有工程code下载链接):
MainActivity.java:
package com.example.anim_2;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView image = null;
private Button alpha = null;
private Button scale = null;
private Button rotate = null;
private Button translate = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView)findViewById(R.id.image);
alpha = (Button)findViewById(R.id.alpha);
scale = (Button)findViewById(R.id.scale);
rotate = (Button)findViewById(R.id.rotate);
translate = (Button)findViewById(R.id.translate);
alpha.setOnClickListener(new AlphaButtonListener());
scale.setOnClickListener(new ScaleButtonListener());
rotate.setOnClickListener(new RotateButtonListener());
translate.setOnClickListener(new TranslateButtonListener());
}
private class AlphaButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
image.startAnimation(animation);
}
}
private class ScaleButtonListener implements OnClickListener{
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
image.startAnimation(animation);
}
}
private class RotateButtonListener implements OnClickListener{
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
image.startAnimation(animation);
}
}
private class TranslateButtonListener implements OnClickListener{
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
image.startAnimation(animation);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
android:id="@+id/image_layout"
android:layout_width="fill_parent"
android:layout_height="250dip"
android:gravity="center"
>
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/olympic"
/>
android:id="@+id/button_group"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/image_layout"
>
android:id="@+id/alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:text="@string/alpha"
android:ems="1"
/>
android:id="@+id/scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:text="@string/scale"
android:ems="1"
/>
android:id="@+id/rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:text="@string/rotate"
android:ems="1"
/>
android:id="@+id/translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:text="@string/translate"
android:ems="1"
/>
anim/alpha.xml:
set
android:interpolator="@android:anim/accelerate_interpolator"
xmlns:android="http://schemas.android.com/apk/res/android">
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="1000"
/>
anim/scale.xml:
android:interpolator="@android:anim/accelerate_interpolator">
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
/>
anim/rotate.xml:
android:interpolator="@android:anim/accelerate_interpolator"
>
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
/>
anim/translate.xml:
android:interpolator="@android:anim/accelerate_interpolator"
>
android:fromXDelta="0%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="1000"
/>
总结:本次实验主要讲android中animation这个类对控件的动画效果设置,对用java代码和用xml代码2种方式都做了个简单的实验。懂得其使用方法的大体流程。
作者:tornadomeet
android tween动画平移怎么设置时间,android Tween Animation属性设置方法实例相关推荐
- Android之动画精讲一:从setTranslationX谈属性动画和view动画的区别
转载:http://blog.csdn.net/yanzi1225627/article/details/47850471 最近又用到了动画,决定把几次项目里用到的动画走过的弯路总结一下,顺便梳理下a ...
- 微信小程序css3动画怎么写,微信小程序动画课程-通过wxss(css)来实现-animation 属性...
animation 属性 微信小程序交流群:111733917 | 微信小程序从0基础到就业的课程:https://edu.csdn.net/topic/huangjuhua 定义和用法 animat ...
- android 左移动画_Android研究院之游戏开发Tween动画的实现(十九)
今天和大伙讨论一下Android开发中的Tween动画的实现.首先它和上一章我们讨论的Frame动画同属于系统提供的绘制动画的方法.Tween动画主要的功能是在绘制动画前设置动画绘制的轨迹,包括时间, ...
- android系统应用程序设置时间,Android应用程序设置系统时间的方法
Android应用程序获取系统时间的方法: System.currentTimeMillis(); Android SDK虽然提供了设置系统时间的方法SystemClock.setCurrentTim ...
- android过渡动画旋转,炫酷的Android过渡动画
[桃花潭水深千尺,不及汪伦送我情] 不知道大家有没有发现,Android版的掘金有下面这个小小动画:点击作者头像跳转到作者的详情页,而作者头像会从当前界面通过动画过渡详情页界面. image 知识贫乏 ...
- 广告牌定时器怎么设置时间_招牌定时器怎么设置时间?简单吗?
夜晚,会有很多闪烁的广告招牌,其实很多招牌都是有灯光控制的,合理设置好时间也是可以节约开支的.想要拥有发光招牌的商家,确实是有必要了解一下招牌定时器的设置,那么,广告招牌定时器怎么设置时间呢?步骤繁琐 ...
- 广告牌定时器怎么设置时间_定时开关如何设置时间呢
路灯定时开关怎么设置定时 内容如下:1.先按取消键5秒就可解锁设置2.如果有设置,先选择,然后选择校时,校分,这个是开:3.再次按设置,校时,校分,第一组就设置完成.以此类推下去就行.4.选择自动,或 ...
- php怎么设置段落之间的距离,css如何设置段落间距?margin 属性设置段落间距(代码实例)...
段落间距是网页设计里一个提升文章阅读体验的重要因素.但网页排版不想Word那样简单,而作为网页设计师,为了读者的良好阅读体验,我们必须为文章的每个小标题和段落都预留一定的空间,用css有效率地设置段落 ...
- echarts地图设置legend_echarts设置图例颜色和地图底色的方法实例
导读热词 前言 本来想写echarts初始化函数的,但最近因为要写一个地图与柱状图的混合方式,也就是每个省的地图上要有柱状图显示.于是仔细使用了一下地图. 1.地图的一些基本属性就不介绍了,还是那些s ...
最新文章
- [更新问题]无法在安装新的版本前,为“./boot/vmlinuz-2.6.24-19-generic”做一个符号链接备份...
- 防火墙 之 iptables 匹配条件讲解
- android 模拟飞行,安卓版模拟飞行 X Plane 9试玩
- 一文回顾阿里云弹性计算云栖大会精彩看点
- python字符串格式化_Python3 字符串格式化
- python 时间序列异常值_python中缺少时间序列值
- 记录通用权限管理系统组件使用心得体会,写技术博客赢IPad2
- DEEPNOVA 技术荟系列公开课回顾:释放海量数据价值,尽显数据智能之美
- java下linux和window通用的获取指定网段的本地ip地址(NetworkInterface)
- 大数据有哪些特点和作用
- 1*1的卷积核的作用
- 每日涉猎技术点存档(2018年6月)
- 在GitHub上面下载或者克隆项目时速度过慢的解决办法
- 怎么成为一名Java架构师 都需要掌握哪些技术
- excel教程自学网_企业Office办公软件自学视频教程word/excel/ppt 2003 2013 2016全套
- 《系统集成项目管理工程师》必背知识点
- 如何玩Chrome的小恐龙游戏
- php stripslashes和addslashes的区别
- 推荐两个适合程序员接国外私单的网站
- Python爬虫学习笔记