如何从可绘制文件夹中动态获取图像?
How can i get images dynamically from drawable folder?
我通过静态地给出像
这样的名称来获取图像
{R.drawable.image1,R.drawable.image2,R.drawable.image3}
如果我有大约 50 张图像,我无法在数组中给出每个文件名,因此它需要是动态的,我该如何实现。也请告诉我在哪里放置我的动态代码
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);
}
}
这个解决方案感觉有点老套,不仅因为它使用了反射,还因为它在很大程度上依赖于通过将它们的名称与特定模式匹配来识别您自己的可绘制资源(因此您应该尝试使用真正独特的图片的名称)
也就是说,您可以按如下方式更改代码:
保留 private int[] image;
但在适配器的构造函数中对其进行初始化:
SwipeAdapter(Context cx){
this.cx=cx;
buildImageArray();
}
使用新方法(注意:您必须导入 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);
}
}
如果您所有的可绘制对象的名称如下:image1
、image2
、image3
、....
然后你可以像这样得到他们的资源ID:
int i = 1; // get the id of image1
int resId = context.getResources().getIdentifier("image" + i, "drawable", context.getPackageName());
并使用它:
imageView.setImageResource(resId);
为此,您必须提供有效的 Context
(在 activity 中,您可以使用 this
)。
更改适配器的构造函数 class,如下所示。只需添加图像列表作为参数
public class SwipeAdapter extends PagerAdapter {
private ArrayList<Integer> image;
private Context cx;
SwipeAdapter(Context cx, ArrayList<Integer> image){
this.cx=cx
this.image =image
}
@Override
public int getCount() {
return image.size;
}
@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);
}
使用图像列表实例化适配器如下。为了在可绘制资源中的图像之间循环,如果您想在适配器中使用 50 张图像,则需要在其名称中按 1 到 50 的顺序添加索引
ArrayList<Integer> images=new ArrayList<Integer>()
for(int i =0;i<50; i++){
int res = getResources().getIdentifier("<your pakecgename>:drawable/abc"+i, null, null);
images.add(res);
}
SwipeAdapter adapter = new SwipeAdapter(activity, images)
我通过静态地给出像
这样的名称来获取图像{R.drawable.image1,R.drawable.image2,R.drawable.image3}
如果我有大约 50 张图像,我无法在数组中给出每个文件名,因此它需要是动态的,我该如何实现。也请告诉我在哪里放置我的动态代码
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);
}
}
这个解决方案感觉有点老套,不仅因为它使用了反射,还因为它在很大程度上依赖于通过将它们的名称与特定模式匹配来识别您自己的可绘制资源(因此您应该尝试使用真正独特的图片的名称)
也就是说,您可以按如下方式更改代码:
保留 private int[] image;
但在适配器的构造函数中对其进行初始化:
SwipeAdapter(Context cx){
this.cx=cx;
buildImageArray();
}
使用新方法(注意:您必须导入 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);
}
}
如果您所有的可绘制对象的名称如下:image1
、image2
、image3
、....
然后你可以像这样得到他们的资源ID:
int i = 1; // get the id of image1
int resId = context.getResources().getIdentifier("image" + i, "drawable", context.getPackageName());
并使用它:
imageView.setImageResource(resId);
为此,您必须提供有效的 Context
(在 activity 中,您可以使用 this
)。
更改适配器的构造函数 class,如下所示。只需添加图像列表作为参数
public class SwipeAdapter extends PagerAdapter {
private ArrayList<Integer> image;
private Context cx;
SwipeAdapter(Context cx, ArrayList<Integer> image){
this.cx=cx
this.image =image
}
@Override
public int getCount() {
return image.size;
}
@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);
}
使用图像列表实例化适配器如下。为了在可绘制资源中的图像之间循环,如果您想在适配器中使用 50 张图像,则需要在其名称中按 1 到 50 的顺序添加索引
ArrayList<Integer> images=new ArrayList<Integer>()
for(int i =0;i<50; i++){
int res = getResources().getIdentifier("<your pakecgename>:drawable/abc"+i, null, null);
images.add(res);
}
SwipeAdapter adapter = new SwipeAdapter(activity, images)