如何获取 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,对画布没有任何改变。

Drawable类

Drawable是一个通用的抽象类,它的目的是告诉你什么东西是可以画的。你会发现基于Drawable类扩展出各种绘图的类,见下面的表格,当然你可以继承它来创建你自己的绘图类.

Interfaces

Animatable Interface that drawables suporting animations should implement.
Drawable.Callback Implement this interface if you want to create an animated drawable that extends Drawable.

Classes

AnimationDrawable An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.
BitmapDrawable A Drawable that wraps a bitmap and can be tiled, stretched, or aligned.
ClipDrawable A Drawable that clips another Drawable based on this Drawable's current level value.
ColorDrawable A specialized Drawable that fills the Canvas with a specified color, with respect to the clip region.
Drawable 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.ConstantState  
DrawableContainer  
DrawableContainer.DrawableContainerState  
GradientDrawable A Drawable with a color gradient for buttons, backgrounds, etc.
InsetDrawable A Drawable that insets another Drawable by a specified distance.
LayerDrawable A Drawable that manages an array of other Drawables.
LevelListDrawable A resource that manages a number of alternate Drawables, each assigned a maximum numerical value.
NinePatchDrawable A resizeable bitmap, with stretchable areas that you define.
PaintDrawable Drawable that draws its bounds in the given paint, with optional rounded corners.
PictureDrawable Drawable subclass that wraps a Picture, allowing the picture to be used whereever a Drawable is supported.
RotateDrawable

A Drawable that can rotate another Drawable based on the current level value.

ScaleDrawable A Drawable that changes the size of another Drawable based on its current level value.
ShapeDrawable A Drawable object that draws primitive shapes.
ShapeDrawable.ShaderFactory Base class defines a factory object that is called each time the drawable is resized (has a new width or height).
StateListDrawable Lets you assign a number of graphic images to a single Drawable and swap out the visible item by a string ID value.
TransitionDrawable An extension of LayerDrawables that is intended to cross-fade between the first and second layer.

有三种方法可以定义和实例化一个Drawable:保存一个图片到你工程资源中,使用XML文件来描述Drawable属性或者用一个正常的类去构造。下面我们将讨论两种技术(对一个有开发经验的开发者来说构造并不是最新的技术)。

从资源图像文件中创建

一个比较简单的方法是添加一个图片到你的程序中,然后通过资源文件引用这个文件,支持的文件类型有PNG(首选的) JPG(可接受的)GIF(不建议),显然这种对于显示应用程序的图标跟来说是首选的方法,也可以用来显示LOGO,其余的图片可以用在例如游戏中。

把一个图片资源,添加你的文件到你工程中res/drawable/目录中去,从这里,你就可以引用它到你的代码或你的XML布局中,也就是说,引用它也可以用资源编号,比如你选择一个文件只要去掉后缀就可以了(例如:my_image.png 引用它是就是my_image)。

注意:SDK指出,为了缩小图片的存储空间,在Build的时候又可能对图片进行压缩,如果不想被压缩,可以将图片放在res/raw/目录中。

SDK给出的例子:

LinearLayout mLinearLayout;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// Create a LinearLayout in which to add the ImageViewmLinearLayout = new LinearLayout(this);// Instantiate an ImageView and define its propertiesImageView i = new ImageView(this);i.setImageResource(R.drawable.my_image);i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensionsi.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));// Add the ImageView to the layout and set the layout as the content viewmLinearLayout.addView(i);setContentView(mLinearLayout);
}

获取Drawable对象:

Resources res = mContext.getResources();
Drawable myImage = res.getDrawable(R.drawable.my_image);

注意:保持每个资源类型的一至,可以保证你项目状态的一致性,就不用担心有许多不同类型的对象来实例化它。例如:如果使用相同的图像资源来实例化两个Drawable对象。然后修改一个Drawables的属性(例如alpha),然后不幸得是这个效果也会出现在另一个对象上去。所以当处理同一个资源的多个实例对象时,不是直接转换为Drawable,而是应该执行tween animation

如何添加资源到ImageView:
<ImageView   android:layout_width="wrap_content"android:layout_height="wrap_content"android:tint="#55ff0000"android:src="@drawable/my_image"/>
 
从XML文件中创建

到如今,你应该比较熟悉按Android的原则去开发一个用户接口,因此,你也应该理解了定义一个XML文件对于对象的作用与灵活的重要性。这个理念无数次用于Drawables.

如果你想创建一个Drawable对象,而这个对象并不依赖于变量或用户的交换,把它定义到XML中去应该是一个不错的方法。即使你期望在你的应用程序中改变其属性来增加用户体验。你应该考虑把对象放入XML中,因为你可以随时修改其属性

