android drawable实践 xml 实现各种效果

背景

在看法中我们经常要使用图片或者drawable文件夹下的xml,来实现一些效果,Drawable的用法都和xml相关,我们可以使用shape、layer-list等标签绘制一些背景,还可以通过selector标签定义View的状态的效果等。当然了基本每个标签都对应于一个真正的实体类。下面,来总结、温习下这些用法。

所有drawable.xml对应的Java类如下

这里写图片描述

1.如何使用?
我们都拿一个控件来举例
a.button的slector.xml这个很简单

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/holo_blue_dark" android:state_pressed="true"></item>
    <item android:drawable="@android:color/holo_blue_light" android:state_pressed="false"></item>
</selector>

b.imageView的level_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<level-list xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item 
        android:drawable="@drawable/icon_seller_credit_level_first1" 
        android:maxLevel="1"/> 
    <item 
        android:drawable="@drawable/icon_seller_credit_level_first2" 
        android:maxLevel="2"/> 
    <item 
        android:drawable="@drawable/icon_seller_credit_level_first3" 
        android:maxLevel="3"/>      
</level-list> 

在布局文件中

<ImageView 
           android:id="@+id/rating" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_alignParentRight="true" 
           android:layout_marginRight="50dp" 
           android:layout_marginTop="6dip" 
           android:src="@drawable/level_credit_rating" /> 

代码中:
imagview.setImageLevel(3);
c.layer-list.xml,这里我们给一个activity,设置them的例子

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape
            android:innerRadius="10dp"
            android:shape="rectangle" >//形状
            <solid android:color="@android:color/holo_orange_light" >//填充
            </solid>

            <stroke
                android:width="2dp"
                android:color="@android:color/holo_purple" >//边框
            </stroke>
        </shape>
    </item>
    <item>
        <bitmap
            android:gravity="center"//在中间画图形
            android:src="@drawable/ic_launcher" >
        </bitmap>
    </item>

</layer-list>

这个看起来,很强大,可以实现一些复杂的图形

d.clip裁减,看到有人,用这个类实现了一个(有底色的)进度条

<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="horizontal"
    android:drawable="@drawable/bg_pic"
    android:gravity="left" >
</clip>

在代码中:

imageView = (ImageView) findViewById(R.id.imageView1);
        drawable = (ClipDrawable) imageView.getBackground();
        drawable.setLevel(levle);//clipDrawable内部levle最大为3000;
        开启一个线程,不断刷新levle来实现进度

e.nine-patch的使用;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值