安卓第十八天筆記--簡單動畫

来源:http://www.cnblogs.com/liunanjava/archive/2016/03/21/5250049.html
-Advertisement-
Play Games

載入使用AnimationUtils.load方法載入 一般都用代碼控制 如果需要 用XML控制可以在res\下建立 animator,即可, 在這個目錄建立XML文件如果 下方法導入



title:動畫

動畫

1.補間動畫

  1. Animation.ABSOLUTE:用於指定後面的數值是絕對值的像素。
  2. Animation.RELATIVETOSELF: 用於指定後面的數值是自己寬高的倍數
  3. Animation.RELATIVETOPARENT : 用於指定後面的數值是自己的父控制項寬高的倍數
  4. Animation.RESTART播放重新回到原點
  5. Animation.REVERSE播放回接著,反向播放

2.平移

fromXType  水平播放的類型
fromXValue 從哪開始,0表示現在的位置
toXType,   到什麼位置的類型
toXValue 到什麼坐標
fromYType 垂直播放的類型
fromYValue 從哪開始,0表示現在的位置
toYType 到什麼位置的類型
toYValue 到什麼坐標
TranslateAnimation translate = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, -2, 
            Animation.RELATIVE_TO_SELF, 2, 
            Animation.RELATIVE_TO_SELF, 0,
            Animation.RELATIVE_TO_SELF, 1);
    //設置顯示時間
    translate.setDuration(3000);
    //播放資源為無限迴圈
    translate.setRepeatCount(Animation.INFINITE);
    //播放模式,反轉播放,先順著播完,就反著播放
    translate.setRepeatMode(Animation.REVERSE);
    //哪個組件在播放,設置
    imageView.setAnimation(translate);
    //開始播放
    translate.start();

 

3.旋轉

/*
     * fromDegrees, 從什麼角度開始 0 從現在的 toDegrees, 270度 pivotXType, X中心點類型
     * Animation.RELATIVE_TO_SELF自身 pivotXValue, 中心點值 0.5表示這個圖片的一半置
     * pivotYType, Y中心點類型Animation.RELATIVE_TO_SELF自身
     *  pivotYValue 0.5f 
     */
    RotateAnimation rotate = new RotateAnimation(0, 360,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            3);
    //播放顯示時間
    rotate.setDuration(5000);
    rotate.setRepeatCount(Animation.INFINITE);
    rotate.setRepeatMode(Animation.RESTART);

    imageView.setAnimation(rotate);
    rotate.start();

 

4.縮放

    /*
     * fromX, toX, 從坐標什麼地址開始到什麼坐標,0,表示當前 fromY, toY, 
     * pivotXType,  旋轉的中心點
     * pivotXValue, 類型, pivotYType, pivotYValue
     */
    ScaleAnimation scale = new ScaleAnimation(1, 2, 1, -2,
            Animation.RELATIVE_TO_SELF, -2, Animation.RELATIVE_TO_SELF, 3);

    // 播放顯示時間
    scale.setDuration(5000);
    scale.setRepeatCount(Animation.INFINITE);
    scale.setRepeatMode(Animation.RESTART);

    imageView.setAnimation(scale);
    scale.start();

 

5.透明度

    /*
     * fromAlpha,  從什麼透明度開始
     * toAlpha 到什麼透明度結束
     */
    AlphaAnimation alpha = new AlphaAnimation(1, 0);

    // 播放顯示時間
    alpha.setDuration(2000);
    alpha.setRepeatCount(Animation.INFINITE);
    alpha.setRepeatMode(Animation.REVERSE);

            imageView.setAnimation(alpha);
            alpha.start();
}

 

6.集合

就是建立一個
AnimationSet set = new AnimationSet();
set.addAnimation(scale);
    set.addAnimation(rotate);
    set.addAnimation(translate);
    set.start();

 


就可以播放多個動畫

7. XML配置

在res/目錄下建立anim文件目錄
 <!-- 
        如果平移的數值是數字,那麼表示的是絕對值
        如果是百分比,那麼表明是自己的寬高的倍數
        如果是百分比加上 p,那麼表明是自己的父元素的寬高的倍數
         -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate 
    android:fromXDelta="200%"
    android:toXDelta="200%"
    android:fromYDelta="-100%"
    android:toYDelta="200%"
    android:duration="4000"
    android:repeatCount="infinite"
    android:repeatMode="reverse"/>

    <rotate
       android:fromDegrees="0"
       android:toDegrees="360"
       android:toYScale="0.0"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="4000" 
       android:repeatCount="infinite"
       android:repeatMode="reverse"/>

</set>

 

載入使用AnimationUtils.load方法載入

Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation_set);

    imageView.startAnimation(animation);

 

8.屬性動畫

一般都用代碼控制

/**
 * 水平
 * 
 * @param v
 */
public void translate(View v) {
    /**
     * 參數一: 誰去播放這個動畫 參數二: 屬性名字 想要改變x方向的移動 參數三:動畫執行的數值
     */
    ObjectAnimator objectAnimatrX = ObjectAnimator.ofFloat(imageView,
            "translationX", -50, 50);
    ObjectAnimator objectAnimatrY = ObjectAnimator.ofFloat(imageView,
            "translationY", 0, 60);

    objectAnimatrX.setDuration(2000);

    objectAnimatrX.setRepeatCount(ObjectAnimator.INFINITE);

    objectAnimatrX.setRepeatMode(ObjectAnimator.REVERSE);

    objectAnimatrX.start();




}

