本文整理汇总了Java中android.graphics.drawable.StateListDrawable类的典型用法代码示例。如果您正苦于以下问题:Java StateListDrawable类的具体用法?Java StateListDrawable怎么用?Java StateListDrawable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StateListDrawable类属于android.graphics.drawable包,在下文中一共展示了StateListDrawable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: update
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
private void update() {
StateListDrawable background = new StateListDrawable();
background.addState(new int[]{android.R.attr.state_pressed}, createDrawable(backgroundColorPressed, cornerColorPressed, cornerWidth, cornerRadius));
background.addState(new int[]{-android.R.attr.state_enabled}, createDrawable(backgroundColorDisabled, cornerColorDisabled, cornerWidth, cornerRadius));
background.addState(StateSet.WILD_CARD, createDrawable(backgroundColor, cornerColor, cornerWidth, cornerRadius));
setBackground(background);
setTextColor(new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_pressed},
new int[]{-android.R.attr.state_enabled},
new int[]{}
},
new int[]{
textColorPressed,
textColorDisabled,
textColor
}
));
}
示例2: createFillDrawable
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{android.R.attr.state_pressed}, createRectDrawable(mColorPressed));
drawable.addState(new int[]{}, createRectDrawable(mColorNormal));
if (Util.hasLollipop()) {
RippleDrawable ripple = new RippleDrawable(new ColorStateList(new int[][]{{}},
new int[]{mColorRipple}), drawable, null);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
});
setClipToOutline(true);
mBackgroundDrawable = ripple;
return ripple;
}
mBackgroundDrawable = drawable;
return drawable;
}
示例3: setBackgroundColor
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void setBackgroundColor(int colorPressed, int colorNormal){
StateListDrawable sd = new StateListDrawable();
int[] state_pressed = new int[]{android.R.attr.state_pressed};
int[] state_normal = new int[]{android.R.attr.state_enabled};
GradientDrawable pressed = new GradientDrawable();
pressed.setColor(colorPressed);
pressed.setCornerRadius(10);
GradientDrawable enable = new GradientDrawable();
enable.setColor(colorNormal);
enable.setCornerRadius(10);
sd.addState(state_pressed, pressed);
sd.addState(state_normal, enable);
bg.setBackground(sd);
}
示例4: onActionDown
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionDown() {
if (mUsingStyle) {
mBackgroundDrawable = getBackground();
}
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{android.R.attr.state_pressed});
} else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
ripple.setVisible(true, true);
}
// setPressed(true);
}
示例5: createStateListDrawable
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
/**
* create state list drawable
*
* @param context
* @param idNormal
* @param idPressed
* @param idFocused
* @param idUnable
* @return
*/
public static StateListDrawable createStateListDrawable(Context context, int idNormal, int idPressed, int idFocused, int idUnable) {
StateListDrawable bg = new StateListDrawable();
Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
Drawable focused = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
Drawable unable = idUnable == -1 ? null : context.getResources().getDrawable(idUnable);
// View.PRESSED_ENABLED_STATE_SET
bg.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
// View.ENABLED_FOCUSED_STATE_SET
bg.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focused);
// View.ENABLED_STATE_SET
bg.addState(new int[]{android.R.attr.state_enabled}, normal);
// View.FOCUSED_STATE_SET
bg.addState(new int[]{android.R.attr.state_focused}, focused);
// View.WINDOW_FOCUSED_STATE_SET
bg.addState(new int[]{android.R.attr.state_window_focused}, unable);
// View.EMPTY_STATE_SET
bg.addState(new int[]{}, normal);
return bg;
}
示例6: setCircleButtonStateListDrawable
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
public void setCircleButtonStateListDrawable(View circleButton, int radius,int color) {
WeakReference<Bitmap> imageNormal = new WeakReference<>(Bitmap.createBitmap(2 * radius, 2 * radius, Bitmap.Config.ARGB_8888));
Canvas canvasNormal = new Canvas(imageNormal.get());
Paint paintNormal = new Paint();
paintNormal.setAntiAlias(true);
paintNormal.setColor(color);
canvasNormal.drawCircle(radius, radius, radius, paintNormal);
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(StateSet.WILD_CARD, new BitmapDrawable(circleButton.getContext().getResources(), imageNormal.get()));
if (android.os.Build.VERSION.SDK_INT >= 16) {
circleButton.setBackground(stateListDrawable);
} else {
circleButton.setBackgroundDrawable(stateListDrawable);
}
}
示例7: drawableStateChanged
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (!mIsThumbUseDrawable && mThumbColor != null) {
mCurrThumbColor = mThumbColor.getColorForState(getDrawableState(), mCurrThumbColor);
} else {
setDrawableState(mThumbDrawable);
}
int[] nextState = isChecked() ? UNCHECKED_PRESSED_STATE : CHECKED_PRESSED_STATE;
ColorStateList textColors = getTextColors();
if (textColors != null) {
int defaultTextColor = textColors.getDefaultColor();
mOnTextColor = textColors.getColorForState(CHECKED_PRESSED_STATE, defaultTextColor);
mOffTextColor = textColors.getColorForState(UNCHECKED_PRESSED_STATE, defaultTextColor);
}
if (!mIsBackUseDrawable && mBackColor != null) {
mCurrBackColor = mBackColor.getColorForState(getDrawableState(), mCurrBackColor);
mNextBackColor = mBackColor.getColorForState(nextState, mCurrBackColor);
} else {
if (mBackDrawable instanceof StateListDrawable && mFadeBack) {
mBackDrawable.setState(nextState);
mNextBackDrawable = mBackDrawable.getCurrent().mutate();
} else {
mNextBackDrawable = null;
}
setDrawableState(mBackDrawable);
if (mBackDrawable != null) {
mCurrentBackDrawable = mBackDrawable.getCurrent().mutate();
}
}
}
示例8: generateRoundDrawable
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
/**
* Generate bg drawable drawable.
*
* @param radii the radii
* @param pressColor the press color
* @param defaultColor the default color
* @return the drawable
*/
public static Drawable generateRoundDrawable(Resources res, float radii, int pressColor, int defaultColor) {
radii = dpToPx(res, radii);
//外环的圆角矩形
float[] outRadii = new float[]{radii, radii, radii, radii, radii, radii, radii, radii};//四个角的 圆角幅度,8个可以设置的值,每个角都有2个边 2*4=8个
//按下状态
Shape roundRectShape = new RoundRectShape(outRadii, null, null);//圆角背景
ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape
shopDrawablePress.getPaint().setColor(pressColor);//设置颜色
//正常状态
Shape roundRectShapeNormal = new RoundRectShape(outRadii, null, null);
ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShapeNormal);
shopDrawableNormal.getPaint().setColor(defaultColor);
StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape
bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态
bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态
return bgStateDrawable;
}
示例9: generateBorderDrawable
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
/**
* 正常 圆角边框;
* 按下 圆角色块
*/
public static Drawable generateBorderDrawable(float radii, float borderWidth, int pressColor, int defaultColor) {
//外环的圆角矩形
float[] outRadii = new float[]{radii, radii, radii, radii, radii, radii, radii, radii};//四个角的 圆角幅度,8个可以设置的值,每个角都有2个边 2*4=8个
RectF inset = new RectF(borderWidth, borderWidth, borderWidth, borderWidth);
//按下状态
Shape roundRectShape = new RoundRectShape(outRadii, null, null);//圆角背景
ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape
shopDrawablePress.getPaint().setColor(pressColor);//设置颜色
//正常状态
Shape roundRectShapeNormal = new RoundRectShape(outRadii, inset, outRadii);
ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShapeNormal);
shopDrawableNormal.getPaint().setColor(defaultColor);
StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape
bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态
bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态
return bgStateDrawable;
}
示例10: YearPickerView
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
public YearPickerView(Context context, DatePickerController controller) {
super(context);
this.mController = controller;
this.mController.registerOnDateChangedListener(this);
setLayoutParams(new LayoutParams(-1, -2));
Resources res = context.getResources();
this.mViewSize = res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height);
this.mChildSize = res.getDimensionPixelOffset(R.dimen.mdtp_year_label_height);
setVerticalFadingEdgeEnabled(true);
setFadingEdgeLength(this.mChildSize / 3);
init(context);
setOnItemClickListener(this);
setSelector(new StateListDrawable());
setDividerHeight(0);
onDateChanged();
}
示例11: getNewSelector
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
/**
* 通过代码配置一个selector XML对象 . <br>
* @author liulongzhenhai 2012-7-4 上午10:45:03 <br>
* @param normal 没有状态
* @param pressed 按下状态
* @param focused 获取焦点状态
* @param unable 无状态
* @return 返回selector 的对象布局
*/
public static StateListDrawable getNewSelector(final Drawable normal, final Drawable pressed,
final Drawable focused, final Drawable unable) {
final StateListDrawable bg = new StateListDrawable();
// View.PRESSED_ENABLED_STATE_SET
bg.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
// View.ENABLED_FOCUSED_STATE_SET
bg.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);
// View.ENABLED_STATE_SET
bg.addState(new int[] { android.R.attr.state_enabled }, normal);
// View.FOCUSED_STATE_SET
bg.addState(new int[] { android.R.attr.state_focused }, focused);
// View.WINDOW_FOCUSED_STATE_SET
bg.addState(new int[] { android.R.attr.state_window_focused }, unable);
// View.EMPTY_STATE_SET
bg.addState(new int[] {}, normal);
return bg;
}
示例12: createFillDrawable
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[]{-android.R.attr.state_enabled}, createCircleDrawable(mColorDisabled));
drawable.addState(new int[]{android.R.attr.state_pressed}, createCircleDrawable(mColorPressed));
drawable.addState(new int[]{}, createCircleDrawable(mColorNormal));
if (Util.hasLollipop()) {
RippleDrawable ripple = new RippleDrawable(new ColorStateList(new int[][]{{}},
new int[]{mColorRipple}), drawable, null);
setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, view.getWidth(), view.getHeight());
}
});
setClipToOutline(true);
mBackgroundDrawable = ripple;
return ripple;
}
mBackgroundDrawable = drawable;
return drawable;
}
示例13: initIdleStateDrawable
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
private void initIdleStateDrawable() {
int colorNormal = getNormalColor(mIdleColorState);
int colorPressed = getPressedColor(mIdleColorState);
int colorFocused = getFocusedColor(mIdleColorState);
int colorDisabled = getDisabledColor(mIdleColorState);
if (background == null) {
background = createDrawable(colorNormal);
}
StrokeGradientDrawable drawableDisabled = createDrawable(colorDisabled);
StrokeGradientDrawable drawableFocused = createDrawable(colorFocused);
StrokeGradientDrawable drawablePressed = createDrawable(colorPressed);
mIdleStateDrawable = new StateListDrawable();
mIdleStateDrawable.addState(new int[]{android.R.attr.state_pressed}, drawablePressed.getGradientDrawable());
mIdleStateDrawable.addState(new int[]{android.R.attr.state_focused}, drawableFocused.getGradientDrawable());
mIdleStateDrawable.addState(new int[]{-android.R.attr.state_enabled}, drawableDisabled.getGradientDrawable());
mIdleStateDrawable.addState(StateSet.WILD_CARD, background.getGradientDrawable());
}
示例14: onActionUp
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
void onActionUp() {
if (mUsingStyle) {
mBackgroundDrawable = getBackground();
}
if (mBackgroundDrawable instanceof StateListDrawable) {
StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
drawable.setState(new int[]{});
} else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
ripple.setState(new int[]{});
ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
ripple.setVisible(true, true);
}
// setPressed(false);
}
示例15: testCreateMultiStateDrawable
import android.graphics.drawable.StateListDrawable; //导入依赖的package包/类
/**
* Tests the {@link Coloring#createMultiStateDrawable(Drawable, Drawable, Drawable, boolean)} method.
* <p>
* Unfortunately some Drawable properties are not shadowed by Robolectric yet, so we can test only the basic stuff here.
*/
@Test
public final void testCreateMultiStateDrawable() {
// noinspection deprecation - can't enforce Lollipop here
final BitmapDrawable normal = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_on);
assertNotNull("Normal drawable is null", normal);
// noinspection deprecation - can't enforce Lollipop here
final BitmapDrawable clicked = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_off);
assertNotNull("Clicked drawable is null", clicked);
// noinspection deprecation - can't enforce Lollipop here
final BitmapDrawable checked = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.star_off);
assertNotNull("Checked drawable is null", checked);
final StateListDrawable stateList = Coloring.createMultiStateDrawable(normal, clicked, checked, true);
assertNotNull("Contrast state drawable is null", stateList);
assertTrue("Contrast state drawable is not stateful", stateList.isStateful());
final Drawable.ConstantState constantState = stateList.getConstantState();
assertNotNull("Constant state is null", constantState);
}