图库始终将图像保存在视图中?

时间:2011-07-28 01:25:03

标签: android gallery

是否始终将图像保留在图库集中?

除非在屏幕外或图像上被用户滑出视图时重新加载,是否可以将图像设置为图像永远不会重新加载的位置,除非应用程序已关闭?

编辑:如何将此图像附加到Horizo​​ntalScrollView?

 public class ImageAdapter extends BaseAdapter {
                /** The parent context */
                private Context myContext;public ImageAdapter() {
                    // TODO Auto-generated constructor stub
                }
                /** URL-Strings to some remote images. */

                public String[] myRemoteImages = {imageUrl,imageUrl2,imageUrl3,imageUrl4};






                /** Simple Constructor saving the 'parent' context. */
                public ImageAdapter(Context c) { this.myContext = c; }





                /** Returns the amount of images we have defined. */
                public int getCount() { 
                    return 10000;
                }

                /* Use the array-Positions as unique IDs */
                public Object getItem(int position) { 
                    return position; }
                public long getItemId(int position) { 
                    return position; 
                    }

                /** Returns a new ImageView to
                * be displayed, depending on
                * the position passed. */
                public View getView(int position, View convertView, ViewGroup parent) {
                ImageView i = new ImageView(this.myContext);



                try {

                                URL aURL = new URL(myRemoteImages[position]);
                                URLConnection conn = aURL.openConnection();
                                conn.setUseCaches(true);
                                conn.connect();
                                InputStream is = conn.getInputStream();
                                /* Buffered is always good for a performance plus. */
                                BufferedInputStream bis = new BufferedInputStream(is);
                                /* Decode url-data to a bitmap. */
                                Bitmap bm = BitmapFactory.decodeStream(bis);
                                bis.close();
                                is.close();
                                Log.v(imageUrl, "Retrieving image");

                                /* Apply the Bitmap to the ImageView that will be returned. */
                                i.setImageBitmap(bm);

                        } catch (IOException e) {



                                Log.e("DEBUGTAG", "Remtoe Image Exception", e);





                /* Image should be scaled as width/height are set. */
                i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                /* Set the Width/Height of the ImageView. */
                if(Build.VERSION.SDK_INT >= 11){
                    i.setLayoutParams(new Gallery.LayoutParams(450, 300));
                return i;
                }
                else{
                    i.setLayoutParams(new Gallery.LayoutParams(125, 125));
                    return i;
                }
                        }
                return i;
                }




                /** Returns the size (0.0f to 1.0f) of the views
                * depending on the 'offset' to the center. */
                public float getScale(boolean focused, int offset) {
                /* Formula: 1 / (2 ^ offset) */
                return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
                }
                }


((Gallery) findViewById(R.id.gallery))
                          .setAdapter(new ImageAdapter(MainMenu.this));

2 个答案:

答案 0 :(得分:0)

AdapterView s的目的正是你的“不受欢迎的行为”,尽可能少的内容加载到内存中。真正的解决方案是将您的图像缓存在磁盘中,这样您就不必再次检索它们。除非您已经从磁盘中检索它们,否则我会问,为什么要这样?

编辑:

当然这里的人都知道如何缓存,也许你没有问正确的问题?是的,你肯定想要实现一个缓存。无论如何,缓存Bitmap s:

private void putBitmapInDiskCache(Uri url, Bitmap avatar) {
    File cacheDir = new File(context.getCacheDir(), "thumbnails");
    File cacheFile = new File(cacheDir, ""+url.hashCode());
    try {
      cacheFile.createNewFile();
      FileOutputStream fos = new FileOutputStream(cacheFile);
      avatar.compress(CompressFormat.PNG, 100, fos);
      fos.flush();
      fos.close();
    } catch (Exception e) {
      Log.e(LOG_TAG, "Error when saving image to cache. ", e);
    }
  }

要阅读它们,它们是相似的:

  fis = new FileInputStream(cacheFile);
  Bitmap local = BitmapFactory.decodeStream(fis);

答案 1 :(得分:0)

如何使用Horizo​​ntalScrollView代替?但是如果你不断添加图像就要小心,那么很快就会出现内存异常。

此致

陈子腾

相关问题