android XML file之ClipDrawable

在drawable文件夹下创建一个clip_demo.xml文件,在xml文件中使用<clip.../>元素定义ClipDrawable对象,可指定如下三个属性:

1⃣️android:drawable 指定截取的drawable对象

2⃣️android:clipOrientation 指定截取的方向,可设置为水平截取或者垂直截取

3⃣️android:gravity 指定截取时对齐方式

使用ClipDrawable对象时可以调用setLevel(int level)方法来设置截取的区域大小;当level为0时,截取的图片片段为空,当level为10000时,截取整张图片


clip_demo.xml

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

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/clip_demo" />
</LinearLayout>
 
MainActivity
 
public class ThirdActivity extends AppCompatActivity {

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);

        imageView = (ImageView) findViewById(R.id.imageView);

        final ClipDrawable clipDrawable = (ClipDrawable) imageView.getDrawable();

        final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0x123){
                    clipDrawable.setLevel(clipDrawable.getLevel() + 500);
                }
            }
        };


        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                Message msg = new Message();
                msg.what = 0x123;
                handler.sendMessage(msg);
                if (clipDrawable.getLevel() > 10000){
                    timer.cancel();
                }
            }
        },0,500);

    }
}

实现的效果就是从ic_launcher这张图片的最左端到最右端一点一点的展现出来



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值