首先,如何获取 res 中的资源
数据包package:android.content.res
主要类:Resources
Android SDK中的简介:Class for accessing an application’s resources.Class for accessing an application’s resources. This sits on top of the asset manager of the application (accessible through getAssets()) and provides a higher-level API for getting typed data from the assets.
其主要接口按照功能,划分为以下三部分:
getXXXX()
例如:
int getColor(int id)
Drawable getDrawable(int id)
String getString(int id)
|
直接获取res中存放的资源 |
InputStream openRawResource(int id)
|
获取资源的数据流,读取资源数据
|
void parseBundleExtras(
XmlResourceParser parser, Bundle outBundle) |
从XML文件中获取数据 |
Resource为每种资源提供了相应的接口来获取这种资源,除了可以直接获取资源外,还额外提供了以数据流的方式获取资源,这在以后的应用程序开发中会经常使用,那么如何获取Resources了,如下:Resources r = this.getContext().getResources();
其次,如何获取资源中的画图对象
数据包package:android.graphics.drawable
主要类:Drawable
Android SDK中的简介:A Drawable is a general abstraction for “something that can be drawn.” Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms.
看了以上简介,发现Drawable是个virtual class,具体如何画图,需要具体分析Drawable的子类,例如:BitmapDrawable
Android SDK中的简介:A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object. It can be defined in an XML file with the <bitmap>
element.
其主要接口如下:
BitmapDrawable()
BitmapDrawable(Bitmap bitmap)
BitmapDrawable(String filepath)
BitmapDrawable(InputStream is)
|
void draw(Canvas canvas)
|
Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).
|
final Bitmap getBitmap() |
final Paint getPaint() |
Drawable是个抽象类,在BitmapDrawable中我们就看到位图的具体操作,在仔细看下BitmapDrawable的构造函数,我们就会发现与Resource中的openRawResource()接口是相对应的,就可以通过以下方法来获取位图:
Resources r = this.getContext().getResources();
Inputstream is = r.openRawResource(R.drawable.my_background_image);
BitmapDrawable bmpDraw = new BitmapDrawable(is);
Bitmap bmp = bmpDraw.getBitmap();
然后,看几个常用的辅助类
- Paint
数据包package:android.graphics
Android SDK中的简介:The Paint class holds the style and color information about how to draw geometries, text and bitmaps. 主要就是定义:画刷的样式,画笔的大小/颜色等。
- Typeface
数据包 package:android.graphics
Android SDK中的简介:The Typeface class specifies the typeface and intrinsic style of a font. 主要就是定义:字体。
最后,核心类显示资源
数据包package:android.graphics
主要类:Canvas
Android SDK中的简介:The Canvas class holds the “draw” calls. To draw something, you need 4 basic components: 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).
按照结构的功能,将主要接口分为以下3部分:
boolean clipXXXX() |
Region区域操作:
DIFFERENCE
INTERSECT
REPLACE
REVERSE_DIFFERENCE
UNION
XOR |
void drawXXXX() |
画图函数 |
void rotate()
void scale()
void skew()
void translate() |
画布操作函数 |
Region在这里需要特殊说明下:Region就是一个区域,也就是画布(Canvas)中的有效区域,在无效区域上draw,对画布没有任何改变
另外一种方法,可以切割源图
bt = Bitmap.createBitmap(40, 40, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bt);
Resources r = this.getContext().getResources();
Drawable pic=r.getDrawable(R.drawable.icon);
pic.setBounds(0, 0, 40, 40);
pic.draw(canvas);
分享到:
相关推荐
Android画图学习总结(五)——Paint_玉树临疯_ 京华志 京华志出品 必数精华
• Android画图学习总结(一)——类的简介 • Android画图学习总结(二)——Bitmap • Android画图学习总结(三)——Drawable • Android画图学习总结(四)——Animation(上) • Android画图学习总结(四...
Android画图学习总结(二)-转_玉树临疯_www.jinghuazhi.com 京华志出品 必属精华 网站(www.jinghuazhi.com)可免费下载每日最新资源
Android画图学习总结(一)——类的简介 • Android画图学习总结(二)——Bitmap • Android画图学习总结(三)——Drawable • Android画图学习总结(四)——Animation(上) • Android画图学习总结(四)...
在本章的拓展思考部分,笔者提出了几个问题,旨在激发读者深入思考和学习Android的欲望。 本书特色 笔者认为,本书最大的特点在于,较全面、系统、深入地讲解了Android系统中的几大重要组成部分的工作原理...
10.2.3 android.process.media媒体扫描工作的流程总结 10.3 MediaScanner分析 10.3.1 Java层分析 10.3.2 JNI层分析 10.3.3 PVMediaScanner分析 10.3.4 关于MediaScanner的总结 10.4 拓展思考 10.4.1 ...
10.2.3 android.process.media媒体扫描工作的流程总结 / 471 10.3 MediaScanner分析 / 472 10.3.1 Java层分析 / 472 10.3.2 JNI层分析 / 476 10.3.3 PVMediaScanner分析 / 479 10.3.4 关于MediaScanner的总结 / 485 ...
在学习android中图形图像处理技术这部分内容时,对绘制圆弧函数canvas.drawArc()的用法、参数含义及画图原理很是不理解,在网上搜索了一些,加上自己的理解,在此做个小总结,下面来一起看看吧。 示例代码 public ...