Android LevelDrawable模拟开灯灭灯的场景

来源:互联网 发布:win10 ssd 磁盘优化 编辑:程序博客网 时间:2024/04/28 03:14

模拟非诚勿扰灭灯场景大笑 请大家一起来灭灯

首先第一步下载资源,没有的小伙伴我这里提供啦


然后在Drawable目录创建一个level.xml

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        
        android:drawable="@drawable/off"
        android:minLevel="6"
        android:maxLevel="10"></item>
     <item 
        
        android:drawable="@drawable/on"
        android:minLevel="12"
        android:maxLevel="20"></item>

</level-list>


然后创建布局文件activity_level

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


    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/level" />


    <Button
        android:id="@+id/btn_on"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="30dp"
        android:onClick="on"
        android:text="开灯" />


    <Button
        android:id="@+id/btn_off"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="30dp"
        android:onClick="off"
        android:text="关灯" />


</LinearLayout>

LevelActivity

package com.android.xiong.gridlayoutTest;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;


public class LevelActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level);
iv=(ImageView) findViewById(R.id.iv);
iv.setImageLevel(10);
}
public void on(View v){
iv.setImageLevel(15);
//开灯
}
       public void off(View v){
iv.setImageLevel(10);
//关灯
}

}

效果如下




1 0
原创粉丝点击