xml代码块:
<?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" >
<ImageView
android:id="@+id/img_animations"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/img1"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ff00ff"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
>
<Button
android:id="@+id/but1"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"
android:textColor="#ff00ff"/>
<Button
android:id="@+id/but2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="位移"
android:textColor="#ff00ff"/>
<Button
android:id="@+id/but3"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="渐变"
android:textColor="#ff00ff"/>
<Button
android:id="@+id/but4"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="缩放"
android:textColor="#ff00ff"/>
</LinearLayout>
</LinearLayout>
JAVA代码块:
package com.sumzom.animations;
import com.example.com.sumzom.teach.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
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;
import android.widget.Toast;
/**
* @author sumzom
* QQ2356066132
* tween动画基本使用方法
* */
public class AnimationsTest extends Activity implements OnClickListener{
private Button but1,but2,but3,but4 = null;
private ImageView img_animations = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animations);
initWithView();
initWithLogic();
}
private void initWithLogic() {
// TODO Auto-generated method stub
}
private void initWithView() {
// TODO Auto-generated method stub
but1 = (Button) findViewById(R.id.but1);
but2 = (Button) findViewById(R.id.but2);
but3= (Button) findViewById(R.id.but3);
but4 = (Button) findViewById(R.id.but4);
img_animations = (ImageView) findViewById(R.id.img_animations);
but1.setOnClickListener(this);
but2.setOnClickListener(this);
but3.setOnClickListener(this);
but4.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.but1:
but1();
Toast.makeText(getApplicationContext(), "旋转",
Toast.LENGTH_SHORT).show();
break;
case R.id.but2:
but2();
Toast.makeText(getApplicationContext(), "位移",
Toast.LENGTH_SHORT).show();
break;
case R.id.but3:
but3();
Toast.makeText(getApplicationContext(), "渐变",
Toast.LENGTH_SHORT).show();
break;
case R.id.but4:
but4();
Toast.makeText(getApplicationContext(), "缩放",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
//缩放
private void but4() {
// fromX, toX, fromY, toY, pivotXType,
//pivotXValue, pivotYType, pivotYValue)
AnimationSet set = new AnimationSet(true);
ScaleAnimation animation = new ScaleAnimation(
0, 0.1f, 0, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(3000);
set.addAnimation(animation);
img_animations.startAnimation(set);
}
/**
* @author sumzom
* QQ2356066132
* tween动画基本使用方法
* 渐变
* */
private void but3() {
//fromAlpha, toAlpha起始背景透明度,结束透明度
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0.2f);
alphaAnimation.setDuration(5000);
animationSet.addAnimation(alphaAnimation);
img_animations.startAnimation(animationSet);
}
//位移
private void but2() {
// TODO Auto-generated method stub
/**
* @author sumzom
* QQ2356066132
* tween动画基本使用方法
* 1~2参数 fromXType fromXValue:起始位置X类型与值
* 3~4参数 toXType toXValue:结束位置X类型与值
* 5~6参数 fromYType fromYValue:起始位置Y类型与值
* 7~8参数 toYType toYValue:结束位置Y类型与值
* */
AnimationSet set = new AnimationSet(true);
TranslateAnimation TSAM = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0.5f);
TSAM.setDuration(3000);
set.addAnimation(TSAM);
img_animations.startAnimation(set);
}
//旋转
private void but1() {
// TODO Auto-generated method stub
AnimationSet set = new AnimationSet(true);
/**
* @author sumzom
* QQ2356066132
* tween动画基本使用方法
* RotateAnimation:旋转动画
* 参数一:fromDegrees,起始角度
* 参数二:toDegrees,结束角度
* 后四参数确定圆心是哪个
* 参数三:pivotXType,X坐标类型
* 参数四:pivotXValue,X值
* 参数五:pivotYType,Y类型
* 参数六:pivotYValue,Y值
* */
RotateAnimation animation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(3000);
set.addAnimation(animation);
img_animations.startAnimation(set);
}
}
运行效果: