Android位图操作
Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。对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、路径、文件、数据流等方式来获取位图。
Android
可支持的图片格式如下: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);
(二)位图显示
Bitmap
显示到画布上:
(1)通过
Canvas 画布 drawbitmap
到屏幕上
/* 在屏幕(left,top)处开始显示图片bitmap
*/
canvas.drawbitmap(Bitmap bitmap, float left,float top, Paint paint);
(2)通过
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的相关资源均通过该类来管理。
intgetColor(int id)
对应res/values/colors.xml
DrawablegetDrawable(int id)
对应res/drawable/
XmlResourceParser getLayout(int id)
对应res/layout/
StringgetString(int id)
和CharSequencegetText(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
floatgetDimension(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 intgetHeight()
获取高度
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实现在Android.graphics包中。但是Bitmap类的构造函数是私有的,外面并不能实例化,只能是通过JNI实例化。这必然是某个辅助类提供了创建Bitmap的接口,而这个类的实现通过JNI接口来实例化Bitmap的,这个类就是BitmapFactory。
作为Bitmap对象的I/O类,BitmapFactory类提供了丰富的构造Bitmap对象的方法,比如从一个字节数组、文件系统、资源ID、以及输入流中来创建一个Bitmap对象,下面本类的全部成员,除了decodeFileDescriptor外其他的重载方法都很常用。
static BitmapdecodeByteArray(byte[] data, int offset, int length) //从字节数组创建
static BitmapdecodeByteArray(byte[] data, int offset, int length, BitmapFactory.Optionsopts)
static BitmapdecodeFile(String pathName, BitmapFactory.Options opts) //从文件创建,路径要写全
static BitmapdecodeFile(String pathName)
static BitmapdecodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Optionsopts) //从输入流句柄创建
static BitmapdecodeFileDescriptor(FileDescriptor fd)
static BitmapdecodeResource(Resources res, int id) //从Android的APK文件资源中创建,android123提示是从/res/的drawable中
static BitmapdecodeResource(Resources res, int id, BitmapFactory.Options opts)
static BitmapdecodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad,BitmapFactory.Options opts)
static BitmapdecodeStream(InputStream is) //从一个输入流中创建
static BitmapdecodeStream(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()
Canvas(Bitmap bitmap)
Canvas(GL gl)
同时Canvas类的一些字段保存着重要的绘制方法定义,比如Canvas.HAS_ALPHA_LAYER_SAVE_FLAG保存时需要alpha层,对于Canvas类提供的方法很多,每个都很重要,下面我们一一作介绍
voidconcat(Matrix matrix)
voiddrawARGB(int a, int r, int g, int b)
void drawArc(RectF oval, float startAngle, float sweepAngle, booleanuseCenter, Paint paint)
voiddrawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)
voiddrawBitmap(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)
voiddrawCircle(float cx, float cy, float radius, Paint paint)
voiddrawColor(int color)
voiddrawColor(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/2011-09/43349p2.htm
有关图形的变换、缩放等相关操作常用的方法有:
voidreset() //
重置一个matrix对象。
voidset(Matrix src) //复制一个源矩阵,和本类的构造方法Matrix(Matrix
src) 一样
booleanisIdentity() //返回这个矩阵是否定义(已经有意义)
voidsetRotate(float degrees) //指定一个角度以0,0为坐标进行旋转
voidsetRotate(float degrees, float px, float py) //指定一个角度以px,py为坐标进行旋转
voidsetScale(float sx, float sy) //
缩放
voidsetScale(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) //倾斜
分享到:
相关推荐
android位图的一些东西,对一些基本的操作的demo。主要是view和bitmap一些东西 比如说倒影之类的东西
Android Bitmap位图渲染与操作源码.rar
安卓Android源码——(Bitmap位图渲染与操作).zip
安卓Android源码——(Bitmap位图渲染与操作).rar
Android应用源码之(Bitmap位图渲染与操作.zip项目安卓应用源码下载Android应用源码之(Bitmap位图渲染与操作.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术...
源码参考,欢迎下载
Android 应用开发源码 参考与学习使用
Android应用源码开发Demo,主要用于毕业设计学习。
通过postRotate方法设置旋转角度,然后用createBitmap方法创建一个经过旋转处理的Bitmap对象,最后用drawBitmap方法绘制到屏幕上,于是就实现了旋转操作。 下面例子中把原位图和经旋转处理的位图都绘制到屏幕上,...
Android单点触控技术,对图片进行平移,缩放,旋转操作, 详情请查看http://blog.csdn.net/xiaanming/article/details/42833893
这一联盟将会支持 Google 发布的 Android 手机操作系统或者应用软件,共同开发名为 Android 的 开 放源代码的移动系统。开放手机联盟包括手机制造商、手机芯片厂商和移动运营商几类。目前,联盟成员 数 量已经达到了...
60个Android开发精典案例 Android软件源码: 2-1(Activity生命周期) 3-1(Button与点击监听器) 3-10-1(列表之ArrayAdapter适配) 3-10-2(列表之SimpleAdapter适配) 3-11(Dialog对话框) 3-12-5(Activity跳转与操作) 3-12...
StickerView是Android上的一个可以进行添加,缩放,拖动,删除操作的即时贴控件。如图所示:用法:StickerView 是基于 ImageView 布局的扩展。 android:id="@ id/sticker_view" android:layout_width=...
7.1.2 位图操作的基本知识 7.1.3 具体实现 7.2 绘制几何图形 7.3 手机屏保 7.4 照片拖动效果 7.5 存储卡中的图片 7.6 获取内置媒体文件 7.7 调节音量 7.8 播放mp3资源文件 7.9 录制音频 7.10 实现相机拍照功能 ...
android 用canvas.clipPath 画不规则的Bitmap 通过使用canvas.clipPath,可以将一个规则的位图裁剪为不规则的位图 通过canvas.clipPath(path);方法可以达到这个效果
Android是一个专门针对移动设备的软件集,它包括一个操作系统,中间件和一些重要的应用程序。Beta 版 的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 • ...
本程序是利用位图操作来实现一个蒙板效果的类进度条程序。
通常用于将位图标记到另一个。 例如:pdf 用途:可用于pdf签名的操作控件 安装 Gradle allprojects { repositories { ... maven { url ' https://jitpack.io ' } } } implementation ' ...
android 源码学习. 资料部分来源于合法的互联网渠道收集和整理,供大家学习参考与交流。本人不对所涉及的版权问题或内容负法律责任。如有侵权,请通知本人删除。感谢CSDN官方提供大家交流的平台