Java Code Examples for android.graphics.drawable.StateListDrawable.getConstantState()

The following are Jave code examples for showing how to use getConstantState() of the android.graphics.drawable.StateListDrawable class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: silly-android   File: ColoringTest.java   View Source Code Vote up 6 votes
/**
 * 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);
}
 
Example 2
Project: GitHub   File: WoWoStateListColorAnimation.java   View Source Code Vote up 5 votes
private void setColors(View view, int[] colors) {
    Drawable drawable = view.getBackground();
    if (drawable instanceof StateListDrawable) {
        StateListDrawable stateListDrawable = (StateListDrawable) drawable;
        DrawableContainerState drawableContainerState = (DrawableContainerState) stateListDrawable.getConstantState();
        if (drawableContainerState != null) {
            Drawable[] drawables = drawableContainerState.getChildren();
            for (int i = 0; i < colors.length; i++) if (drawables[i] instanceof GradientDrawable) ((GradientDrawable) drawables[i]).setColor(colors[i]);
        }
    } else Log.w(TAG, "Drawable of view must be StateListDrawable in WoWoStateListColorAnimation");
}
 
Example 3
Project: silly-android   File: ColoringTest.java   View Source Code Vote up 5 votes
/**
 * Tests the {@link Coloring#createStateList(Context, int, int, int, boolean, int)} method.
 * <p>
 * Unfortunately {@link StateListDrawable#getCurrent()} is not shadowed by Robolectric yet.
 */
@Test
public final void testCreateStateList() {
    final int colorNormal = Color.WHITE;
    final int colorActive = Color.GRAY;
    final int colorFocused = Color.YELLOW;
    final StateListDrawable drawable = Coloring.createStateList(mActivityContext, colorNormal, colorActive, colorFocused, true, 0);
    assertNotNull("StateListDrawable is null", drawable);
    drawable.setState(new int[] {});
    assertTrue("StateListDrawable is not stateful", drawable.isStateful());
    final Drawable.ConstantState constantState = drawable.getConstantState();
    assertNotNull("Constant state is null", constantState);
    final Drawable currentState = drawable.getCurrent();
    assertNull("Robolectric started shadowing current state", currentState);
}
 
Example 4
Project: silly-android   File: ColoringTest.java   View Source Code Vote up 5 votes
/**
 * Tests the {@link Coloring#createContrastStateDrawable(Context, int, int, boolean, Drawable)} 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 testCreateContrastStateDrawable() {
    // noinspection deprecation - can't enforce Lollipop here
    final BitmapDrawable original = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_on);
    assertNotNull("Original drawable is null", original);

    final StateListDrawable stateList = Coloring.createContrastStateDrawable(mActivityContext, Color.WHITE, Color.BLACK, true, original);
    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);
}
 
Example 5
Project: Custom-Toggle-Button   File: CustomToggleButton.java   View Source Code Vote up 4 votes
public void initialize(Context context, AttributeSet attrs) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.setBackground(getResources().getDrawable(R.drawable.toggle_background, null));
        } else {
            this.setBackground(getResources().getDrawable(R.drawable.toggle_background));
        }

        StateListDrawable stateListDrawable = (StateListDrawable) this.getBackground();
        DrawableContainer.DrawableContainerState dcs = (DrawableContainer.DrawableContainerState) stateListDrawable.getConstantState();
        Drawable[] drawableItems = dcs.getChildren();
        GradientDrawable unChecked = (GradientDrawable) drawableItems[0];
        GradientDrawable checked = (GradientDrawable) drawableItems[1];
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomToggleButton);

//        getting all the attributes values set from the typed array i.e from user
        int toggleOnColor = typedArray.getColor(R.styleable.CustomToggleButton_checkedColor, Color.parseColor("#FF4081"));
        int toggleOffColor = typedArray.getColor(R.styleable.CustomToggleButton_uncheckedColor, Color.parseColor("#FF4081"));
        float borderWidth = typedArray.getDimension(R.styleable.CustomToggleButton_borderWidth, 4.0f);
        float radius = typedArray.getDimension(R.styleable.CustomToggleButton_radius, 15.0f);
        int checkedTextColor = typedArray.getColor(R.styleable.CustomToggleButton_checkedTextColor, getResources().getColor(R.color.CheckedTextColor));
        int uncheckedTextColor = typedArray.getColor(R.styleable.CustomToggleButton_uncheckedTextColor, getResources().getColor(R.color.uncheckedTextColor));
        Log.d(TAG, "initialize: " + borderWidth);
        ColorStateList colorStateList = new ColorStateList(
                new int[][]{
                        new int[]{android.R.attr.state_checked},
                        new int[]{-android.R.attr.state_checked}
                },
                new int[]{
                        checkedTextColor,
                        uncheckedTextColor
                }
        );

        this.setTextColor(colorStateList);


        checked.setStroke(Math.round(borderWidth), toggleOnColor);
        checked.setColor(toggleOnColor);
        checked.setCornerRadius(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radius, getResources().getDisplayMetrics()));

        unChecked.setStroke(Math.round(borderWidth), toggleOffColor);
        unChecked.setCornerRadius(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radius, getResources().getDisplayMetrics()));


    }