当你在你的XML中定义了一个Drawable,保存这个XML文件到你工程目录下res/drawable目录中,然后通过调用Resource.getDrawable()来检索并实例化,传递给它XML文件中的资源ID号。任何Drawable的子类都支持inflate这个方法,这个方法会通过XML来实例化你的程序。任何Drawable都支持XML的扩展来利用特殊的XML属性来帮助定义对象的属性,可以查看任何Drawable子类文档来看如何定义XML文件。

如下定义了一个TransitionDrawable:An extension of LayerDrawables that is intended to cross-fade between the first and second layer. It can be defined in an XML file with the <transition> element. Each Drawable in the transition is defined in a nested <item>. 有关TransitionDrawable的详细信息查看http://androidappdocs.appspot.com/reference/android/graphics/drawable/TransitionDrawable.html。

将其定义在res/drawable/expand_collapse.xml:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/pic1"/><item android:drawable="@drawable/pic2"/>
</transition>

下面实例化并处理:

public class MainActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Resources res=getResources();TransitionDrawable trans=(TransitionDrawable )res.getDrawable(R.drawable.expand_collapse);ImageView image = (ImageView)findViewById(R.id.ImageView01);image.setImageDrawable(trans);trans.startTransition(3000);}
}
ShapeDrawable

当你想去画一些动态的二维图片,一个ShapeDrawable对象可能会对你有很大的帮助。通过ShapeDrawable,你可以通过编程画出任何你想到的图像与样式。

ShapeDrawable继承了Drawable, 所以你可以调用Drawable里有的函数,比如视图的背景,通过setBackgroundDrawable()设置。当然,你可以在自定义的视图布局中画你的图形,因为ShapeDrawable有自己的draw()方法。你可以在View.OnDraw()方法期间创建一个视图的子类去画ShapeDrawable。

ShapeDrawable类(像很多其他Drawable类型在android.graphics.drawable包)允许你定义drawable公共方法的各种属性。有些属性你可以需要调整,包括透明度,颜色过滤,不透明度,颜色。

例子:

