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);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需求高精度PT100设计电路和算法
  • ¥15 单片机配网,继电器开关,广播
  • ¥60 Qcustomplot绘制实时动态曲线
  • ¥20 运用matlab画x-y图
  • ¥15 用idea运行项目,运行tomcat报错:断言失败
  • ¥15 Sqlserver查询链接服务器数据问题
  • ¥15 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题