在 Android Studio 中按顺序播放 gif
Play gifs sequentally in Android Studio
我正在尝试在 Android Studio 中播放 两个或更多 gif。我正在使用 gif 图像视图,但每次我尝试在其中播放更多 gif 时,它只显示最后一个。我找不到连续播放所有 gif 的方法。有什么想法吗?
List gifs = Arrays.asList("gif1", "gif2", "gif3");
GifImageView gifView = view.findViewById(R.id.gifView);
for (int i=0; i < gifs.size(); i++) {
String word = gifs.get(i);
int idNum = getResources().getIdentifier(word, "drawable"
getActivity().getPackageName());
gifView.setImageResource(idNum);
}
由于您使用循环将 gif 加载到视图中,因此每次迭代都会覆盖除最后一张之外的所有 gif,因此您应该在完全播放 gif 后才加载下一张 gif。
下面是使用 Glide 和 ImageView 实现此目的的示例
public class MainActivity extends AppCompatActivity {
int gifLoaded = 0;
List gifs = Arrays.asList("https://i.kym-cdn.com/photos/images/newsfeed/001/708/675/4b5.gif",
"https://c.tenor.com/tOH34YVvpq4AAAAC/malayalam.gif",
"https://c.tenor.com/AKtcHf4ZTAsAAAAC/malayalam-kerala.gif"
);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadImage();
}
private void loadImage() {
if (gifLoaded > gifs.size() - 1) {
gifLoaded = 0;
}
String url = (String) gifs.get(gifLoaded);
ImageView giv = findViewById(R.id.iv_gif);
Glide.with(this).asGif().load(url).listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
resource.setLoopCount(1);
resource.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
gifLoaded++;
loadImage();
}
});
return false;
}
}).into(giv);
}
}
我正在尝试在 Android Studio 中播放 两个或更多 gif。我正在使用 gif 图像视图,但每次我尝试在其中播放更多 gif 时,它只显示最后一个。我找不到连续播放所有 gif 的方法。有什么想法吗?
List gifs = Arrays.asList("gif1", "gif2", "gif3");
GifImageView gifView = view.findViewById(R.id.gifView);
for (int i=0; i < gifs.size(); i++) {
String word = gifs.get(i);
int idNum = getResources().getIdentifier(word, "drawable"
getActivity().getPackageName());
gifView.setImageResource(idNum);
}
由于您使用循环将 gif 加载到视图中,因此每次迭代都会覆盖除最后一张之外的所有 gif,因此您应该在完全播放 gif 后才加载下一张 gif。
下面是使用 Glide 和 ImageView 实现此目的的示例
public class MainActivity extends AppCompatActivity {
int gifLoaded = 0;
List gifs = Arrays.asList("https://i.kym-cdn.com/photos/images/newsfeed/001/708/675/4b5.gif",
"https://c.tenor.com/tOH34YVvpq4AAAAC/malayalam.gif",
"https://c.tenor.com/AKtcHf4ZTAsAAAAC/malayalam-kerala.gif"
);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadImage();
}
private void loadImage() {
if (gifLoaded > gifs.size() - 1) {
gifLoaded = 0;
}
String url = (String) gifs.get(gifLoaded);
ImageView giv = findViewById(R.id.iv_gif);
Glide.with(this).asGif().load(url).listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
resource.setLoopCount(1);
resource.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
gifLoaded++;
loadImage();
}
});
return false;
}
}).into(giv);
}
}