安卓资源文件Resource简述(一)

Android资源的分类
这里写图片描述

Resources

用于获取android资源的类。获取实例的方法可以用类方Resources.getSystem()和context.getResources()方法。获取到实例后可以根据要获取的资源类型来使用响应的方法获取资源。

1.Resources resources=MainActivity.this.getResources();
2.Resources resources=Resources.getSystem();
3.public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config)
4.public Resources(@Nullable ClassLoader classLoader)

当获取到实例后,使用方法很多。

1.获取字符 文本 数组 Drawable movie 颜色  甚至可以根据内容获取输入流等。
public CharSequence getText(@StringRes int id)
public String getString(@StringRes int id)
public CharSequence[] getTextArray(@ArrayRes int id)
public String[] getStringArray(@ArrayRes int id)
public float getDimension(@DimenRes int id)
public Drawable getDrawable(@DrawableRes int id)
public Movie getMovie(@RawRes int id)
public int getColor(@ColorRes int id)
public InputStream openRawResource(@RawRes int id)
public InputStream openRawResource(@RawRes int id, TypedValue value)

values

values文件夹下的xml相对其他文件来说比较简单,他就是值数据。对于文件的命名没有规定,但是约定string值的文件命名strings,颜色值的文件命名colors,样式命名styles…
这里写图片描述

比如在colors文件中写一个string,然后用R.string.text来获取他。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color> 
    <string name="text">这是一个字符串!</string>
</resources>

我们一般直接使用textView.setText(R.string.text),但是实际上却是先获取文件资源转换成字符串在设置的。
public final void setText(@StringRes int resid) {
setText(getContext().getResources().getText(resid));
mTextFromResource = true;
}
命名约束只是为了更容易的区分我们所定义的资源,方便使用。 在values文件夹下的资源文件,根布局为:resources所以他们的访问模式也是一样的没有文件名称的限制。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name="integer_array">
        <item>1</item>
        <item>2</item>
    </integer-array>
    <string-array name="string_array">
        <item>1</item>
        <item>2</item>
    </string-array>
    <style name="mystyle" parent="AlertDialog.AppCompat">
      <item name="android:layout_weight">wrap_content</item>
      <item name="android:layout_height">wrap_content</item>
    </style>

    <!--其他都是一样的模式-->

</resources>

color

Color文件夹下的color与Values文件夹下的color有什么不同呢?

//Color下的color文件是一个selector 每个item中除了一个颜色值是必须条件外,还有一个状态属性,意思是控件在某种状态下有某种颜色,值可以是16进制的数值也可以引用values中的颜色值,至于这个颜色用在哪里要看文件的位置,因为如果把color文件夹下的某个文件复制到Drawable文件夹下,那么他代表的Drawable对象,比如可以做背景色,或单色图片。在Color文件夹下就只是一个数值颜色,比如字体颜色。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/button_color" android:state_pressed="true" ></item>
</selector>

控件的状态,可以写多条,条件多的写在最上面,条件少的写在下面。(值为true或者false)

android:state_pressed    按下或者没有按下
android:state_focused    有焦点或者没有焦点
android:state_hovered    API11引入,光标在view上悬停或不悬停
android:state_selected    选中或者没有选中
android:state_checked    是选中状态或者不是选中状态
android:state_checkable    能或者不能被选中
android:state_enabled    启用或者禁用
android:state_activated    激活或者未激
android:state_window_focused    父窗口有焦点或者没有焦点
//Values文件夹下的color 他表示一个资源数值,基本上他只代表颜色值,八位或6位十六进制数值。
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

Drawable

Drawable文件夹下除了放置图片之外,还有一些XML定义的外观类的‘View’,因为我们基本上只需要定义它的性质。引用鸿洋大佬博客中的一章图片。
这里写图片描述

  • selector 其实在Color文件夹下的颜色也属于一种selector使用范围是控件状态改变时候颜色的变化,drawable文件下是‘图片’的变化。
//他的item中必须项目是一个颜色或者drawable对象和若干状态属性。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimary" android:state_pressed="true"></item>
    <item android:drawable="@drawable/ic_launcher_foreground" android:state_pressed="false"></item>
</selector>

  <!--// 实例化一个StateListDrawable对象,相当于在drawable文件夹下新建一个selector的xml文件-->
 StateListDrawable stateListDrawable=new StateListDrawable();
  <!-- //添加状态属性,相当于编写item,添加的顺序和规则和xml一样,状态true多的先添加,状态少无状态的后添加。-->
  stateListDrawable.addState(new int[]{android.R.attr.state_pressed},getResources().getDrawable(R.mipmap.ic_launcher));
  <!--//找到控件,相当于在XML文件中找到某控件-->
  mButton = (Button) findViewById(R.id.button);
  <!--//给该控件设置draw对象-->
  mButton.setBackground(stateListDrawable);
  • level-list
    LevelList类型的图形用来管理一组可进行切换的图片 ,他跟状态选择的比较相似,不过他的等级设定是由用户来控制的。
<!--在xml中设置好范围,比如等级在0-10之间图片是第一个在11-20之间是显示第二个。。比如电量,wifi信号等-->
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:maxLevel="10"
        android:minLevel="0"
        android:drawable="@color/colorPrimary"></item>
    <item android:maxLevel="20"
        android:minLevel="11"
        android:drawable="@color/colorAccent"></item>
</level-list>
<!-- 获取方式和使用方法基本相同 不同的是要自己设置level的数值-->
       final LevelListDrawable levelListDrawable= (LevelListDrawable) getDrawable(R.drawable.level_list);
        mTextview.setBackground(levelListDrawable);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
        levelListDrawable.setLevel(levelListDrawable.getLevel()>10?5:15);
            }
        });
  • layer-list
    每一个item都代表了一个独立的层级,他更像ps中的层级一样,单独的item具有drawable的特性。可以稍微复杂的图片可以用跟ps一样的方式在layer中实现。
<!--两个图片中心重合 第一个item在下,第二个item在上重叠起来,这里的drawable可以是其他的shape对象,selector对象等drawable-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" />
    <item android:drawable="@drawable/logo_overlay" />
    <!--其他属性-->
        <item         
        android:drawable="@[package:]drawable/drawable_resource"//我们要使用的资源文件
        android:id = ""         //方便在代码中对当前item进行查找
        android:left = "dimension"   //距离容器布局左边的距离     
        android:right = "dimension"   //距离容器布局右边的距离
        android:top = "dimension"     //距离容器布局顶端的距离    
        android:bottom = "dimension"  //距离容器布局底端的距离
        />   
</layer-list>

我们也可以用代码对layer-list进行操作。
这里写图片描述
方法比较多,基本都是查找某layer设置边距等属性。

  • transtion
    本意是过度变化转换等。
<!--同样item的属性基本都一样,包括java中的方法也大致一样与前面静态的不同的是,这一个是可以“动的”-->
<?xml version="1.0" encoding="utf-8"?>
<transition
xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:id="@[+][package:]id/resource_name"
        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension"
        android:left="dimension" />
</transition> 
<!--获取到实例后通过startTransition(int time)-->
 TransitionDrawable transition=new TransitionDrawable(new Drawable[]{});
transition.startTransition(Millis 毫秒);
  • Color
<!--只有一个颜色值没有item 代表的是一个drawable对象,严重怀疑这个玩意儿的实用性-->
<?xml version="1.0" encoding="utf-8"?>
<color xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorPrimaryDark">
</color>
  • shape
    图形形状,类别有 rectangle: 矩形, oval: 椭圆形,大多用来画圆形。 line: 线形,可以画实线和虚线 ring: 环形,可以画环形进度。 当然每个图形的某些属性略有区别。自己在实战中体会吧。
<!--每个形状又有一下属性-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <padding>间距</padding>
    <corners>圆角</corners>
    <gradient>渐变</gradient>
    <size>大小</size>
    <solid>填充色</solid>
    <stroke>描边</stroke>
</shape>
  • scale
//图片缩放
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="drawable"
    android:scaleGravity="变化后的中心"
    android:scaleHeight="高度变化"
    android:scaleWidth="宽度变化"
    android:level="等级">
</scale>
  • clip
    通过设置一章drawable的显示比例来实现出另一个drawable的效果,当我第一次看到这个解释的时候,卧槽这个不是进度条么,实际上就是这样,但他不仅仅局限于进度条。
<?xml version="1.0" encoding="utf-8"?>
<clip
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
    android:clipOrientation=["horizontal" | "vertical"]
    android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                     "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                     "center" | "fill" | "clip_vertical" | "clip_horizontal"] />

