Android 来自 php 的动态图库网格视图

Android dynamic gallery gridview from php

我有一个带 ImageAdapter 的工作 GalleryActivity。 它在屏幕上显示一个网格视图(3 列)从可绘制数组加载图像。

public class GalleryActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_layout);

    GridView gridView = (GridView) findViewById(R.id.grid_view);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
            i.putExtra("id", position);
            startActivity(i);
        }
    });
}}

public class ImageAdapter extends BaseAdapter {
private Context mContext;

//Images in array
public Integer[] mThumbIds = {
        R.drawable.img1, R.drawable.img2,
        R.drawable.img3, R.drawable.img4,
        R.drawable.img5, R.drawable.img6,
        R.drawable.img7, R.drawable.img8,
        R.drawable.img9, R.drawable.img10,
        R.drawable.img11, R.drawable.img12,
        R.drawable.img13, R.drawable.img14,
        R.drawable.img15, R.drawable.img16,
        R.drawable.img17, R.drawable.img18,
        R.drawable.img19
};

public ImageAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    //getResizedBitmap(mThumbIds[position], 80, 80);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
    return imageView;
}

public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
    int width = image.getWidth();
    int height = image.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;
}}

现在我将通过包含图像路径最后一部分的 php 文件从输出数组加载这些图像,例如 url + .getString("img")

[{"img":"2015.jpg"},{"img":"to.jpg"},{"img":"its.jpg"},{"img":"eat.jpg"},
{"img":"ice.jpg"},{"img":"ata.jpg"},{"img":"ello.jpg"}]

数组不是静态的。 谁能给我一些想法或建议吗? 我知道我必须解析一个 json 数组,但不知道如何加载它。

如有任何帮助,我们将不胜感激。

代码在这里

public Bitmap getImage(String pic_string){

    String IMAGE_URL = "http://yourwebsite.com/photo/"+pic_string;

                InputStream in = new URL(IMAGE_URL).openStream();
                 bmp = BitmapFactory.decodeStream(in);
                 return bmp;

    }

如果您的图片位于可绘制对象中

此函数 return 可绘制,但您必须从字符串中删除 .png/.jpg 等

public Drawable getDrawable(String name) {

        int resourceId = getResources().getIdentifier(name, "drawable", getPackageName());
        return getResources().getDrawable(resourceId);
    }