首页 > 解决方案 > 如何使用来自服务器(Android)的数据创建缩略图照片库?

问题描述

我想创建一个微型照片库。通过单击缩略图,我想以全尺寸显示照片。从服务器我得到 2 个不同的 url。一张用于微型照片,另一张用于全尺寸照片。

这是我关于照片的活动:

public class GalleryActivity extends MyActivity {
private ServerAPI server;
private ArrayList<Photo> gallery ;
List<String> mThumbIds;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);

    gallery = new ArrayList<Photo>();
    server= new ServerAPI(this);
    galleryGridView = findViewById(R.id.galleryGridView);
    loadDataSource();
    GalleryAdapter adapter = new GalleryAdapter(GalleryActivity.this, gallery);
    galleryGridView.setAdapter(adapter);
}

private void loadDataSource() {
    //here get data from server and popolate the Photo array
}

private final class GalleryAdapter extends BaseAdapter {
        private Context mContext;
        private ArrayList<Photo> images = new ArrayList<Photo>();

}

public GalleryAdapter(Context c, ArrayList<Photo> gallery) {
            mContext = c;
            images = gallery;
}

@Override
        public int getCount() {
            return images.size();
        }

        
        @Override
        public Photo getItem(int i) {
            return images.get(i);
        }

        @Override
        public long getItemId(int i) {
            return images.get(i).id;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent){
          ...
          ...
          ...
          ...

}

public class Photo{

    public int id;
    public String url;
    public String url_thumb;
    public String text = "";

    public Photo(int id, String url, String url_thumb, String text) {
        this.id= id;    
        this.url = url;
        this.url_thumb = url_thumb;
        this.text= text;
    }
}

画廊活动.xml

 <GridView
            android:id="@+id/galleryGridView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="auto_fit"
            android:gravity="center">

  </GridView>

layout_image_view.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#21bfaf">

    <ImageView
        android:id="@+id/imageId"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:scaleType="fitXY"/>

</RelativeLayout>

在单击缩略图时,我会打开一张全尺寸的照片。

谢谢,

标签: androidgridviewimageview

解决方案


推荐阅读