kowala's home

kowala's home
這裡是我的學習筆記,陸續增加中。
http://kowala21.blogspot.com

2011-07-03

Android Tween Animation test

kowala's home
http://kowala21.blogspot.com

look at my video first.


my test code under list


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;

    private ImageView imageView;
    private Button alphaBtn,rotateBtn,scaleBtn,translateBtn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        imageView = (ImageView) findViewById(R.id.imageView1);     
        alphaBtn = (Button) findViewById(R.id.button1);
        alphaBtn.setOnClickListener(btnClickListen);
        rotateBtn = (Button) findViewById(R.id.button2);
        rotateBtn.setOnClickListener(btnClickListen);
        scaleBtn = (Button) findViewById(R.id.button3);
        scaleBtn.setOnClickListener(btnClickListen);
        translateBtn = (Button) findViewById(R.id.button4);
        translateBtn.setOnClickListener(btnClickListen);
    }
    private OnClickListener btnClickListen = new OnClickListener(){
        @Override
        public void onClick(View v) {
            switch(v.getId()){
              case R.id.button1://alpha
                  doAlpha(1,0);
                  break;
              case R.id.button2://rotate
                  doRotate(0,720);             
                  break;
              case R.id.button3://scale
                 doScale(0f,0f,18f,18f);                
                  break;
              case R.id.button4://translate                 
                  doGoto(-2.4f,-2.6f,2.7f,3.8f);
                  break;
            }
        }
    };
    //淡入淡出
    private void doAlpha(float alpha1,float alpha2){      
        Animation alphaAnimation = new AlphaAnimation(alpha1,alpha2);
        alphaAnimation.setDuration(1500);      
        imageView.startAnimation(alphaAnimation);
    }
    //逆時針轉,順時針轉
    private void doRotate(float deg1,float deg2){          
        Animation rotateAnimation = new RotateAnimation(deg1,deg2,
        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        rotateAnimation.setDuration(2000);      
        imageView.startAnimation(rotateAnimation);      
    }
    //放大縮小
    private void doScale(float x1,float y1,float x2,float y2){      
        Animation scaleAnimation = new ScaleAnimation(x1,x2,y1,y2,
        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);          
        scaleAnimation.setFillAfter(false);
        scaleAnimation.setDuration(4000);
        imageView.startAnimation(scaleAnimation);
    }
    //移動
    private void doGoto(float x1,float y1,float x2,float y2){      
        Animation tAnimation = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, x1, Animation.RELATIVE_TO_SELF, x2,
        Animation.RELATIVE_TO_SELF, y1, Animation.RELATIVE_TO_SELF, y2);          
        tAnimation.setFillAfter(false);
        tAnimation.setDuration(4000);      
        imageView.startAnimation(tAnimation);
    }
}


layout file main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="horizontal" android:layout_height="wrap_content" android:paddingTop="20sp">
   
<TextView 
    android:layout_height="wrap_content" android:textColor="#ff8888ff"
    android:text="@string/hello" android:layout_width="fill_parent" android:gravity="center_horizontal" android:textSize="20sp" android:typeface="sans" android:textStyle="bold"/>
<ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:src="@drawable/dedo"></ImageView>
    <LinearLayout
    android:id="@+id/relativeLayout1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:paddingBottom="20sp">
    <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:text="透明"></Button>
    <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋轉"></Button>
    <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="比例"></Button>
    <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="移動"></Button>
</LinearLayout>
</RelativeLayout>

Reference:
Android 动画框架详解
http://www.ibm.com/developerworks/cn/opensource/os-cn-android-anmt1/index.html

Android画图学习总结(四)
http://www.moandroid.com/?p=790
http://www.moandroid.com/?p=808

2 則留言:

  1. 請問要怎麼讓ImageView連續移動呢?
    例如先從A移動到B,再從B移動到C....以此類推

    回覆刪除
    回覆
    1. 我想您需要的是 SurfaceView,它可以做到許多動畫的功能,很多 Game 是繼承 SurfaceView 類別,這有個說明可以參考看看。
      http://milochen.wordpress.com/2010/07/23/difference_of_surfaceview_and_view/

      刪除

請提供您的寶貴意見 ;-)