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)的更多相关文章

  1. [Android] 图像各种处理系列文章合集

        这是我近期在做Android随手拍一个项目的各种网上关于图片处理的资料,曾经学过数字图像处理都是用C++写的,以下的资料个人觉得是很优秀的各种集合,还有一方面它是在线笔记,希望对大家有所帮助吧 ...

  2. [Android] 随时拍图像处理部分总结及源码分享

    http://blog.csdn.net/eastmount/article/details/45492065#comments [Android] 图像各种处理系列文章合集 http://blog. ...

  3. Android学习之——图形图像处理(Bitmap、BitmapFactory)(一)

    转载自http://blog.csdn.net/csxwc/article/details/10345235 Bitmap是Android系统中的图像处理的最重要的类之一.用它可以获取图像文件信息,对 ...

  4. android图像处理系列之四-- 给图片添加边框(上)

    图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...

  5. android图像处理系列之五-- 给图片添加边框(中)

    前面一篇讲到给图片加边框的方式,只能给图片加一些有规则的边框,如果想加一些比较精美的效果,就有点麻烦了.下面就给出解决这个问题的思路. 思路是:一些比较精美的花边图片我们是很难用代码控制,就目前本人水 ...

  6. android图像处理系列之四--给图片添加边框(上)

    图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...

  7. android图像处理系列之五--给图片添加边框(中)

    前面一篇讲到给图片加边框的方式,只能给图片加一些有规则的边框,如果想加一些比较精美的效果,就有点麻烦了.下面就给出解决这个问题的思路. 思路是:一些比较精美的花边图片我们是很难用代码控制,就目前本人水 ...

  8. android图像处理系列之七--图片涂鸦,水印-图片叠加

    图片涂鸦和水印其实是一个功能,实现的方式是一样的,就是一张大图片和一张小点图片叠加即可.前面在android图像处理系列之六--给图片添加边框(下)-图片叠加中也讲到了图片叠加,里面实现的原理是直接操 ...

  9. Android性能优化系列之Bitmap图片优化

    https://blog.csdn.net/u012124438/article/details/66087785 在Android开发过程中,Bitmap往往会给开发者带来一些困扰,因为对Bitma ...

随机推荐

  1. pfile 与 spfile

    启动方式与顺序: 启动顺序:dbs 下的 init --> dbs 下的 spfile 如果 pfile 中没有指定 spfile 参数,那么数据库以 pfile 方式启动 如果 pfile 中 ...

  2. Linux 忘记密码解决方法

    很多朋友经常会忘记Linux系统的root密码,linux系统忘记root密码的情况该怎么办呢?重新安装系统吗?当然不用!进入单用户模式更改一下root密码即可. 步骤如下: 重启linux系统 3  ...

  3. 关于BS响应式的网站建设

    一.首先是导航 html部分: <!-- 导航 --> <nav class="navbar navbar-default navbar-fixed-top"&g ...

  4. iOS开发UI篇—ios应用数据存储方式(归档)

    iOS开发UI篇—ios应用数据存储方式(归档)  一.简单说明 在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦: 偏好设置(将所有的东西都保存在同 ...

  5. iOS开发UI篇—Quartz2D简单使用(一)

    iOS开发UI篇—Quartz2D简单使用(一) 一.画直线 代码: // // YYlineview.m // 03-画直线 // // Created by apple on 14-6-9. // ...

  6. JavaWeb chapeter 5 Web应用程序状态管理

    1.  HTTP协议使用的是无状态连接,对容器而言,每一个请求都来自于一个新的客户. 2. html表单隐藏字段:对用户在网站上的访问进行会话跟踪.为服务器端程序提供预定义的输入.存储动态产生的页面上 ...

  7. .htaccess更改目录下的默认主页

    我们知道apache的配置文件httpd.conf可以配置网站目录的默认主页.配置文件该部分定义如下: #DirectoryIndex: sets the file that Apache will ...

  8. PHP投票系统

    1.投票页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  9. 拓扑编号 vijos1790

    题意就是拓扑排序,要求1的序号尽可能小,然后2的序号尽可能小,3,4... 一开始很容易想到直接贪心,每次选一个入度为0的点,如果有多个,就选编号最小的那个,但是很容易找到反例. 看了下题解,应该是反 ...

  10. STL模板之_map,stack(计算矩阵相乘的次数)

    #include <map>#include <stack>#include <iostream>using namespace std; struct Node ...