2012-07-26 9 views
12

私はアプリケーションがサーバーの画像を表示しています。今は私のイメージを怠惰に読み込みます。 最初に画像のロードが完了するとプログレスバーが表示されます。にvisibility属性を設定してプログレスバーを削除すると、画像が表示されます。これは私がイメージ画像が表示されているときにプログレスバーを表示して画像を読み込みません

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" > 

    <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center" 
      android:contentDescription="@string/app_name" 
      android:scaleType="centerInside" 
      android:src="@drawable/ic_launcher" /> 

    <RelativeLayout 
     android:id="@+id/loading_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@android:color/white" > 

     <ProgressBar 
      android:id="@+id/progressBar1" 
      style="?android:attr/progressBarStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:background="@android:color/white" /> 
    </RelativeLayout> 

</FrameLayout> 

問題プログレスバーが映像画面に表示されますが破損しているが消える に使用するレイアウトファイルです。この画像のように

私はリストをリフレッシュすると、キャッシュディレクトリから読み込まれた画像が画面に表示されたときに、破損することなく正しく表示されます。私は怠惰なロードイメージ

public class ImageLoader { 
    // @@ JUST FOR THIS PROJECT 
    BaseAdapter mAdapter; 
    // @@ 
    MemoryCache memoryCache = new MemoryCache(); 
    FileCache fileCache; 
    private Map<ImageView, String> imageViews = Collections 
      .synchronizedMap(new WeakHashMap<ImageView, String>()); 
    ExecutorService executorService; 

    boolean addRoundCournerAndStroke = false; 
    boolean scale = false; 

    boolean localfile = false; 
    int default_image; 

    public ImageLoader(Context context, boolean flag, boolean scale, 
      boolean localfile) { 
     fileCache = new FileCache(context); 
     this.addRoundCournerAndStroke = flag; 
     executorService = Executors.newFixedThreadPool(5); 
     this.scale = scale; 
     this.localfile = localfile; 

    } 

    public ImageLoader(Context context, boolean flag, boolean scale, 
      boolean localfile, int default_image_id) { 
     this(context, flag, scale, localfile); 
     this.default_image = default_image_id; 
    } 

    public ImageLoader(Context context, boolean flag, boolean scale, 
      boolean localfile, int default_image_id, BaseAdapter adapter) { 
     this(context, flag, scale, localfile); 
     this.default_image = default_image_id; 
     this.mAdapter = adapter; 
    } 

    public void DisplayImage(String url, ImageView imageView) { 
     imageViews.put(imageView, url); 
     Bitmap bitmap = memoryCache.get(url); 
     if (bitmap != null) { 
      changeProgressBarVisibilty(imageView, false); 
      imageView.setImageBitmap(bitmap); 
     } 

     else { 
      queuePhoto(url, imageView); 
      imageView.setImageResource(this.default_image); 
     } 
    } 

    private void queuePhoto(String url, ImageView imageView) { 
     PhotoToLoad p = new PhotoToLoad(url, imageView); 
     executorService.submit(new PhotosLoader(p)); 
    } 

    private Bitmap getBitmap(String url) { 
     File f = null; 
     if (localfile) 
      f = new File(url); 
     else 
      f = fileCache.getFile(url); 

     // Log.d("bytes", "decode"); 
     // from SD cache 
     Bitmap b = decodeFile(f); 
     if (b != null) 
      return b; 

     // from web 
     try { 
      Bitmap bitmap = null; 

      URL imageUrl = new URL(url); 
      HttpURLConnection conn = (HttpURLConnection) imageUrl 
        .openConnection(); 
      conn.setConnectTimeout(30000); 
      conn.setReadTimeout(30000); 
      conn.setInstanceFollowRedirects(true); 
      InputStream is = conn.getInputStream(); 
      OutputStream os = new FileOutputStream(f); 
      Utils.CopyStream(is, os); 
      os.close(); 

      bitmap = decodeFile(f); 
      // //Log.d("bytes", "decode"); 
      return bitmap; 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
      return null; 
     } 
    } 

    private Bitmap decodeFileWithoutScaling(File f) { 
     try { 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = 1; 
      //o2.inPurgeable = true; 
      if (this.localfile) 
       return BitmapFactory.decodeFile(f.getAbsolutePath(), o2); 
      else 
       return BitmapFactory.decodeStream(new FileInputStream(f), null, 
         o2); 
     } catch (FileNotFoundException e) { 
     } 
     return null; 
    } 

    private Bitmap decodeFile(File f) { 

     if (this.scale) { 
      return decodeFileWithScalling(f); 
     } else { 
      return decodeFileWithoutScaling(f); 
     } 
    } 

    // decodes image and scales it to reduce memory consumption 
    private Bitmap decodeFileWithScalling(File f) { 
     try { 
      // decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 

     // o.inPurgeable = true; 
      if (this.localfile) 
       BitmapFactory.decodeFile(f.getAbsolutePath(), o); 
      else 
       BitmapFactory.decodeStream(new FileInputStream(f), null, o); 

      // Find the correct scale value. It should be the power of 2. 
      final int REQUIRED_SIZE = 70; 
      int width_tmp = o.outWidth, height_tmp = o.outHeight; 
      // Log.d("width", width_tmp + ""); 
      // Log.d("height", height_tmp + ""); 
      int scale = 1; 
      while (true) { 
       if (width_tmp/2 < REQUIRED_SIZE 
         || height_tmp/2 < REQUIRED_SIZE) 
        break; 
       width_tmp /= 2; 
       height_tmp /= 2; 
       scale *= 2; 
      } 

      // decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
     // o2.inPurgeable = true; 
      // Log.d("after shave width", o2.outWidth + ""); 
      // Log.d("after shave height", o2.outHeight + ""); 
      if (this.localfile) 
       return BitmapFactory.decodeFile(f.getAbsolutePath(), o2); 
      else 
       return BitmapFactory.decodeStream(new FileInputStream(f), null, 
         o2); 
     } catch (FileNotFoundException e) { 
     } 
     return null; 
    } 

