ANDROID_MARS學習筆記_S02_007_Animation第一種使用方式:代碼

來源:互聯網
上載者:User

標籤:

一、簡介

 

 

二、代碼
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     @Override21     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         @Override41         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         @Override54         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         @Override69         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         @Override80         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第一種使用方式:代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.