当前位置:   article > 正文

android淡入淡出动画_在Android中淡入动画示例

android xml淡入动画

android淡入淡出动画

1) XML File: activity_main

1)XML文件:activity_main

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context="com.example.faraz.Animation_Example.MainActivity">
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent">
  10. </LinearLayout>
  11. <ViewFlipper
  12. android:id="@+id/backgroundViewFlipper1"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent">
  15. <ImageView
  16. android:id="@+id/backgroundImageView1"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. android:scaleType="centerCrop"
  20. android:src="@drawable/tiger_2"
  21. android:adjustViewBounds="true" />
  22. <ImageView
  23. android:id="@+id/backgroundImageView2"
  24. android:layout_width="fill_parent"
  25. android:layout_height="fill_parent"
  26. android:scaleType="centerCrop"
  27. android:src="@drawable/tigertiger"
  28. android:adjustViewBounds="true"/>
  29. <ImageView
  30. android:id="@+id/backgroundImageView3"
  31. android:layout_width="fill_parent"
  32. android:layout_height="fill_parent"
  33. android:scaleType="centerCrop"
  34. android:src="@drawable/white2"
  35. android:adjustViewBounds="true"/>
  36. <ImageView
  37. android:id="@+id/backgroundImageView4"
  38. android:layout_width="fill_parent"
  39. android:layout_height="fill_parent"
  40. android:scaleType="centerCrop"
  41. android:src="@drawable/white4"
  42. android:adjustViewBounds="true"/>
  43. <ImageView
  44. android:id="@+id/backgroundImageView5"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:scaleType="centerCrop"
  48. android:src="@drawable/white_tiger_copy"
  49. />
  50. <ImageView
  51. android:id="@+id/backgroundImageView6"
  52. android:layout_width="fill_parent"
  53. android:layout_height="fill_parent"
  54. android:scaleType="centerCrop"
  55. android:src="@drawable/tiger"
  56. android:adjustViewBounds="true"/>
  57. <ImageView
  58. android:id="@+id/backgroundImageView7"
  59. android:layout_width="fill_parent"
  60. android:layout_height="fill_parent"
  61. android:scaleType="centerCrop"
  62. android:src="@drawable/tigress"
  63. android:adjustViewBounds="true"/>
  64. <ImageView
  65. android:id="@+id/backgroundImageView8"
  66. android:layout_width="fill_parent"
  67. android:layout_height="fill_parent"
  68. android:scaleType="centerCrop"
  69. android:src="@drawable/wildcat"
  70. android:adjustViewBounds="true"/>
  71. </ViewFlipper>
  72. </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” 。

Android - Fade In Example 0

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

  1. package com.example.faraz.Animation_Example;
  2. import android.os.Bundle;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.view.animation.Animation;
  5. import android.view.animation.AnimationUtils;
  6. import android.widget.ViewFlipper;
  7. public class MainActivity extends AppCompatActivity {
  8. Animation fade_in, fade_out;
  9. ViewFlipper viewFlipper;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. viewFlipper= (ViewFlipper) this.findViewById(R.id.backgroundViewFlipper1);
  15. fade_in= AnimationUtils.loadAnimation(this,android.R.anim.fade_in);
  16. fade_out=AnimationUtils.loadAnimation(this,android.R.anim.fade_out);
  17. viewFlipper.setAnimation(fade_in);
  18. viewFlipper.setAnimation(fade_out);
  19. viewFlipper.setAutoStart(true);
  20. viewFlipper.setFlipInterval(5000);
  21. viewFlipper.startFlipping();
  22. }
  23. }

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文件夹。

Android - Fade In Example 1

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <alpha
  4. android:duration="2000"
  5. android:fromAlpha="1.0"
  6. android:toAlpha="0.0"
  7. android:interpolator="@android:anim/accelerate_interpolator"/>
  8. </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淡入淡出动画

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/660274
推荐阅读
相关标签