安卓图片淡入淡出切换效果(支持循环)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34379916/article/details/79092566

日前做项目重构,想在酒店介绍图片展示里增加些动画,觉得淡入淡出的效果不错,上网问度娘,找到一篇博客写的是我需要的答案  ,但是他是重写一个Relativelayout,有几张图片就新建几个Imageview,再把这些Imageview加到Relaytivelayout里边去,根据显示的第几张图片,设置透明度来实现效果,看完之后觉得这种方法不太理想,少的几张图片倒不会出现什么问题,但是图片一旦多了,就可能造成内存溢出,于是我想到用两张图片交替出现通过属性动画来实现这个需求,下面贴上关键代码,

   主Activity

package com.lyf.gradientdemo;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ImageView image1, image2;
    private ImageView[] imageViews = new ImageView[2];
    private int currentpos = 1;
    private Animator animator1, animator2;
    private int image[] = {R.mipmap.a04, R.mipmap.a05, R.mipmap.a06, R.mipmap.a07, R.mipmap.a08, R.mipmap.a09};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image1 = (ImageView) findViewById(R.id.image1);
        image2 = (ImageView) findViewById(R.id.image2);
        imageViews[0] = image1;
        imageViews[1] = image2;
        image1.setImageResource(image[0]);
        image2.setImageResource(image[1]);
        animator1 = AnimatorInflater.loadAnimator(this, R.animator.out_anim);
        animator2 = AnimatorInflater.loadAnimator(this, R.animator.in_anim);
        animator1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                currentpos++;
                if (currentpos % imageViews.length == 0) {
                    image1.setImageResource(image[currentpos % image.length]);
                } else if (currentpos % imageViews.length == 1) {
                    image2.setImageResource(image[currentpos % image.length]);
                }
                super.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        animator1.setTarget(imageViews[(currentpos + 1) % imageViews.length]);
                        animator2.setTarget(imageViews[currentpos % imageViews.length]);
                        animator1.start();
                        animator2.start();
                    }
                });
            }
        }, 5000, 5000);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
 
 
in_anim.xml
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="alpha"
    android:valueFrom="0"
    android:duration="2000"
    android:valueTo="1"
    android:valueType="floatType" />

out_anim.xml
 
 
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:propertyName="alpha"
    android:valueFrom="1"
    android:valueTo="0"
    android:valueType="floatType" />
图片自己加几张吧

猜你喜欢

转载自blog.csdn.net/qq_34379916/article/details/79092566