Color About——Second

下面来简要的说一下Android开发中如何对某一个Activity进行背景色的设置。下面我以名字为FirstActivity的Activity的背景色的设置进行说明,先说一下Drawable类:

关于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. Unlike a View, a Drawable does not have any facility to receive events or otherwise interact with the user.

Though usually not visible to the application, Drawables may take a variety of forms:

  • Bitmap: the simplest Drawable, a PNG or JPEG image.
  • Nine Patch: an extension to the PNG format allows it to specify information about how to stretch it and place things inside of it.
  • Shape: contains simple drawing commands instead of a raw bitmap, allowing it to resize better in some cases.
  • Layers: a compound drawable, which draws multiple underlying drawables on top of each other.
  • States: a compound drawable that selects one of a set of drawables based on its state.
  • Levels: a compound drawable that selects one of a set of drawables based on its level.
  • Scale: a compound drawable with a single child drawable, whose overall size is modified based on the current level.

更多详情请参考官方网站:

https://developer.android.com/reference/android/graphics/drawable/Drawable.html

-----------------------------------------------------------------------------

对官方的介绍做一个简要的说明(笔者英语不是非常擅长,以下翻译仅供参考):

Drawable就是一个可画的对象,你经常需要按照绘画到屏幕上的资源类型对Drawable进行处理,Drawable类提供一个通用的API,以处理各种格式的可视化资源。Drawable类与View类不同,因为Drawable类没有任何功能用于接收事件或者与用户进行交互。

Drawable类有以下几种类型:

-->Bitmap:最简单的Drawable,资源是PNG或者JPEG图片(官方写到:Android supports bitmap files in a three formats: .png (preferred), .jpg (acceptable), .gif(discouraged).也就是说最好是PNG格式的图片,其次是JPEG格式,最好不要是GIF格式图片)

-->Nine Patch:PNG格式的扩展,可以声明在其内部如何伸缩和放置。

-->Shape:包含简单的绘画命令以修改bitmap中的资源,允许调整资源大小尺寸以更好的适应对应的情况。

-->Layers:一个复合的drawable,在多个图层中,对每一个图层都可以进行绘制。

-->States:一个复合的drawable,基于它自身的情况,选择drawables集中的一个。

-->Levels:一个复合的drawable,基于它自身的级数,选择drawables集中的一个。

-->Scale:一个单一的带有子drawable的drawable,它的总尺寸基于当前等级进行修改。

总之我们根据画图的需求,创建相应的可画对象,就可以将这个可画对象当作一块“画布(Canvas)”,在其上面操作可画对象,并最终将这种可画对象显示在画布上。

-----------------------------------------------------------------------------

好了,进入正题:

1:打开FirstActivity.java的布局文件activity_first.xml,

这一步在xml布局文件中声明。

加入语句:

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

android:background="@drawable/bgcolor"

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

2:我们在上一步中声明的变量bgcolor需要进行赋值,赋值在res/value/strings.xml中进行。打开strings.xml,添加语句:

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

<drawable name="bgcolor">#F0E68C</drawable> 

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

3:基本步骤搞定后,敲入java代码,打开FirstActivity.java,输入一下代码:

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

//得到Resources类的对象

        Resources res = getResources(); 

//得到Drawable类的对象
        Drawable dra = res.getDrawable(R.drawable.bgcolor);

//设置当前窗口的背景色 
        this.getWindow().setBackgroundDrawable(dra);

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。


作者:KillerLegend
出处:http://www.cnblogs.com/KillerLegend/
分享最新的资源,分享个人所得,欢迎关注我的新浪微博
新浪微博主页:ikey4u
我的个人博客:www.ikey4u.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 
原文地址:https://www.cnblogs.com/killerlegend/p/3240820.html