[layout]
<GridView
android:id="@+id/grid_view_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
[adapter]
public class MyImageAdapter extends BaseAdapter {
private Context mContext;
private int total_pic_cnt = 0;
public Bitmap image[];
public String select_func[];
public String js_url[];
// Constructor
public MyImageAdapter(Context c, String jsFileURL[], Bitmap pic[], String func[], int num){
mContext = c;
js_url = jsFileURL;
image = pic;
select_func = func;
total_pic_cnt = num;
}
@Override
public int getCount() {
return total_pic_cnt;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(image[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new GridView.LayoutParams(100, 70));
return imageView;
}
}
[new a adapter and start off the rendering]
MainActivity UIthread = (MainActivity)mContext;
UIthread.adapter = new MyImageAdapter(UIthread, jsFileURL, pic, onSelect_func, loaded_pic_cnt);
UIthread.rendering();
[download and convert an image]
pic[loaded_pic_cnt] = BitmapFactory.decodeStream(parser.getURLAsInputStream(n2_1_image));
[draw]
public void rendering() {
runOnUiThread(new Runnable() {
public void run() {
System.out.println("render it!");
selectMenu = (GridView)findViewById(R.id.grid_view_2);
selectMenu.setAdapter(adapter);
selectMenu.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
...
}
});
}
});
}
Reference links
沒有留言:
張貼留言