gpt4 book ai didi

java - 将 Assets 文件夹中的自定义 gif 加载到 recyclerview android

转载 作者:行者123 更新时间:2023-11-29 22:58:54 25 4
gpt4 key购买 nike

我在 assets 文件夹中创建了一个名为 gifs 的子文件夹,我在其中添加了 5 个 gifs。我想在 recyclerview 中显示这些 gif,但由于 recyclerview 是空的,所以我无法做到。我不明白哪里出了问题,请帮助我。提前致谢!

public class GifAdapter extends RecyclerView.Adapter < GifAdapter.GifViewHolder > {
Context context;
List<String> gifList;
GifAdapterListener listener;

public GifAdapter(Context context, GifAdapterListener listener) {
this.context = context;
this.gifList = loadGifs();
this.listener = listener;
}

private List<String> loadGifs() {
List<String> gifs = new ArrayList();
gifs.add("a.gif");
gifs.add("aa.gif");
gifs.add("aaa.gif");
gifs.add("aaaa.gif");
gifs.add("aaaaa.gif");
return gifs;
}

// more code

// onBindViewHolder
@Override public void onBindViewHolder(@NonNull GifViewHolder holder, int position) {

try {
InputStream inputStream = context.getAssets().open("gifs");
byte[] bytes = IOUtils.toByteArray(inputStream);
holder.gifView.setBytes(bytes); // gifView is GifImageView using felipecsl.gifimageview library
holder.gifView.startAnimation();

} catch (IOException e) {
e.printStackTrace();
}


}

// my fragment oncreate

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_gif, container, false);

recyclerGifs = view.findViewById(R.id.recyclerGifs);
recyclerGifs.setHasFixedSize(true);
recyclerGifs.setLayoutManager(new GridLayoutManager(getActivity(), 2));
gifAdapter = new GifAdapter(getContext(), this);
recyclerGifs.setAdapter(gifAdapter);


return view;
}
}

最佳答案

Make use of Glide and get gifs from assets like this:

private List<String> loadGifs() {
List<String> gifs = new ArrayList();
gifs.add("file:///android_asset/a.gif");
gifs.add("file:///android_asset/aa.gif");
gifs.add("file:///android_asset/aaa.gif");
return gifs;
}

Then load:

 @Override
public void onBindViewHolder(@NonNull GifViewHolder holder, int position) {
Glide.with(this).asGif().load(gifs(position)).into(imageView);
}

Simple example to load GIF:

ImageView imageView = (ImageView) findViewById(R.id.imageView);
Glide.with(this).asGif().load(R.raw.image_gif).into(imageView);

关于java - 将 Assets 文件夹中的自定义 gif 加载到 recyclerview android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57141824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com