效果图:

在平时载入图片时,我会使用SetImageBitmap、setImageResource、BitmapFactory.decodeResource来设置一张图

片通过以上方法来设置图片时。会通过Java层的createBitmap来完毕。这种话会消耗非常多内存。easy导致

OOM(Out Of Memory),因此推荐使用BitmapFactory.Options这个类来设置一张资源图。

參看下面代码:

public class MainActivity extends Activity {
private ImageView imageView1;
private ImageView imageView2;
Bitmap mBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
initView(); } private void initView(){
imageView1=(ImageView)findViewById(R.id.imageView1);
imageView2=(ImageView)findViewById(R.id.imageView2);
//读取资源图片
mBitmap=readBitMap();
//对资源图片进行缩放
imageView2.setImageBitmap(zoomBitmap(mBitmap, mBitmap.getWidth()/4, mBitmap.getHeight()/4));
} /**
* 读取资源图片
* @return
*/
private Bitmap readBitMap(){
BitmapFactory.Options opt=new BitmapFactory.Options();
/*
* 设置让解码器以最佳方式解码
*/
opt.inPreferredConfig=Bitmap.Config.RGB_565;
//以下两个字段须要组合使用
opt.inPurgeable=true;
opt.inInputShareable=true;
/*
* 获取资源图片
*/
InputStream is=this.getResources().openRawResource(R.drawable.mei);
return BitmapFactory.decodeStream(is, null, opt);
} /**
* 缩放图片
* @param bitmap
* @param w
* @param h
* @return
*/
public Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidht = ((float) w / width);
float scaleHeight = ((float) h / height);
/*
* 通过Matrix类的postScale方法进行缩放
*/
matrix.postScale(scaleWidht, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
return newbmp;
} }

image.xml:

<?xml version="1.0" encoding="utf-8"?

>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/mei" /> <ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:src="@drawable/mei" /> </RelativeLayout>

转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/44281087情绪控_

版权声明:本文博主原创文章,博客,未经同意不得转载。

一起学习android图像缩放资源 (27)的更多相关文章

  1. opencv学习笔记——图像缩放函数resize

    opencv提供了一种图像缩放函数 功能:实现对输入图像缩放到指定大小 函数原型: void cv::resize ( InputArray src, OutputArray dst, Size ds ...

  2. Android应用程序资源的查找过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8806798 我们知道,在Android系统中, ...

  3. OpenCV计算机视觉学习(11)——图像空间几何变换(图像缩放,图像旋转,图像翻转,图像平移,仿射变换,镜像变换)

    如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 图像 ...

  4. Android资源之图像资源(图像级别资源)

    图像状态资源仅仅能定义有限的几种状态. 假设须要很多其它的状态,就要使用图像级别资源. 在该资源文件里能够定义随意多个图像级别. 每一个图像级别是一个整数区间,能够通过ImageView.setIma ...

  5. Android应用程序资源的编译和打包过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8744683 我们知道,在一个APK文件中,除了 ...

  6. 一培训机构设计的学习android课程内容:供大家参考

    转自:http://www.cnblogs.com/csj007523/archive/2011/06/16/2082682.html 一培训机构设计的学习android课程内容:供大家参考 第一阶段 ...

  7. 一起来学习Android自定义控件1

    概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...

  8. Andorid-如何为你的Android应用缩放图片

    很难为你的应用程序得到正确的图像缩放吗?是你的图片过大,造成内存问题?还是图片不正确缩放造成不良用户体验的结果?为了寻求一个好的解决方案,我们咨询了Andreas Agvard(索尼爱立信软件部门), ...

  9. android学习——android架构

    android架构:在了解全局的情况下进行细致化的分析才能更有效的学习android的运行原理,才能更深刻的理解android开发: 1.架构图直观 2.架构详解 2.1.Linux Kernel 2 ...

随机推荐

  1. Linux IO 调度器

    Linux IO Scheduler(Linux IO 调度器) 每个块设备或者块设备的分区,都对应有自身的请求队列(request_queue),而每个请求队列都可以选择一个I/O调度器来协调所递交 ...

  2. thinkphp框架的相关总结

    参考链接地址:http://gongwen.sinaapp.com/article-205.html 1. 模板中不能使用的标签 {$content} {$i} 2. If标签 如: <if c ...

  3. openstack之nova-api服务流程分析

    nova-api公布api服务没实用到一个些框架,基本都是从头写的.在不了解它时,以为它很复杂,难以掌握.花了两三天的时间把它分析一遍后,发现它本身的结构比較简单,主要难点在于对它所使用的一些类库不了 ...

  4. socket用法

    INADDR_ANY是ANY,是绑定地址0.0.0.0上的监听, 能收到任意一块网卡的连接:INADDR_LOOPBACK, 也就是绑定地址LOOPBAC, 往往是127.0.0.1, 只能收到127 ...

  5. coco2dx c++ HTTP实现

    coco2dx c++ HTTP实现 达到的结果如下面的 iPhone截图 android 日志截图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdnBp ...

  6. Linux 该文件命令查看内容

    Linux系统,请使用以下命令来查看文件的内容: cat tac  从最后一行開始显示.能够看出 tac 是 cat 的倒著写! nl   显示的时候,顺道输出行号! more 一页一页的显示文件内容 ...

  7. Windows Phone开发(27):隔离存储A

    原文:Windows Phone开发(27):隔离存储A 在很多资料或书籍上都翻译为"独立存储",不过,我想了一下,决定将IsolatedStorage翻译为"隔离存储& ...

  8. Oracle SQL Lesson (5) - 使用组函数输出聚合数据

    组函数AVGCOUNTMAXMINSUMVARIANCE:方差STDDEV:标准差 SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary)F ...

  9. 给节点设置tag【从零開始cocos3.0final 】

    在cocos中通过tag来管理节点是非经常常使用的:以下介绍一个关于在cocos中使用tag的实例: typedef enum{ tag1 }Tag; 这里能够使用枚举类型,来为多个节点设置tag: ...

  10. Objective C (iOS) for Qt C++ Developers(iOS开发,Qt开发人员需要了解什么?)

    Qt/C++开发人员眼中的Obj-C      对于我们第一次自己定义iOS应用来说,对于来自Qt/C++开发人员来说,我不得不学习Objective-C相关语法与知识 为了让读者可以更easy理解这 ...