ANDROID_MARS学习笔记_S02_007_Animation第一种使用方式:代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ANDROID_MARS学习笔记_S02_007_Animation第一种使用方式:代码相关的知识,希望对你有一定的参考价值。
一、简介
二、代码
1.xml
(1)activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="fill_parent" 4 android:layout_height="fill_parent"> 5 6 <Button android:id="@+id/scaleButtonId" android:layout_width="fill_parent" 7 android:layout_height="wrap_content" android:layout_alignParentBottom="true" 8 android:text="测试scale动画效果" /> 9 10 <Button android:id="@+id/rotateButtonId" android:layout_width="fill_parent" 11 android:layout_height="wrap_content" android:layout_above="@id/scaleButtonId" 12 android:text="测试rotate动画效果" /> 13 14 <Button android:id="@+id/alphaButtonId" android:layout_width="fill_parent" 15 android:layout_height="wrap_content" android:layout_above="@id/rotateButtonId" 16 android:text="测试alpha动画效果" /> 17 18 <Button android:id="@+id/translateButtonId" 19 android:layout_width="fill_parent" android:layout_height="wrap_content" 20 android:layout_above="@id/alphaButtonId" android:text="测试translate动画效果" /> 21 22 <LinearLayout android:orientation="vertical" 23 android:layout_width="fill_parent" android:layout_height="fill_parent"> 24 25 <ImageView android:id="@+id/imageViewId" 26 android:layout_width="wrap_content" android:layout_height="100dp" 27 android:layout_centerInParent="true" android:layout_marginTop="100dip" 28 android:src="@drawable/android" /> 29 </LinearLayout> 30 </RelativeLayout>
2.java
(1)MainActivity.java
1 package com.animation1; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.view.animation.AlphaAnimation; 8 import android.view.animation.Animation; 9 import android.view.animation.AnimationSet; 10 import android.view.animation.RotateAnimation; 11 import android.view.animation.ScaleAnimation; 12 import android.view.animation.TranslateAnimation; 13 import android.widget.Button; 14 import android.widget.ImageView; 15 16 public class MainActivity extends Activity { 17 18 private ImageView imageView = null; 19 private Button rotateButton, scaleButton, alphaButton, translateButton = null; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 25 imageView = (ImageView) findViewById(R.id.imageViewId); 26 27 rotateButton = (Button) findViewById(R.id.rotateButtonId); 28 scaleButton = (Button) findViewById(R.id.scaleButtonId); 29 alphaButton = (Button) findViewById(R.id.alphaButtonId); 30 translateButton = (Button) findViewById(R.id.translateButtonId); 31 32 rotateButton.setOnClickListener(new RotateButtonListener()); 33 scaleButton.setOnClickListener(new ScaleButtonListener()); 34 alphaButton.setOnClickListener(new AlphaButtonListener()); 35 translateButton.setOnClickListener(new TranslateButtonListener()); 36 37 } 38 39 private class RotateButtonListener implements OnClickListener { 40 @Override 41 public void onClick(View v) { 42 AnimationSet animationSet = new AnimationSet(true); 43 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, 44 AnimationSet.RELATIVE_TO_PARENT, 1f, 45 Animation.RELATIVE_TO_PARENT, 0f); 46 rotateAnimation.setDuration(5000); 47 animationSet.addAnimation(rotateAnimation); 48 imageView.startAnimation(animationSet); 49 } 50 } 51 52 private class ScaleButtonListener implements OnClickListener { 53 @Override 54 public void onClick(View v) { 55 AnimationSet animationSet = new AnimationSet(true); 56 ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f, 57 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 58 animationSet.addAnimation(scaleAnimation); 59 animationSet.setStartOffset(1000); 60 animationSet.setFillAfter(true); 61 animationSet.setFillBefore(false); 62 animationSet.setDuration(2000); 63 imageView.startAnimation(animationSet); 64 } 65 } 66 67 private class AlphaButtonListener implements OnClickListener { 68 @Override 69 public void onClick(View v) { 70 AnimationSet animationSet = new AnimationSet(true); 71 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); 72 alphaAnimation.setDuration(1000); 73 animationSet.addAnimation(alphaAnimation); 74 imageView.startAnimation(animationSet); 75 } 76 } 77 78 private class TranslateButtonListener implements OnClickListener { 79 @Override 80 public void onClick(View v) { 81 AnimationSet animationSet = new AnimationSet(true); 82 TranslateAnimation translateAnimation = new TranslateAnimation( 83 Animation.RELATIVE_TO_SELF, 0f, 84 Animation.RELATIVE_TO_SELF, 0.5f, 85 Animation.RELATIVE_TO_SELF, 0f, 86 Animation.RELATIVE_TO_SELF, 1.0f); 87 translateAnimation.setDuration(1000); 88 animationSet.addAnimation(translateAnimation); 89 imageView.startAnimation(translateAnimation); 90 } 91 } 92 }
以上是关于ANDROID_MARS学习笔记_S02_007_Animation第一种使用方式:代码的主要内容,如果未能解决你的问题,请参考以下文章
ANDROID_MARS学习笔记_S01原始版_007_Handler及线程的简单使用
ANDROID_MARS学习笔记_S04_007_从服务器获取微博数据时间线
ANDROID_MARS学习笔记_S01_007Linear_layout嵌套与layout_weight的设置
ANDROID_MARS学习笔记_S02_003_AutoCompleteTextView