2014-07-06 2 views
0

이것은 처음 ListView에서 작업 한 내용입니다. 인터넷에서 이미지를 다운로드하고 디코드하고 크기를 조절하는 Imageloader 클래스가 있습니다 (parse.com에서 가져옴).). 전체 프로세스가 잘 작동하지만 이미지가 이미지 너비에 맞지 않으면 이미지가로드 될 때 ... 사용 된 것과 관계없이 전체 화면을 채우는 스퀘어 이미지가있는 스탁 그램 디스플레이와 비슷한 것을 얻으려고합니다. 장치.이미지 뷰에 맞게 다운로드 한 이미지의 크기를 조절하십시오

이 실제로 무엇을 가지고 : http://i.imgur.com/OMDyG8I.jpg 이 내가 원하는 무엇인가 : http://i.imgur.com/nGHADzK.jpg

은 내가 튜토리얼에있는 Imageloader.java을 사용하고 정말 정말 어떻게 작동하는지 understandd하지 않습니다. 그래서 사람들이 내게 설명하는 방법이 있다면 어떻게하면 내 목록보기에 정사각형 전체 화면 이미지를 적용 할 수 있습니다. 사전 :)에 감사

이 내 ImageLoader.java (지금은 내 레이아웃에 특별한 아무것도 ... 경기 - 부모의 높이와 witdh 만 이미지 뷰)입니다 : 모든

package com.hichamridouane.smartshop; 


import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
importenter code here java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.Collections; 
import java.util.Map; 
import java.util.WeakHashMap; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 

import android.os.Handler; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.widget.ImageView; 

public class ImageLoader { 
    MemoryCache memoryCache = new MemoryCache(); 
    FileCache fileCache; 
private Map<ImageView, String> imageViews = Collections 
     .synchronizedMap(new WeakHashMap<ImageView, String>()); 
ExecutorService executorService; 
// Handler to display images in UI thread 
Handler handler = new Handler(); 

public ImageLoader(Context context) { 
    fileCache = new FileCache(context); 
    executorService = Executors.newFixedThreadPool(5); 
} 

final int stub_id = R.drawable.temp_img; 

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

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 = fileCache.getFile(url); 

    Bitmap b = decodeFile(f); 
    if (b != null) 
     return b; 

    // Download Images from the Internet 
    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(); 
     conn.disconnect(); 
     bitmap = decodeFile(f); 
     return bitmap; 
    } catch (Throwable ex) { 
     ex.printStackTrace(); 
     if (ex instanceof OutOfMemoryError) 
      memoryCache.clear(); 
     return null; 
    } 
} 

// Decodes image and scales it to reduce memory consumption 
private Bitmap decodeFile(File f) { 
    try { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     FileInputStream stream1 = new FileInputStream(f); 
     BitmapFactory.decodeStream(stream1, null, o); 
     stream1.close(); 

     // Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE = 600; 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     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; 
     FileInputStream stream2 = new FileInputStream(f); 
     Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2); 
     stream2.close(); 
     return bitmap; 
    } catch (FileNotFoundException e) { 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    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; 
    } 

    @Override 
    public void run() { 
     try { 
      if (imageViewReused(photoToLoad)) 
       return; 
      Bitmap bmp = getBitmap(photoToLoad.url); 
      memoryCache.put(photoToLoad.url, bmp); 
      if (imageViewReused(photoToLoad)) 
       return; 
      BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad); 
      handler.post(bd); 
     } catch (Throwable th) { 
      th.printStackTrace(); 
     } 
    } 
} 

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; 
     if (bitmap != null) 
      photoToLoad.imageView.setImageBitmap(bitmap); 
     else 
      photoToLoad.imageView.setImageResource(stub_id); 
    } 
} 

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

답변

1

먼저, decodeFile() 메서드는 이미지 파일을 캐시에서 디코딩 한 다음 작은 비트 맵으로 크기를 조정하기 때문에 크기가 조정되지 않습니다. 따라서 레이아웃이나 ImageView에서 지정된 옵션없이 ImageView에 디코딩 된 비트 맵을로드하면 원래의 비율로 이미지가 표시됩니다.

은 내가 여기 제안하는 것입니다 :

  1. 이 레이아웃에 이미지 뷰의 당신의 너비와 높이를 수정합니다.

  2. 내가 광장에있는 모든 이미지 뷰 표시하려면이 사용자 정의 이미지 뷰를 사용하고 있습니다 (I 그것 때문에 디바이스의 화면 크기의 차이 작품 확실하지 않다) :

Custom Square ImageView

및 다음은 xml 레이아웃에서 사용하는 예입니다.

<your.package.to.ENESquareImageView 
    android:id="@+id/mini_cover" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:contentDescription="@null" 
    android:scaleType="centerCrop" /> 

희망이 있습니다.

+0

첫 번째 해결책은 화면 크기의 차이로 인해 적합하지 않습니다. 두 번째 해결책을 살펴 보겠습니다. 답장 btw에 감사드립니다. – RidRoid

+0

귀하의 요구 사항을 성공적으로 완료했는지 확인하십시오. –

+0

예. 고맙습니다 :) ^^ 화면을 채우기 위해 정사각형 이미지를 얻으려면 android : adjustViewBounds = "true"를 추가해야했습니다. – RidRoid

관련 문제