Android使用BitmapRegionDecoder加载超大图片方案
来源:互联网 发布:男人眼中的女神 知乎 编辑:程序博客网 时间:2024/05/29 16:34
BitmapRegionDecoder
主要用于显示图片的某一块矩形区域,如果你需要显示某个图片的指定区域,那么这个类非常合适。
对于该类的用法,非常简单,既然是显示图片的某一块区域,那么至少只需要一个方法去设置图片;一个方法传入显示的区域即可;详见:
BitmapRegionDecoder提供了一系列的newInstance方法来构造对象,支持传入文件路径,文件描述符,文件的inputstrem等。
例如:
- BitmapRegionDecoder bitmapRegionDecoder =
- BitmapRegionDecoder.newInstance(inputStream, false);
上述解决了传入我们需要处理的图片,那么接下来就是显示指定的区域。
- bitmapRegionDecoder.decodeRegion(rect, options);
参数一很明显是一个rect,参数二是BitmapFactory.Options,你可以控制图片的inSampleSize
,inPreferredConfig
等。
那么下面看一个超级简单的例子:
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.BitmapRegionDecoder;
- import android.graphics.Rect;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.widget.ImageView;
-
-
- import java.io.IOException;
- import java.io.InputStream;
-
- public class LargeImageViewActivity extends AppCompatActivity
- {
- private ImageView mImageView;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_large_image_view);
-
- mImageView = (ImageView) findViewById(R.id.id_imageview);
- try
- {
- InputStream inputStream = getAssets().open("tangyan.jpg");
-
-
- BitmapFactory.Options tmpOptions = new BitmapFactory.Options();
- tmpOptions.inJustDecodeBounds = true;
- BitmapFactory.decodeStream(inputStream, null, tmpOptions);
- int width = tmpOptions.outWidth;
- int height = tmpOptions.outHeight;
-
-
- BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, false);
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inPreferredConfig = Bitmap.Config.RGB_565;
- Bitmap bitmap = bitmapRegionDecoder.decodeRegion(new Rect(width / 2 - 100, height / 2 - 100, width / 2 + 100, height / 2 + 100), options);
- mImageView.setImageBitmap(bitmap);
-
-
- } catch (IOException e)
- {
- e.printStackTrace();
- }
-
-
- }
-
- }
上述代码,就是使用BitmapRegionDecoder去加载assets中的图片,调用bitmapRegionDecoder.decodeRegion
解析图片的中间矩形区域,返回bitmap,最终显示在ImageView上。
根据上面的分析呢,我们这个自定义控件思路就非常清晰了:
- 提供一个设置图片的入口
- 重写onTouchEvent,在里面根据用户移动的手势,去更新显示区域的参数
- 每次更新区域参数后,调用invalidate,onDraw里面去regionDecoder.decodeRegion拿到bitmap,去draw
代码如下:
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.BitmapRegionDecoder;
- import android.graphics.Canvas;
- import android.graphics.Rect;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.view.View;
-
- import java.io.IOException;
- import java.io.InputStream;
-
- public class LargeImageView extends View
- {
- private BitmapRegionDecoder mDecoder;
-
-
-
- private int mImageWidth, mImageHeight;
-
-
-
- private volatile Rect mRect = new Rect();
-
- private MoveGestureDetector mDetector;
-
-
- private static final BitmapFactory.Options options = new BitmapFactory.Options();
-
- static
- {
- options.inPreferredConfig = Bitmap.Config.RGB_565;
- }
-
- public void setInputStream(InputStream is)
- {
- try
- {
- mDecoder = BitmapRegionDecoder.newInstance(is, false);
- BitmapFactory.Options tmpOptions = new BitmapFactory.Options();
-
- tmpOptions.inJustDecodeBounds = true;
- BitmapFactory.decodeStream(is, null, tmpOptions);
- mImageWidth = tmpOptions.outWidth;
- mImageHeight = tmpOptions.outHeight;
-
- requestLayout();
- invalidate();
- } catch (IOException e)
- {
- e.printStackTrace();
- } finally
- {
-
- try
- {
- if (is != null) is.close();
- } catch (Exception e)
- {
- }
- }
- }
-
-
- public void init()
- {
- mDetector = new MoveGestureDetector(getContext(), new MoveGestureDetector.SimpleMoveGestureDetector()
- {
- @Override
- public boolean onMove(MoveGestureDetector detector)
- {
- int moveX = (int) detector.getMoveX();
- int moveY = (int) detector.getMoveY();
-
- if (mImageWidth > getWidth())
- {
- mRect.offset(-moveX, 0);
- checkWidth();
- invalidate();
- }
- if (mImageHeight > getHeight())
- {
- mRect.offset(0, -moveY);
- checkHeight();
- invalidate();
- }
-
- return true;
- }
- });
- }
-
-
- private void checkWidth()
- {
-
-
- Rect rect = mRect;
- int imageWidth = mImageWidth;
- int imageHeight = mImageHeight;
-
- if (rect.right > imageWidth)
- {
- rect.right = imageWidth;
- rect.left = imageWidth - getWidth();
- }
-
- if (rect.left < 0)
- {
- rect.left = 0;
- rect.right = getWidth();
- }
- }
-
-
- private void checkHeight()
- {
-
- Rect rect = mRect;
- int imageWidth = mImageWidth;
- int imageHeight = mImageHeight;
-
- if (rect.bottom > imageHeight)
- {
- rect.bottom = imageHeight;
- rect.top = imageHeight - getHeight();
- }
-
- if (rect.top < 0)
- {
- rect.top = 0;
- rect.bottom = getHeight();
- }
- }
-
-
- public LargeImageView(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- init();
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent event)
- {
- mDetector.onToucEvent(event);
- return true;
- }
-
- @Override
- protected void onDraw(Canvas canvas)
- {
- Bitmap bm = mDecoder.decodeRegion(mRect, options);
- canvas.drawBitmap(bm, 0, 0, null);
- }
-
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
- {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
-
- int width = getMeasuredWidth();
- int height = getMeasuredHeight();
-
- int imageWidth = mImageWidth;
- int imageHeight = mImageHeight;
-
-
- mRect.left = imageWidth / 2 - width / 2;
- mRect.top = imageHeight / 2 - height / 2;
- mRect.right = mRect.left + width;
- mRect.bottom = mRect.top + height;
-
- }
-
-
- }
根据上述源码:
- setInputStream里面去获得图片的真实的宽度和高度,以及初始化我们的mDecoder
- onMeasure里面为我们的显示区域的rect赋值,大小为view的尺寸
- onTouchEvent里面我们监听move的手势,在监听的回调里面去改变rect的参数,以及做边界检查,最后invalidate
- 在onDraw里面就是根据rect拿到bitmap,然后draw了
ok,上面并不复杂,不过大家有没有注意到,这个监听用户move手势的代码写的有点奇怪,恩,这里模仿了系统的ScaleGestureDetector
,编写了MoveGestureDetector
,代码如下: