android 图形图像,Android图形图像Drawable的使用(二)

在本文中,程序统一使用的原图为image.jpg,如图 1。

android 图形图像,Android图形图像Drawable的使用(二)_第1张图片

图 1

(1) BitmapDrawable

首先是MainActivity的布局文件activity_main.xml。

本文引用地址:http://emb.hqyj.com/Column/7551.html

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

tools:context="com.example.drawabletest.MainActivity" >

android:id="@+id/text1"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="2dp"

android:background="@drawable/bitmap_drawable_repeat" />

android:id="@+id/text2"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="2dp"

android:background="@drawable/bitmap_drawable_mirror" />

android:id="@+id/text3"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="2dp"

android:background="@drawable/bitmap_drawable_clamp" />

上面三个TextView分别使用了不同的BitmapDrawable的tileMode,下面分别贴出这三个Drawable的xml文件。

bitmap_drawable_repeat.xml:

android:src="@drawable/image"

android:tileMode="repeat" />

bitmap_drawable_mirror.xml:

android:src="@drawable/image"

android:tileMode="mirror" />

bitmap_drawable_clamp.xml:

android:src="@drawable/image"

android:tileMode="clamp" />

显示效果如图 2。

android 图形图像,Android图形图像Drawable的使用(二)_第2张图片

图 2

(2) ShapeDrawable

首先是MainActivity的布局文件activity_main.xml。

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

tools:context="com.example.drawabletest.MainActivity" >

android:id="@+id/text1"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="2dp"

android:background="@drawable/shape_drawable_gradient_linear" />

android:id="@+id/text2"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="2dp"

android:background="@drawable/shape_drawable_gradient_radius" />

android:id="@+id/text3"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="2dp"

android:background="@drawable/shape_drawable_gradient_sweep" />

上面三个TextView分别使用了不同的ShapeDrawable的type,下面分别贴出这三个Drawable的xml文件。

shape_drawable_gradient_linear.xml:

android:shape="rectangle" >

android:angle="45"

android:centerColor="#00ff00"

android:centerX="0.5"

android:centerY="0.5"

android:endColor="#0000ff"

android:startColor="#ff0000"

android:type="linear" />

shape_drawable_gradient_radius.xml:

android:shape="rectangle" >

android:centerColor="#00ff00"

android:endColor="#0000ff"

android:gradientRadius="50"

android:startColor="#ff0000"

android:type="radial" />

shape_drawable_gradient_sweep.xml:

android:shape="rectangle" >

android:centerColor="#00ff00"

android:endColor="#0000ff"

android:gradientRadius="50"

android:startColor="#ff0000"

android:type="radial" />

显示效果如图 3。

android 图形图像,Android图形图像Drawable的使用(二)_第3张图片

图 3

(3) LayerDrawable

直接贴出layer_drawable.xml代码,作为TextView的background。

<>

android:bottom="10dp"

android:left="10dp"

android:right="10dp">

显示效果如图 4。

android 图形图像,Android图形图像Drawable的使用(二)_第4张图片

图 4

(4) StateListDrawable

直接贴出statelist_drawable.xml代码,作为TextView的background。

android:drawable="@drawable/button_pressed"

android:state_pressed="true"/>

android:drawable="@drawable/button_focused"

android:state_focused="true"/>

(5) LevelListDrawable

直接贴出levellist_drawable.xml代码,作为TextView的background。

android:drawable="@drawable/low_level"

android:maxLevel="1000"

android:minLevel="0"/>

android:drawable="@drawable/high_level"

android:maxLevel="10000"

android:minLevel="1001"/>

(6) InsetDrawable

直接贴出inset_drawable.xml代码,作为TextView的background。

android:insetBottom="15dp"

android:insetLeft="15dp"

android:insetRight="15dp"

android:insetTop="15dp" >

(7) ClipDrawable

直接贴出clip_drawable.xml代码,作为ImageView的background。

android:clipOrientation="vertical"

android:drawable="@drawable/image"

android:gravity="bottom" />

ImageView的布局为:

android:id="@+id/img1"

android:layout_width="100dp"

android:layout_height="100dp"

android:gravity="center"

android:src="@drawable/clip_drawable" />

MainActivity.java代码中:

ImageView mImageView = (ImageView) findViewById(R.id.img1);

ClipDrawable clipDrawable = (ClipDrawable)mImageView.getDrawable();

clipDrawable.setLevel(5000);

你可能感兴趣的:(android,图形图像)