Android 基础动画效果
来源:互联网 发布:商场数据采集 编辑:程序博客网 时间:2024/05/21 18:18
Android 的基础动画效果有透明度动画,旋转动画,比例动画,位移动画,自定义动画
在下面会各给出一个示例:
新建一个Activity:TweenAnimationActivity.java
activity_tween_animation.xml文件内布局如下:
<LinearLayout 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" tools:context=".TweenAnimationActivity" android:orientation="vertical" > <ImageView android:id="@+id/animation_image" android:layout_width="150dp" android:layout_height="150dp" android:layout_marginTop="50dp" android:src="@drawable/appico" android:layout_gravity="center_horizontal"/> <Button android:id="@+id/alpha_animation" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:layout_gravity="center_horizontal" android:text="透明度动画"/> <Button android:id="@+id/rotate_animation" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="旋转动画"/> <Button android:id="@+id/scale_animation" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="比例动画"/> <Button android:id="@+id/translate_animation" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="位移动画"/> <Button android:id="@+id/xml_animation" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="XML自定义动画"/></LinearLayout>
实际效果如下:
Activity 内代码如下:
public class TweenAnimationActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_tween_animation);final ImageView iv=(ImageView) findViewById(R.id.animation_image);final Button btn=(Button) findViewById(R.id.alpha_animation);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//alpha透明度变化动画//第一个参数fromAlpha为动画开始时的透明度//第二个参数toAlpha为动画结束时的透明度//0.0表示完全透明;1.0表示完全不透明Animation alphaAnimation=new AlphaAnimation(1.0f, 0.1f);//设置动画时间alphaAnimation.setDuration(1000);//停留在最后一帧,避免恢复原状,仅为测试,如在工程中,应写监听事件alphaAnimation.setFillAfter(true);alphaAnimation.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {//iv.setVisibility(View.GONE);//iv.setVisibility(View.INVISIBLE);}});iv.startAnimation(alphaAnimation);}});Button rotate_btn=(Button) findViewById(R.id.rotate_animation);rotate_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//旋转动画//第一个参数fromDegrees表示动画起始时的角度,第二个参数toDegrees表示动画结束时的角度//默认以左上角为原点//Animation rotateAnimation=new RotateAnimation(0f, 360f);Animation rotateAnimation=new RotateAnimation(-90f, 90f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);rotateAnimation.setRepeatCount(10);rotateAnimation.setRepeatMode(Animation.REVERSE);rotateAnimation.setDuration(500);iv.startAnimation(rotateAnimation);}});Button scale_btn=(Button) findViewById(R.id.scale_animation);scale_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//比例动画//第一个参数fromXDelta,第二个参数toXDelta:分别是动画的起始、结束时的X坐标//第三个参数fromYDelta,第四个参数toYDelta:分别是动画的起始、结束时的Y坐标//Animation scaleAnimation=new ScaleAnimation(0.1f,1.0f,0.1f,1.0f);//第五个参数pivotXType为动画在X轴相对于物件位置类型//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置//第七个参数pivotYType为动画在Y轴相对于物件位置类型//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置Animation scaleAnimation=new ScaleAnimation(1.0f,2.0f,1.0f,2.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);scaleAnimation.setRepeatCount(10);scaleAnimation.setRepeatMode(Animation.RESTART);//设置动画时间scaleAnimation.setDuration(60);iv.startAnimation(scaleAnimation);}});Button translate_btn=(Button) findViewById(R.id.translate_animation);translate_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//位移动画//第一个参数fromXDelta,第二个参数toXDelta:分别是动画起始结束时X坐标//第三个参数fromYDelta,第四个参数toYDelta:分别是动画起始结束时Y坐标Animation translateAnimation=new TranslateAnimation(0.1f, 0.1f,0.1f, 150.f);translateAnimation.setRepeatCount(105);translateAnimation.setRepeatMode(Animation.REVERSE);translateAnimation.setDuration(500);iv.startAnimation(translateAnimation);}});Button xml_btn=(Button) findViewById(R.id.xml_animation);xml_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Animation myAnimation=AnimationUtils.loadAnimation(TweenAnimationActivity.this, R.anim.complex_animation);iv.startAnimation(myAnimation);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.tween_animation, menu);return true;}}
自定义动画需要在res文件夹内新建一个anim文件夹,之后在anim文件夹内新建xml文件complex_animation.xml
complex_animation.xml内容如下:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="1000"/> <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="0" android:toYDelta="150"/></set>
我设置的这几个动画效果还是很有意思的,大家也可以玩一玩!
0 0
- android基础--动画效果
- Android 基础动画效果
- android 基础动画效果
- android Animation动画效果基础
- android anim 动画效果编程基础
- Android 动画效果编程基础--Animation
- 动画效果 编程基础--Animation Android
- android基础--overridePendingTransition动画效果无效解决
- android动画效果的基础设置
- Android之动画效果编程基础
- Android 动画效果(二):四种基础动画的 **动态设置、动画监听、组合动画
- 动画效果编程基础
- 动画效果编程基础:
- 动画效果编程基础
- 基础动画效果
- android基础笔记——android动画效果:淡入淡出
- Android动画效果translate、scale、alpha、rotate详解(基础篇)
- 基础篇:android动画效果之scale使用
- Java Socket实战之四 传输压缩对象
- WinForm下禁止TextBox右键菜单
- 微信页面打开显示的样式与手机浏览器打开显示的样式不一样
- Objective-C学习 继承之僵尸练习
- scp命令的使用
- Android 基础动画效果
- ASP.NET 实现伪静态网页方法
- fragment的历程
- 颤抖的唇
- (keil MDK)ucos浮点数支持不正常的解决方案
- Android中Intent结合Bundle传递数据
- hadoop学习--hadoop2.3.0安装过程记录
- error while loading shared libraries: xxx.so.x"错误的原因和解决办法
- 黑夜里不敢点灯