Drawable子类之——InsetDrawable (嵌入)

本文出自 “阿敏其人” 简书博客,转载或引用请注明出处。

InsetDrawable对应的标签是<inset>
他可以将其他的Drawable内嵌到自己的里面。个人觉得其实没什么用,他能做到的,LayerDrawable也能做,或者有的时候直接设置一下padding就可以了。

一、语法


<?xml version="1.0" encoding="utf-8"?>
<inset
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:drawable="@drawable/drawable_resource"
   android:insetTop="dimension"
   android:insetRight="dimension"
   android:insetBottom="dimension"
   android:insetLeft="dimension" />

没什么好说的,四个方向就是想要拉开的距离,比如一个图片本来应该是占满屏幕的,然后上下左右个设置为20dp,那么待会这个图片就不会占满屏幕,就是距离上下左右都有20dp的距离。

二、demo

insetdrawable_simple.xml

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="60dp"
    android:insetLeft="30dp"
    android:insetRight="30dp"
    android:insetTop="60dp" >

    <!--待会要插入的一个蓝色的矩形-->
    <shape android:shape="rectangle" >
        <solid android:color="#0000ff" />
    </shape>
</inset>

.
.
布局文件


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <!--Inset这东西其实就没什么用,大概也就这样子吧,
    背景一个,图片一个,利用Inset让背景比较大-->
    <ImageView
        android:id="@+id/mIv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:background="@mipmap/op"
        android:src="@drawable/insetdrawable_simple"
         />
</RelativeLayout>

.
.
Activity代码什么都不要动


import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //InsetDrawable insetDrawable=new InsetDrawable(getResources().getDrawable(R.drawable.XXX), 20, 30, 30, 20);

        
        
    }
}

.
.

demo效果:

demo效果.png

了解更多的Drawable分类 Drawable图像资源抽象类
本篇完。

推荐阅读更多精彩内容