天天看點

Animation 參數詳解

Animations分兩類:

第一類:漸變的(Tweened): 淡入淡出(Alpha),旋轉(Rotate),移動(Translate),縮放(Scale);

第二類:Frame-by-Frame: 就如電影一般由多張圖檔按照一定的時間間隔顯示。

使用Tweened Animations的Java代碼使用步驟(當然你也可以不用AnimationSet):

1. 建立一個AnimationSet對象

2. 根據需要建立相應的Animation對象(AlphaAnimation、RotateAnimation、ScaleAnimation、TranslateAnimation)

3. 設定Animation對象相應的資料(duration, startoffset......)

4. 使用addAnimation方法将Animation對象添加到AnimationSet對象當中

5. 使用控件對象開始執行AnimationSet 

l view.startAnimation(animation);

l view.setAnimation(animation);

view.startNow();

取消動作:

l animation.cancel(); //動作本身取消

l animationset.cancel(); //動作集取消

l (View控件)img.clearAnimation(); //控件取消附在其上的動作

AnimationSet

用于控制View對象進行多個動作的組合,該類繼承于Animation類

l AnimationSet animationSet = new AnimationSet(true);

l animationSet.addAnimation(rotateAnimation);

l animationSet.cancel();

e.g.建立一個移動的動作

Animation 參數詳解

l //設定動畫執行事件(機關:毫秒)

l setDuration(long durationMills);

l //如果fillAfter的值為true,則動畫執行後,控件将停留在執行結果的狀态

l setFillAfter(boolean fillAfter);

l //如果fillBefore的值為true,則動畫執行後,控件将回到動畫執行之前的狀态

l setFillBefore(boolen fillBefore);

l //設定動畫執行之前的等待時間

l setStartOffSet(long startOffSet);

l //設定動畫再重複執行的次數 注意repeatcount(x)共執行x+1次

l setRepeatCount(int repeatCount);

l //設定動作重複的模式 repeatMode為Animation.REVERSE或Animation.RESTART

l setRepeatMode(int repeatMode);

AlphaAnimation

public AlphaAnimation (float fromAlpha, float toAlpha)

起始透明度和終止透明度,1為不透明,0為透明。

RotateAnimation

l RotateAnimation(float fromDegrees, float toDegrees) 

l RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) 

l RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 

參數說明: 

float fromDegrees:旋轉的開始角度。 0f

float toDegrees:旋轉的結束角度。    360f

這2個參數确定從什麼角度旋轉到什麼角度

int pivotXType:X軸的伸縮模式,可以取值為ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 

float pivotXValue:旋轉中心X坐标的伸縮值。 

int pivotYType:Y軸的伸縮模式,可以取值為ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 

float pivotYValue:旋轉中心Y坐标的伸縮值。 

這4個參數确定旋轉的中心點,即以哪個點為軸進行旋轉。

Animation.ABSOLUTE:具體的坐标值,指絕對的螢幕像素機關

Animation.RELATIVE_TO_SELF:相對自己的坐标值,0.1f是指自己的坐标值乘以0.1

Animation.RELATIVE_TO_PARENT:相對父容器的坐标值,0.1f是指父容器的坐标值乘以0.1

TranslateAnimation

l TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

l TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

參數說明: 

float fromXDelta:這個參數表示動畫開始的點離目前View X坐标上的內插補點;

float toXDelta, 這個參數表示動畫結束的點離目前View X坐标上的內插補點;

float fromYDelta, 這個參數表示動畫開始的點離目前View Y坐标上的內插補點;

float toYDelta, 這個參數表示動畫開始的點離目前View Y坐标上的內插補點;

這4個參數确定移動的起點和終點

fromXType:x軸(Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT);

fromXValue:第二個參數是第一個參數類型的起始值;

toXType,toXValue:第三個參數與第四個參數是x軸方向的終點參照與對應值;

這8個參數也是确定移動的起點和終點

ScaleAnimation

l ScaleAnimation(float fromX, float toX, float fromY, float toY)

l ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)

l ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

參數說明:

float fromX 動畫起始時 X坐标上的伸縮尺寸 

float toX 動畫結束時 X坐标上的伸縮尺寸 

