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

报告相同问题?

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上