Android 资源文件

当你还不能写出自己满意的程序时,你就不要去睡觉


好久没写博客了,最近项目实在太紧了,所以在博客这里花的时间就少了很多。不过以后的每周末都会更新一篇文章,不管时间多么

紧张,都会去找时间学习一些知识拿出来分享。以后的每次博客都会写上一句话来激励这个不怎么优秀的自己。下面进入正文。


今天主要讲的是我们 Android 中的资源文件,其实主要是我们的自定义的一些 Drawable 文件,还有我们的自定义属性。那我们下面开始说。


一、自定义属性

       

       自定义属性基本大部分用到的地方是我们的自定义控件,在我们自定义控件的时候,我们可以在 xml 中设置自定义控件的自定义属性,然后在自定义控件中获取这个设置的属性值去设置控件的大小、展示等。

       那这个自定义属性要怎么定义和使用呢?分为以下几个步骤:

              1、在我们的 values 下面新建一个 attrs.xml 文件,然后在这个文件中去定义我们的属性。

              2、我们新建一个属性的名字通过 attr 这个标签

              3、在我们的 xml 中定义一个类似于xmlns:android="http://schemas.android.com/apk/res/android"

              4、在我们的 xml 去使用这个属性

              5、最后我们在自定义控件中获取这个属性并使用下面看代码


<resources>
    <declare-styleable name="image">
        <attr name="imageDurction" format="integer"/>
        <attr name="durction" format="integer"/>
        <attr name="traslate" format="integer"/>
    </declare-styleable>
    <declare-styleable name="LeftScrolling">
        <attr name="aFew" format="integer" />
    </declare-styleable>
</resources>


这段代码中我们定义了四个属性,分别是 image 下面的 imageDurction,durction,traslate,还有 LeftScrolling 下面的 aFew属性,我们可以看到定义属性的时候我们需要先写一个 declare-styleable 属性,其实这个属性不是必须的,单是如果不写这个属性的话,在我们获取的时候需要写很多的代码,然后这个 declare-styleable 的定义一般是我们的控件的名字,这样的话也比较好区分我们定义的属性使用在哪个控件上面。这样我们就完成了 1 、2 步的工作。

这个 formagt 是设置属性的类型,那这个类型可以使很多种看下面的列表:


format取值 format说明 format读取
reference 资源ID attrs.getResourceId(R.styleable.View名称_attr名称, 默认值);
color 颜色值 attrs.getColor(R.styleable.View名称_attr名称, 默认值);
boolean 布尔值 attrs.getBoolean(R.styleable.View名称_attr名称, 默认值);
dimension 尺寸值 attrs.getDimension(R.styleable.View名称_attr名称, 默认值);
float 浮点值 attrs.getFloat(R.styleable.View名称_attr名称, 默认值);
integer 整型值 attrs.getInteger(R.styleable.View名称_attr名称, 默认值);
string 字符串 attrs.getString(R.styleable.View名称_attr名称);
fraction 百分比(%) attrs.getString(R.styleable.View名称_attr名称);
enum 枚举值 attrs.getInt(R.styleable.View名称_attr名称, 默认值);
flag 位或运算 attrs.getInt(R.styleable.View名称_attr名称, 默认值);



下面我们看 3、4 步的代码:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:andya="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

   
    <com.example.andya.resourcetest.MyImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        andya:durction="1000"
        andya:imageDurction="1000"
        andya:traslate="1000"/>
</LinearLayout>


从这段代码中我们可以看到我们首先定义了一个 andya ,然后又使用了 andya 属性设置了 durction 等属性的值,这样我们也就完成了我们 3、4 步的工作。


最后我们看一下获取的代码:



TypedArray typedArray = context.obtainStyledAttributes(R.styleable.image);
        int imageDurction = typedArray.getInt(R.styleable.image_imageDurction,0);
        int durction = typedArray.getInteger(R.styleable.image_durction,0);
        int traslate = typedArray.getInteger(R.styleable.image_traslate,0);



这就是获取的代码,其实获取的方式不仅这种方式,这种方式只是获取了我们指定的属性,还有一种方法是获取我们控件设置的全部属性的方法,下面看代码:


public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        int count = attrs.getAttributeCount();
        for (int i = 0; i < count; i++) {
            String attrName = attrs.getAttributeName(i);
            String attrVal = attrs.getAttributeValue(i);
            Log.e(TAG, "attrName = " + attrName + " , attrVal = " + attrVal);
        }
    }


当我们自定义控件的时候需要定义一个构造函数,那么这个构造函数里面会有一个 AttributeSet 的值,那么这个值也就是包含了这个控件设置的全部属性,通过这个属性我们可以得到我们设置的属性名和值,我们可以看到我们打印出来的值是:


MyImageView(4136): attrName = layout_width , attrVal = 150.0dp
MyImageView(4136): attrName = layout_height , attrVal = 150.0dip
MyImageView(4136): attrName = layout_gravity, attrVal = center
MyImageView(4136): attrName = durction, attrVal = 1000
MyImageView(4136): attrName = imageDurction, attrVal = 1000
MyImageView(4136): attrName = traslate, attrVal = 1000



