Skip to content

Instantly share code, notes, and snippets.

@laurencedawson
Created October 30, 2013 11:53
Show Gist options
  • Save laurencedawson/7231390 to your computer and use it in GitHub Desktop.
Save laurencedawson/7231390 to your computer and use it in GitHub Desktop.
// Create the resampling runnable
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && mSample == null && sampleName!=null ){
mSample = new Runnable() {
@Override
public void run() {
try{
// Only decode if the pointer is not touching
if(mTouchStatus!=MotionEvent.ACTION_DOWN){
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(sampleName, options);
options.inJustDecodeBounds = false;
float top = getValue(mMatrix, Matrix.MTRANS_Y);
float left = getValue(mMatrix, Matrix.MTRANS_X);
float scale = getValue(mMatrix, Matrix.MSCALE_X);
// The adjusted scale of the current image showing
float sampled_scale = ((float)mBitmap.getWidth()/(float)options.outWidth);
sampled_scale *= scale;
// The position of the top of the loading rect
float loaded_top = -top;
loaded_top /= sampled_scale;
// The position of the bottom of the loading rect
float loaded_bottom = (float) getHeight() / (float) sampled_scale;
loaded_bottom += loaded_top;
loaded_bottom = Math.min(loaded_bottom, options.outHeight);
loaded_bottom = Math.max(0.f, loaded_bottom);
// The position of the left of the loading rect{
float loaded_left = -left;
loaded_left /= sampled_scale;
// The position of the right of the loading rect
float loaded_right = (float) getWidth() / (float) sampled_scale;
loaded_right += loaded_left;
loaded_right = Math.min(loaded_right, (float) options.outWidth);
loaded_right = Math.max(0.f, loaded_right);
// Create the bounding box for loading the bitmap
Rect rect = new Rect(
(int) ( loaded_left < 0 ? 0 : loaded_left),
(int) ( loaded_top < 0 ? 0 : loaded_top),
(int) ( loaded_left < 0 ? options.outWidth : loaded_right ),
(int) ( loaded_top < 0 ? options.outHeight : loaded_bottom ) );
// Check if the sample image is smaller than the max texture size
if(((loaded_right-loaded_left) < 2048) && ((loaded_bottom-loaded_top) < 2048)){
// Load a section of the image
FileInputStream fis = new FileInputStream(sampleName);
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(fis,false);
mSampledBitmap = decoder.decodeRegion(rect,options);
mSampledMatrix = new Matrix();
mSampledMatrix.postScale(sampled_scale,sampled_scale);
mSampledMatrix.postTranslate(
( loaded_left < 0 ? left : 0 ),
( loaded_top < 0 ? top : 0 ) );
// Recycle the decoder
decoder.recycle();
fis.close();
}
}
}catch(Exception e){
// e.printStackTrace();
}
if(mSampledBitmap!=null)
invalidate();
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment