Android中BitMap和Drawable

Android中BitMap和Drawable
Android中BitMap和Drawable

Android中Bitmap和Drawable

一、相关概念

1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象

2、Canvas画布,绘图的目的区域,用于绘图

3、Bitmap位图,用于图的处理

4、Matrix矩阵

二、Bitmap

1、从资源中获取Bitmap

Java代码

1.Resources res = getResources();

2.Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);

2、Bitmap → byte[]

Java代码

1.public byte[] Bitmap2Bytes(Bitmap bm) {

2.ByteArrayOutputStream baos = new ByteArrayOutputStream();

https://www.360docs.net/doc/fa15798836.html,press(https://www.360docs.net/doc/fa15798836.html,pressFormat.PNG, 100, baos);

4.return baos.toByteArray();

5.}

3、byte[] → Bitmap

Java代码

1.public Bitmap Bytes2Bimap(byte[] b) {

2.if (b.length != 0) {

3.return BitmapFactory.decodeByteArray(b, 0, b.length);

4.} else {

5.return null;

6.}

7.}

4、Bitmap缩放

Java代码

1.public static Bitmap zoomBitmap(Bitmap bitmap, int width, int

height) {

2.int w = bitmap.getWidth();

3.int h = bitmap.getHeight();

4.Matrix matrix = new Matrix();

5.float scaleWidth = ((float) width / w);

6.float scaleHeight = ((float) height / h);

7.matrix.postScale(scaleWidth, scaleHeight);

8.Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,

true);

9.return newbmp;

10.}

5、将Drawable转化为Bitmap

Java代码

1.public static Bitmap drawableToBitmap(Drawable drawable) {

2.// 取 drawable 的长宽

3.int w = drawable.getIntrinsicWidth();

4.int h = drawable.getIntrinsicHeight();

5.

6.// 取 drawable 的颜色格式

7.Bitmap.Config config = drawable.getOpacity() !=

PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

8.: Bitmap.Config.RGB_565;

9.// 建立对应 bitmap

10.Bitmap bitmap = Bitmap.createBitmap(w, h, config);

11.// 建立对应 bitmap 的画布

12.Canvas canvas = new Canvas(bitmap);

13.drawable.setBounds(0, 0, w, h);

14.// 把 drawable 内容画到画布中

15.drawable.draw(canvas);

16.return bitmap;

17.}

6、获得圆角图片

Java代码

1.public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float

roundPx) {

2.int w = bitmap.getWidth();

3.int h = bitmap.getHeight();

4.Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);

5.Canvas canvas = new Canvas(output);

6.final int color = 0xff424242;

7.final Paint paint = new Paint();

8.final Rect rect = new Rect(0, 0, w, h);

9.final RectF rectF = new RectF(rect);

10.paint.setAntiAlias(true);

11.canvas.drawARGB(0, 0, 0, 0);

12.paint.setColor(color);

13.canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

14.paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

15.canvas.drawBitmap(bitmap, rect, rect, paint);

16.

17.return output;

18.}

7、获得带倒影的图片

Java代码

1.public static Bitmap createReflectionImageWithOrigin(Bitmap

bitmap) {

2.final int reflectionGap = 4;

3.int w = bitmap.getWidth();

4.int h = bitmap.getHeight();

5.

6.Matrix matrix = new Matrix();

7.matrix.preScale(1, -1);

8.

9.Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,

10.h / 2, matrix, false);

11.

12.Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),

13.Config.ARGB_8888);

14.

15.Canvas canvas = new Canvas(bitmapWithReflection);

16.canvas.drawBitmap(bitmap, 0, 0, null);

17.Paint deafalutPaint = new Paint();

18.canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);

19.

20.canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);

21.

22.Paint paint = new Paint();

23.LinearGradient shader = new LinearGradient(0, bitmap.getHeight(),

0,

24.bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,

25.0x00ffffff, TileMode.CLAMP);

26.paint.setShader(shader);

27.// Set the Transfer mode to be porter duff and destination in

28.paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

29.// Draw a rectangle using the paint with our linear gradient

30.canvas.drawRect(0, h, w, bitmapWithReflection.getHeight()

31.+ reflectionGap, paint);

32.

33.return bitmapWithReflection;

34.}

三、Drawable

1、Bitmap转换成Drawable

Java代码

1.Bitmap bm=xxx; //xxx根据你的情况获取

2.BitmapDrawable bd= new BitmapDrawable(getResource(), bm);

3.因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。2、Drawable缩放

Java代码

1.public static Drawable zoomDrawable(Drawable drawable, int w, int

h) {

2.int width = drawable.getIntrinsicWidth();

3.int height = drawable.getIntrinsicHeight();

4.// drawable转换成bitmap

5.Bitmap oldbmp = drawableToBitmap(drawable);

6.// 创建操作图片用的Matrix对象

7.Matrix matrix = new Matrix();

8.// 计算缩放比例

9.float sx = ((float) w / width);

10.float sy = ((float) h / height);

11.// 设置缩放比例

12.matrix.postScale(sx, sy);

13.// 建立新的bitmap,其内容是对原bitmap的缩放后的图

14.Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,

15.matrix, true);

16.return new BitmapDrawable(newbmp);

17.}

Android布局属性大全

Android布局属性大全 布局: AbsoluteLayout(绝对布局): xmlns:android="https://www.360docs.net/doc/fa15798836.html,/apk/res/android" style="@..." android:clipChildren="true|false" android:clipToPadding="true|false" android:layoutAnimation="@---" android:animationCache="true|false" android:persistentDrawingCache="none|animation|scrolling|all":持续高速缓存绘图 android:alwaysDrawnWithCache="true|false" android:andStatesFromChildre="true|false" android:descendantFocusability="beforeDescendants|afterDescendants|bl ocksDescendants":后裔可聚焦 android:id="@+id/absoluteLayout" android:tag="@---" android:android:scrollX="---" android:android:scrollY="---" android:background="@---" android:padding="----" android:paddingLeft="----" android:paddingTop="----" android:paddingRight="----" android:paddingBotton="---" android:focusable="true|false" android:focusableInTouchMode="true|false" android:visibility="visible|invisible|gone" android:fitsSystemWindows="true|false":适合系统窗口 android:scrollbars="none|horizontal|vertical" android:scrollbarStyle="insideOverlay(内覆盖)|insideInset(内插 图)|outsideOverlay(外覆盖)|outsideInset(外插图)" android:isScrollContainer="true|false":是一个滚动集合 android:fadeScrollbars="true|false":褪色的滚动条 android:scrollbarFadeDuration="---":卷轴淡出 android:scrollDefaultDelayBeforeFade="---":滚动前默认延迟 android:scrollbarSize="---" android:scrollbarThumbHorizontal="@----":拇指水平滚动条 android:scrollbarThumbVertical="@----":拇指垂直滚动条 android:scrollbarTrackVertical="@---":垂直滚动条轨道 android:scrollbarTrackHorizontal="@---":水平滚动条轨道 android:scrollbarAlwaysDrawHorizontalTrack="true|false":水平滚动条总是吸引轨道

Android日历完整实现

实用第一智慧密集 2011. 05 实现基于Android 的日历系统 摘要: Android 作为目前较为流行的智能手机操作系统已成为大多数人的首选。在美国乃至世界 的很多地方的出货量已经超越Iphone,成为世界上最大智能手机操作系统。因此,世界各地的程 序员都跃跃欲试地想学习Android 的开发,并希望从中捞得属于自己的第一桶金。在此给出一个 基于Android 的日历系统的完整实现过程。 关键词: Android;日历;绘画;农历;记录;提醒 1 引言 要实现的日历除了常规的日历功能外,还可以显示与当前 日期相关的信息,如当前日期的农历日期、天干地支、节日等 信息。下面先看看日历的绚丽界面,如图1、图2 所示。 主要功能

2 绘画基础 由于实现的日历系统要涉及到大量的Android 绘图技术, 因此,要简单介绍Android 的绘图技术。 绘制图形通常在Android.view.View 或其子类的onDraw 方 法中进行。该方法的定义如下: protected void onDraw(Canvas canvas); 其中Canvas 对象提供了大量用于绘图的方法,这些方法 主要包括绘制像素点、直线、圆形、弧、文本,这些都是组成 复杂图形的基本元素。如果要画更复杂的图形,可以采用组合 这些图形基本元素的方式来完成。例如,可以采用画3 条直线 的方式来画三角形。下面来看一下绘制图形基本元素的方法。 2.1 绘制像素点 public native void drawPoint(float x, float y, Paint paint); // 画一个像素点 public native void drawPoints(float[] pts, int offset, int count, Paint paint); // 画多个像素点 public void drawPoints(float[] pts, Paint paint); // 画多个像素点 参数的含义如下: (1) x:像素点的横坐标。 (2) y:像素点的纵坐标。 (3) paint:描述像素点属性的Paint 对象。可设置像素点 的大小、颜色等属性。绘制其他图形元素的Paint 对象与绘制 像素点的Paint 对象的含义相同。在绘制具体的图形元素时可 根据实际的情况设置Paint 对象。 (4) pts: drawPoints 方法可一次性画多个像素点。pts 参数 表示多个像素点的坐标。该数组元素必须是偶数个,两个一组 为一个像素点的坐标。 (5) offset: drawPoints 方法可以取pts 数组中的一部分连 续元素作为像素点的坐标,因此,需要通过offset 参数来指定 取得数组中连续元素的第一个元素的位置,也就是元素偏移 量,从0 开始。例如,要从第3 个元素开始取数组元素,那么 offset 参数值就是2。 (6) count:要获得的数组元素个数, count 必须为偶数 (两个数组元素为一个像素点的坐标)。 要注意的是, offset 可以从任意一个元素开始取值,例如, offset 可以为1,然后count 为4。

Android UI开发专题

https://www.360docs.net/doc/fa15798836.html,/cmdn/bbs/viewthread.php?tid=18736&page=1 #pid89255 Android UI开发专题(一) 之界面设计 近期很多网友对Android用户界面的设计表示很感兴趣,对于Android UI开发自绘控件和游戏制作而言掌握好绘图基础是必不可少的。本次专题分10节来讲述,有关OpenGL ES相关的可能将放到以后再透露。本次主要涉及以下四个包的相关内容:android.content.res 资源类 android.graphics 底层图形类 android.view 显示类 android.widget 控件类 一、android.content.res.Resources 对于Android平台的资源类android.content.res.Resources可能很多网友比较陌生,一起来看看SDK上是怎么介绍的吧,Contains classes for accessing application resources, such as raw asset files, colors, drawables, media or other other files in the package, plus important device configuration details (orientation, input types, etc.) that affect how the application may behave.平时用到的二进制源文件raw、颜色colors、图形drawables和多媒体文件media的相关资源均通过该类来管理。 int getColor(int id) 对应res/values/colors.xml Drawable getDrawable(int id) 对应res/drawable/ XmlResourceParser getLayout(int id) 对应res/layout/ String getString(int id) 和CharSequence getText(int id) 对应res/values/strings.xml InputStream openRawResource(int id) 对应res/raw/ void parseBundleExtra (String tagName, AttributeSet attrs, Bundle outBundle) 对应res/xml/ String[] getStringArray(int id) res/values/arrays.xml float getDimension(int id) res/values/dimens.xml 二、android.graphics.Bitmap 作为位图操作类,Bitmap提供了很多实用的方法,常用的我们总结如下: boolean compress(https://www.360docs.net/doc/fa15798836.html,pressFormat 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开始就支持了,所以大家可以放心使用。

Android Canvas绘图详解

Android Canvas绘图详解(图文) 摘要Android中使用图形处理引擎,2D部分是android SDK内部自己提供,3D部分是用Open GL ES 1.0。今天我们主要要了解的是2D相关的,如果你想看3D的话那么可以跳过这篇文章。大部分2D 使用的api都在android.graphics和android.graphics.drawable包中。他们提供了图 Android中使用图形处理引擎,2D部分是android SDK内部自己提供,3D部分是用Open GL ES 1.0。今天我们主要要了解的是2D相关的,如果你想看3D的话那么可以跳过这篇文章。 大部分2D使用的api都在android.graphics和android.graphics.drawable包中。他们提供了图形处理相关的:Canvas、ColorFilter、Point(点)和RetcF(矩形)等,还有一些动画相关的:AnimationDrawable、BitmapDrawable和TransitionDrawable等。以图形处理来说,我们最常用到的就是在一个View上画一些图片、形状或者自定义的文本内容,这里我们都是使用Canvas来实现的。你可以获取View中的Canvas对象,绘制一些自定义形状,然后调用View. invalidate方法让View重新刷新,然后绘制一个新的形状,这样达到2D动画效果。下面我们就主要来了解下Canvas的使用方法。 Canvas对象的获取方式有两种:一种我们通过重写View.onDraw方法,View中的Canvas 对象会被当做参数传递过来,我们操作这个Canvas,效果会直接反应在View中。另一种就是当你想创建一个Canvas对象时使用的方法: 1 2 Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Canvas c =new Canvas(b); 上面代码创建了一个尺寸是100*100的Bitmap,使用它作为Canvas操作的对象,这时候的Canvas就是使用创建的方式。当你使用创建的Canvas在bitmap上执行绘制方法后,你还可以将绘制的结果提交给另外一个Canvas,这样就可以达到两个Canvas协作完成的效果,简化逻辑。但是android SDK建议使用View.onDraw参数里提供的Canvas就好,没必要自己创建一个新的Canvas对象。接下来我们看看Canvas提供我们哪些绘制图形的方法。我们创建一个自定义View对象,使用onDraw方法提供的Canvas进行绘制图形。 CanvasDemoActivity.java: 1 2 3 4 5 6 package com.android777.demo.uicontroller.graphics; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color;