publicclassCustomDrawableViewextendsView{    privateShapeDrawable mDrawable;

    publicCustomDrawableView(Context context){        super(context);

        int x =10;        int y =10;        int width =300;        int height =50;

        mDrawable =newShapeDrawable(newOvalShape());        mDrawable.getPaint().setColor(0xff74AC23);        mDrawable.setBounds(x, y, x + width, y + height);    }

    protectedvoid onDraw(Canvas canvas){        mDrawable.draw(canvas);    }

显示:

CustomDrawableView mCustomDrawableView;

protectedvoid onCreate(Bundle savedInstanceState){    super.onCreate(savedInstanceState);    mCustomDrawableView =newCustomDrawableView(this);

    setContentView(mCustomDrawableView);}

在XML中定义方法:如果你想用XML文件配置来取代原有布局来画自定义的drawable,于是你自定义的Drawable类必须重载view (Context, AttributeSet) 构造函数。

<com.example.shapedrawable.CustomDrawableView    android:layout_width="fill_parent"     android:layout_height="wrap_content"     />
NinePatchDrawable

NinePatchDrawable 绘画的是一个可以伸缩的位图图像,Android会自动调整大小来容纳显示的内容。一个例子就是NinePatch为背景,使用标准的Android按钮,按钮必须伸缩来容纳长度变化的字符

NinePatchDrawable是一个标准的PNG图像,它包括额外的1个像素的边界,你必须保存它后缀为.9.png,并且保持到工程的res/drawable目录中。

这个边界是用来确定图像的可伸缩和静态区域。你可以在左边和上边的线上画一个或多个黑色的1个像素指出可伸缩的部分(你可以需要很多可伸缩部分),它的相对位置在可伸缩部分相同,所以大的部分总是很大的。

你还有可以在图像的右边和下边画一条可选的drawable区域(有效的,内边距线)。如果你的视图对象设置NinePath为背景然后指定特殊的视图字体,它将自行伸缩使所有的文本来适应根据右线与底部线设计好的区域(如果有的话),当然内边距线不包括其中,Android可以使用左边的线与上面的线来定义一个drawable区域。

我们来澄清一下这两条不同的线,左边跟顶部的线来定义哪些图像的像素允许在伸缩时被复制。底部与右边的线用来定义一个相对位置内的图像,视图的内容就放入其中。

使用方法:

<Buttonid="@+id/tiny"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerInParent="true"        android:text="Tiny"        android:textSize="8sp"        android:background="@drawable/my_button_background"/>

<Buttonid="@+id/big"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerInParent="true"        android:text="Biiiiiiig text!"        android:textSize="30sp"        android:background="@drawable/my_button_background"/>

Android Drawable绘图学习笔记相关推荐

  1. android - Drawable - ColorDrawable 学习笔记

    ColorDrawable 使用比较简单,就是一个使用单色填写整个画面的 Drawable 对象.使用起来很简单,在 XML 中定义: <resources> <drawable n ...

  2. Android编译及编译脚本、Android构建基础学习笔记

    Android编译及编译脚本.Android构建基础学习笔记 Android编译及编译脚本 概述 Android.mk转换成Android.bp 例子(简单Android.mk文件转Android.b ...

  3. Android开发技术学习笔记

    目标:全面学习知识点,能理解原理,多实战练习并做笔记与总结学习心得 Android开发技术学习笔记记录如下: Android开发Google的官网https://developer.android.g ...

  4. Oxyplot实时绘图学习笔记(下)

    (接上帖) 绘图控件的相关设置 简略监测区域图表 原项目使用的实时绘图逻辑是"当绘制完十个点后,其后每画一个新点,就将第一个点删除"从而达成实时绘图的效果. 但是这个逻辑很显然会导 ...

  5. Android Drawable Resource学习(五)、StateListDrawable

     一个StateListDrawable就是一个在xml文件中定义,根据该对象不同的状态,用几张不同的图片来代表相同的图形.比如,一个按钮,有多种状态,获取焦点,失去焦点,点击等等,使用State ...

  6. Android V7包学习笔记更新中.....

    关于V4 V7 V13 VX包介绍转自这里 1, Android Support V4, V7, V13是什么? 本质上就是三个java library. 2, 为什么要有support库? 如果在低 ...

  7. Android底层开发学习笔记 第一天

    今天,开始学习Android底层的开发.(注:我是新手小白,纯粹学习笔记,大神们可以直接略过了) 第一部分 编译运行我的第一个Android system. 准备工作: 下载源码:包括u-boot源码 ...

  8. Android Drawable Resource学习(一)、Drawable Resource简介

    Drawable Resource 一个Drawable资源是绘图中的一个普通概念,可以再屏幕上绘制出来.可以通过APIs中的getDrawable(int)方法检索出来,也可以在其他的XML资源中通 ...

  9. android路径动画学习笔记

    先上效果图: 效果图由于是用studio录制视频,然后转码gif,再经过photoshop裁剪,有些颜色已经丢失,也没有实际效果那么流畅了. 这种线条动画,完全由android 原生的SDK就可以做出 ...

最新文章

  1. 何恺明大神新作--UnNAS:无监督神经网络架构搜索
  2. git清空工作区和暂存区
  3. 基于cmake28来编译安装mysql服务配置解析
  4. Django框架深入了解_04(DRF之url控制、解析器、响应器、版本控制、分页)(一)
  5. Hive belline提交命令Error: org.apache.thrift.transport.TTransportException: java.net.SocketException:
  6. Ubuntu 14.10 下运行进程实时监控pidstat命令详解
  7. 华为畅享max有没有人脸识别_华为畅享7s有人脸识别吗 让我来告诉你
  8. js函数引用、函数调用与回调函数
  9. 2018一季度app Top500榜单,谁在抢线上最后的流量?
  10. 视觉SLAM十四讲学习笔记-第三讲-相似、仿射、射影变换和eigen程序、可视化演示
  11. Markdown笔记神器Typora配置Gitee图床
  12. Qt编程之mapx组件编程
  13. 绝对经典英语学习资料
  14. python批量获取百度贴吧_python网络爬虫案例:批量爬取百度贴吧页面数据
  15. protues仿真器51单片机定时器内部定时方式
  16. CentOS7 yum 安装 maven
  17. 用户访谈与问卷调查怎么做
  18. flash 调试版本
  19. 2010年度 中国WEB应用防火墙厂家和产品大全
  20. Hive面试基本须知

热门文章

  1. js中select下拉框重置_如何利用CSS3制作炫酷的下拉框
  2. 区块链中的密码学,使用ABE结合区块链
  3. 英语一段落排序题技巧
  4. 起步前要做哪些准备?
  5. 解决:If the number of processors is expected to increase from one, then you should configure the numbe
  6. Struts2中<s:iterator>基本用法及示例
  7. C# 打开文件/跳转链接
  8. html 基本布局介绍
  9. WKWebview加载本地图片时出现路径问题
  10. 表单验证的初步实现和省市级联