下面继续使用drawable资源,包括上文介绍的StateListDrawable资源和ShapeDrawable资源,还有本文要介绍的LayerDrawable资源、ClipDrawable资源。实现输入框的焦点得到、失去时发生颜色变化,进度条的颜色渐变、图片拼接、图片截取等功能。代码如下:
Activity:
package com.lovo.activity; import android.app.Activity; import android.os.Bundle; import android.widget.ImageView; import com.lovo.R; public class TestDrawableActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_test_drawable); // 得到图片对象ImageView ImageView clipImg = (ImageView) findViewById(R.id.main_test_drawable_clip_img); // 设置图片截取的比例。范围为0——10000,10000为完全截取 clipImg.getDrawable().setLevel(6000); } }
布局XML(main_test_drawable.xml):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/drawable_test_bitmap" android:orientation="vertical" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@drawable/drawable_edittext_focus" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@drawable/drawable_edittext_focus" /> <ProgressBar style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" android:progressDrawable="@drawable/drawable_progress_bg" /> <SeekBar style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:progressDrawable="@drawable/drawable_progress_bg" /> <!-- 下面为图片拼接 --> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/drawable_multi_img" /> <!-- 分隔符 --> <View android:layout_width="match_parent" android:layout_height="2px" android:layout_marginTop="50dp" android:background="@drawable/drawable_split_line" /> <!-- 截取图片 --> <ImageView android:id="@+id/main_test_drawable_clip_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/drawable_clip_img" /> </LinearLayout>
drawable中的drawable_test_bitmap.xml:
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center" android:src="@drawable/logo" android:tileMode="disabled" > </bitmap>
drawable中的得到失去焦点而发生颜色变化的XML(drawable_edittext_focus.xml):
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:color="#f00"></item> <item android:state_focused="false" android:color="#00f"></item> </selector>
drawable中的进度条颜色变化XMl(drawable_progress_bg.xml):
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 指定显示为目标组件的背景 --> <item android:id="@android:id/background" android:drawable="@drawable/list_below_sel"> </item> <!-- 指定显示为目标组件已覆盖的进度颜色 --> <item android:id="@android:id/progress" android:drawable="@drawable/about_background_land"> </item> </layer-list>
drawable中的分隔符设置XMl(drawable_split_line.xml):
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:centerColor="#efefef" android:endColor="#111111" android:startColor="#111111" /> </shape>
drawable中的图片截取XML(drawable_clip_img.xml)
<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:clipOrientation="horizontal" android:drawable="@drawable/logo" android:gravity="center" > </clip>
附上图片效果: