安卓巨图加载及触摸拖动浏览,仿鸿洋大神使用BitmapRegionDecoder。

来源:互联网 发布:淘宝上代购靠谱的店铺 编辑:程序博客网 时间:2024/06/08 01:05

在加载大图的时候经常会OOM,因此找了资料,鸿洋大神的这篇用的最好:http://blog.csdn.net/lmj623565791/article/details/49300989

使用了BitmapRegionDecoder的相关方法,只显示需要显示的部分,并且封装了一个手势控制的类。

我本人技术不高,对手势控制的封装类用起来有些吃力,所有自己写了个用touch控制的拖动方法,以便新手都可以看懂。代码如下:

首先在资源文件assets中加入qm.jpg图

1、代码:

public class LargeImageActivity extends AppCompatActivity {      private Context mContext;    ImageView mImageView;      String path = "file:///android_asset/qm.jpg";       BitmapRegionDecoder mDecoder;    Rect mRect = new Rect();    BitmapFactory.Options mOptions = new BitmapFactory.Options();    int x0 = 0;    int y0 = 0;    private int mImageWidth, mImageHeight;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.c_largeimage);        mContext = this;        mOptions.inPreferredConfig = Bitmap.Config.RGB_565;               mImageView = (ImageView) findViewById(R.id.i1);                               InputStream inputStream = getAssets().open("qm.jpg");                    mDecoder = BitmapRegionDecoder.newInstance(inputStream, false);                    BitmapFactory.Options tmpOptions = new BitmapFactory.Options();                    tmpOptions.inJustDecodeBounds = true;                    BitmapFactory.decodeStream(inputStream, null, tmpOptions);                    mImageHeight = tmpOptions.outHeight;                    mImageWidth = tmpOptions.outWidth;                    mRect.left = 0;                    mRect.top = 0;                    mRect.right = mImageView.getWidth();                    mRect.bottom = mImageView.getHeight();                    Bitmap bitmap = mDecoder.decodeRegion(mRect, mOptions);                    mImageView.setImageBitmap(bitmap);                      mImageView.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                int action = event.getAction();                switch (action) {                    case MotionEvent.ACTION_DOWN:                        x0 = (int) event.getRawX();                        y0 = (int) event.getRawY();                        break;                    case MotionEvent.ACTION_MOVE:                        int dx = (int) event.getRawX() - x0;                        int dy = (int) event.getRawY() - y0;                        mRect.offset(-dx, -dy);                        int left = mRect.left;                        int top = mRect.top;                        int right = mRect.right;                        int bottom = mRect.bottom;                        if (left < 0) {                            left = 0;                            right = mImageView.getWidth();                        }                        if (top < 0) {                            top = 0;                            bottom = mImageView.getHeight();                        }                        if (right > mImageWidth) {                            right = mImageWidth;                            left = right - mImageView.getWidth();                        }                        if (bottom > mImageHeight) {                            bottom = mImageHeight;                            top = bottom - mImageView.getHeight();                        }                        mRect.set(left, top, right, bottom);                        Bitmap bm = mDecoder.decodeRegion(mRect, mOptions);                        mImageView.setImageBitmap(bm);                        x0 = (int) event.getRawX();                        y0 = (int) event.getRawY();                        break;                    case MotionEvent.ACTION_UP:                        break;                }                return true;            }        });    }}

2、布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="5dp">        <ImageView        android:id="@+id/i1"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>






阅读全文
0 0
原创粉丝点击