Android 的四中動畫效果
初步講解android的淡進淡出、旋轉、移動、透明度的問題
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1" android:layout_alignLeft="@+id/button1" android:layout_marginBottom="20dp" android:text="Button" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button2" android:layout_alignLeft="@+id/button2" android:layout_marginBottom="16dp" android:text="Button" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button3" android:layout_alignRight="@+id/button3" android:layout_marginBottom="38dp" android:text="Button" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button4" android:layout_alignParentTop="true" android:layout_marginTop="26dp" android:src="@drawable/ic_launcher" /> </RelativeLayout>
這是布局文件
package com.example.move;
import android.os.Bundle;
import android.app.Activity;
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 Button alphaBut;//淡靜淡出
private ImageView imageView;
private Button rotateBut;//旋轉
private Button scaleBut;//縮小也可以放大
private Button tranBut;//移動
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alphaBut=(Button) findViewById(R.id.button1);
rotateBut=(Button) findViewById(R.id.button2);
scaleBut=(Button) findViewById(R.id.button3);
tranBut=(Button) findViewById(R.id.button4);
imageView=(ImageView) findViewById(R.id.imageView1);
alphaBut.setOnClickListener(new AlphaButoon());
rotateBut.setOnClickListener(new RotateButoon());
scaleBut.setOnClickListener(new ScaleButoon());
tranBut.setOnClickListener(new TranButoon());
}
class TranButoon implements OnClickListener{
@Override
public void onClick(View arg0) {
AnimationSet animationSet=new AnimationSet(true);
Animation animation=new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(2000);
animationSet.addAnimation(animation);
imageView.startAnimation(animationSet);
tranBut.startAnimation(animationSet);
}
}
class ScaleButoon implements OnClickListener{
@Override
public void onClick(View arg0) {
AnimationSet animationSet=new AnimationSet(true);
Animation animation=new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(2000);
animationSet.addAnimation(animation);
imageView.startAnimation(animationSet);
scaleBut.startAnimation(animationSet);
}
}
class AlphaButoon implements OnClickListener{
@Override
public void onClick(View arg0) {
AnimationSet animationSet=new AnimationSet(true);
Animation animation=new AlphaAnimation(0, 1);
animation.setDuration(2000);
animationSet.addAnimation(animation);
imageView.startAnimation(animationSet);
alphaBut.startAnimation(animationSet);
}
}
class RotateButoon implements OnClickListener{
@Override
public void onClick(View arg0) {
AnimationSet animationSet=new AnimationSet(true);
//0-360從那個角度轉到那個角度,X軸的參考值,0,5f百分之50,Y軸的參考值,0,5f百分之50
Animation rotate=new RotateAnimation(0, 360,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);
Animation alpha=new AlphaAnimation(0, 1);
rotate.setDuration(2000);
animationSet.addAnimation(rotate);
alpha.setDuration(100);
animationSet.addAnimation(alpha);
imageView.startAnimation(animationSet);
rotateBut.startAnimation(animationSet);
}
}
} 主函數
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!