属性的定义基本上差不多就这样,具体的内部实现可以看一下源码,源码真的是清晰无比。


二、stateListDrawable


这个属性主要作用是设置我们的 View 的点击背景或者不同状态的不同展示。

它主要的标签是 selector 我们看一下代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@color/colorAccent"/>
    <item android:state_pressed="false" android:drawable="@color/colorPrimary"/>
</selector>

从这段代码中我们可以看到两个 item ,那么这两个 item 的状态分别是按下为 true 和按下为 false。

那当我们给 view 设置这个 xml 为背景的时候,当点击和正常的时候回呈现不同的效果。

其实 item 中可以设置的状态有很多,例如:

state_active 是否处于激活状态
state_first 是否处于开始状态
state_window_focused 是否处于焦点状态
其实还有很多这里就不多说,在我们的 item 中还可以定义我们的 shapedrawable ,那这个 shapeDrawable 是什么我们下节会介绍。

三、shapeDrawable

那这个 shapeDrawable 是什么呢,例如我们有些时候的项目中需要给 View 设置椭圆背景,或者长方形、正方形等背景,或者是渐变颜色的背景都可以用到我们的 shapeDrawable 这个 shapeDrawable 写完之后基本可以相当于一个图片去使用,下面看代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:bottomLeftRadius="1dp"
        android:bottomRightRadius="1dp"
        android:topRightRadius="1dp"
        android:topLeftRadius="1dp"/>
    <gradient
        android:angle="1dp"
        android:centerColor="@color/colorAccent"
        android:centerX="1dp"
        android:centerY="1dp"
        android:endColor="@color/colorPrimary"
        android:gradientRadius="1dp"
        android:startColor="@color/colorPrimaryDark"
        android:type="radial"/>
    <stroke android:color="@color/colorPrimaryDark"/>
    <solid android:color="@color/colorPrimaryDark"/>

</shape>


可以看到这里面有四个子属性,其实还有几个我这里面列举了几个比较重要的,作用分别是圆角、渐变色、边框颜色、和填充颜色。那这个 android:shape 这个属性也就是指定了我们画的形状是什么,可以是线,矩形等。

这个 stroke 我要说一下,这个属性里面有一个 dashGap 和 dashWidth 属性,通过这两个属性可以设置虚线。



四、ClipDrawable


主要就是截取位图,通常就是做一点一点展开的图片等

下面看代码:

<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/back_home"
    android:clipOrientation="vertical"
    android:gravity="clip_horizontal">

</clip>

final ClipDrawable drawable = (ClipDrawable)imageView.getDrawable();
        final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                drawable.setLevel(drawable.getLevel()+200);
            }
        };
       
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                handler.sendMessage(message);
                if(drawable.getLevel() >= 10000){
                    timer.cancel();
                }
            }
        },0,300);


指定截取方向
android:clipOrientation="" horizontal | vertical
指定截取时对其方式 大概的意思就是截取从哪里开始
android:gravity="" bottom| left | right | center_vertical | fill_vertical | clip_vertical | clip_horizontal | fill

五、 LayerDrawable:

这个 Drawable 主要是定义 View 的多图片显示,可以实现层叠和阴影效果。下面说一下几个属性
android:drwable 指定一个drawable
android:id 为该 drawable 指定一个标识,也就是这个 drawable 作用于哪里。
android:buttom|top|left|button 用于设置该 drawable 对象绘制到目标组件的指定位置。
下面看代码:
<layer-list xmlns **********************>
//定义背景图片
<item android:id="@android:id/background"
   android:drawable="@drawable/grow">
<item android:id="@android:id/process"
   android:drawable="@drawable/ok">
</layer-list>

六、几个特殊的资源文件

样式(style):定义一个整体的样式
<style>
可以在 styles 里面定义一个 style ,style 里面有一个 name 和一个 parent,这个 Style 可以定义 View 的所有属性,你也可以理解这个 style 是相同控件的属性集合。 windowframe 可以设置边框的

主题(Theme):
创建项目的时候回有一个 <style name="AppBaseTheme" parent="android:Theme.Light">
然后我们自定义 Theme 的时候需要继承这个主题就可以了。
在主题中可以设置一些 window 的属性,如果你想写一个透明的 Activity 需要在我们的 Theme 中设置,
因为我们的 Activity 最终是放在 window 上面的,我们要把这个 window 设置为透明。
Theme 可以在 AndroidManifest 中设置,也可以在setContentView 前面设置
Theme 不可以设置在单个View 上面。


总结:
当初写这篇文章的时候,其实大部分知识已经了解了,只不过有些属性不是很熟,所以写了这篇文章加深记忆,然后资源的定义我们基本上说的差不多了,那资源的获取其实也是很简单的我们只需要通过 resource 类获取就可以了,可以通过 getString、getColor 等方法获取,那这篇文章基本就说到这里了,如有错误欢迎指出, Thanks。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值