效果图:

在平时载入图片时,我会使用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. SVN与TortoiseSVN实战:补丁详解(转)

    硬广:<SVN与TortoiseSVN实战>系列已经写了五篇,第二篇<SVN与TortoiseSVN实战:标签与分支>和第三篇<SVN与TortoiseSVN实战:Tor ...

  2. hdu3974(线段树+dfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定点的上下级关系,规定如果给i分配任务a,那么他的所有下属.都停下手上的工作,开始做a. ...

  3. java性能缓慢

    虚拟帝国上面有很多营销软件是JAVA开发的!创业公司通常选择开源技术减少项目管理费用. 除了使用Java编程语言,创业公司也可以利用Java开发工具包的好处(JDK),Java运行时环境(JRE)和J ...

  4. A Game of Thrones(3) - Daenerys

    Her brother held the gown up for her inspection. “This is beauty. Touch it. Go on. Caress(爱抚,抚抱) the ...

  5. web.xml的运行顺序

    整体上的顺序为 <context-param> <listener> <filter> <servlet> 往下依次运行. 当中,每一个类别内部都是按序 ...

  6. VMware workstation 安装错误提示1021解决方法

    Failed to create the requested registry key Key: Installer Error: 1021 解决方法:删除注册表--HKEY_LOCAL_MACHIN ...

  7. ImageButton消除使用setImageDrawable造成的边框问题。

    使用ImageButton的时候,如果使用setBackgroundDrawable设置图片,会对可点击范围有影响,使用setImageDrawable方法设置图片,图片小的话,可以会有,这种有白色边 ...

  8. Qt学习一门:直接使用QT具

    今天,通过直接使用QT一些工具来编写命令行程序.你可以看到一种Qt更一般的用法. 内容很easy,输出电流日期. 首先,用一个QDate分类,可以使用QDate类的静态方法currentDate为了得 ...

  9. POJ 1146:ID Codes

    ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description ...

  10. Unity3D 游戏开发架构篇 ——性格一流的设计和持久性

    在游戏开发.游戏人物占了非常大的空间.所有内容都是由主角可以说游戏驱动. 下面来介绍一下一流的设计和持久性的作用. 一.应用场景 游戏中的角色类型不一而足,有不同的技能.有不同的属性等等.有些一个玩家 ...