    // Task for the queue 
    private class PhotoToLoad { 
     public String url; 
     public ImageView imageView; 

     public PhotoToLoad(String u, ImageView i) { 
      url = u; 
      imageView = i; 
     } 
    } 

    class PhotosLoader implements Runnable { 
     PhotoToLoad photoToLoad; 

     PhotosLoader(PhotoToLoad photoToLoad) { 
      this.photoToLoad = photoToLoad; 
     } 

     public void run() { 
      if (imageViewReused(photoToLoad)) { 
       return; 
      } 

      Bitmap bmp = getBitmap(photoToLoad.url); 

      // if (addRoundCournerAndStroke) { 
      // // bmp = ImageHelper.rotateAndFrame(bmp, 10); 
      // bmp = ImageHelper.getRoundedCornerBitmap(bmp, 10); 
      // } 

      memoryCache.put(photoToLoad.url, bmp); 
      if (imageViewReused(photoToLoad)) 
       return; 
      BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad); 
      Activity a = (Activity) photoToLoad.imageView.getContext(); 
      a.runOnUiThread(bd); 
     } 
    } 

    boolean imageViewReused(PhotoToLoad photoToLoad) { 
     String tag = imageViews.get(photoToLoad.imageView); 
     if (tag == null || !tag.equals(photoToLoad.url)) 
      return true; 
     return false; 
    } 

    // Used to display bitmap in the UI thread 
    class BitmapDisplayer implements Runnable { 
     Bitmap bitmap; 
     PhotoToLoad photoToLoad; 

     public BitmapDisplayer(Bitmap b, PhotoToLoad p) { 
      bitmap = b; 
      photoToLoad = p; 
     } 

     public void run() { 
      if (imageViewReused(photoToLoad)) 
       return; 
      changeProgressBarVisibilty(photoToLoad.imageView, false); 
      if (bitmap != null) { 
       photoToLoad.imageView.setImageBitmap(bitmap); 


      } else { 
       photoToLoad.imageView 
         .setImageResource(ImageLoader.this.default_image); 

      } 
      if (mAdapter != null) { 
       mAdapter.notifyDataSetChanged(); 
      } 

     } 
    } 

    private void changeProgressBarVisibilty(ImageView image, boolean visible) { 
     ViewGroup layout = (ViewGroup) image.getParent(); 

     try { 
      View v = layout.findViewById(R.id.loading_layout); 
      v.setVisibility(visible ? View.VISIBLE : View.GONE); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 



    } 



    public void clearCache() { 
     memoryCache.clear(); 
     fileCache.clear(); 
    } 

} 
+2

画像を読み込むために[Universal Image Loader](https://github.com/nostra13/Android-Universal-Image-Loader)ライブラリを使用してください。おそらく、この問題はありません。 – NOSTRA

+0

プログレスバーを非表示にする代わりに、イメージが読み込まれると、イメージビューが表示されます。読み込みを開始する前に非表示にしてください... – Ronnie

答えて

5

に使用

enter image description here

ImageLoaderクラスは、ProgressDialogの代わりに、プログレスバーを使用してみてください。 ProgressDialogではプログレスバーも使用できます。 ProgressDialogはXML型ではありません。あなたはshow()メソッドまたはdismiss()メソッドを使うことができます。おそらく、この問題はありません。 私はこの問題がRelativeLayoutから来たと思います。 ProgressBar Visiblityを使用すると、RelativeLayoutはまだ可視です(背景はありません)。また、RelativeLayoutにはアイテムがあります。私は空のレイアウトがあなたに腐敗を与えたと思います。 ProgressDialogを使用するとベストです ...

-1

は、私はあなたが見るには、まだ場所を保持し、泰が言ったように、それはRelativeLayoutを持ついくつかの問題可能View.INVISIBLEを設定するときので、View.GONEとして可視性を設定しようとすることができると思います。

乾杯お返事

3

破損した画像は、破損したJPEGアーティファクトを示します。

BitmapFactory.decodeStreamメソッドから問題が発生することがあります。すべてのJPEGがオンザフライデコード(「プログレッシブ」)に最適化されているわけではありませんので、正しくデコードする前に全体として利用できる必要があります。通常、非プログレッシブ画像はトップダウンでデコードすることができますが、それはすべてのファイルで保持されない場合や、デコーダがそれを防ぐいくつかの欠陥を持つ可能性があります。

一方、イメージが後でキャッシュされたファイルからフェッチされた場合、ストリーム化されたデータを気にする必要のないBitmapFactory.decodeFileが使用され、イメージファイルにランダムにアクセスしてJPEGをデコードできます。イメージを読み込んでいる間に行単位の更新が必要ない場合は、このコードのパスを削除し、デコードする前にイメージ全体をダウンロードすることをお勧めします。

+0

フライデコードでも同様の問題がありました。一時ファイルとしてそれらを最初に格納することで問題を解決しました。 – stpn108

関連する問題