安卓巨图加载及触摸拖动浏览,仿鸿洋大神使用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
- 安卓巨图加载及触摸拖动浏览,仿鸿洋大神使用BitmapRegionDecoder。
- Android使用BitmapRegionDecoder加载超大图片方案
- Android使用BitmapRegionDecoder加载超大图片方案
- Android开发——使用BitmapRegionDecoder类加载高清巨图方案
- 加载巨图不压缩(BitmapRegionDecoder, DestureDetetor)
- Android加载大图——BitmapRegionDecoder
- Windows Phone 触摸拖动
- 触摸[2] 拖动
- 基于zepto的H5/移动端tab切换触摸拖动加载更多数据
- Android2.3使用BitmapRegionDecoder获取指定区域的图片
- Android使用BitmapRegionDecoder获取指定区域的图片
- android开发之图片的浏览、缩放、拖动及自动居中
- Android中大图片加载及快速浏览处理
- 使用handler使用图片轮播及触摸停止
- JavaScript: 让拖动支持iphone/ipad触摸
- JavaScript: 让拖动支持iphone/ipad触摸
- U3D判断触摸拖动的方向
- 高仿微信图片放大拖动浏览,自定义ImageView
- 目标跟踪VOT2016 BenchMark评价标准介绍
- 归并排序 单链表
- Redis的几种数据结构,和各自应用场景的列举示例
- 【c++小程序】1.动态数组实现图书录入程序
- Xposed 包名类名加密要如何hook?多dex如何查找对应的方法?
- 安卓巨图加载及触摸拖动浏览,仿鸿洋大神使用BitmapRegionDecoder。
- oracle11g 修改字符集 修改为ZHS16GBK
- 如何利用缺陷的管理提高软件开发质量三 -如何利用时间点发现缺陷数
- MySQL错误ERROR 2002 (HY000): Can't connect to local MySQL server through socket
- MySQL常用的面试题
- 60_数组_多维数组_基本语法_内存分析
- Linux 下安装Redis异常处理及安装
- 5.2 窗函数法设计FIR
- 51单片机学习第二天