android drawable

绘制资源类型 (Drawable)类中有个setLevel()的方法他的作用是选择某个级别中可绘制的资源

有时候,我们为了在一个image view中显示不同的图片,往往会使用: 

if (条件1)


image.setBackground(R.id.xxx1); 
}

else if (条件2)


image.setBackground(R.id.xxx2); 
} ... 
可以使用setLevel()更好的实现

在xml中:

?
1
2
3
4
5
6
7
8
9
<? xml version = "1.0" encoding = "utf-8" ?>
< level-listxmlns:android = "http://schemas.android.com/apk/res/android" >
     < item
         android:drawable = "@drawable/status_off"
         android:maxLevel = "0" />
     < item
         android:drawable = "@drawable/status_on"
         android:maxLevel = "1" />
</ level-list >

调用setLevel显示不同的图片

在java中:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
leftPoint.getDrawable().setLevel( 1 );
         viewPager.setOnPageChangeListener( new ViewPager.SimpleOnPageChangeListener() {
             @Override
             public void onPageSelected( int position) {
                 super .onPageSelected(position);
                 switch (position) {
                     case 0 :
                         leftPoint.getDrawable().setLevel( 1 );
                         centerPoint.getDrawable().setLevel( 0 );
                         rightPoint.getDrawable().setLevel( 0 );
                         break ;
                     case 1 :
                         leftPoint.getDrawable().setLevel( 0 );
                         centerPoint.getDrawable().setLevel( 1 );
                         rightPoint.getDrawable().setLevel( 0 );
                         break ;
                     case 2 :
                         leftPoint.getDrawable().setLevel( 0 );
                         centerPoint.getDrawable().setLevel( 0 );
                         rightPoint.getDrawable().setLevel( 1 );
                         break ;
 
                 }
             }
         });
         addView(view);
     }

你可能感兴趣的:(android drawable)