douguachi0056 2015-03-04 22:15
浏览 41
已采纳

来自php的Android动态图库gridview

I have a working GalleryActivity with an ImageAdapter. It display on the screen a gridview (3 columns) loading image from a drawable array.

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);
        }
    });
}}

and

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;
}}

Now I would load those images from an output array by a php file containing the last part of image path like url + .getString("img")

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

The array is not static. Could anyone please give me ideas or suggestion about it? I know I have to parse a json array but don't know how to load it.

Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • dr200166 2015-03-10 08:03
    关注

    code is here

    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;
    
        }
    

    if your pic reside in drawables

    this function will return drawable but you must remove .png/.jpg etc from string

    public Drawable getDrawable(String name) {
    
            int resourceId = getResources().getIdentifier(name, "drawable", getPackageName());
            return getResources().getDrawable(resourceId);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。