Android之ClipDrawable资源简单使用

使用ClipDrawable实现图片徐徐展开效果

ClipDrawable 代表从其他位图上截取一个”图片片段” 调用 setLevel(int level) 方法来设置截取的区域大小 当level为0时,截取的图片片段为空; 当level为10000时,截取整张图片。 一开始level默认为0。

以下为源代码


在drawable文件下定义一个xml文件

<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/aaa" <!--指定截取源文件-->
    android:clipOrientation="horizontal"<!--截取方向-->
    android:gravity="center"<!--截取时的对齐方式-->
    >
</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"
    tools:context="com.example.brazen.test.MainActivity">
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/clipdrawable"/><!--引用文件-->
</LinearLayout>


Java代码

public class MainActivity extends Activity {
    Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);
        //获取imageview
        ImageView imageview = (ImageView) findViewById(R.id.imageview);
        //获取图片所显示的ClipDrawable对象
        //补充:匿名内部类引用外部类局部变量要用final修饰
        final ClipDrawable clipDrawable = (ClipDrawable) imageview.getDrawable();

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //修改level值,默认为0,所以一开始什么都没显示。最大值为10000表示完全显示
                clipDrawable.setLevel(clipDrawable.getLevel() + 200);

            }
        };
//创建一个定时器
//补充:第一个参数表示线程,第二个参数表示延迟即什么时候开始执行第一个run(),第三个参数表示间隔。
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (clipDrawable.getLevel() < 10000)
                    timer.cancel();
                    mHandler.sendEmptyMessage(0x123);
            }
        }, 0, 300);
     }
}


运行效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值