安卓开发_浅谈Android动画(一)

时间:2023-03-09 18:40:54
安卓开发_浅谈Android动画(一)

动画效果,针对图片实现

现在学习四种基本的简单动画效果

一、Tween Animation共同属性

  1、Duration:动画持续时间(毫秒单位)
  2、fillAfter:设置为true,动画转化在动画结束后被应用
  3、fillBefore:设置为true,动画转化在动画开始前被应用
  4、interpolator:动画插入器(加速,减速插入器)
  5、repeatCount:动画重复次数
  6、repateMode:顺序重复/倒序重复
  7、startOffset:动画之间的时间间隔

二、Animation实现方式

  1、配置文件(/res/anim)--alpha,scale,translate,rotatae
  2、JAVA代码实现--AlphaAnimation,ScaleAnimation,TranslateAnimation,RotateAnimation

三、四种基本动画

 1、AlphaAnimation(透明度动画)
  (1)fromAlpha:动画起始时的透明度
  (2)toAlpha:动画终止时的透明度
  0.0表示完全透明 1.0表示完全不透明

 2、ScaleAnimation(缩放动画)
  (1)fromX,toX分别是起始和结束时x坐标上的伸缩尺寸
  (2)fromY,toY分别是起始和结束时y坐标上的伸缩尺寸
  (3)pivotX,pivotY分别为伸缩动画相对于x,y,坐标开始的位置

 3、TranslateAnimation(位移动画)
  (1)、fromXDelta,fromYDelta分别是起始时X,Y的坐标
  (2)、toXDelta,toYDelta分别是结束时X,Y的坐标

 4、RotateAnimation(旋转动画)
  (1)、fromDegrees 起始的角度
  (2)、toDegrees 终止的角度
  (3)、pivoteX,pivoteY分别为旋转动画相对于x,y的坐标的开始位置

四、示例

配置文件实现方法

 package other;

 import com.example.allcode.ImageTest;
import com.example.allcode.R; 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 Donghua extends Activity implements OnClickListener{
private Button toumingdu;
private Button suofang;
private Button weiyi;
private Button xuanzhuan; private ImageView donghua_image;
private Animation loadAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.donghua); toumingdu = (Button) findViewById(R.id.donghua_touming);
suofang = (Button) findViewById(R.id.donghua_suofang);
weiyi= (Button) findViewById(R.id.donghua_weiyi);
xuanzhuan= (Button) findViewById(R.id.donghua_xuanzhuan); donghua_image = (ImageView) findViewById(R.id.donghua_image);
toumingdu.setOnClickListener(this);
donghua_image.setOnClickListener(this);
suofang.setOnClickListener(this);
weiyi.setOnClickListener(this);
xuanzhuan.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.donghua_touming: //透明动画
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
donghua_image.startAnimation(loadAnimation);
break;
case R.id.donghua_suofang: //缩放动画
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.scale);
donghua_image.startAnimation(loadAnimation);
break;
case R.id.donghua_weiyi: //位移动画
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
donghua_image.startAnimation(loadAnimation);
break;
case R.id.donghua_xuanzhuan: //旋转动画
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);
donghua_image.startAnimation(loadAnimation);
break;
default:
break;
}
} }

布局文件:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/donghua_touming"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AlphaAnimation(透明度动画)" /> <Button
android:id="@+id/donghua_suofang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ScaleAnimation(缩放动画)" /> <Button
android:id="@+id/donghua_weiyi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TranslateAnimation(位移动画)" /> <Button
android:id="@+id/donghua_xuanzhuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RotateAnimation(旋转动画)" /> <ImageView
android:id="@+id/donghua_image"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:src="@drawable/icon_72" /> </LinearLayout>

配置文件:

目录。res-anim 自己新建

安卓开发_浅谈Android动画(一)

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha
android:duration=""
android:fromAlpha="0.1"//初始透明度10%
android:toAlpha="1.0" //结束透明度100%
>
</alpha> </set>

alpha.xml透明动画

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <scale
android:duration=""
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" /> </set>

scale.xml缩放动画

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate
android:duration=""
android:fromXDelta=""
android:fromYDelta=""
android:toXDelta=""
android:toYDelta="" /> </set>

translate.xml位移动画

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" > <rotate
android:duration=""
android:fromDegrees=""
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="+360" /> </set>

rotate.xml旋转动画

效果图:

安卓开发_浅谈Android动画(一)