<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Android 動畫教程 > 原文: [https://javatutorial.net/animations-with-android-tutorial](https://javatutorial.net/animations-with-android-tutorial) 本教程借助一個示例來說明什么是動畫以及如何在 Android 應用程序中使用動畫。 ## 動畫 你知道什么是動畫嗎? 動畫實際上是對運動的幻想。 在這項技術中,相差很小的連續圖像會產生運動效果。 動畫在移動應用程序中非常有用。 教育中的動畫用于解釋理論和概念。 應用程序中的動畫用來顯示一些準則或過程。 向 Android 應用程序添加動畫會增加用戶的可加性。 它增加了樂趣,并平滑了用戶體驗。 在本教程中,您將學習如何通過在 Android 應用中添加動畫來增加用戶體驗。 ## Android 中的動畫 Android 提供了一個名為`Animations`的類。 此類提供了許多不同的方法。 以下是其中一些: * `loadAnimation(Context, Layout)`:此方法用于加載動畫。 它有兩個參數。 * `start()`:用于啟動動畫。 * `setDuration(long duration)`:此方法設置 Android 中動畫的持續時間。 * `getDuration()`:用于獲取 Android 動畫的持續時間。 * `end()`:用于結束動畫。 * `cancel()`:用于取消動畫。 ## Android 中的動畫示例 讓我們開始為 Android 中的動畫創建示例。 我將討論四種類型的動畫:眨眼,淡入淡出,順時針和滑動。 打開您的 Android Studio 并創建一個新活動。 在主要活動中,有一個圖像視圖和一個按鈕。 我用足球的形象。 這是`activity_main.xml`的代碼 ```java <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="368dp" android:layout_height="495dp" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="JavaTutorial" android:id="@+id/textView2" android:textColor="#ff3eff0f" android:textSize="35dp" android:layout_centerHorizontal="true" /> <ImageView android:layout_width="200dp" android:layout_height="200dp" android:id="@+id/imageView" android:src="@drawable/football" android:layout_below="@+id/textView2" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" android:layout_marginTop="50dp" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" android:layout_marginTop="59dp" android:backgroundTint="@color/colorAccent" android:onClick="Animation" android:text="Animation" /> </RelativeLayout> ``` 現在為眨眼動畫,淡入淡出動畫,順時針動畫和幻燈片動畫創建四個布局文件。 這是`blink_animation.xml`的代碼 ```java <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:duration="600" android:repeatMode="reverse" android:repeatCount="infinite"/> </set> ``` 這是`fade_animation.xml`的代碼 ```java <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" > <alpha android:fromAlpha="0" android:toAlpha="1" android:duration="2000" > </alpha> <alpha android:startOffset="2000" android:fromAlpha="1" android:toAlpha="0" android:duration="2000" > </alpha> </set> ``` 這是`clockwise_animation.xml`的代碼 ```java <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="5000" > </rotate> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:startOffset="5000" android:fromDegrees="360" android:toDegrees="0" android:pivotX="50%" android:pivotY="50%" android:duration="5000" > </rotate> </set> ``` 這是`slide_animation.xml`的代碼 ```java <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" > <scale android:duration="500" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/linear_interpolator" android:toXScale="1.0" android:toYScale="0.0" /> </set> ``` 現在創建一個 Java 類`MainActivity.java`并粘貼以下代碼 ```java package com.example.admin.androidanimations; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { int count=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void Animation(View view){ if(count==0){ ImageView image = (ImageView)findViewById(R.id.imageView); Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise_animation); image.startAnimation(animation); } if(count==1){ ImageView image = (ImageView)findViewById(R.id.imageView); Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_animation); image.startAnimation(animation1); } if(count==2){ ImageView image = (ImageView)findViewById(R.id.imageView); Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blink_animation); image.startAnimation(animation1); } if(count==3){ ImageView image = (ImageView)findViewById(R.id.imageView); Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_animation); image.startAnimation(animation1); count=0; } count++; } } ``` 當用戶首次單擊`count = 0`的動畫按鈕時,圖像將順時針旋轉。 當用戶第二次單擊`count = 1`時,圖像將消失。 當用戶第三次單擊`count = 2`時,圖像將開始閃爍。 第四次`count = 3`,則圖像將顯示幻燈片動畫。 現在運行它,這是輸出 ![animation example](https://img.kancloud.cn/35/93/35930501a74226223e51d6874a91de13_369x653.jpg) 動畫示例 這是它閃爍時的輸出 ![blink animation](https://img.kancloud.cn/8f/9c/8f9c706293c27c15bd883f007f2ef217_369x653.jpg) 閃爍動畫 您可以通過單擊[鏈接](https://github.com/JavaTutorialNetwork/Tutorials/blob/master/AndroidAnimations.rar)下載本教程。 ## 資源 [官方 Android 動畫指南](https://developer.android.com/training/animation/index.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看