Code example for BitmapRegionDecoder

Methods: getHeight, getWidth

1
 
	@Override 
	public boolean setData(final BitmapRegionDecoder decoder, final Bitmap bitmap, final int oroentation) {
		try { 
			if (decoder != null) {
				setScreenNail(bitmap, decoder.getWidth(), decoder.getHeight());
			} else { 
				if (bitmap == null) return false;
				setScreenNail(bitmap, bitmap.getWidth(), bitmap.getHeight());
			} 
			setRegionDecoder(decoder);
			mPhotoView.notifyImageChange();
			return true; 
		} catch (final Throwable t) {
			Log.w(TAG, "fail to decode large", t);
			return false; 
		} 
	} 
 
	private int calculateLevelCount() { 
		return Math.max(0, GalleryUtils.ceilLog2((float) mImageWidth / mScreenNail.getWidth()));
	} 
 
	private Bitmap getTileWithoutReusingBitmap(final int level, final int x, final int y, final int tileSize,
			final int borderSize) {
		final int b = borderSize << level;
		final int t = tileSize << level;
		final Rect wantRegion = new Rect(x - b, y - b, x + t + b, y + t + b);
 
		BitmapRegionDecoder regionDecoder;
		Rect overlapRegion;
 
		synchronized (this) {
			regionDecoder = mRegionDecoder;
			if (regionDecoder == null) return null;
			overlapRegion = new Rect(0, 0, mImageWidth, mImageHeight);
			GalleryUtils.assertTrue(overlapRegion.intersect(wantRegion));
		} 
 
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inPreferredConfig = Config.ARGB_8888;
		options.inPreferQualityOverSpeed = true;
		options.inSampleSize = 1 << level;
		Bitmap bitmap = null;
 
		// In CropImage, we may call the decodeRegion() concurrently. 
		synchronized (regionDecoder) {
			bitmap = regionDecoder.decodeRegion(overlapRegion, options);
		} 
 
		if (bitmap == null) {
			Log.w(TAG, "fail in decoding region");
		} 
 
		if (wantRegion.equals(overlapRegion)) return bitmap;
 
		final int s = tileSize + 2 * borderSize;
		final Bitmap result = Bitmap.createBitmap(s, s, Config.ARGB_8888);
		final Canvas canvas = new Canvas(result);
		canvas.drawBitmap(bitmap, overlapRegion.left - wantRegion.left >> level,
				overlapRegion.top - wantRegion.top >> level, null);
		return result;
	} 
 
	private synchronized void setRegionDecoder(final BitmapRegionDecoder decoder) {
		mRegionDecoder = decoder;
		if (decoder == null) return;
		mImageWidth = decoder.getWidth();
		mImageHeight = decoder.getHeight();
		mLevelCount = calculateLevelCount();
	} 
 
	private void setScreenNail(final Bitmap bitmap, final int width, final int height) {
		mBitmapScreenNail = new BitmapScreenNail(bitmap);