如何创建图像数组?

时间:2013-11-27 06:00:31

标签: java android

我想创建一个包含多个图像的数组。稍后我必须在我的代码中循环使用该数组。任何人都可以建议我是否可以创建一个图像数组。?

4 个答案:

答案 0 :(得分:0)

定义像这样的图像ID数组

int[] p = {R.drawable.image1, R.drawable.image2....}

现在针对不同条件使用此数组的成员

yourbutton.setBackgroundResource(p[0])  // or p[1]

或者您可以使用ENUM使其更具可读性。

答案 1 :(得分:0)

这将解决您的问题:

int imageArray[] = new int[number_of_images];
for (int i = 0; i < numImages; i++)
imageArray[i] = getDrawableId(getApplicationContext(),"R.drawable." + image_names[i]);

答案 2 :(得分:0)

您可以定义图像文件名数组:

String fileNames[] = {"temp.jpg", "sample_img28.jpg", "normImg.jpg", "drawing.png", "film.png"};

并在初始化应用程序时添加MeidaTracker中的那些

MediaTracker tracker = null;    

public void init() {

    tracker = new MediaTracker(this);
    for(int i = 0; i < fileNames.length; i++) {
        System.out.println(" path :"+this.getCodeBase());
        image[i] = getImage(this.getCodeBase(),fileNames[i]);
        image[i] = image[i].getScaledInstance(256, 256, Image.SCALE_SMOOTH);
        tracker.addImage(image[i], i);
    }
    try {
        tracker.waitForAll();
    }
}

答案 3 :(得分:0)

创建自己的customImageAdapter,扩展ArrayAdapter
arrayAdapter的示例代码:

public class CustomImageAdapter extends BaseAdapter{
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public CustomImageAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
 // if you want to display the image modify the content according o your need
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);//thumb image


        return vi;
    }