Res Resource directory Introduction

Android project resources directory (RES)

What do resources mean in Android?

Resources are additional files and static content that code uses, such as bitmaps, layout definitions, interface strings, animation instructions, and so on.

Once the resources have been placed in their respective directories, they can be accessed using the resource ids generated in the project R class. Such as R.rawable. Icon, r.layout.main_activity. Class R is automatically generated. On behalf of the resources.

Grouped Resource types

Put various resources into specific subdirectories of the project res/ directory. The name of the subdirectory is particularly important. We put different resources in different subdirectories.

  • Animator / : XML file used to define property animations.
  • Anim / : XML file used to define gradient animations. (Property animations can also be saved in this directory, but the animator/ directory is preferred for property animations to distinguish between the two types.)
  • Color / : XML file used to define a list of color states.
  • Drawable / : bitmap files (.png,.9.png,.jpg,.gif) or XML files compiled into one of the following subtypes of drawable object resources: bitmap files, nine cells (resizable bitmaps), state lists, shapes, animatable drawable objects, and other drawable objects.
  • Mipmap / : Drawable object files for different initiator icon densities. I’ll put my app icon here.

Mipmap is followed by dPI categories, such as HDPI MDPI, in which the icon size is different. You can cut the App icon by referring to the default icon size. If you want to save time, you can use an icon to copy into each directory.

  • Layout / : XML file used to define the layout of the user interface. Layout the layout file.
  • Menu / : XML file used to define application menus such as options menus, context menus, or submenus.
  • Raw / : Any file to be saved in its original form. If you want to use the original InputStream to open these Resources, please use the resource ID (i.e. R.r aw. The filename) call Resources. OpenRawResource (). However, if you need access to raw file names and file hierarchies, consider storing some resources in assets/ instead of res/raw/. The files in assets/ do not have resource ids, so they can only be read using AssetManager.
  • values/: AN XML file containing simple values such as strings, integers, and colors. XML resource files in other RES/subdirectories define a single resource based on XML file names, while files in values/ directories can describe multiple resources. For files in this directory,<resources style="box-sizing: inherit; margin-top: 0px; margin-bottom: 0px;" >Each child of the element defines a resource. For example,<string style="box-sizing: inherit;" >The element creates the R.strand resource,<color style="box-sizing: inherit;" >The element creates the R.collor resource. Because each resource is defined using its own XML element, you can name files arbitrarily and put different resource types in one file.

However, you may need to put unique resource types in different files to make them obvious. For example, for resources that can be created in this directory, the following filename conventions are given: arrays.xml: Resource arrays (arrays of types). Color.xml: color value. Dimens. XML: size value. Strings. XML: string value. Styles.xml: styles.

  • xml/: can be called at run timeResources.getXML()Any XML file read. Various XML configuration files, such as searchable configuration, must be saved here.
  • Font / : a font file with an extension (such as.ttf,.otf, or.ttc), or an XML file containing elements.

Note: Do not save resource files directly in the res/ directory, as this will cause compilation errors.

At the beginning, we were exposed to a lot of layout directories. If you want to add some images, you can just put them in the drawable directory.

Modify the application icon, should be put in the mipmap corresponding directory. For example, ICONS in mimap-hdPI are 72×72 pixels and ICONS in MImap-MDpi are 48×48 pixels. To make this easier, take an icon and copy it into all the DPI directories of Mipmap with the same file name, such as IC_your_launcher.png. Then in the manifest file androidmanifest.xml, modify the icon.

<application
        android:icon="@mipmap/ic_your_launcher"
Copy the code

Later on if we want to define some style of Button, TextView, setting the color, setting the background. The drawable directory is touched.

Shape drawing and use

There is a drawable folder in the project directory, which holds some static image resource files. For example, bitmap files (.png,.9.png,.jpg,.gif); Or some XML file (referred to in this article as a drawable file) of a subtype of a drawable object resource.

When we want to give a button or a TextView a background, we think of a solid background. If you want a rounded background, or a gradient background, how do we do that? One way to do this is to create corresponding art materials, which are cut images. Another option is to use resource files in XML format. This article introduces shape. Some of the simpler art can be done using these resources.

example

Let’s try a new Shape with rounded corners and a gradient inside. In the res/drawable/ directory, right-click and create a new drawable Resource file named shape_rect_gradient_red.xml. Root Element select Shape.

Code:

<? The XML version = "1.0" encoding = "utf-8"? > <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="2dp" android:color="#0E30B1" /> <corners android:radius="6dp" /> <gradient android:centerColor="#F55353" android:endColor="#F71515" android:startColor="#FD7575" /> </shape>Copy the code

You can open the preview on the right side of the AS to see how it looks. Among them

  • android:shape="rectangle"Select the shape of rectangle.
  • The stroke tag represents the border. Inside set the border width to 2dp and the border color to #0E30B1.
  • The corners tag represents rounded corners. If not set, the default is right Angle. So let’s say that the radius of the corner is 6dp.
  • Gradient means gradient. You can set the start, middle and end color values respectively.

In Layout, use this shape for the Button’s background Settings. The file name of the XML is its resource name.

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_rect_gradient_red" />
Copy the code

A little more refinement of the Button.

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_rect_gradient_red"
        android:text="RFDev btn 1"
        android:textAllCaps="false"
        android:textColor="#ffffff" />
Copy the code

Let’s run it and see what happens.

Shape is introduced

Shape is also known as a “shape-drawable object”. For simplicity, these are called shapes or “shape files”. Shape is a generic shape defined in an XML file. Once compiled, you get a GradientDrawable object.

The resource reference

  • In Java: r.draable.filename
  • In XML: @[package:]drawable/filename

grammar

We know several elements in the above chestnut, gradient, Corners and stroke. Here is the syntax in detail.

<? The XML version = "1.0" encoding = "utf-8"? > <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape=["rectangle" | "oval" | "line" | "ring"] > <corners android:radius="integer" android:topLeftRadius="integer" android:topRightRadius="integer" android:bottomLeftRadius="integer" android:bottomRightRadius="integer" /> <gradient android:angle="integer" android:centerX="float" android:centerY="float" android:centerColor="integer" android:endColor="color" android:gradientRadius="integer" android:startColor="color" android:type=["linear" | "radial" | "sweep"] android:useLevel=["true" | "false"] /> <padding android:left="integer" android:top="integer" android:right="integer" android:bottom="integer" /> <size android:width="integer" android:height="integer" /> <solid android:color="color" /> <stroke android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer" /> </shape>Copy the code
Example of a rounded background

The use of corners and corners was discussed in the preceding paragraph. Let’s look at the implementation of a rounded background. Create the shape_btn_2_normal.xml file

<? The XML version = "1.0" encoding = "utf-8"? > <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#6CB3DD" /> <corners android:radius="25dp" /> </shape>Copy the code

Use this background for the TextView in layout.

<TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="50dp" Android :background="@drawable/shape_btn_2_normal" Android :gravity="center" Android :text="RFDev "android:background="@drawable/shape_btn_2_normal" android:gravity="center" Android :text="RFDev" android:textColor="#ffffff" />Copy the code

The TextView height is set to 50dp and the background radius is set to 25dp. Run it and you can see the effect.

If you want gradient, add the gradient setting.

Use resources in code

Use resources in Java code, such as setting a background in an Activity.

Resources res = getResources();
Drawable shape = ResourcesCompat.getDrawable(res, R.drawable.shape_btn_2_normal, getTheme());

TextView tv = (TextView)findViewById(R.id.tv1);
tv.setBackground(shape);
Copy the code

In this way, we can implement some simple button effects ourselves. More complex colors and effects require the support of the graphic designer.

The ring example

Size and length set by yourself.

Annular thumb_round_1. XML.

<? The XML version = "1.0" encoding = "utf-8"? > <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#ffffff"  /> <stroke android:width="@dimen/audio_seek_bar_thumb_ring_width" android:color="#7095fc" /> <size android:width="@dimen/audio_seek_bar_thumb_size" android:height="@dimen/audio_seek_bar_thumb_size" /> </shape>Copy the code

The ring thumb_round_progress_1.xml with gradient.

<? The XML version = "1.0" encoding = "utf-8"? > <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <size android:width="@dimen/audio_seek_bar_thumb_size" android:height="@dimen/audio_seek_bar_thumb_size" /> <solid android:color="#ffffff" /> </shape> </item> <item> <shape android:innerRadius="4dp" android:thicknessRatio="6" android:shape="ring" android:useLevel="false"> <gradient android:endColor="#ffffff" android:startColor="#7095fc" android:type="sweep" /> <size android:width="@dimen/audio_seek_bar_thumb_size" android:height="@dimen/audio_seek_bar_thumb_size" /> </shape> </item> </layer-list>Copy the code

Android Zero Basics tutorial video reference