float fromY 動畫起始時Y坐标上的伸縮尺寸 

float toY 動畫結束時Y坐标上的伸縮尺寸 

這4個參數确定從什麼大小縮放到什麼大小

int pivotXType 動畫在X軸相對于物件位置類型 

float pivotXValue 動畫相對于物件的X坐标的開始位置 

int pivotYType 動畫在Y軸相對于物件位置類型 

float pivotYValue 動畫相對于物件的Y坐标的開始位置 

這4個參數确定開始縮放的坐标,最後坐标是原來的坐标

疑問:

明明已經在每個動作開始之前取消了動作,但是……

若在漸變過程中點選其他動作,如旋轉,則圖檔還是會以漸變過程中保持的透明度進行其他動作,而後正常。

但若在選擇過程中點選其他動作,如移動,那圖檔的方向還是正常的。

也就是說,取消動作并不能改變目前圖檔的透明度,但是方向卻恢複了正常。

代碼如下:

<?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">



    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/n"
            android:id="@+id/img"
            android:layout_gravity="center"></ImageView>

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="漸變"
            android:id="@+id/button1"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"/>

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋轉"
            android:id="@+id/button2"
            android:layout_alignRight="@+id/button1"
            android:layout_below="@+id/button1"/>

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="移動"
            android:id="@+id/button3"
            android:layout_alignRight="@+id/button2"
            android:layout_below="@+id/button2"/>

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="縮放"
            android:id="@+id/button4"
            android:layout_alignRight="@+id/button3"
            android:layout_below="@+id/button3"/>

</RelativeLayout>
           
package com.example.Animations;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.*;
import android.widget.Button;
import android.widget.ImageView;

public class MyActivity extends Activity
{
    private Button button1, button2, button3, button4;
    private ImageView img;

    /*
    * 若在漸變過程中點選其他動作,如旋轉,則圖檔還是會以漸變過程中保持的透明度進行其他動作,而後正常
    */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
        Alpha();
        Rotate();
        Translate();
        Scale();
    }

    public void init()
    {
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        img = (ImageView) findViewById(R.id.img);
    }

    public void Alpha()
    {
        //public AlphaAnimation (float fromAlpha, float toAlpha)
        //起始透明度和終止透明度,1為不透明,0為透明。
        final AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        alphaAnimation.setDuration(1000);
        alphaAnimation.setRepeatCount(1);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setStartOffset(10);
        button1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                //若不清除,則每按一次動作疊加。
                img.clearAnimation();
                img.startAnimation(alphaAnimation);
            }
        });
    }

    public void Rotate()
    {
        final RotateAnimation rotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 1.5f,
                Animation.RELATIVE_TO_SELF, 1.5f);
        rotateAnimation.setDuration(3000);
        button2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                img.clearAnimation();
                img.startAnimation(rotateAnimation);
            }
        });
    }

    public void Translate()
    {
        final TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 2f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 2f);
        translateAnimation.setDuration(3000);
        translateAnimation.setRepeatMode(Animation.REVERSE);
        translateAnimation.setRepeatCount(3);
        button3.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                img.clearAnimation();
                img.startAnimation(translateAnimation);
            }
        });
    }

    public void Scale()
    {
        final ScaleAnimation scaleAnimation = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 1.5f,
                Animation.RELATIVE_TO_SELF, 1.5f);
        scaleAnimation.setDuration(3000);
        scaleAnimation.setRepeatMode(Animation.RESTART);
        scaleAnimation.setRepeatCount(2);
        button4.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                img.clearAnimation();
                img.startAnimation(scaleAnimation);
            }
        });
    }
}
           

效果圖:

漸變

Animation 參數詳解

旋轉

Animation 參數詳解

移動

Animation 參數詳解

縮放

Animation 參數詳解

ps.這篇本來是在wps上寫好的,若直接複制過來格式全沒了,幸好找到了一個方法,就是将wps另存為html,然後将html的源代碼複制到這裡的html編輯框,很實用。以後就不用每次都用這個垃圾編輯框了。不過在wps上有些格式處理的不統一,是以有些地方格式亂了。

Nice To Meet You! 請多多指教哈~