-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDrawableUtil.java
40 lines (35 loc) · 1.16 KB
/
DrawableUtil.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
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.itheima.demo2;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build.VERSION;
public class DrawableUtil {
/**
* 生成圆角图片,对应的xml中的shape标签
* @return
*/
public static Drawable generateDrawable(int rgb,float radius){
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);//设置为矩形,默认就是矩形
drawable.setColor(rgb);
drawable.setCornerRadius(radius);
return drawable;
}
/**
* 用java代码的方式动态生成状态选择器
* @param pressed
* @param normal
* @return
*/
public static Drawable generateSelector(Drawable pressed,Drawable normal){
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{android.R.attr.state_pressed}, pressed);//设置按下的图片
drawable.addState(new int[]{}, normal);//设置默认的图片
//设置状态选择器过度动画
if(VERSION.SDK_INT>10){
drawable.setEnterFadeDuration(500);
drawable.setExitFadeDuration(500);
}
return drawable;
}
}