《Android开发艺术探索》笔记——Drawable

来源:互联网 发布:汽车电脑编程语言 编辑:程序博客网 时间:2024/06/16 07:22

BitmapDrawable

bitmap_drawable.xml

<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android"    android:src="@mipmap/ic_launcher"    android:antialias="true"    android:dither="true"    android:filter="true"    android:gravity="center"    android:tileMode="repeat"    ></bitmap>

ShapeDrawable

shape_dash_drawable.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <solid        android:color="@color/colorAccent"/>    <stroke        android:dashGap="2dp"        android:dashWidth="10dp"        android:width="2dp"        android:color="@color/colorPrimary"/></shape>


shape_ring_drawable.xml


<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:innerRadiusRatio="3"    android:shape="ring"    android:thicknessRatio="9"    android:useLevel="false">        <gradient        android:endColor="#2F90BD"        android:startColor="#FFFFFF"        android:type="sweep"        />    </shape>


LayerDrawable

layer_drawable.xml

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item>        <shape android:shape="rectangle">            <solid android:color="#0ac39e" />        </shape>    </item>    <item android:bottom="6dp">        <shape android:shape="rectangle">            <solid android:color="#ffffff" />        </shape>    </item>    <item        android:bottom="1dp"        android:left="1dp"        android:right="1dp">        <shape android:shape="rectangle">            <solid android:color="#ffffff" />        </shape>    </item></layer-list>

LevelListDrawable

level_list_drawable.xml

<?xml version="1.0" encoding="utf-8"?><level-list xmlns:android="http://schemas.android.com/apk/res/android"><item    android:drawable="@mipmap/dir1"    android:maxLevel="1"/>    <item        android:drawable="@mipmap/dir2"        android:maxLevel="2"/>    <item        android:drawable="@mipmap/dir3"        android:maxLevel="3"/>    <item        android:drawable="@mipmap/dir4"        android:maxLevel="4"/></level-list>

TransitionDrawable

transition_drawable.xml

<?xml version="1.0" encoding="utf-8"?><transition xmlns:android="http://schemas.android.com/apk/res/android"><item    android:drawable="@color/white"/>    <item        android:drawable="@color/colorPrimaryDark"/></transition>

InsetDrawable

inset_drawable.xml

<?xml version="1.0" encoding="utf-8"?><inset xmlns:android="http://schemas.android.com/apk/res/android"    android:insetBottom="20dp"    android:insetLeft="20dp"    android:insetRight="20dp"    android:insetTop="20dp"    >    <shape        android:shape="rectangle">        <solid            android:color="@color/colorPrimary"/>        <stroke            android:color="@color/white"/>    </shape>    </inset>

ScaleDrawable

scale_drawable.xml

<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@mipmap/ic_launcher"    android:scaleHeight="70%"    android:scaleWidth="70%"></scale>


ClipDrawable

clip_drawable.xml

<?xml version="1.0" encoding="utf-8"?><clip xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@mipmap/ic_launcher"    android:clipOrientation="vertical"    android:gravity="top"></clip>

activity_drawable.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/shape_dash_drawable"    >        <ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical">            <ImageView                android:layout_width="200dp"                android:layout_height="200dp"                android:src="@drawable/shape_ring_drawable"/>            <EditText                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginLeft="16dp"                android:layout_marginRight="16dp"                android:padding="2dp"                android:background="@drawable/layer_drawable"/>            <ImageView                android:id="@+id/iv_level_list"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/level_list_drawable"                />            <TextView                android:id="@+id/tv_transition"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="55555555555555555"                android:background="@drawable/transition_drawable"/>            <ImageView                android:layout_width="100dp"                android:layout_height="100dp"                android:background="@drawable/inset_drawable"                android:id="@+id/imageView" />            <ImageView                android:id="@+id/iv_scale"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@drawable/scale_drawable"/>            <ImageView                android:id="@+id/iv_clip"                android:layout_width="100dp"                android:layout_height="100dp"                android:src="@drawable/clip_drawable"/>        </LinearLayout>    </ScrollView></LinearLayout>

DrawableActivity

public class DrawableActivity extends AppCompatActivity {    private ImageView iv_level_list;    private TextView tv_transition;    private ImageView iv_scale;    private ImageView iv_clip;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_drawable);        iv_level_list = (ImageView) findViewById(R.id.iv_level_list);        iv_level_list.setImageLevel(4);        tv_transition = (TextView) findViewById(R.id.tv_transition);        TransitionDrawable transitionDrawable = (TransitionDrawable) tv_transition.getBackground();        transitionDrawable.startTransition(1000);        iv_scale = (ImageView) findViewById(R.id.iv_scale);        ScaleDrawable scaleDrawable = (ScaleDrawable) iv_scale.getBackground();        scaleDrawable.setLevel(5);        iv_clip = (ImageView) findViewById(R.id.iv_clip);        ClipDrawable clipDrawable = (ClipDrawable) iv_clip.getDrawable();        //0-10000        clipDrawable.setLevel(5000);    }}


0 0
原创粉丝点击