ClipDrawable的使用,图片的徐徐展开效果

本文介绍了如何在Android中使用ClipDrawable实现图片徐徐展开的效果。通过设置ClipDrawable的clipOrientation和gravity属性,配合ImageView的scaleType,达到动态改变图片截取区域的目的。在代码示例中,通过Handler和Timer定时更新ClipDrawable的level值,实现图片逐渐显示。源码可供下载参考。
摘要由CSDN通过智能技术生成

效果图:




代码:

clip 属性的使用:

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

layout 中的设置:

<RelativeLayout 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"
    tools:context=".MainActivity" >


    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:scaleType="fitStart"
        android:src="@drawable/clip_style"
       />


</RelativeLayout>


代码:

package com.example.clipdrawable;


import java.util.Timer;
import java.util.TimerTask;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.drawable.ClipDrawable;
import android.view.Menu;
import android.widget.ImageView;


public class MainActivity extends Activity {


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

ImageView imageView = (ImageView) findViewById(R.id.image);
//获取图片所显示的ClipDrawable对象
final ClipDrawable drawable = (ClipDrawable) imageView.getDrawable();
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if (msg.what==0x123) {
/**setlevel()设置图片截取的大小
* 修改ClipDrawable的level值,level值为0--10000;
* 0:截取图片大小为空白,10000:截取图片为整张图片;
*/
drawable.setLevel(drawable.getLevel()+200);
}
}

};
final Timer timer = new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
// TODO Auto-generated method stub
Message msg = new Message();
msg.what = 0x123;
handler.sendMessage(msg);
if (drawable.getLevel()>=10000) {
timer.cancel();
}
}
}, 0,300);
}
}

源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值