
Bitmap是Android系统中的图像处理的最重要的类之一。用它可以获取图像文件信息,对图像进行旋转,剪切,放大,缩小等 *** 作。
Bitmap代表一张位图,使我们在开发中常用的资源,下面就对Bitmap进行简单的介绍。
Bitmap的获取方法:
1、使用BitmapDrawable
BitmapDrawable里封装的就是一个Bitmap对象,我们要把Bitmap包装成BitmapDrawable对象,可以调用BitmapDrawable的构造方法:
BItmapDrawbale drawable = new BItmapDrawable(bitmap);
如果要获取BitmapDrawable所包装的Bitmap对象,则可调用BitmapDrawable的getBitmap()方法:
Bitmap bitmap = drawbalegetBitmap();
2、Bitmap提供了一些静态方法来创建Bitmap对象(仅列举几个):
createBitmap(Bitmap source,int x,int y,int width,int height):从原位图source的指定坐标(x,y)开始,从中挖取宽width,高heigtht的一块出来,创建新的Bitmap对象。
createScaledBitmap(Bitmap source,int width,ing height,boolean fliter):对源位图进行缩放,缩放称宽width,高heigth的新位图。
createBitmap(int width,int height,BitmapConfig config):创建一个宽width,高height的可变的新位图。
createBitmap(Bitmap source, int x,int y,int width,int height ,Matrix m,boolean fliter):从源位图source的指定坐标(x,y)开始,挖取宽width,高height的一块来,创建新的Bitmap对象,并按照Matrix指定的规则进行变换。
3、通过对资源文件的解析获取Bitmap对象
在这里就要用到BitmapFactory这个工具类,提供的方法如下:
decodeByteArray(byte[] data, int offset,int length):从指定字节数组的offset位置开始,将长度为length的字节数据解析成Bitmap对象。
decodeFIle(String pathName):从pathName指定的文件中解析、创建Bitmap对象。
decodeFileDescriptor(FileDescriptor fd):用于从FileDescriptor对应的文件中解析、创建Bitmap对象。
decodeResource(Resource res,int id):用于根据给定的资源ID从指定的资源文件中解析、创建Bitmap对象。
decodeStream(InputStream is):用于从指定输入流中介解析、创建Bitmap对象。
但是,在系统不断的解析、创建Bitmap的过程中,可能会由于内存小或其他原因,导致程序运行时发生OutOfMemory错误。
为此,Android为Bitmap提供了内存回收方法:
void recycle():强制回收Bitmap对象。
还有用于判断Bitmap 对象是否被回收的方法:
boolean isRecycle();
如果Android应用需要访问系统相册,都需要借助BitmapFactory解析、创建Bitmap对象。
4 从安卓无忧中看bitmap的几种例子,下面是加载bitmap的例子,可以看里面的源码:
如果您对答案满意,请您关注一下名字中微博。
Bitmap bmp=BitmapFactorydecodeResource(r, Rdrawableicon);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmpcompress(BitmapCompressFormatPNG, 100, baos);
baostoByteArray();
希望你能帮到你
在ImageView中的image,可以使用getWidth()和getHeight()来获取宽度和高度,但是获得的image宽度和高度不是很精确的;对于背景,你首先要获取背景的Drawable对象,然后将Drawable对象转换为BitmapDrawable,这样你就可以将背景作为Bitmap对象并获取其宽度和高度了。代码如下:Bitmap b = ((BitmapDrawble)imageViewgetBackground())getBitmap(); int w = bgetWidth(); int h = bgetHeight(); or do like this wayimageViewsetDrawingCacheEnabled(true); Bitmap b = imageViewgetDrawingCache(); int w = bgetWidth(); int h = bgetHeight(); 或者也可以像下面这样:imageViewsetDrawingCacheEnabled(true); Bitmap b = imageViewgetDrawingCache(); int w = bgetWidth(); int h = bgetHeight(); 上面的代码仅仅可以为你获取当前ImageView的大小:imageViewgetWidth(); imageViewgetHeight(); 如果你要获取Drawable image对象的大小,可用如下代码:Drawable d = getResources()getDrawable(Rdrawableyourimage); int h = dgetIntrinsicHeight(); int w = dgetIntrinsicWidth();
方法/步骤
1
如下图所示,需要根据URL地址获取加载到图中Anroid机器人所在的位置,这是运行前的效果:
2
首先需根据URL地址获取,如下所示,urladdr即为地址,返回Drawable对象:
//download image from network using @urladdress
private Drawable loadImageFromNetwork(String urladdr) {
// TODO Auto-generated method stub
Drawable drawable = null;
try{
//judge if has picture locate or not according to filename
drawable = DrawablecreateFromStream(new URL(urladdr)openStream(), "imagejpg");
}catch(IOException e){
Logd("test",egetMessage());
}
if(drawable == null){
Logd("test","null drawable");
}else{
Logd("test","not null drawable");
}
return drawable;
}
3
获取到后,需要更新主线程UI资源,考虑到时间以及界面反应延迟等,所以采用线程加以处理,如下图所示:
// image
new Thread(new Runnable(){
Drawable drawable = loadImageFromNetwork(urladdress);
@Override
public void run(){
//post() is quite important,update pictures in UI main thread
imagepost(new Runnable(){
@Override
public void run(){
//TODO Auto-generated method stub
imagesetImageDrawable(drawable);
}
});
}
//download image from network using @urladdress
private Drawable loadImageFromNetwork(String urladdr) {
// 略(如 1 中所示)
}
})start(); //线程启动
4
说明:在上述示例代码中,image是ImageView类的一个对象,也就是APP中的一个显示图像组件,利用获取到的drawable去更新image,运行效果如下所示:
private ImageView imageView,imageView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutactivity_main);
imageView=(ImageView)superfindViewById(RidimageView);
imageView2=(ImageView)superfindViewById(RidimageView2);//找到你layout中对应的imageView
Drawable drawable=imageViewgetBackground();//获取它的资源,返回类型是Drawable
//假设你想把imageView的设置到其他容器中,比如imageView2,
imageView2setBackground(drawable);//imageView2就显示和imageView一样的了
}
使用ImageLoader
源码地址
>
以上就是关于怎么给bitmap赋值 android全部的内容,包括:怎么给bitmap赋值 android、android中如何将drawable中的图片内容读取出来并转换为byte数据形式。 最好有代码可以参考的、怎么拿到 drawable 的 宽高等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)