I have encounterd an performance issue. The scenario is that sometimes there are lots of pics on the picasa and i could get all the pics source image url and use Bitmap bm = BitmapFactory.decodeStream(is) to get the pic and display all these on one GridView. Gridview isn't response before all the pics were downloaded from interenet.
I am thinking of using multi-thread to increase the downloading speed, however, the user interaction is still not good. We normally see all the black screen for a long time, then all the pics showed up.
When we making the Swing app, there are some swingworker thread in the background help you to do so, which is a good user experience.
Btw, Pic size is not the problem, since i could add some filter condition to control the pics' size.
Here is some code snippet i wrote:
Using java Syntax Highlighting
- class PhotoAdapter extends BaseAdapter {
- private LayoutInflater mInflater;
- private List<Photo> mPhotos;
- public PhotoAdapter(Context context, List<Photo> photos) {
- mInflater = LayoutInflater.from(context);
- mPhotos = photos;
- }
- public int getCount() {
- return mPhotos.size();
- }
- public Object getItem(int position) {
- return position;
- }
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View conView, ViewGroup par) {
- ViewHolder holder;
- if (conView == null) {
- conView = mInflater.inflate(R.layout.photo, null);
- holder = new ViewHolder();
- holder.image = (ImageView)conView.findViewById(R.id.photo);
- // holder.image.setLayoutParams(new GridView.LayoutParams(85, 85));
- // holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
- // holder.image.setPadding(8, 8, 8, <img src="http://www.anddev.org/images/smilies/cool.png" alt="8)" title="Cool" />;
- conView.setTag(holder);
- } else {
- holder = (ViewHolder) conView.getTag();
- }
- Photo photo = mPhotos.get(position);
- URL url;
- try {
- url = new URL(photo.getThumbnailUrl());
- URLConnection conn = url.openConnection();
- conn.connect();
- InputStream is = conn.getInputStream();
- Bitmap bm = BitmapFactory.decodeStream(is);
- holder.image.setImageBitmap(bm);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return conView;
- }
- private class ViewHolder {
- ImageView image;
- }
- }
- public class ShowPhotosActivity extends Activity {
- List<Photo> photos = null;
- String keyword = "";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.showphotos);
- GridView showPhotoGridView = (GridView) findViewById(R.id.showPhotoGridView);
- Intent intent = this.getIntent();
- Bundle bundle = intent.getExtras();
- keyword = bundle.getString("keyword");
- photos = getPhotos(keyword);
- showPhotoGridView.setAdapter(new PhotoAdapter(this, photos));
- showPhotoGridView.setOnItemClickListener(
- new AdapterView.OnItemClickListener()
- {
- @Override
- public void onItemClick(AdapterView<?> arg0,View arg1,
- int arg2,long arg3)
- {
- Intent intent = new Intent();
- intent.setClass(ShowPhotosActivity.this, ShowPhotoActivity.class);
- Bundle bundle = new Bundle();
- bundle.putString("photoUrl",photos.get(arg2).getUrl());
- intent.putExtras(bundle);
- startActivity(intent);
- }
- });
- }
- private List<Photo> getPhotos(String keyword) {
- System.setProperty("http.proxyHost", "10.254.144.158");
- System.setProperty("http.proxyPort", "8080");
- List<Photo> photos = null;
- String path = "http://picasaweb.google.com/data/feed/api/all?kind=photo&q=" + keyword + "&max-results=9&imgmax=512&thumbsize=64";
- URL url = null;
- try {
- url = new URL(path);
- PhotoXmlHandler handler = new PhotoXmlHandler();
- Xml.parse(url.openConnection().getInputStream(),
- Xml.Encoding.UTF_8, handler);
- photos = handler.getPhotos();
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return photos;
- }
- @Override
- protected void onStart() {
- super.onStart();
- }
- @Override
- protected void onRestart() {
- super.onRestart();
- }
- @Override
- protected void onResume() {
- super.onResume();
- }
- @Override
- protected void onPause() {
- super.onPause();
- }
- @Override
- protected void onStop() {
- super.onStop();
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- }
- }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4
Please help on this. Thanks in advance.

