android淡入淡出動畫
1) XML File: activity_main
1)XML文件:activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.faraz.Animation_Example.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
<ViewFlipper
android:id="@+id/backgroundViewFlipper1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/backgroundImageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="@drawable/tiger_2"
android:adjustViewBounds="true" />
<ImageView
android:id="@+id/backgroundImageView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="@drawable/tigertiger"
android:adjustViewBounds="true"/>
<ImageView
android:id="@+id/backgroundImageView3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="@drawable/white2"
android:adjustViewBounds="true"/>
<ImageView
android:id="@+id/backgroundImageView4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="@drawable/white4"
android:adjustViewBounds="true"/>
<ImageView
android:id="@+id/backgroundImageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/white_tiger_copy"
/>
<ImageView
android:id="@+id/backgroundImageView6"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="@drawable/tiger"
android:adjustViewBounds="true"/>
<ImageView
android:id="@+id/backgroundImageView7"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="@drawable/tigress"
android:adjustViewBounds="true"/>
<ImageView
android:id="@+id/backgroundImageView8"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="@drawable/wildcat"
android:adjustViewBounds="true"/>
</ViewFlipper>
</android.support.constraint.ConstraintLayout>
In the above code in every ImageView you will find android:src="@drawable/tigress" shows an error. Actually the error is concern about image/images. The particular images I use in this article will not be there in your computer, so first you have to copy your own images from your computer to ‘drawable’ folder in android studio and also write the same spelling of image in android:src="@drawable/your_image_name".
在每個ImageView中的上述代碼中,您會發現android:src =“ @ drawable / tigress”顯示錯誤。 實際上,該錯誤與圖像有關。 我在本文中使用的特定圖像不會在您的計算機中出現,因此首先您必須將計算機中的圖像復制到android studio中的“ drawable”文件夾中,并且還要在android:src =“中寫入相同的圖像拼寫: @ drawable / your_image_name” 。
Here in ‘drawable’ folder you should copy your images which you would like to see in animation. As you copy your images, in drawble, red color will change in green color android:src="@drawable/your_image_name ". Error removes.
在“可繪制”文件夾中,您應該復制想要在動畫中看到的圖像。 復制圖像時,在drawble中,紅色將變為綠色android:src =“ @ drawable / your_image_name” 。 錯誤消除。
2) File: MainActivity.java
2)文件:MainActivity.java
package com.example.faraz.Animation_Example;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;
public class MainActivity extends AppCompatActivity {
Animation fade_in, fade_out;
ViewFlipper viewFlipper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper= (ViewFlipper) this.findViewById(R.id.backgroundViewFlipper1);
fade_in= AnimationUtils.loadAnimation(this,android.R.anim.fade_in);
fade_out=AnimationUtils.loadAnimation(this,android.R.anim.fade_out);
viewFlipper.setAnimation(fade_in);
viewFlipper.setAnimation(fade_out);
viewFlipper.setAutoStart(true);
viewFlipper.setFlipInterval(5000);
viewFlipper.startFlipping();
}
}
In android, there are many possible ways to make ‘fade in’ animation and here I used alpha tag and it is one the easy and most used ways of making animation. For the fade in animation using alpha tag you have to create an anim folder in your res directory.
在android中,有很多方法可以制作“淡入”動畫,在這里我使用了alpha標記,這是制作動畫最簡單且最常用的方法之一。 對于使用alpha標簽的淡入動畫,您必須在res目錄中創建一個anim文件夾。
After creating anim folder in res/ directory and also create fadein.xml file in res/anim/ directory and place the following content.
在res /目錄中創建anim文件夾,并在res / anim /目錄中創建fadein.xml文件后,放置以下內容。
3) XML File: fadein.xml
3)XML文件:fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"/>
</set>
Output: After executing your code on your device/virtual device, you get animated images.
輸出:在設備/虛擬設備上執行代碼后,您將獲得動畫圖像。
翻譯自: https://www.includehelp.com/android/fade-in-animation-example.aspx
android淡入淡出動畫