ANDROID_MARS学习笔记_S02_007_Animation第一种使用方式:代码

时间:2024-07-18 23:35:14

一、简介

ANDROID_MARS学习笔记_S02_007_Animation第一种使用方式:代码

ANDROID_MARS学习笔记_S02_007_Animation第一种使用方式:代码

二、代码
1.xml
(1)activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"> <Button android:id="@+id/scaleButtonId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentBottom="true"
android:text="测试scale动画效果" /> <Button android:id="@+id/rotateButtonId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_above="@id/scaleButtonId"
android:text="测试rotate动画效果" /> <Button android:id="@+id/alphaButtonId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_above="@id/rotateButtonId"
android:text="测试alpha动画效果" /> <Button android:id="@+id/translateButtonId"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_above="@id/alphaButtonId" android:text="测试translate动画效果" /> <LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/imageViewId"
android:layout_width="wrap_content" android:layout_height="100dp"
android:layout_centerInParent="true" android:layout_marginTop="100dip"
android:src="@drawable/android" />
</LinearLayout>
</RelativeLayout>

2.java
(1)MainActivity.java

 package com.animation1;

 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; public class MainActivity extends Activity { private ImageView imageView = null;
private Button rotateButton, scaleButton, alphaButton, translateButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageViewId); rotateButton = (Button) findViewById(R.id.rotateButtonId);
scaleButton = (Button) findViewById(R.id.scaleButtonId);
alphaButton = (Button) findViewById(R.id.alphaButtonId);
translateButton = (Button) findViewById(R.id.translateButtonId); rotateButton.setOnClickListener(new RotateButtonListener());
scaleButton.setOnClickListener(new ScaleButtonListener());
alphaButton.setOnClickListener(new AlphaButtonListener());
translateButton.setOnClickListener(new TranslateButtonListener()); } private class RotateButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
AnimationSet.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
rotateAnimation.setDuration(5000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
}
} private class ScaleButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationSet.addAnimation(scaleAnimation);
animationSet.setStartOffset(1000);
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
animationSet.setDuration(2000);
imageView.startAnimation(animationSet);
}
} private class AlphaButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
imageView.startAnimation(animationSet);
}
} private class TranslateButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(translateAnimation);
}
}
}