Android使用BitmapRegionDecoder获取指定区域的图片
2015-01-22 00:03
429 查看
Android系统处理图片方面并不怎么理想,稍不注意就会内存溢出。在处理大尺寸图片时,有时候只需要显示指定区域的图片,Android在2.3中提供了BitmapRegionDecoder类来解决此问题。
关于解决Bitmap加载内存溢出问题,可以查看Android使用BitmapFactory.Options解决加载大图片内存溢出问题。
转自:http://orgcent.com/android-bitmapregiondecoder-image/
关于解决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/
相关文章推荐
- Android2.3使用BitmapRegionDecoder获取指定区域的图片
- Android使用BitmapRegionDecoder加载超大图片方案
- Android使用BitmapRegionDecoder加载超大图片方案
- 举例说明android中Bitmap、ListView以及ImageView的综合使用---显示图片并获取图片的宽和高
- Android 从相机获取图片,存到指定文件进行编辑,bitmap总是为空
- Android BitmapRegionDecoder截取图片
- Android获取图片指定大小的缩略图--inJustDecodeBounds的使用
- 【Android Training - 09】高效地显示Bitmap图片 [ Lesson 5 - 使用ViewPager与GridView显示图片 ]
- Android获取图片大小以及获取指定大小的缩略图
- android获取网络图片的用法 BitmapFactory.decodeByteArray
- android - BitmapRegionDecoder 翻译
- [Android算法] bitmap 将图片压缩到指定的大小
- android2.3.3新类--BitmapRegionDecoder
- [Android] bitmap/drawable/byte的转换和获取圆角/倒影图片
- Android获取图片指定大小的缩略图
- Android图像处理之Bitmap 生成图片 解析 保存文件 获取 问题
- android中使用BitmapFactory的decodeStream()方法解码图片失败问题
- 转载 Android中使用BitmapFactory的decodeStream()方法解码图片失败问题
- Android获取图片大小以及获取指定大小的缩略图
- android中使用Canvas绘制指定位置和宽高度的图片