Android使用BitmapRegionDecoder获取指定区域的图片
来源:互联网 发布:网络女直播 编辑:程序博客网 时间:2024/05/16 11:20
Android系统处理图片方面并不怎么理想,稍不注意就会内存溢出。在处理大尺寸图片时,有时候只需要显示指定区域的图片,Android在2.3中提供了BitmapRegionDecoder类来解决此问题。
关于解决Bitmap加载内存溢出问题,可以查看Android使用BitmapFactory.Options解决加载大图片内存溢出问题。
public class DisplayImageRegionActivity extends Activity implements OnTouchListener { private final Rect mRect = new Rect(); private BitmapRegionDecoder mDecoder; private ImageView mView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mView = new ImageView(this); mView.setAdjustViewBounds(true); mView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); mView.setScaleType(ScaleType.CENTER); mView.setOnTouchListener(this); setContentView(mView); try { InputStream is = getResources().openRawResource(R.drawable.a); mDecoder = BitmapRegionDecoder.newInstance(is, true); } catch (IOException e) { e.printStackTrace(); } } @Override public boolean onTouch(View v, MotionEvent event) { final int action = event.getAction() & MotionEvent.ACTION_MASK; final int x = (int) event.getX(); final int y = (int) event.getY(); switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: setImageRegion(x, y); break; } return true; } private void setImageRegion(int left, int top) {// BitmapFactory.Options opts = new BitmapFactory.Options(); final int width = mView.getWidth(); final int height = mView.getHeight(); final int imgWidth = mDecoder.getWidth(); final int imgHeight = mDecoder.getHeight(); int right = left + width; int bottom = top + height; if(right > imgWidth) right = imgWidth; if(bottom > imgHeight) bottom = imgHeight; if(left < 0) left = 0; if(top < 0) top = 0; mRect.set(left, top, right, bottom); Bitmap bm = mDecoder.decodeRegion(mRect, null); mView.setImageBitmap(bm); }}
转自:http://orgcent.com/android-bitmapregiondecoder-image/
0 0
- Android使用BitmapRegionDecoder获取指定区域的图片
- Android2.3使用BitmapRegionDecoder获取指定区域的图片
- Android使用BitmapRegionDecoder加载超大图片方案
- Android使用BitmapRegionDecoder加载超大图片方案
- Android BitmapRegionDecoder截取图片
- MFC获取当前窗口指定区域的图片
- Android获取图片指定大小的缩略图--inJustDecodeBounds的使用
- 拉伸图片的指定区域
- Android获取图片指定大小的缩略图
- Android获取图片指定大小的缩略图
- Android获取图片指定大小的缩略图
- Android获取图片指定大小的缩略图
- 【android】获取指定目录下的图片
- Android中实现绘制图片指定区域
- 截取指定区域的并保存图片
- 指定区域显示图片
- Android开发——使用BitmapRegionDecoder类加载高清巨图方案
- android - BitmapRegionDecoder 翻译
- 字符串的处理
- 树状数组应用
- My97DatePicker无权限问题
- 播放系统音效、自定义音效工具类
- 稳定的存储环境决定业务正常运作
- Android使用BitmapRegionDecoder获取指定区域的图片
- osg::BillBoard的用法
- 【BZOJ 1618】 [Usaco2008 Nov]Buying Hay 购买干草
- ZooKeeper源码分析:Quorum请求的整个流程
- JavaScript 中的"类"与"实例"
- Android:不同Activity之间的数据传递 (简单的demo)
- select监听多个socket的例子
- 互联网分享希望
- Java 动态代理