Android七种布局解析

我们对Android应用程序运行原理及布局文件可谓有了比较深刻的认识和理解,并且用“Hello World!” 程序来实践证明了。在继续深入Android开发之旅之前,有必要解决前两篇中没有介绍的遗留问题:View 的几种布局显示方法,以后就不会在针对布局方面做过多的介绍。View的布局显示方式有下面几种: 线性布局(Linear Layout)、 相对布局(Relative Layout)、 表格布局(Table Layout)、 网格视图(Grid View)、 标签布局(Tab Layout)、 列表视图(List View)、 绝对布局(AbsoluteLayout)。本文虽然是介绍View的布局方式,但不仅仅是这样,其中涉及了很多小的知识点,绝对能给你带来Android大餐! 本文的主要内容就是分别介绍以上视图的七种布局显示方式效果及实现,大纲如下: ?1、View布局概述 ?2、线性布局(Linear Layout) o 2.1、Tips:android:layout_weight="1" ?3、相对布局(Relative Layout) ?4、表格布局(Table Layout) ?5、列表视图(List View) o 5.1、一个小的改进 o 5.2、补充说明 ?6、网格视图(Grid View) ?7 、绝对布局() ?8、标签布局(Tab Layout) 1、view的布局显示概述 通过前面的学习我们知道:在一个Android应用程序中,用户界面通过View和ViewGroup对象构建。A ndroid中有很多种View和ViewGroup,他们都继承自View类。View对象是Android平台上表示用户界面的基本单元。 View的布局显示方式直接影响用户界面,View的布局方式是指一组View元素如何布局,准确的说是一个ViewGroup中包含的一些View怎么样布局。ViewGroup类是布局(layout)和视图容器(View containe r)的基类,此类也定义了https://www.360docs.net/doc/fa15798836.html,youtParams类,它作为布局参数的基类,此 类告诉父视图其中的子视图想如何显示。例如,XML布局文件中名为layout_so mething的属性(参加上篇的4.2节)。我们要介绍的View的布局方式的类,都是直接或间接继承自ViewGroup类,如下图所示:

android UI界面设计

Android UI开发专题(一) 之界面设计 发帖日期:2010-02-09 10:49:28 标签:ophone 近期很多网友对Android用户界面的设计表示很感兴趣,对于Android UI开发自绘控件和游戏制作而言掌握好绘图基础是必不可少的。本次专题分10节来讲述,有关OpenGL ES相关的可能将放到以后再透露。本次主要涉及以下四个包的相关内容: android.content.res 资源类 android.graphics 底层图形类 android.view 显示类 android.widget 控件类 一、android.content.res.Resources 对于Android平台的资源类android.content.res.Resources可能很多网友比较陌生,一起来看看SDK上是怎么介绍的吧,Contains classes for accessing application resources, such as raw asset files, colors, drawables, media or other other files in the package, plus important device configuration details (orientation, input types, etc.) that affect how the application may behave.平时用到的二进制源文件raw、颜色colors、图形drawables和多媒体文件media的相关资源均通过该类来管理。 int getColor(int id) 对应res/values/colors.xml Drawable getDrawable(int id) 对应res/drawable/ XmlResourceParser getLayout(int id) 对应res/layout/ String getString(int id) 和CharSequence getText(int id) 对应 res/values/strings.xml InputStream openRawResource(int id) 对应res/raw/ void parseBundleExtra (String tagName, AttributeSet attrs, Bundle outBundle) 对应res/xml/ String[] getStringArray(int id) res/values/arrays.xml float getDimension(int id) res/values/dimens.xml 二、android.graphics.Bitmap 作为位图操作类,Bitmap提供了很多实用的方法,常用的我们总结如下: boolean compress(https://www.360docs.net/doc/fa15798836.html,pressFormat format, int quality, OutputStream stream) 压缩一个Bitmap对象根据相关的编码、画质保存到一个OutputStream中。其中第一个压缩格式目前有JPG和PNG void copyPixelsFromBuffer(Buffer src) 从一个Buffer缓冲区复制位图像素

Android_布局详解【图文】

Android 布局详解【图文】 Android 布局是开发中非常重要的一个知识部分,它的布局分为以下几种: Linear Layout:线性布局 Relative Layout:相对布局 Table Layout:表格布局 FrameLayout AbsoluteLayout Grid View:网格布局 Tab Layout:选项卡布局 List View:列表布局 一、Linear Layout 简单来说,直着排,横着排都可以,还可以嵌套,此布局运用的非常多。下面直接上示例代码及截图:

接下来,看一下布局XML文件: