1. When developing Android projects, you often see a very beautiful UI interface. For example, when you click on an image and are selected, cover it with a transparent image. Indicates that it is selected, the difference is from those that are not selected. How is this achieved? The answer is the effect of LayerDrawable use. Below is a summary of LayerDrawable. If there is any incorrectness, please correct me.
2. Simply put, LayerDrawable inherits and Drawable. Drawable is a drawable object, which may be a bitmap BitmapDrawable, a graphic ShapeDrawable, or a layer LayerDrawable. Create corresponding drawing objects according to different drawing requirements.
The LayerDrawable system will draw these Drawable objects in the order of arrays. The largest index Drawable object will be drawn on the top, defining the root element layer-list of the XML file of the LayerDrawable object. This element can contain multiple item elements.
3. The code implementation is as follows:
Method 1: XML method:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:andro >
<item>
<bitmap
android:gravity="center"
android:src="@drawable/ic_03" />
</item>
<item
android:left="25dp"
android:top="25dp">
<bitmap
android:gravity="center"
android:src="@drawable/ic_03" />
</item>
<item
android:left="50dp"
android:top="50dp">
<bitmap
android:gravity="center"
android:src="@drawable/ic_03" />
</item>
</layer-list>
Method 2: Code method: Class:
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends Activity {
private ImageView mView;
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView(.activity_main);
mView = (ImageView) findViewById(.imageView1);
// Create resource object
Resources resources = getResources();
// Create an array object
Drawable[] layers = new Drawable[2];
layers[0] = (.ic_01);
layers[1] = (.ic_02);
LayerDrawable layerDrawable = new LayerDrawable(layers);
// Set background
(layerDrawable);
}
}