/**
 * 轉換
 * 
 * @param v
 */
public void rotate(View v) {
    ObjectAnimator rotateX = ObjectAnimator.ofFloat(imageView, "rotationX", 0,360);
    ObjectAnimator rotateY = ObjectAnimator.ofFloat(imageView, "rotationY", 0,360);
    rotateX.setDuration(3000);
    rotateX.setRepeatMode(Animation.REVERSE);
    //rotateX.setRepeatCount(Animation.INFINITE);
    rotateY.setDuration(3000);
    rotateY.setRepeatMode(Animation.REVERSE);
    //rotateY.setRepeatCount(Animation.INFINITE);

    AnimatorSet set  = new AnimatorSet();
    set.playSequentially(rotateX,rotateY);
    set.start();
}

/**
 * 綻放
 * 
 * @param v
 */
public void scale(View v) {
    ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 2.0f,5f);
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 2.0f,5f);
    scaleX.setDuration(3000);
    scaleX.setRepeatMode(Animation.REVERSE);
    scaleX.setRepeatCount(Animation.INFINITE);
    scaleY.setDuration(3000);
    scaleY.setRepeatMode(Animation.REVERSE);
    scaleY.setRepeatCount(Animation.INFINITE);

    AnimatorSet set = new AnimatorSet();
    set.playTogether(scaleX,scaleY);
    set.start();


}

/**
 * 透明
 * 
 * @param v
 */
public void alpha(View v) {

    ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f,0f);
    alpha.setDuration(3000);
    alpha.setRepeatMode(Animation.REVERSE);
    alpha.setRepeatCount(Animation.INFINITE);

    alpha.start();
}

/**
 * 集合
 * 
 * @param v
 */
public void set(View v) {
    /**
     * 參數一: 誰去播放這個動畫 參數二: 屬性名字 想要改變x方向的移動 參數三:動畫執行的數值
     */
    ObjectAnimator objectAnimatrX = ObjectAnimator.ofFloat(imageView,
            "translationX", -50, 100);
    ObjectAnimator objectAnimatrY = ObjectAnimator.ofFloat(imageView,
            "translationY", -70, 100);

    objectAnimatrX.setDuration(2000);

    //objectAnimatrX.setRepeatCount(ObjectAnimator.INFINITE);

    objectAnimatrX.setRepeatMode(ObjectAnimator.REVERSE);
    objectAnimatrY.setDuration(2000);

    //objectAnimatrY.setRepeatCount(ObjectAnimator.INFINITE);
    objectAnimatrY.setRepeatMode(ObjectAnimator.REVERSE);

    AnimatorSet set = new AnimatorSet();

    //set.playTogether(objectAnimatrX,objectAnimatrY);
    set.playSequentially(objectAnimatrX,objectAnimatrY);
    set.start();


}

 

如果需要 用XML控制可以在res\下建立 animator,即可, 在這個目錄建立XML文件如果 下方法導入

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
R.anim.property_animator);
set.setTarget(myObject);
set.start();

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 就在不長也不短的時間前,蘋果正式命令咱們要向NSURLSession看,因此我們不得不認認真真的聽從老大的教導,努力認知NSURLSession。其實呢,三方早已為我們解決了問題,但是呢,我們還是有必要大概瞭解一下NSURLSession。下麵呢,我就為大家簡單介紹NSURLSession。 *下麵
  • selector 選擇器 在App的使用中經常能看到selector的身影 如:一個按鍵看上去白色或者其它顏色,可能是一張圖片 按下去又顯示其它的顏色或者另外一張圖片 這裡使用shape配合使用 正常狀態 按下狀態 selector 佈局中引用 有圖片就去需要建立一個selector 在drawab
  • ListView可能是Android開發中最常用的一個控制項,但要用的純熟還需要不斷的鍛煉。 建立簡單的ListView 1.在佈局文件(.xml)中添加<ListView>標簽 2.在MainActivity.java中用適配器綁定 可以看到 數組內容無法直接加到ListView中,所以要用適配器。
  • 文章出自:聽雲博客 Reveal簡介: 這是個神奇的工具,它能常透徹地分析個App的UI結構。 這個工具包括兩部分,部分是在PC上運行的一個獨立應用,即Reveal.app,另一部分代碼在你要分析的某個App中,為此,Reveal提供了一個Framework和一個Dylib供使用。這兩部分之間通過B
  • shape 先看下,系統自帶的EditText和Button的外形 下麵看加了shape後的效果 簡單點講,shape可以為組件加上背景邊框,圓角之類的可以配合selector使用 shapeXXX.xml定義在drawable目錄下 EditText使用的 Button使用的定義的都 一樣 佈局中
  • java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx...}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from Curs
  • 如果想對自己自定義的類進行解檔和歸檔的話 必須遵循一個協議:NSCoding Student.h 文件 Student.m 文件 客戶端代碼 運行結果:
  • 此文章來自:聽雲博客 很多時候需要網路抓包分析,在iPhone上抓包稍有不同,下麵介紹三種常用的方式。分析工具以wireshark為例。 一、最簡單的方式:用PC作為熱點,在PC上抓包 優點:簡單 缺點:不能抓真機2g/3g/4g網路數據 步驟如下: 1、PC接上有線 2、PC用wifi方式共用網路
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...