Android Drawable绘图学习笔记

如何获取 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

AnimatableInterface that drawables suporting animations should implement.
Drawable.CallbackImplement this interface if you want to create an animated drawable that extends Drawable.

Classes

AnimationDrawableAn 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.
BitmapDrawableA Drawable that wraps a bitmap and can be tiled, stretched, or aligned.
ClipDrawableA Drawable that clips another Drawable based on this Drawable's current level value.
ColorDrawableA specialized Drawable that fills the Canvas with a specified color, with respect to the clip region.
DrawableA 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 
GradientDrawableA Drawable with a color gradient for buttons, backgrounds, etc.
InsetDrawableA Drawable that insets another Drawable by a specified distance.
LayerDrawableA Drawable that manages an array of other Drawables.
LevelListDrawableA resource that manages a number of alternate Drawables, each assigned a maximum numerical value.
NinePatchDrawableA resizeable bitmap, with stretchable areas that you define.
PaintDrawableDrawable that draws its bounds in the given paint, with optional rounded corners.
PictureDrawableDrawable 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.

ScaleDrawableA Drawable that changes the size of another Drawable based on its current level value.
ShapeDrawableA Drawable object that draws primitive shapes.
ShapeDrawable.ShaderFactoryBase class defines a factory object that is called each time the drawable is resized (has a new width or height).
StateListDrawableLets you assign a number of graphic images to a single Drawable and swap out the visible item by a string ID value.
TransitionDrawableAn 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区域。

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

image

使用方法:

<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"/>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/463336.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在类中调用delete this问题

转载&#xff1a;https://blog.csdn.net/kuimzzs/article/details/81517451 很多时候&#xff0c;一些定义在类内的变量的生命周期在类外并不是很好的掌控&#xff0c;这样就很容易造成内存泄漏得到问题 比如以下代码&#xff1a; class Test { public: void foo(); pri…

C++中对多态的理解

1 多态&#xff1a;是指类中具有相似功能的不同函数&#xff0c;使用同一个名称来实现&#xff1b;是对类的行为再抽象&#xff1b;多态是通过重载函数和虚函数来实现的。 2 继承讨论的是类与类的层次关系&#xff0c;多态则是考虑在不同层次的类中&#xff0c;以及在同一个类…

C#.NET中的事件2