既然是drawable这个条件是必须的,但是clip的方向clipOrientation有水平和垂直两个,但是水平从左还是右,垂直是上还是下。那就需要gravity来辅助了,如果是left或者right那么久从上下clip,反之一样,当时center的时候clipOrientation是水平放下就左右一起裁剪,反之一样。

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/android" 
    android:clipOrientation="horizontal"
    android:gravity="left" />
 ClipDrawable clipDrawable= (ClipDrawable) mTextview.getBackground();
        clipDrawable.setLevel(5000);

这里写图片描述

  • rotate
    旋转drawable,同样的使用方法。设置一个初始位置,一个结束位置,设置等级为0-10000,0开始10000结束,但是本身并不是动画,他仅仅代表了一个最终状态的drawable。
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/ic_launcher"
    android:fromDegrees="0"
    android:toDegrees="90"
    android:pivotX="0.5"
    android:pivotY="0.5"
    android:visible="true">
</rotate>

    <TextView android:id="@+id/textview"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:text="Hello World!"
        android:background="@drawable/rotate"/>

代码获取并使用,当然也可以从资源文件中获取rotateDrawable对象,用View.setbackground(Drawable)也可以。

    RotateDrawable rotateDrawable= (RotateDrawable) mTextview.getBackground(); 
    rotateDrawable.setLevel(5000);
  • animation-list
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:visible="true"
    android:oneshot="false"
    android:variablePadding="true">
    <item android:drawable="@drawable/ic_launcher" android:duration="150"></item>
    <item android:drawable="@drawable/ic_launcher" android:duration="150"></item>
</animation-list>

通过设置animationDrawable.selectDrawable(index)index是item的下标,从上到下,从0开始,他可以实现和level-list同样的效果,他的这种使用方法和上面的一样。但是他的主要作用是作为动画效果出现的,在下面会再看。

  • inset
    InsetDrawable 表示一个drawable根据指定的距离嵌入到另外一个drawable内部。(我们看到的其实还是同一张图片,只是会空出一些边距)当控件需要的背景比实际的边框小的时候比较适合使用InsetDrawable。很像drawable的padding属性,区别在于 padding表示drawable的内容与drawable本身的边距,insetDrawable表示两个drawable和容器之间的边距。有些类似margin和padding的区别(可能不太恰当)。
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/resources_2"
    android:visible="true"
    android:insetTop="10dp"
    android:insetRight="10dp"
    android:insetBottom="10dp"
    android:insetLeft="10dp">
</inset>
<!--drawable 是否可见  上右下左距离  由于本身只有一个状态跟shape相似可以直接设置background 或者做其他使用-->
  • bitmap
    Bitmap,代表一个位图图像,BitmapDrawable 它代表的就是一张图片,在开发过程中,我们之间引用原始图片即可,只是BitmapDrawable 可以设置更多的显示效果。除了必须的drawable对象外,还有其他属性。
    这里写图片描述
    分别是透明度(alpha:0-1),抗锯齿(antialias:true | false),镜像(autoMirrored:true | false),抖动(dither:true | false ),过滤(filter:true | false 拉伸或压缩会有好点的效果 ,位置(gravity:top等),文理映射(mipmap :true | false 没搞清楚是嘛),图文分析(titleMode(X|Y):clamp颜色扩散,repeat图片重复,mirror镜像),渲染(tint:true | false ) ,渲染模式(src_in 只显示设置的遮罩颜色。 相当于遮罩在里面。 src_over遮罩颜色和图片都显示。相当于遮罩在图片上方。(特别是色值带透明度的) src_atop遮罩在图片上方 multiply 混合色遮罩 add 混合遮罩,drawable颜色和透明度。具体的自己可以体会下,其中好多我是分辨不出来(⊙o⊙)…)并且有些属性是冲突的不能同时设置。

  • nine-patch
    网上绘制点9的文章很多不多说,他主要是保留了部分外观,允许非主要部分拉伸,收缩的一种对drawable处理手段,主要用于背景,最常见的就是聊天时文字框了。

  • drawable小结,这个文件夹下的xml文件对应的drawable对象中的属性都有相应的代码,而且基本相似,静态(不管状态选择,外部形状,过渡,还是,图层,动画列表,我们可以实现某种动态效果,但不能否认他的确是一个drawable对象而非一个动画对象)。

MARK(8)

上善若水。水善利万物而不争,处众人之所恶,故几於道。居善地,心善渊,与善仁,言善信,正善治,事善能,动善时。夫唯不争,故无尤。

猜你喜欢

转载自blog.csdn.net/joy_chow/article/details/80829431