android levellistdrawable 基本使用

levellistdrawable可以通过改变level值来切换相应的图片。

一:在drawablw文件夹下:

 

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <level-list xmlns:android="http://schemas.android.com/apk/res/android">

  3. <item android:drawable="@drawable/lamp_on" //灯亮的图片

  4. android:minLevel="12"

  5. android:maxLevel="20"/>

  6.  
  7. <item android:drawable="@drawable/lamp_off" //灯灭的图片

  8. android:minLevel="6"

  9. android:maxLevel="10"/>

  10. </level-list>


二:布局文件

 

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:orientation="vertical"

  4. android:layout_width="match_parent"

  5. android:layout_height="match_parent">

  6.  
  7. <ImageView

  8. android:src="@drawable/bitmap"

  9. android:layout_width="wrap_content"

  10. android:layout_height="wrap_content"

  11. android:id="@+id/iv_drawable"/>

  12.  
  13. <Button

  14. android:id="@+id/btn_on"

  15. android:text="light on"

  16. android:layout_width="wrap_content"

  17. android:layout_height="wrap_content"/>

  18.  
  19. <Button

  20. android:id="@+id/btn_off"

  21. android:text="light off"

  22. android:layout_width="wrap_content"

  23. android:layout_height="wrap_content"/>

  24. </LinearLayout>

三:activity

 

 
  1. package com.example.kirito.myapplication.testdrawable;

  2.  
  3. import android.os.Bundle;

  4. import android.support.annotation.Nullable;

  5. import android.support.v7.app.AppCompatActivity;

  6. import android.view.View;

  7. import android.widget.Button;

  8. import android.widget.ImageView;

  9.  
  10. import com.example.kirito.myapplication.R;

  11.  
  12. /**

  13. * Created by kirito on 2016.10.31.

  14. */

  15.  
  16. public class TestDrawable extends AppCompatActivity implements View.OnClickListener{

  17. private Button on,off;

  18. private ImageView iv;

  19.  
  20. @Override

  21. protected void onCreate(@Nullable Bundle savedInstanceState) {

  22. super.onCreate(savedInstanceState);

  23. setContentView(R.layout.testdrawable);

  24.  
  25. on = (Button) findViewById(R.id.btn_on);

  26. off = (Button) findViewById(R.id.btn_off);

  27. on.setOnClickListener(this);

  28. off.setOnClickListener(this);

  29. iv = (ImageView) findViewById(R.id.iv_drawable);

  30. iv.setImageLevel(8);

  31. }

  32.  
  33. @Override

  34. public void onClick(View v) {

  35. if (v.getId() == R.id.btn_off){

  36. //设置的level值必须在6-10之间

  37. iv.setImageLevel(8);

  38. }else if (v.getId() == R.id.btn_on){

  39. //设置的level值必须在12-20之间

  40. iv.setImageLevel(18);

  41. }

  42. }

  43. }


四:效果图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值