/** Created by SharpDevelop. * User: noo * Date: 2009-8-17 * Time: 15:34 * * 事件2 */usingSystem ;usingSystem .Windows .Forms ;classTest { staticvoidMain()//入口函数{ Form frmnewForm ();//新建一窗体frm.Text "我的窗体"; …

HTML学习之基础

HTML是网页的标记语言不是编程语言&#xff0c;有一些标记段组成。大小写不敏感&#xff0c;可以用常用的编辑器软件编写用浏览器打开即可 有不同的版本<!DOCTYPE html> <meta charset"utf-8">在<title>前为了能正常显示中文。 超链接&#xff1a;…

linux最基础的几个指令

cd 切换目录使用 pwd 显示当前目录路径 ls 列出目录内容 跟参数 -a 显示所有文件和目录 -A 显示所有文件包括隐藏-l 使用详细格式列出-d 显示目录名称而非内容-i 显示文件和目录的inode号-Z 显示文件和目录的上下文-R 子目录显示touch 创建文件或者改变文件时间 touch 2…

Advapi 登录类型8的错误

登录类型8&#xff1a;网络明文&#xff08;NetworkCleartext&#xff09; 这种登录表明这是一个像类型3一样的网络登录&#xff0c;但是这种登录的密码在网络上是通过明文传输的&#xff0c;WindowsServer服务是不允许通过明文验证连接到共享文件夹或打印机的&#xff0c;据我…

Varint

什么是Varint Varint 是一种紧凑的表示数字的方法。它用一个或多个字节来表示一个数字&#xff0c;值越小的数字使用越少的字节数。这能减少用来表示数字的字节数。 比如对于 int32 类型的数字&#xff0c;一般需要 4 个 byte 来表示。但是采用 Varint&#xff0c;对于很小的 …

Ubuntu 17.10安装Qt 5.10环境与Qt Creator 4.5开发工具(转自linux公社)

记录下在Ubuntu 17.10搭建Qt环境与安装Qt Creator开发工具的过程。机器装的Linux是Ubuntu 17.10&#xff0c;16.04与17.04的应该也相同。Qt 5.10和Qt 3D Studio发布 http://www.linuxidc.com/Linux/2017-12/149267.htm 1&#xff0c;Qt安装 1.1 下载Qt 这里提供Qt Creator的下…

服务器USB启动故障一例

前几日&#xff0c;本公司一台prop平台服务器&#xff08;hp 380G5服务器&#xff09;接上key&#xff08;usb口&#xff09;后&#xff0c;出现重启不能启动现象&#xff0c;进入服务器bios设置启动项为硬盘启动也不行&#xff0c;之后仔细查看服务器usb控制选项&#xff1a;可…

C#基础(201)--常量枚举

本文知识点&#xff1a; 1.掌握常量的定义和使用方法 2.理解枚举的作用和特点 3.掌握枚举的使用方法 1.1.常量的定义语法 const 数据类型 常量名称 值&#xff1b; 1.2.常见错误 1.3常量的使用时机 经常使用并且值不变的变量&#xff0c;可以定义为常量 2.1枚举的作用及其…

自定义EL函数解决JSTL标签不足之处——按字节长度截取字符串

题外话&#xff1a;做过大型网站前台的Web开发程序员最头疼的事情就是前台首页的布局&#xff0c;这其中最最头疼的就是标题字符的截取的问题&#xff0c;虽然有fn:substring&#xff0c;但表现率以及对中文的支持均不够完善&#xff0c;其实最终还是归结为客户要求太高了&…

进程之父子进程的关系

转载&#xff1a;https://blog.csdn.net/qq_28840229/article/details/79844763 fork之后&#xff1a; 父子相同处: 全局变量、.data、.text、栈、堆、环境变量、用户ID、宿主目录、进程工作目录、信号处理方式... 父子不同处: 1.进程ID 2.fork返回值 3.父进程ID 4.进…

C语言快速排序算法

void sort(int *a, int left, int right) {if(left > right)/*如果左边索引大于或者等于右边的索引就代表已经整理完成一个组了*/{return ;}int i left;int j right;int key a[left];while(i < j) /*控制在当组内寻找一遍*/{while(i &l…

unbuntu使用经典界面

为什么80%的码农都做不了架构师&#xff1f;>>> 昨天升级到UBUNTU 11.04, 发现新的Unity界面很不适应&#xff0c;于是将其恢复到旧式经典界面&#xff0c;具体操作模式方法如下&#xff1a; 在已经登录的状态下&#xff0c;选择 [注销]然后在重新登录的时候&#…

学习:深入浅出之正则表达式(转)

本文是Jan Goyvaerts为RegexBuddy写的教程的译文&#xff0c;版权归原作者所有&#xff0c;欢迎转载。但是为了尊重原作者和译者的劳动&#xff0c;请注明出处&#xff01;谢谢&#xff01; 1. 什么是正则表达式 基本说来&#xff0c;正则表达式是一种用来描述一定数量文本…

C语言判断计算机存储是大端还是小端

计算机的一个int型数据是用多个字节表示的&#xff0c;如果在内存中存放时标称该数据的地址中存放的是数据的最低字节&#xff0c;就叫小端机&#xff0c;反之把高字节存放在标称地址中的则称为大端机。所以可以定义一个int变量&#xff0c;将其地址强制为char *型地址&#xf…

服务器开发中网络数据分析与故障排查经验漫谈

转载大牛的文章&#xff1a; https://mp.weixin.qq.com/s/6ZmiKUz4ZQvwLXkzzBrFcg

每天干的啥?(2019.3)

【本年目标】 【本月目标】【本月总结】 【3月每日记录】 去洗照片24张9寸&#xff0c;研究手机root&#xff08;y66不支持啊&#xff09;&#xff0c;下午党学习照相、看完第4课blake《用户数据ugame》、研究轮滑gif、晚上钢琴首次调律。 去妈妈那吃饭&#xff0c;学钢琴、佰洲…

【Andorid X 项目笔记】禁用ListView的Fling功能(1)

前言新的项目正在紧张开发中&#xff0c;初步估计2个月时间开发完成第一版&#xff0c;我负责Android端开发&#xff0c;由于不便过早公布&#xff0c;本系列将命名为“X项目笔记”&#xff0c;并于项目结束后最终公布名称。本系列主要记录与分享"X项目"中遇到的问题…

C语言题目地图上有m个城市,序号依次为1,2,3....m,刚开始你在1,若每次只能从当前城市去往当前序号加1或者加3的城市,要到达m城市(m3),有多少种走法

地图上有m个城市&#xff0c;序号依次为1,2,3....m&#xff0c;刚开始你在1&#xff0c;若每次只能从当前城市去往当前序号加1或者加3的城市&#xff0c;要到达m城市&#xff08;m>3&#xff09;,有多少种走法 要想到达m&#xff0c;则前一个应该是m - 1或者m -3&#xff1b…