2015-11-06 117 views
0

我開發了一個帶滑動圖像功能的畫廊應用程序(向左/向右拖動並出現新的全屏圖像)。 問題是,我沒有成功克服大圖像的小延遲,直到它們出現(Whatsapp圖像 - 沒有問題,相機圖像不像在內置圖庫應用程序中那樣平滑)。 我嘗試了幾種重新調整大小/解碼圖像的方法,但仍然存在延遲。立即掃描大圖像

我的代碼:

FullScreenImageAdapter.java

public class FullScreenImageAdapter extends PagerAdapter { 

private Activity _activity; 
private ArrayList<String> _imagePaths; 
private LayoutInflater inflater; 
private final int IMAGE_MAX_SIZE = 800; 

// constructor 
public FullScreenImageAdapter(Activity activity, 
           ArrayList<String> imagePaths) { 
    this._activity = activity; 
    this._imagePaths = imagePaths; 
} 

@Override 
public int getCount() { 
    return this._imagePaths.size(); 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return view == ((RelativeLayout) object); 
} 

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    ImageView imgDisplay; 

    inflater = (LayoutInflater) _activity 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container, 
      false); 

    imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay); 

    Bitmap bitmap = decodeSampledBitmapFromPath(_imagePaths.get(position),1000,1000); 

    imgDisplay.setImageBitmap(bitmap); 

    ((ViewPager) container).addView(viewLayout); 

    return viewLayout; 
} 

@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    ((ViewPager) container).removeView((RelativeLayout) object); 

} 

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, 
               int reqHeight) { 

    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    options.inSampleSize = calculateInSampleSize(options, reqWidth, 
      reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    Bitmap bmp = BitmapFactory.decodeFile(path, options); 
    return bmp; 
} 

public static int calculateInSampleSize(BitmapFactory.Options options, 
             int reqWidth, int reqHeight) { 

    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     if (width > height) { 
      inSampleSize = Math.round((float) height/(float) reqHeight); 
     } else { 
      inSampleSize = Math.round((float) width/(float) reqWidth); 
     } 
    } 
    return inSampleSize; 
} 

}

FullScreenViewActivity.java

public class FullScreenViewActivity extends Activity{ 

private FullScreenImageAdapter adapter; 
private ViewPager viewPager; 
int selectedFilePosition; 
String imageFolder; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_full_screen_view); 

    //geting the selected image from the intent 
    /* Getting ImageURI from Gallery from Main Activity */ 
    Uri selectedImgUri = getIntent().getData(); 
    if (selectedImgUri!=null) { 
     imageFolder = getRealPathFromURI(this, selectedImgUri); 
    } 

    viewPager = (ViewPager) findViewById(R.id.pager); 

    adapter = new FullScreenImageAdapter(FullScreenViewActivity.this, getFilePaths(imageFolder)); 

    viewPager.setAdapter(adapter); 

    // displaying selected image first 
    viewPager.setCurrentItem(selectedFilePosition); 
} 

// Reading file paths from SDCard 
public ArrayList<String> getFilePaths(String selectedFilePath) { 
    ArrayList<String> filePaths = new ArrayList<String>(); 

    File file = new File(selectedFilePath); 
    File directory = file.getParentFile(); 

    // check for directory 
    if (directory.isDirectory()) { 
     // getting list of file paths 
     File[] listFiles = directory.listFiles(); 

     // Check for count 
     if (listFiles.length > 0) { 
       // loop through all files 
       for (int i = 0; i < listFiles.length; i++) { 
        // get file path 
        String filePath = listFiles[i].getAbsolutePath(); 
        //set position if this is the required image to show 
        if (filePath.equals(selectedFilePath)) { 
         selectedFilePosition = i; 
         // Add image path to array list 
         filePaths.add(filePath); 
        } 
       } 
     } 

    } 
    return filePaths; 
} 

public String getRealPathFromURI(Context context, Uri contentUri) { 
    Cursor cursor = null; 
    try { 
     String[] proj = { MediaStore.Images.Media.DATA }; 
     cursor = context.getContentResolver().query(contentUri, proj, null, null, null); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
} 

}

回答

1

爲了提高性能,您需要另一個線程來加載它的異步。以下是第三方庫(Picasso,Glide),您可以在處理大圖像時使用這些庫。

1

我也會使用畢加索將圖像加載到ImageView中。如果加載一次,則Picasso緩存圖像。要預加載一些元素,您可以使用Consumner/Producer Pattern!示例here

+0

我實現了畢加索,性能略勝一籌,但我認爲問題在於預加載的圖像數量。 – Andy

+0

目前,viewpager正在加載所需圖像並預加載另外2個圖像(一個位於所需圖像的左側和另一個的右側)。滑動至預加載的圖像會有所反應,但在向相同方向滑動數次時會出現問題。有沒有一種方法可以通過viewpager自動預載2個以上的圖像? – Andy