android图像与图像处理系列(一、Bitmap和BitmapFactory)
1、Drawable对象
Android应用添加了Drawabe资源之后,Android SDK会为这份资源文件在R清单文件中创建一个索引项:R.drawable.file_name,接着我们可以在xml资源文件中通过@drawable/file_name来访问该drawable对象,也可以在java代码中通过R.drawable.file_name来访问该drawable对象,在java代码中R.drawable.file_name只是一个int类型的常量,它只代表了drawable对象的一个id,如果要获取实际的drawable对象,则需要调用Resources的getDrawable(int id)方法来获取。
2、Bitmap与BitmapFactory
(1)Bitmap代表一张位图,BitmapDrawable里封装的图片就是一个Bitmap对象,开发者为了把一个Bitmap对象封装成BitmapDrawable对象,可以调用BitmapDrawable的构造器:
把一个Bitmap对象包装成BitmapDrawable对象
BitmapDrawable drawable = new BitmapDrawable(bitmap);
(2)如果需要获取BitmapDrawable所包装的Bitmap对象,可以调用BitmapDrawable的getBitmap()方法:
获取BitmapDrawable对象所包装的Bitmap对象
Bitmap bitmap = drawable.getBitmap();
(3)Bitmap提供了一些静态方法来创建新的Bitmap对象:
Bitmap.createBitmap(source, x, y, width,height):从源位图source的指定坐标点x,y开始,从中挖取宽width、高height的一块区域,创建新的Bitmap
Bitmap.createScaledBitmap(src, dstWidth, dstHeight,filter):对源位图src进行缩放,缩成宽dstWidth、高DSTHeight的新位图
Bitmap.createBitmap(width,height, config):创建一个宽width、高height的新位图
Bitmap.createBitmap(source, x, y,width, height, m,filter):从源位图source的指定坐标点x,y开始,从中挖取宽width、高height的一块区域,创建新的Bitmap
,并按照Matrix指定的规则进行变换
(4)BitmapFactory是一个工具类,他提供了一些方法用于从不同的数据源来解析、创建Bitmap对象:
BitmapFactory.decodeByteArray(byte[] data, int offset, int length):从指定字节数组的offset位置开始,将长度为length的字节数据解析成Bitmap对象
BitmapFactory.decodeFile(String pathName):从pathName指定的文件中解析、创建Bitmap对象
BitmapFactory.decodeFileDescriptor(FileDescriptor fd):用于从FileDescriptor对应的文件中解析、创建Bitmap对象
BitmapFactory.decodeResource(Resources res, int id):用于根据给定的资源ID从指定资源文件中解析、创建Bitmap对象
BitmapFactory.decodeStream(InputStream is):用于从指定输入流中解析、创建Bitmap对象
(5)Android为Bitmap提供了两个方法来判断它是否已经回收,以及强制Bitmap回收自己
isRecycled():判断Bitmap对象是否已经回收
recycle():强制一个Bitmap对象立即回收自己
3、实例:图片查看器,查询assets目录下的图片
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" > <ImageView
android:id="@+id/imageview"
android:layout_width="300dp"
android:layout_height="300dp" /> <Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="搜索下一个" /> </LinearLayout>
activity文件:
package com.example.image; import java.io.IOException;
import java.io.InputStream; import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; /**
* Bitmap和BitmapFactory
* @author yinbenyang
*/
public class MainActivity extends Activity { private ImageView imageview;
String[] images = null;
private Button btn;
// 用于管理assets文件夹下的资源
AssetManager assets = null;
// 当前图片
int currentImg = 0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview = (ImageView) findViewById(R.id.imageview);
btn = (Button) findViewById(R.id.btn);
assets = getAssets();
try {
// 获取/assets目录下的所有文件
images = assets.list("");
} catch (IOException e) {
e.printStackTrace();
}
// 点击按钮查看下一张图片
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//如果图片是最后一个了,就置为第一个
if (currentImg >= images.length) {
currentImg = 0;
}
//找到下一个图片文件
while (!images[currentImg].endsWith(".png")
&& !images[currentImg].endsWith(".jpg")
&& !images[currentImg].endsWith(".gif")) {
currentImg++;
if (currentImg >= images.length) {
currentImg = 0;
}
}
InputStream assetFile = null;
try {
//打开指定资源对应的输入流
assetFile = assets.open(images[currentImg++]);
} catch (IOException e) {
e.printStackTrace();
}
BitmapDrawable bitDrawable = (BitmapDrawable)imageview.getDrawable();
//如果图片没有回收,先强制回收该图片
if(bitDrawable != null && !bitDrawable.getBitmap().isRecycled()){
bitDrawable.getBitmap().recycle();
}
//改变ImageView显示的图片
imageview.setImageBitmap(BitmapFactory.decodeStream(assetFile));
}
});
}
}
实例效果如下:点击搜索下一个,轮换显示图片



