如何从可绘制文件夹动态获取图像? [英] How can i get images dynamically from drawable folder?
问题描述
我正在通过静态给定的名称
I'm getting images by statically given the name like
{R.drawable.image1,R.drawable.image2,R.drawable.image3}
如果我有大约50张图像,我将无法给出数组中每个文件的名称,因此它必须是动态的,我该如何实现.并且还请告诉我将动态代码放在哪里
If I have some 50 images I cant give each and every file name in array so it needs to be dynamic how can I achieve this. And also please tell me where to put my dynamically code
package com.example.nauman.swipeapp;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class SwipeAdapter extends PagerAdapter {
private int[]image = {R.drawable.image1,R.drawable.image2,R.drawable.image3};
private Context cx;
SwipeAdapter(Context cx){
this.cx=cx;
}
@Override
public int getCount() {
return image.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return (view==(RelativeLayout)object);
}
@SuppressLint("SetTextI18n")
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
LayoutInflater layoutInflater = (LayoutInflater) cx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert layoutInflater != null;
View view= layoutInflater.inflate(R.layout.page_fragment,container,false);
ImageView imageView= view.findViewById(R.id.imageView);
imageView.setImageResource(image[position]);
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((RelativeLayout) object);
}
}
推荐答案
此解决方案感到有点棘手,不仅因为它使用了反射,而且还因为它很大程度上依赖于能够通过将其名称与之匹配来识别您自己的可绘制资源.某种模式(因此您应该尝试为图片使用真正唯一的名称)
This solution feels a little bit hacky not only because it uses reflection but also because it relies heavily on being able to recognize your own drawable resources by matching their names against a certain pattern (so you should try to use really unique names for the pictures)
话虽如此,您可以按以下方式更改代码:
That being said, you can change your code as follows:
保留private int[] image;
,但在适配器的构造函数中对其进行初始化:
Keep the private int[] image;
but initialize it in the Adapter's constructor:
SwipeAdapter(Context cx){
this.cx=cx;
buildImageArray();
}
使用新方法(注意:您必须导入java.lang.reflect.Field;)
with a new method (note: you have to import java.lang.reflect.Field;)
private void buildImageArray() {
// this will give you **all** drawables, including those from e.g. the support libraries!
Field[] drawables = R.drawable.class.getDeclaredFields();
SparseIntArray temp = new SparseIntArray();
int index = 0;
for (Field f : drawables) {
try {
System.out.println("R.drawable." + f.getName());
// check the drawable is "yours" by comparing the name to your name pattern
// this is the point where some unwanted drawable may slip in,
// so you should spend some effort on the naming/ matching of your pictures
if (f.getName().startsWith("image")) {
System.out.println("R.drawable." + f.getName() + "==========================================");
int id = cx.getResources().getIdentifier(f.getName(), "drawable", cx.getPackageName());
temp.append(index, id);
index++;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
image = new int[index];
for (int i = 0; i < index; i++) {
image[i] = temp.valueAt(i);
}
}
这篇关于如何从可绘制文件夹动态获取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!