2014-09-04 69 views
1

我正在使用來自YouTube的服務,它向我返回一個URL。通過這個URL,我可以繼續使用NetworkImageView。問題是,圖像不佔用所有我定義字段NetworkImageViewVolley不能正確調整圖像大小

我的URL

https://i.ytimg.com/vi/2HA41DV_K7s/hqdefault.jpg 

XML

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <com.android.volley.toolbox.NetworkImageView 
      android:id="@+id/banner_channel" 
      android:layout_width="120dp" 
      android:layout_height="90dp" 
      android:scaleType="centerCrop" 
      android:layout_marginLeft="15dp" 
      android:layout_marginTop="10dp" /> 
</LinearLayout> 

在我的適配器

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

     Context ctx = parent.getContext(); 
     if (convertView == null) { 
      convertView = LayoutInflater.from(ctx).inflate(LAYOUT, null); 
     } 
     NetworkImageView img = (NetworkImageView) convertView.findViewById(R.id.banner_channel); 


     img.setImageUrl(mVideoList.items.get(position).snippet.thumbnails.high.url, VolleySingleton.getInstance(
       ctx).getImageLoader()); 
     img.setDefaultImageResId(R.drawable.channel_thumb_default); 

     return convertView; 
    } 

類的空間緩存

/** 
* Created by Douglas on 24/08/2014. 
*/ 
public class VolleySingleton { 

    private static VolleySingleton mInstance = null; 
    // Fila de execução 
    private RequestQueue mRequestQueue; 
    // Image Loader 
    private ImageLoader mImageLoader; 

    private VolleySingleton(Context context) { 
     mRequestQueue = Volley.newRequestQueue(context); 
     mImageLoader = new ImageLoader(this.mRequestQueue, new ImageLoader.ImageCache() { 
      private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10); 

      public void putBitmap(String url, Bitmap bitmap) { 
       mCache.put(url, bitmap); 
      } 

      public Bitmap getBitmap(String url) { 
       return mCache.get(url); 
      } 
     }); 
    } 

    public static VolleySingleton getInstance(
      Context context) { 

     if (mInstance == null) { 
      mInstance = new VolleySingleton(context); 
     } 
     return mInstance; 
    } 

    public RequestQueue getRequestQueue() { 
     return this.mRequestQueue; 
    } 

    public ImageLoader getImageLoader() { 
     return this.mImageLoader; 
    } 
} 

圖片我的名單

http://i.imgur.com/gsEkt9R.png

+0

的什麼是你希望看到的是沒有發生多少?對我來說,就像120x90調整圖像大小一樣。 – jlmcdonald 2014-09-04 01:15:35

回答

1

它工作正常的。 letterboxing效果(頂部和底部的黑色條)是您獲取圖像的一部分。

編輯:

創建與原始圖像,但沒有黑條的位圖,使用

Bitmap android.graphics.Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height) 

其中

源 - 原始位與黑條

X - 源中第一個像素的x座標

Ÿ - 源第一像素的y座標

寬度 - 像素數每排

高度 - 行

+0

有可能我刪除這個黑條? – 2014-09-04 01:28:52

+1

當然可以。分析圖像並檢查所有圖像中的黑條是否大小一致,然後在位圖上進行一些簡單的圖像裁剪(請參閱我的答案中的編輯) – josephus 2014-09-04 04:17:36