android图像与图像处理系列(一、Bitmap和BitmapFactory)的更多相关文章
- [Android] 图像各种处理系列文章合集
这是我近期在做Android随手拍一个项目的各种网上关于图片处理的资料,曾经学过数字图像处理都是用C++写的,以下的资料个人觉得是很优秀的各种集合,还有一方面它是在线笔记,希望对大家有所帮助吧 ...
- [Android] 随时拍图像处理部分总结及源码分享
http://blog.csdn.net/eastmount/article/details/45492065#comments [Android] 图像各种处理系列文章合集 http://blog. ...
- Android学习之——图形图像处理(Bitmap、BitmapFactory)(一)
转载自http://blog.csdn.net/csxwc/article/details/10345235 Bitmap是Android系统中的图像处理的最重要的类之一.用它可以获取图像文件信息,对 ...
- android图像处理系列之四-- 给图片添加边框(上)
图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...
- android图像处理系列之五-- 给图片添加边框(中)
前面一篇讲到给图片加边框的方式,只能给图片加一些有规则的边框,如果想加一些比较精美的效果,就有点麻烦了.下面就给出解决这个问题的思路. 思路是:一些比较精美的花边图片我们是很难用代码控制,就目前本人水 ...
- android图像处理系列之四--给图片添加边框(上)
图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...
- android图像处理系列之五--给图片添加边框(中)
前面一篇讲到给图片加边框的方式,只能给图片加一些有规则的边框,如果想加一些比较精美的效果,就有点麻烦了.下面就给出解决这个问题的思路. 思路是:一些比较精美的花边图片我们是很难用代码控制,就目前本人水 ...
- android图像处理系列之七--图片涂鸦,水印-图片叠加
图片涂鸦和水印其实是一个功能,实现的方式是一样的,就是一张大图片和一张小点图片叠加即可.前面在android图像处理系列之六--给图片添加边框(下)-图片叠加中也讲到了图片叠加,里面实现的原理是直接操 ...
- Android性能优化系列之Bitmap图片优化
https://blog.csdn.net/u012124438/article/details/66087785 在Android开发过程中,Bitmap往往会给开发者带来一些困扰,因为对Bitma ...
随机推荐
- __VA_ARGS__可变参数宏
#define qWiFiDebug(format, ...) qDebug("[WiFi] "format" File:%s, Line:%d, Function:%s ...
- canvas初体验之加载图片
上一篇的介绍主要是画一些基本的图案,这一篇主要是加载图案. canvas加载图片主要分为两个步骤: 1.获取图片资源. 2.将图片资源画到画布上. 1.1获取图片资源,canvasAPI为我们提供了多 ...
- JDE FORM开发--checkBox
checkBox设置时,必须指定DD.
- RHCE 系列(一):如何设置和测试静态网络路由
RHCE(Red Hat Certified Engineer,红帽认证工程师)是红帽公司的一个认证,红帽向企业社区贡献开源操作系统和软件,同时它还给公司提供训练.支持和咨询服务. 这个 RHCE 是 ...
- hdoj 5003
题意:给你一个数组a,降序排序后,求sum+=0.95^(i-1)*ai 这题wa了两发,因为我没看清题意,要排序! 精度上面通过a^(i-1)=e^((i-1)*log(a)) 提到精度,就要想到底 ...
- Android TextView里显示两种颜色
今天介绍一个小技巧,在Android的TextView里设置两种颜色,直接上代码: TextView TV = (TextView)findViewById(R.id.mytextview01); S ...
- CircleImageView
package com.cainiao5.cainiaoheadimg; import android.content.Context;import android.content.res.Typed ...
- [安卓]应用程序资源(App Resources)
谷歌推荐我们,在开发安卓系统应用程序的时候,要把资源从代码中分离出来,这样便于我们单独维护它们.采取分离的资源设计,我们还可以提供可选资源,支持特定的设备配置譬如不同的语言或屏幕尺寸,随着越来越多的A ...
- 学会使用Constant常量或者Enum枚举
好多时候我们在数据库表中存放的类型是一个代号CHAR(1) 0,1,2,3等分别代表些什么. 那么你是怎么知道0,1,2,3代表什么的呢? 有的是建表,连接查询,但很少人用. 有的是在jsp页面c:i ...
- ubuntu 配置JDK环境
/etc/profile中加入以下代码 JAVA_HOME为JDK包解压的路径export JAVA_HOME=/home/exayong/jvm/jdk1.8.0_111 export JRE_HO ...