Android電視關機動畫

來源:互聯網
上載者:User

MainActivity
Java代碼 
package org.wp.activity; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
 
public class MainActivity extends Activity { 
    private ImageView myImageView = null; 
    private Button myButton = null; 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        myImageView = (ImageView) this.findViewById(R.id.myImageView); 
        myButton = (Button) this.findViewById(R.id.myButton); 
        myButton.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                myImageView.startAnimation(new TVOffAnimation()); 
            } 
        }); 
    } 

 
TVOffAnimation
Java代碼 
package org.wp.activity; 
 
import android.graphics.Matrix; 
import android.view.animation.AccelerateDecelerateInterpolator; 
import android.view.animation.Animation; 
import android.view.animation.Transformation; 
 
public class TVOffAnimation extends Animation { 
    /** 中心點X座標 **/ 
    private int centerX = 0; 
    /** 中心點Y座標 **/ 
    private int centerY = 0; 
 
    @Override 
    public void initialize(int width, int height, int parentWidth, 
            int parentHeight) { 
        // void setDuration (long durationMillis) 
        // Since: API Level 1 How long this animation should last. 
        // The duration cannot be negative. 
        setDuration(500); 
 
        // void setFillAfter(boolean fillAfter) 
        // If fillAfter is true, the transformation that this animation 
        // performed will persist when it is finished. 
        setFillAfter(true); 
 
        // 獲得圖片中心點X座標 
        centerX = width / 2; 
        // 獲得圖片中心點Y座標 
        centerY = height / 2; 
 
        // void setInterpolator (Interpolator i) 
        // Since: API Level 1 Sets the acceleration curve for this animation. 
        // Defaults to a linear interpolation. 
        // setInterpolator(new AccelerateDecelerateInterpolator()) 
        // 選擇一個速度的效果 
        // AccelerateDecelerateInterpolator 
        // An interpolator where the rate of change starts and ends slowly 
        // but accelerates through the middle. 
        setInterpolator(new AccelerateDecelerateInterpolator()); 
    } 
 
    /**
     * preScale(float sx, float sy, float px, float py) 
     * px 和  py 是固定點,
     * 例如 px,py=0,0 的話,
     * 映像會以左上方為基點,向右向下放大縮小。
     * 
     * 如果是圖中心的話,映像便會以圖中心為基點,
     * 向上下左右等比例的放大縮小。
     * 
     * 一般情況下,如果映像的內部座標不重要的話,
     * 只用preScale(sy, sy)就可以了。
     * 要用到px,py的情況,通常是前後還有牽涉Animation的運作。
     * 
     * 簡單講,放大比例不會改變,都是按sx和sy決定。
     * 只是px,py那點,在放大前和放大後都會不變。
     * 
     * 例如: 一個(width)20 (height)10的長方形,
     * 左上方座標是(0,0)。那右下角是(20,10)。
     * 如果sx,sy=2,2  即放大兩倍。
     * 當px ,py=0,0放大後,左上方仍然是(0,0),但右下角會變成(40,20)。
     * 
     * 但同樣是sx,sy=2,2,但px,py=10,5的話,
     * 放大後,左上方會是(-10,-5)而右下角會是(30,15)。
     * 唯一座標不變的就只有10,5 那點。但長方形仍然會放大兩倍。
     * 
     * 看上去沒有什麼不同,但如果用上Animation的話,
     * 因為Animation對座標是有要求,所以效果也會有不同。
     * 
     * 
     * interpolatedTime 表示的是當前動畫的間隔時間 範圍是0-1
     * 
     * 那麼橫向來講前80%的時間我們要橫向展開到150%,
     * 變化的速率為 0.5 / 0.8 = 0.625 
     * 所以橫向縮放值為 1 + 0.625f * interpolatedTime
     * 
     * 縱向在前80%的時間是直接減小,最後只留一條高度為0.01f的線。 
     * 變化的速率為 1 / 0.8 = 1.25 
     * 所以縱向縮放值為 1 - 1.25f * interpolatedTime + 0.01f
     * 當然也可以寫成 1 - interpolatedTime / 0.8f + 0.01f
     * 
     * 後20%的時間裡我們要橫向從150%壓縮至0%, 
     * 變化的速率為 1.5 / 0.2 = 7.5 
     * 所以橫向縮放值為 7.5f * (1 - interpolatedTime)
     * 
     * 縱向保持不變就好了,當橫向為0的時候就全部消失了。
     */ 
    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
        final Matrix matrix = t.getMatrix(); 
        if (interpolatedTime < 0.8) { 
            matrix.preScale(1 + 0.625f * interpolatedTime, 
                    1 - 1.25f * interpolatedTime + 0.01f, centerX, centerY); 
        } else { 
            matrix.preScale(7.5f * (1 - interpolatedTime), 0.01f,  
                    centerX, centerY); 
        } 
    } 

 
main.xml
Xml代碼 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:gravity="center"> 
    <ImageView android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/bg" 
        android:id="@+id/myImageView" /> 
    <Button android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="20dip" 
        android:text="power off" 
        android:id="@+id/myButton" /> 
</LinearLayout> 

作者“onewayonelife”
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.