Android - crossfade of multiple images in ImageView

I am working on an application that requires cross-fading an image between multiple images,

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

What I want: 50 drawable should cross (fade out and fade) sequentially between a few seconds interval

What I tried: based on some answers here on stackoverflow, I tried the TransitionDrawable method, but I could only retrain between the two images, not more, and that concerned.

The video I referred to: https://www.youtube.com/watch?v=atH3o2uh_94

+5
source share
2 answers

Custom view for this:

 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; } } 

Using:

 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; } }); } } 
+10
source

then create your own Drawable that draws two Drawables with varying alpha values, of course you will need to add support for Drawables sharing when you need to support multiple ones.

-1
source

Source: https://habr.com/ru/post/972454/


All Articles