【Android开发】图形图像处理技术-Bitmap和BitmapFactory类

本文涉及的产品
云解析DNS,个人版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介:
一、Bitmap类
Bitmap类代表位图,是Android系统中图像处理的一个重要类。使用该类,不仅可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,而且还可以指定格式保存图像文件。对于这些操作,都可以通过Bitmap类提供的方法来实现。Bitmap类提供的常用方法如表所示:

(1)public final int getHeight():获取位图宽度
(2)public final int getWidth():获取位图高度
(3)public static Bitmap createBitmap(Bitmap src):通过位图资源创建位图实例
(4)public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height):在指定位置创建位图,可放大缩小位图  source是位图资源,(x,y)表示位图左上角坐标,width表示位图宽度,height表示位图高度
(5)public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter):截取位图中指定区域中的位图  source是目标位图资源,(x,y)表示剪切位图左上角起始点坐标,width表示目标位图宽度,height表示目标位图高度,m表示选择区域,filter是当其为true且m表示的区域大于前面参数所描述的区域时就填充截取的位图空白处
(6)public boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream):保存图像文件  format是图片保存格式,quality是图片压缩的质量取值在0-100之间 0表示压缩的最小尺寸 100表示压缩的最好质量,stream是图片的输出流
注:对于PNG格式的图片是有损压缩的,quality压缩图片是没有效果的,该参数的设置无影响
(7)creatScaleBitmap(Bitmap source, int x, int y, int width, int height, boolean filter)
用于将源位图缩放为指定宽度和高度的新的Bitmap对象
(8)isRecycled()用于判断Bitmap对象是否被回收
(9)recycle()强制回收Bitmap对象


例如,创建一个包括4个像素(每个像素对应一种颜色)的Bitmap对象的代码如下:
Bitmap bitmap=Bitmap.creatBitmap(new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.MAGENTA},
4,1,Config.RGB_565);

Bitmap小测试:
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tableLayout1"
    android:orientation="vertical"
    >
 	<com.example.test.DrawView
 	    android:id="@+id/drawView1"
 	    android:layout_width="wrap_content"
 	    android:layout_height="wrap_content"/>
</LinearLayout>

DrawView.java:
package com.example.test;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Style;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;


public class DrawView extends View{
	/*
	 * 功能:构造方法
	 * */
	public DrawView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
	}
	
	/*
	 * 功能:重写onDraw方法
	 * */
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint=new Paint();
		paint.setColor(Color.BLACK);
		paint.setStrokeWidth(2);
		paint.setStyle(Style.STROKE);
		Bitmap bitmap=Bitmap.createBitmap(40,60,Config.RGB_565);
		canvas.drawBitmap(bitmap, 150, 150,paint);
	}
}

运行效果如图所示



二、BitmapFactory类
在Android中,还提供了一个Bitmap类,该类作为一个工具类,用于从不同的数据源来解析、创建Bitmap对象。BitmapFactory类提供的创建Bitmap对象的常用方法如表所示:
(1)decodeFile(String pathName)用于从给定的路径所指定的文件中解析、创建Bitmap对象
(2)decodeFileDescriptor(FileDescriptor fd)用于从FileDescriptor对应的文件中解析、创建Bitmap对象
(3)decodeResource(Resource res,int id)用于根据给定的资源id,从指定的资源中解析、创建Bitmap对象
(4)decodeStream(InputStream is)用于从指定的输入流中解析、创建Bitmap对象

例如,要解析SD卡上的图片文件img01.jpg并创建对应的Bitmap对象,可以使用下面的代码:
String path="/sdcard/pictrues/bccd/img01.jpg";
Bitmap bm=BitmapFactory.decodeFile(path);

要解析Drawable资源中保存的图片文件img02.jpg并创建对应的Bitmap对象,可以使用下面的代码:
Bitmap bm=BitmapFactory.decodeResoutce(MainActivity.this,R.drawable.img02.jpg);

这一点在之前写的"移动的小兔子"的实例中用到过。

转载请注明出处:http://blog.csdn.net/acmman/article/details/45177209

相关文章
|
2天前
|
Android开发
技术经验分享:Android前后台切换的监听
技术经验分享:Android前后台切换的监听
|
2天前
|
缓存 测试技术 Shell
详细解读Android开发命令行完全攻略
详细解读Android开发命令行完全攻略
|
20小时前
|
安全 Android开发 iOS开发
探索安卓与iOS开发的差异:平台特性与用户体验的深度对比
在移动应用开发的广阔天地中,安卓和iOS两大平台各占半壁江山。本文旨在通过数据驱动的分析方法,深入探讨这两大操作系统在开发环境、用户界面设计及市场表现等方面的差异。引用最新的行业报告和科研数据,结合技术专家的观点,本文将提供对开发者和市场分析师均有价值的洞见。
|
1天前
|
编解码 开发工具 Android开发
技术心得:打造自己的智能投屏体验——Android投屏开发入门
技术心得:打造自己的智能投屏体验——Android投屏开发入门
|
1天前
|
Java API Android开发
技术经验分享:Android源码笔记——Camera系统架构
技术经验分享:Android源码笔记——Camera系统架构
|
2天前
|
开发工具 Android开发
技术经验分享:Android编译命令m、mm、mmm区别及工程搭建示例
技术经验分享:Android编译命令m、mm、mmm区别及工程搭建示例
|
2天前
|
API Android开发
技术经验分享:Android系统开发获取userId
技术经验分享:Android系统开发获取userId
|
2天前
|
缓存 测试技术 Shell
详细解读Android开发命令行完全攻略
详细解读Android开发命令行完全攻略
|
2天前
|
Java 开发工具 Android开发
详细解读Android开发DNK开发将.c文件打包成os
详细解读Android开发DNK开发将.c文件打包成os
|
安全 Java Android开发
【Android 逆向】类加载器 ClassLoader ( Android 的八种类加载器 | ClassLoader | BaseDexClassLoader | DexClassLoader )
【Android 逆向】类加载器 ClassLoader ( Android 的八种类加载器 | ClassLoader | BaseDexClassLoader | DexClassLoader )
222 0
【Android 逆向】类加载器 ClassLoader ( Android 的八种类加载器 | ClassLoader | BaseDexClassLoader | DexClassLoader )