androidstudio资源4.2中为什么要高效的处理位图资源

博客分类:
尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完成decode后,最终都是通过java层的createBitmap来完成的,需要消耗更多内存。因此,改用先通过BitmapFactory.decodeStream方法,创建出一个bitmap,再将其设为ImageView的 source,decodeStream最大的秘密在于其直接调用JNI&&nativeDecodeAsset()来完成decode,无需再使用java层的createBitmap,从而节省了java层的空间。如果在读取时加上图片的Config参数,可以跟有效减少加载的内存,从而跟有效阻止抛out of Memory异常另外,decodeStream直接拿的图片来读取字节码了, 不会根据机器的各种分辨率来自动适应,&使用了decodeStream之后,需要在hdpi和mdpi,ldpi中配置相应的图片资源,&否则在不同分辨率机器上都是同样大小(像素点数量),显示出来的大小就不对了。另外,以下方式也大有帮助:1. InputStream is = this.getResources().openRawResource(R.drawable.pic1);&&&& BitmapFactory.Options options=new BitmapFactory.Options();&&&& options.inJustDecodeBounds =&&&& options.inSampleSize = 10;&& //width,hight设为原来的十分一&&&& Bitmap btp =BitmapFactory.decodeStream(is,null,options);2. if(!bmp.isRecycle() ){&&&&&&&& bmp.recycle()&& //回收图片所占的内存&&&&&&&& system.gc()& //提醒系统及时回收}以下奉上一个方法:Java代码&& 1. /**&& 2.& * 以最省内存的方式读取本地资源的图片&& 3.& * @param context&& 4.& * @param resId&& 5.& * @return&& 6.& */ &&& 7. public static Bitmap readBitMap(Context context, int resId){ &&& 8.&&&& BitmapFactory.Options opt = new BitmapFactory.Options(); &&& 9.&&&& opt.inPreferredConfig = Bitmap.Config.RGB_565;& && 10.&&&& opt.inPurgeable = && 11.&&&& opt.inInputShareable = && 12.&&&&&&& //获取资源图片 && 13.&&&& InputStream is = context.getResources().openRawResource(resId); && 14.&&&&&&&& return BitmapFactory.decodeStream(is,null,opt); && 15. }================================================================================Android内存溢出的解决办法转自:http://www.cppblog.com/iuranus/archive//124394.html?opt=admin昨天在模拟器上给gallery放入图片的时候,出现java.lang.OutOfMemoryError: bitmap size exceeds VM budget 异常,图像大小超过了RAM内存。&&&&& 模拟器RAM比较小,只有8M内存,当我放入的大量的图片(每个100多K左右),就出现上面的原因。由于每张图片先前是压缩的情况,放入到Bitmap的时候,大小会变大,导致超出RAM内存,具体解决办法如下://解决加载图片 内存溢出的问题&&&&&&&&&&&&&&&&&&& //Options 只保存图片尺寸大小,不保存图片到内存&&&&&&&&&&&&&&& BitmapFactory.Options opts = new BitmapFactory.Options();&&&&&&&&&&&&&&& //缩放的比例,缩放是很难按准备的比例进行缩放的,其值表明缩放的倍数,SDK中建议其值是2的指数值,值越大会导致图片不清晰&&&&&&&&&&&&&&& opts.inSampleSize = 4;&&&&&&&&&&&&&&& Bitmap bmp =&&&&&&&&&&&&&&& bmp = BitmapFactory.decodeResource(getResources(), mImageIds[position],opts);&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&& ...&&&&&&&&&&&& &&&&&&&&&&&&&&& //回收&&&&&&&&&&&&&&& bmp.recycle();通过上面的方式解决了,但是这并不是最完美的解决方式。通过一些了解,得知如下:优化Dalvik虚拟机的堆内存分配对 于Android平台来说,其托管层使用的Dalvik Java VM从目前的表现来看还有很多地方可以优化处理,比如我们在开发一些大型游戏或耗资源的应用中可能考虑手动干涉GC处理,使用 dalvik.system.VMRuntime类提供的setTargetHeapUtilization方法可以增强程序堆内存的处理效率。当然具体 原理我们可以参考开源工程,这里我们仅说下使用方法:&& private final static float TARGET_HEAP_UTILIZATION = 0.75f; 在程序onCreate时就可以调用 VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION); 即可。Android堆内存也可自己定义大小&&& 对于一些Android项目,影响性能瓶颈的主要是Android自己内存管理机制问题,目前手机厂商对RAM都比较吝啬,对于软件的流畅性来说RAM对 性能的影响十分敏感,除了 优化Dalvik虚拟机的堆内存分配外,我们还可以强制定义自己软件的对内存大小,我们使用Dalvik提供的 dalvik.system.VMRuntime类来设置最小堆内存为例:private final static int CWJ_HEAP_SIZE = 6*
;VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE); //设置最小heap内存为6MB大小。当然对于内存吃紧来说还可以通过手动干涉GC去处理bitmap 设置图片尺寸,避免 内存溢出 OutOfMemoryError的优化方法★android 中用bitmap 时很容易内存溢出,报如下错误:Java.lang.OutOfMemoryError : bitmap size exceeds VM budget● 主要是加上这段:BitmapFactory.Options options = new BitmapFactory.Options();&&&&&&&&&&&&&&& options.inSampleSize = 2;● eg1:(通过Uri取图片)private ImageVBitmapFactory.Options options = new BitmapFactory.Options();&&&&&&&&&&&&&&&&&&& options.inSampleSize = 2;//图片宽高都为原来的二分之一,即图片为原来的四分之一&&&&&&&&&&&&&&&&&&& Bitmap bitmap = BitmapFactory.decodeStream(cr&&&&&&&&&&&&&&&&&&&&&&&&&&& .openInputStream(uri), null, options);&&&&&&&&&&&&&&&&&&& preview.setImageBitmap(bitmap);以上代码可以优化内存溢出,但它只是改变图片大小,并不能彻底解决内存溢出。● eg2:(通过路径去图片)private ImageVprivate String fileName= "/sdcard/DCIM/Camera/ 16.01.44.jpg";BitmapFactory.Options options = new BitmapFactory.Options();&&&&&&&&&&&&&&& options.inSampleSize = 2;//图片宽高都为原来的二分之一,即图片为原来的四分之一&&&&&&&&&&&&&&&&&&&&&&& Bitmap b = BitmapFactory.decodeFile(fileName, options);&&&&&&&&&&&&&&&&&&&&&&& preview.setImageBitmap(b);&&&&&&&&&&&&&&&&&&&&&&& filePath.setText(fileName);★Android 还有一些性能优化的方法:●& 首先内存方面,可以参考 Android堆内存也可自己定义大小 和 优化Dalvik虚拟机的堆内存分配●& 基础类型上,因为Java没有实际的指针,在敏感运算方面还是要借助NDK来完成。Android123提示游戏开发者,这点比较有意思的是Google 推出NDK可能是帮助游戏开发人员,比如OpenGL ES的支持有明显的改观,本地代码操作图形界面是很必要的。●& 图形对象优化,这里要说的是Android上的Bitmap对象销毁,可以借助recycle()方法显示让GC回收一个Bitmap对象,通常对一个不用的Bitmap可以使用下面的方式,如if(bitmapObject.isRecycled()==false) //如果没有回收 &&&&&&&&& bitmapObject.recycle();& &●& 目前系统对动画支持比较弱智对于常规应用的补间过渡效果可以,但是对于游戏而言一般的美工可能习惯了GIF方式的统一处理,目前Android系统仅能预览GIF的第一帧,可以借助J2ME中通过线程和自己写解析器的方式来读取GIF89格式的资源。● 对于大多数Android手机没有过多的物理按键可能我们需要想象下了做好手势识别 GestureDetector 和重力感应来实现操控。通常我们还要考虑误操作问题的降噪处理。Android堆内存也可自己定义大小&& 对于一些大型Android项目或游戏来说在算法处理上没有问题外,影响性能瓶颈的主要是Android自己内存管理机制问题,目前手机厂商对RAM都比 较吝啬,对于软件的流畅性来说RAM对性能的影响十分敏感,除了上次Android开发网提到的 优化Dalvik虚拟机的堆内存分配外,我们还可以强制定义自己软件的对内存大小,我们使用Dalvik提供的 dalvik.system.VMRuntime类来设置最小堆内存为例:private final static int CWJ_HEAP_SIZE = 6*
;VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE); //设置最小heap内存为6MB大小。当然对于内存吃紧来说还可以通过手动干涉GC去处理,我们将在下次提到具体应用。优化Dalvik虚拟机的堆内存分配对 于Android平台来说,其托管层使用的Dalvik JavaVM从目前的表现来看还有很多地方可以优化处理,比如我们在开发一些大型游戏或耗资源的应用中可能考虑手动干涉GC处理,使用 dalvik.system.VMRuntime类提供的setTargetHeapUtilization方法可以增强程序堆内存的处理效率。当然具体 原理我们可以参考开源工程,这里我们仅说下使用方法:&& private final static floatTARGET_HEAP_UTILIZATION = 0.75f; 在程序onCreate时就可以调用 VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION); 即可。
介绍一下图片占用进程的内存算法吧。android中处理图片的基础类是Bitmap,顾名思义,就是位图。占用内存的算法如下:图片的width*height*Config。如果Config设置为ARGB_8888,那么上面的Config就是4。一张480*320的图片占用的内存就是480*320*4 byte。前面有人说了一下8M的概念,其实是在默认情况下android进程的内存占用量为16M,因为Bitmap他除了java中持有数据外,底层C++的 skia图形库还会持有一个SKBitmap对象,因此一般图片占用内存推荐大小应该不超过8M。这个可以调整,编译源代码时可以设置参数。
&/ 浏览 (1 / 1748)
Views(...) Comments()Android位图操作 - 1.曲待续 - 博客园
随笔 - 309, 文章 - 0, 评论 - 8, 引用 - 0
Bitmap是系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。对Android用户界面的设计,和对于Android
UI开发自绘控件和游戏制作而言掌握好位图基础是必不可少的。本次主要涉及以下的相关内容。本文从应用的角度,着重介绍怎么用Bitmap来实现这些功能。
一、位图主要操作步骤
(一)获取图片
(1).通过
BitmapDrawable 方式得到 Bitmap
InputStream is =res.openRawResource(R.drawable.picture);
BitmapDrawable bmpDraw= new BitmapDrawable(is);
Bitmap bmp =bmpDraw.getBitmap();
或BitmapDrawable bmpDraw =(BitmapDrawable)res.getDrawable(R.drawable.picture);
从资源文件中装载图片 */
//getResources()-&得到Resources
//getDrawable()-&得到资源中的Drawable对象,参数为资源索引ID
//getBitmap()-&得到Bitmap
mBitQQ =((BitmapDrawable) getResources().getDrawable
(R.drawable.qq)).getBitmap();
Bitmap bmp =bmpDraw.getBitmap();
(2).通过
BitmapFactory 得到 Bitmap
Bitmap bmp =BitmapFactory.decodeResource(res, R.drawable.picture);
BitmapFactory
可以通过资源ID、路径、文件、数据流等方式来获取位图。
可支持的图片格式如下:png、jpg、gif、bmp
//从资源文件中装载图片&getResources()-&得到Resources
//BitmapFactory
可以通过图像文件来获取位图
mBitmapTop = BitmapFactory.decodeResource(getResources(),R.drawable.desktop);
(3)用createBitmap(w,h,
Config.ARGB_8888) 得到屏幕上指定区域的位图
&&&&& int w = 240,h = 120;
String mstrTitle = &感受Android带给我们的新体验&;
&&& mbmpTest = Bitmap.createBitmap(w,h,Config.ARGB_8888);
Canvas canvasTemp = new Canvas(mbmpTest);
canvasTemp.drawColor(Color.WHITE);
Paint p = new Paint();
String familyName = &宋体&;
Typeface font = Typeface.create(familyName,Typeface.BOLD);
p.setColor(Color.RED);
p.setTypeface(font);
p.setTextSize(17);
canvasTemp.drawText(mstrTitle,0,30,p);
p.setColor(Color.YELLOW);
canvasTemp.drawRect(100,40,200,110, p);
canvas.drawBitmap(mbmpTest, 0, 180,
null);&&&&&&
(二)位图显示
显示到画布上:
Canvas 画布 drawbitmap
/* 在屏幕(left,top)处开始显示图片bitmap
&canvas.drawbitmap(Bitmap bitmap, float left,float top, Paint paint);
BitmapDrawable 绘制该图片到控件上&&
//bitmap.draw(canvas);
转换为BitmapDrawable对象显示位图
&&&&&&& Bitmapbmp=BitmapFactory.decodeResource(res, R.drawable.pic180);
&&&&&&& // 转换为BitmapDrawable对象
&&&&&&& BitmapDrawable bmpDraw=new BitmapDrawable(bmp);
&&&&&&& // 显示位图
&&&&&&& ImageView iv2 =(ImageView)findViewById(R.id.ImageView02);
&&&&&& iv2.setImageDrawable(bmpDraw);
(三)位图缩放
(1)重画位图
drawBitmap(Bitmapbitmap, Rect src, Rect dst, Paint paint)
(2)缩放原位图,创建一个新的位图
createBitmap(Bitmapsource, int x, int y, int width, int height, Matrix m, boolean filter)
(3)scale&
Canvas的 scale(float sx, floatsy)
不过要注意此时整个画布都缩放了
(4)用Matrix实现位图缩放
Matrix matrix=newMatrix();
matrix.postScale(0.2f, 0.2f);
Bitmapbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,t rue);
用Matrix实现
Bitmap bmp =BitmapFactory.decodeResource(getResources(), R.drawable.pic180);
Matrix matrix=newMatrix();
matrix.postRotate(45);
Bitmapdstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);
canvas.drawColor(Color.BLACK);
二、位图主要概念
&android.content.res
& android.graphics& 底层图形类
android.content.res.Resources
对于Android平台的资源类android.content.res.Resources可能很多网友比较陌生,一起来看看SDK上是怎么介绍的吧,Contains
classes foraccessing application resources, such as raw asset files, colors, drawables,media or other other files in the package, plus important device configurationdetails (orientation, input types, etc.) that affect how the application maybehave.平时用到的二进制源文件raw、颜色colors、图形drawables和多媒体文件media的相关资源均通过该类来管理。
& int&getColor(int id)&
对应res/values/colors.xml
& Drawable&getDrawable(int id)&
对应res/drawable/
&XmlResourceParser& getLayout(int id)&
对应res/layout/
& String&getString(int id)
和CharSequence&getText(int id)&&
对应res/values/strings.xml
&InputStream& openRawResource(int id)&
对应res/raw/
& voidparseBundleExtra (String tagName, AttributeSet attrs, Bundle outBundle)
对应res/xml/
& String[]&getStringArray(int id)& res/values/arrays.xml
& float&getDimension(int id)& res/values/dimens.xml
&(二)android.graphics.Bitmap
作为位图操作类,Bitmap提供了很多实用的方法,常用的我们总结如下:
& boolean& compress(Bitmap.CompressFormat format, int quality,OutputStream stream)
压缩一个Bitmap对象根据相关的编码、画质保存到一个OutputStream中。其中第一个压缩格式目前有JPG和PNG
& void& copyPixelsFromBuffer(Buffer src) 从一个Buffer缓冲区复制位图像素
& void& copyPixelsToBuffer(Buffer dst)& 将当前位图像素内容复制到一个Buffer缓冲区
我们看到创建位图对象createBitmap包含了6种方法在目前的Android
2.1 SDK中,当然他们使用的是API Level均为1,所以说从Android
1.0 SDK开始就支持了,所以大家可以放心使用。
&&staticBitmap& createBitmap(Bitmap src)
&static Bitmap& createBitmap(int[] colors, int width, int height,Bitmap.Config config)
&static Bitmap& createBitmap(int[] colors, int offset, int stride,int width, int height, Bitmap.Config config)
&static Bitmap& createBitmap(Bitmap source, int x, int y, int width,int height, Matrix m, boolean filter)
&static Bitmap& createBitmap(int width, int height, Bitmap.Configconfig)
&static Bitmap& createBitmap(Bitmap source, int x, int y, int width,int height)
&static Bitmap& createScaledBitmap(Bitmap src, int dstWidth, intdstHeight, boolean filter)& //创建一个可以缩放的位图对象
&final int&getHeight()&
&final int& getWidth()&& 获取宽度
&final boolean& hasAlpha()&& 是否有透明通道
&void& setPixel(int x, int y, int color)&& 设置某像素的颜色
&int& getPixel(int x, int y)& 获取某像素的颜色,android开发网提示这里返回的int型是color的定义
android.graphics.BitmapFactory
Bitmap实现在.graphics包中。但是Bitmap类的构造函数是私有的,外面并不能实例化,只能是通过JNI实例化。这必然是某个辅助类提供了创建Bitmap的接口,而这个类的实现通过JNI接口来实例化Bitmap的,这个类就是BitmapFactory。&
作为Bitmap对象的I/O类,BitmapFactory类提供了丰富的构造Bitmap对象的方法,比如从一个字节数组、文件系统、资源ID、以及输入流中来创建一个Bitmap对象,下面本类的全部成员,除了decodeFileDescriptor外其他的重载方法都很常用。
static Bitmap&decodeByteArray(byte[] data, int offset, int length) //从字节数组创建
static Bitmap&decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Optionsopts)
static Bitmap&decodeFile(String pathName, BitmapFactory.Options opts) //从文件创建,路径要写全
static Bitmap&decodeFile(String pathName)
static Bitmap&decodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Optionsopts)& //从输入流句柄创建
static Bitmap&decodeFileDescriptor(FileDescriptor fd)
static Bitmap&decodeResource(Resources res, int id)& //从Android的APK文件资源中创建,android123提示是从/res/的drawable中
static Bitmap&decodeResource(Resources res, int id, BitmapFactory.Options opts)
static Bitmap&decodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad,BitmapFactory.Options opts)
static Bitmap&decodeStream(InputStream is)& //从一个输入流中创建
static Bitmap&decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts)
android.graphics.Canvas
从J2ME MIDLET时我们就知道Java提供了Canvas类,而目前在Android平台中,它主要任务为管理绘制过程,The
Canvasclass holds the &draw& calls. To draw something, you need 4 basiccomponents: A Bitmap to hold the pixels, a Canvas to host the draw calls(writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap),and a paint (to describe the colors
and styles for the drawing).
该类主要提供了三种构造方法,分别为构造一个空的Canvas、从Bitmap中构造和从GL对象中创建,如下
Canvas(Bitmap bitmap)
Canvas(GL gl)
&同时Canvas类的一些字段保存着重要的绘制方法定义,比如Canvas.HAS_ALPHA_LAYER_SAVE_FLAG保存时需要alpha层,对于Canvas类提供的方法很多,每个都很重要,下面我们一一作介绍
void&concat(Matrix matrix)
void&drawARGB(int a, int r, int g, int b)
void& drawArc(RectF oval, float startAngle, float sweepAngle, booleanuseCenter, Paint paint)
void&drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)
void&drawBitmap(int[] colors, int offset, int stride, float x, float y, int width,int height, boolean hasAlpha, Paint paint)
void& drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
void& drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
void& drawBitmap(int[] colors, int offset, int stride, int x, int y, intwidth, int height, boolean hasAlpha, Paint paint)
void& drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
void& drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[]verts, int vertOffset, int[] colors, int colorOffset, Paint paint)
void&drawCircle(float cx, float cy, float radius, Paint paint)
void&drawColor(int color)
void&drawColor(int color, PorterDuff.Mode mode)
android.graphics.Matrix
利用Bitmap和Matrix实现图像变换:剪切、旋转、缩放等操作。
用源Bitmap通过变换生成新的Bitmap的方法:
public static BitmapcreateBitmap(Bitmap source, int x, int y, intwidth, int height,&&
&&&&&&&&&&&
Matrix m, boolean filter)&&
public static BitmapcreateBitmap(Bitmap source, int x, int y, intwidth, int height)&&
public static BitmapcreateScaledBitmap(Bitmap src, int dstWidth,&&
&&&&&&&&&&&
int dstHeight,boolean filter)&
第一个方法是最终的实现,后两种只是对第一种方法的封装。
第二个方法可以从源Bitmap中指定区域(x,y,
width, height)中挖出一块来实现剪切;第三个方法可以把源Bitmap缩放为dstWidth
x dstHeight的Bitmap。
设置Matrix的Rotate(通过setRotate())或者Scale(通过setScale()),传入第一个方法,可实现旋转或缩放。
原文链接:http://www.linuxidc.com/Linux/49p2.htm&
有关图形的变换、缩放等相关操作常用的方法有:
& void&reset() //
重置一个matrix对象。
& void&set(Matrix src) //复制一个源矩阵,和本类的构造方法Matrix(Matrix
src)& 一样
& boolean&isIdentity() //返回这个矩阵是否定义(已经有意义)
& voidsetRotate(float degrees) //指定一个角度以0,0为坐标进行旋转
& void&setRotate(float degrees, float px, float py)& //指定一个角度以px,py为坐标进行旋转
& void&setScale(float sx, float sy)& //
& void&setScale(float sx, float sy, float px, float py)& //以坐标px,py进行缩放
& voidsetTranslate(float dx, float dy) //平移
&void setSkew(float kx, float ky, float px, float py) //以坐标px,py进行倾斜
&void setSkew(float kx, float ky) //倾斜–Bitmap代表这一张位图,BitmapDrawable里封装的图片就是一个Bitmap对象。如果要将Bitmap对象封装成BitmapDrawable对象只需要用构造方法即可。
//讲bitmap对象包装成BitmapDrawable对象
BitmapDrawable drawable = new BitmapDrawable(bitmap);
//将BitmapDrawable对象转换为bitmap
Bitmap bitmap = drawable.getBitmap();
Bitmap对象提供了一系列静态方法来创建新的Bitmap对象
ocreateBitmap(Bitmap source, int x, int y, int width, int height):从原位图中指定坐标点(x,y)开始,从中挖取宽width、高height的一块出来,创建新的Bitmap对象。
ocreateScaledBitmap(Bitmap source, int dstWidth, int dstHeight, boolean filter):对源位图进行缩放,缩放成指定width、height大小的新位图对象。
ocreateBitmap(int width, int height, Bitmap.Config config):创建一个宽width、高height的新位图。
ocreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix matrix, boolean filter):从原位图中指定坐标点(x,y)开始,从中挖取宽width、高height的一块出来,创建新的Bitmap对象。并按Matrix指定的规则进行变换。
BitmapFactory是一个工具类,它提供了大量的方法来用于从不同的数据源来解析、创建Bitmap对象。包含了如下方法
odecodeByteArray(byte[] data, int offset, int length):从指定的字节数组的offset位置开始,将长度为length的字节数据解析成Bitmap对象。
odecodeFile(String pathName):从pathName指定的文件中解析、创建Bitmap对象。
odecodeFileDescriptor(FileDescriptor fd):从FileDescriptor对应的文件中解析、创建Bitmap对象。
odecodeResource(Resources res, int id):根据给定的资源ID从指定资源中解析、创建Bitmap对象。
odecodeStream(InputStream is):从指定的输入流中解析、创建Bitmap对象。
获取本地图片创建bitmap:
bmp=BitmapFactory.decodeResource(this.getResources(),R.drawable.haha);
当然可以获取网络图片创建bitmap:
URL conurl = new URL(url);
HttpURLConnection con = (HttpURLConnection) conurl.openConnection();
bmp = BitmapFactory.decodeStream(con.getInputStream());
此外附加学习:
android 获取资源文件 r.drawable中的图片转换为drawable、bitmap
Resources resources = mContext.getResources();Drawable drawable = resources.getDrawable(R.drawable.a);imageview.setBackground(drawable);
Resources r = this.getContext().getResources();Inputstream is = r.openRawResource(R.drawable.my_background_image);BitmapDrawable
bmpDraw = new BitmapDrawable(is);Bitmap bmp = bmpDraw.getBitmap();
Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);
Bitmap newb = Bitmap.createBitmap( 300, 300, Config.ARGB_8888 );
InputStream is = getResources().openRawResource(R.drawable.icon);
Bitmap mBitmap = BitmapFactory.decodeStream(is);
保存学习记录
android 生成 Bitmap 的常用几种方式
包括5种方式:
索引文件R文件
fileName 文件路径方式
1.以文件流的方式,假设在sdcard下有 mytest.png图片...
创建一个纯色的Bitmap(任意颜色)
Bitmap bitmap = Bitmap.createBitmap(宽度, 高度,
Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.pa...
android开发之bitmap使用
bitmap是android中重要的图像处理工具类,通过bitmap可以对图像进行剪切、旋转、缩放等操作,同时还可以指定格式和压缩质量保存图像文件。一、拿到一个Bitmap对象查看源码我们知道,Bit...
android自定义View——Bitmap使用详解
先看一个效果图
本节课程实现完成右图效果(三步)以及保存涂鸦过的图片步骤【1】将背景Bitmap图片画到底层canvas上 bitmapBackground = BitmapFactory.deco...
Android中关于Bitmap的裁剪缩放和创建
Android 中常常需要对图片进行缩放裁剪等处理,这里简单的介绍一下这两种处理方式的方法1.裁剪/**
* Returns an immutable bitmap from the spe...
收集了很多bitmap相关的处理方法,几乎全部应用在项目中,所以特记录下!
package com.tmacsky.
import java.io.ByteArrayOutputStr...
将bitmap对象保存到本地,返回保存的图片路径
private static final String SD_PATH = &/sdcard/dskqxt/pic/&;
private static final String IN_PATH...
Android Bitmap用法大全,以后再也不担心了
Android Bitmap用法大全,以后再也不担心了
将资源文件的图片转换成bitmap 的两种方法
1.Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.mingchuseal, newOpts);
Android图片压缩(质量压缩和尺寸压缩)
在做项目中遇到一个头疼的问题,读取本地图片时,由于图片太大,奔溃,对于我这种456M内存的破手机哪里受得了几M的照片
我要总结的有这么几点:
没有更多推荐了,}

我要回帖

更多关于 android有几种资源引用方式 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信