Android BitmapRegionDecoder——大图、长图展示

-. 类介绍
-. 示例
项目地址:项目地址包含之前的内容
效果图

类介绍

  1. 作用
public final class BitmapRegionDecoder {
 private BitmapRegionDecoder(long decoder) {
      	...
    }
}

BitmapRegionDecoder 类被final进行修饰,因此无法被继承。构造方法被私有化,无法直接new对象。

BitmapRegionDecoder 用于从图片中截取一个矩形区域来展示。原图很大,但是我们只能看到屏幕内的,这个类就可以帮我只加载一个屏幕内的大小的图片。

  1. 使用方式
    BitmapRegionDecoder 提供了一系列newInstance,最终都是通过数据流来得到对象。很好理解,加载图片肯定要有图片文件才行。
 public Bitmap decodeRegion(Rect rect, BitmapFactory.Options options) {}

根据decodeRegion方法获得Bitmap对象。最终展示的位图。rect是一个矩形区域。options一些参数信息。

示例

   /**
     * 设置数据源
     *
     * @param mInputStream
     */
    public void setmInputStream(InputStream mInputStream) {
        Log.e(TAG, "setmInputStream");
        if (mOptions == null) {
            mOptions = new BitmapFactory.Options();
        }
        //设置参数,不需要返回bitma
        mOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(mInputStream, null, mOptions);
        //传入的流确定图片尺寸
        outWidth = mOptions.outWidth;
        outHeight = mOptions.outHeight;
        mCenterX = outWidth / SHOW_DEFAULT_DIVSOR;
        mCenterY = outHeight / SHOW_DEFAULT_DIVSOR;

        //实例化初始展示范围
        mRect = new Rect();

        try {
            /**
             * 实例化mDecoder
             */
            mDecoder = BitmapRegionDecoder.newInstance(mInputStream, true);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "IOException:  " + e.getMessage());
        }

        //实例化矩阵
        mMatrix = new Matrix();
    }

...
 @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e(TAG, "onDraw");
        if (mDecoder == null) {
            return;
        }

        if (mOptions == null) {
            return;
        }

        if (mOptions.inJustDecodeBounds) {
            mOptions.inJustDecodeBounds = false;
        }
        //得到bitmap对象
        mBitmap = mDecoder.decodeRegion(mRect, mOptions);

        canvas.drawBitmap(mBitmap, mMatrix, null);
    }

自定义了一个展示大图的view,上面是核心部分。设置数据源,并得到BitmapRegionDecoder对象,然后在onDraw方法中画出矩形内的Bitmap。

图中可以看出,放大的时候是从屏幕左上角向右下角进行的,导致最后多出的部分看不到。肯定是我的方法有问题,哪位大佬指教一下。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值