Drawable基础知识总结----drawable标签LevelList、inset、scale、clip的使用

2017-11-28 18:59:30来源:CSDN作者:An_nAl人点击

分享

1.LevelList

布局代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:gravity="center"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/img_view"        android:scaleType="centerInside"        android:layout_width="381dp"        android:layout_height="216dp" /></LinearLayout>

activity代码:

public class ActLevelList extends AppCompatActivity {    private static ImageView imgView;    static Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            switch( msg.what){                case 1:                case 8:                case 4:                    imgView.getDrawable().setLevel( 1);                    break;                case 7:                case 5:                case 3:                    imgView.getDrawable().setLevel( 0);                    break;                case 6:                case 2:                    imgView.getDrawable().setLevel( 2);                    break;            }        }    };    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_act_levellist);        imgView = (ImageView) this.findViewById( R.id.img_view);        imgView.setImageResource( R.drawable.level_list_drawable);        imgView.setImageLevel( 0);        for( int i = 1; i < 9; i++ ){            handler.sendEmptyMessageDelayed( i, i * 2000);        }    }    @Override    public void onBackPressed() {        super.onBackPressed();        finish();    }}

2.inset

drawable文件inset_drawable:

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

布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:background="@drawable/inset_drawable"    android:layout_width="match_parent"    android:layout_height="match_parent"></LinearLayout>

activity代码:

public class ActInset extends AppCompatActivity{    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_act_inset);    }    @Override    public void onBackPressed() {        super.onBackPressed();        finish();    }}

3.scale

drawable文件scale_drawable_for_big_number:

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

drawable文件scale_drawable_for_small_number:

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

布局:

<?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">    <ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center"            android:orientation="vertical">            <TextView                android:layout_marginTop="30dp"                android:gravity="center"                android:textSize="20sp"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="android:scaleHeight=70%  android:scaleWidth=70%"/>            <ImageView                android:id="@+id/img_view_for_big_number"                android:scaleType="centerInside"                android:layout_width="381dp"                android:layout_height="216dp"                android:background="@drawable/scale_drawable_for_big_number"/>            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:gravity="center"                android:layout_marginTop="30dp"                android:textSize="20sp"                android:text="android:scaleHeight=30%  android:scaleWidth=30%"/>            <ImageView                android:id="@+id/img_view_for_small_number"                android:scaleType="centerInside"                android:layout_width="381dp"                android:layout_height="216dp"                android:background="@drawable/scale_drawable_for_small_number"/>            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:gravity="center"                android:layout_marginTop="30dp"                android:textSize="20sp"                android:text="宽高缩放比例50%  level=1"/>            <ImageView                android:id="@+id/img_view_for_level1"                android:scaleType="centerInside"                android:layout_width="381dp"                android:layout_height="216dp"                android:background="@drawable/scale_drawable_for_small_number"/>            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:gravity="center"                android:layout_marginTop="30dp"                android:textSize="20sp"                android:text="宽高缩放比例50%  level=5000"/>            <ImageView                android:id="@+id/img_view_for_level5000"                android:scaleType="centerInside"                android:layout_width="381dp"                android:layout_height="216dp"                android:background="@drawable/scale_drawable_for_small_number"/>            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:gravity="center"                android:layout_marginTop="30dp"                android:textSize="20sp"                android:text="宽高缩放比例50%  level=10000(原图大小)"/>            <ImageView                android:id="@+id/img_view_for_level10000"                android:scaleType="centerInside"                android:layout_marginBottom="30dp"                android:layout_width="381dp"                android:layout_height="216dp"                android:background="@drawable/scale_drawable_for_small_number"/>        </LinearLayout>    </ScrollView></LinearLayout>

activity代码:

public class ActScale extends AppCompatActivity {    @BindView(R.id.img_view_for_big_number)    ImageView imgViewForBigNumber;    @BindView(R.id.img_view_for_small_number)    ImageView imgViewForSmallNumber;    @BindView(R.id.img_view_for_level1)    ImageView imgViewForLevel1;    @BindView(R.id.img_view_for_level5000)    ImageView imgViewForLevel5000;    @BindView(R.id.img_view_for_level10000)    ImageView imgViewForLevel10000;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_act_scale);        ButterKnife.bind(this);        ScaleDrawable scaleDrawableForBigNumber = (ScaleDrawable) imgViewForBigNumber.getBackground();        scaleDrawableForBigNumber.setLevel(1);        ScaleDrawable scaleDrawableForSmallNumber = (ScaleDrawable) imgViewForLevel1.getBackground();        scaleDrawableForSmallNumber.setLevel(1);        ScaleDrawable scaleDrawableForLevel1 = (ScaleDrawable) imgViewForSmallNumber.getBackground();        scaleDrawableForLevel1.setLevel(1);        ScaleDrawable scaleDrawableForLevel5000 = (ScaleDrawable) imgViewForLevel5000.getBackground();        scaleDrawableForLevel5000.setLevel(5000);        ScaleDrawable scaleDrawableForLevel10000 = (ScaleDrawable) imgViewForLevel10000.getBackground();        scaleDrawableForLevel10000.setLevel(10000);    }    @Override    public void onBackPressed() {        super.onBackPressed();        finish();    }}

4.clip

drawable文件clip_drawable:

<?xml version="1.0" encoding="utf-8"?><clip xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/img3"    android:clipOrientation="horizontal"    android:gravity="center"></clip>

布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:gravity="center"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/img_view"        android:layout_marginTop="15dp"        android:layout_width="381dp"        android:layout_height="216dp"        android:background="@drawable/clip_drawable"/>    <ImageView        android:id="@+id/img_view_for_level"        android:layout_width="381dp"        android:layout_height="216dp"        android:layout_marginTop="15dp"        android:scaleType="fitStart"        android:src="@drawable/clip_drawable"/></LinearLayout>

activity代码:

public class ActClip extends AppCompatActivity {    @BindView(R.id.img_view)    ImageView imgView;    @BindView(R.id.img_view_for_level)    ImageView imgViewForLevel;    private final static int IS_CONTUNUE=0x22;    private  ClipDrawable drawable;    Timer timer = new Timer();    final Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            if (msg.what == IS_CONTUNUE) {                /**setlevel()设置图片截取的大小                 * 修改ClipDrawable的level值,level值为0--10000;                 * 10000:截取图片大小为空白,0:截取图片为整张图片;                 */                drawable.setLevel(drawable.getLevel() + 200);            }        }    };    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_act_clip);        ButterKnife.bind(this);        //ClipDrawable的等级范围是[0,10000],而且level越大裁剪区域越少,        // 当level=10000时,表示不裁剪,我们在这里设置了level为6000则表示裁剪40%的区域        ClipDrawable clipDrawable = (ClipDrawable) imgView.getBackground();        clipDrawable.setLevel(6000);        //获取图片所显示的ClipDrawable对象        drawable = (ClipDrawable) imgViewForLevel.getDrawable();        //定时器        timer.schedule(new TimerTask() {            @Override            public void run() {                Message msg = new Message();                msg.what = IS_CONTUNUE;                handler.sendMessage(msg);                if (drawable.getLevel() >= 10000) {                    timer.cancel();                }            }        }, 0, 200);    }    @Override    public void onBackPressed() {        super.onBackPressed();        finish();    }}

微信扫一扫

第七城市微信公众平台