在ImageView中交叉淡化多个图像

在ImageView中交叉淡化多个图像

本文介绍了Android-在ImageView中交叉淡化多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要在多张图像之间进行交叉淡入淡出的应用程序,

I am working on an application which needs image cross-fading between multiple images,

我所拥有的:一个ImageView和50个可绘制对象(.png),我将从云中下载

What I have: an ImageView and 50 drawables (.png) which I will download from the cloud

我想要的是:50个drawable应该在几秒钟的间隔内依次淡入淡出(淡入和淡出)

What I want: 50 drawable should crossfade (fade in and out) sequentially between an interval of some seconds

我尝试过的事情:基于关于stackoverflow的一些答案,我尝试了TransitionDrawable技术,但是我只能在2张图像之间进行淡入淡出,而不能通过触摸进行更多淡化.

What I have tried: Based on some answers here on stackoverflow, I tried the TransitionDrawable technique, but I could only crossfade between 2 images and not more and that to with touching.

我提到的视频: https://www.youtube.com/watch?v= atH3o2uh_94

推荐答案

用于执行此操作的自定义视图:

A custom view to do that:

public class FadeView extends FrameLayout {
    private long mFadeDelay = 1000;
    private ImageView mFirst;
    private ImageView mSecond;
    private boolean mFirstShowing;

    public FadeView(Context context) {
        super(context);
        init(context);
    }

    public FadeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FadeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context c){
        mFirst = new ImageView(c);
        mSecond = new ImageView(c);

        mFirst.setAlpha(1.0f);
        mSecond.setAlpha(0.0f);

        mFirstShowing = true;

        addView(mFirst);
        addView(mSecond);
    }

    public void setFadeDelay(long fadeDelay) {
        mFadeDelay = fadeDelay;
    }

    public void ShowImage(Drawable d){
        if(mFirstShowing){
            mSecond.setImageDrawable(d);
            mSecond.animate().alpha(1.0f).setDuration(mFadeDelay);
            mFirst.animate().alpha(0.0f).setDuration(mFadeDelay);
        }else {
            mFirst.setImageDrawable(d);
            mSecond.animate().alpha(0.0f).setDuration(mFadeDelay);
            mFirst.animate().alpha(1.0f).setDuration(mFadeDelay);
        }

        mFirstShowing = !mFirstShowing;
    }
}

用法:

public class test extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final FadeView fw = new FadeView(this);
        setContentView(fw);

        fw.setOnClickListener(new View.OnClickListener() {
            Drawable d1 = getResources().getDrawable(android.R.drawable.ic_dialog_alert);
            Drawable d2 = getResources().getDrawable(android.R.drawable.ic_dialog_info);
            boolean flag;

            @Override
            public void onClick(View view) {
                if(flag){
                    fw.ShowImage(d1);
                }else {
                    fw.ShowImage(d2);
                }
                flag = !flag;
            }
        });
    }


}

这篇关于Android-在ImageView中交叉淡化多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 01:57