Search in sources :

Example 96 with StateListDrawable

use of android.graphics.drawable.StateListDrawable in project platform_frameworks_base by android.

the class RenderDrawable method render.

public Result render() {
    checkLock();
    // get the drawable resource value
    DrawableParams params = getParams();
    HardwareConfig hardwareConfig = params.getHardwareConfig();
    ResourceValue drawableResource = params.getDrawable();
    // resolve it
    BridgeContext context = getContext();
    drawableResource = context.getRenderResources().resolveResValue(drawableResource);
    if (drawableResource == null) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    ResourceType resourceType = drawableResource.getResourceType();
    if (resourceType != ResourceType.DRAWABLE && resourceType != ResourceType.MIPMAP) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    Drawable d = ResourceHelper.getDrawable(drawableResource, context);
    final Boolean allStates = params.getFlag(RenderParamsFlags.FLAG_KEY_RENDER_ALL_DRAWABLE_STATES);
    if (allStates == Boolean.TRUE) {
        final List<BufferedImage> result;
        if (d instanceof StateListDrawable) {
            result = new ArrayList<BufferedImage>();
            final StateListDrawable stateList = (StateListDrawable) d;
            for (int i = 0; i < stateList.getStateCount(); i++) {
                final Drawable stateDrawable = stateList.getStateDrawable(i);
                result.add(renderImage(hardwareConfig, stateDrawable, context));
            }
        } else {
            result = Collections.singletonList(renderImage(hardwareConfig, d, context));
        }
        return Status.SUCCESS.createResult(result);
    } else {
        BufferedImage image = renderImage(hardwareConfig, d, context);
        return Status.SUCCESS.createResult(image);
    }
}
Also used : HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceType(com.android.resources.ResourceType) StateListDrawable(android.graphics.drawable.StateListDrawable) BufferedImage(java.awt.image.BufferedImage) DrawableParams(com.android.ide.common.rendering.api.DrawableParams)

Example 97 with StateListDrawable

use of android.graphics.drawable.StateListDrawable in project android_frameworks_base by ParanoidAndroid.

the class ProgressBar method tileify.

/**
     * Converts a drawable to a tiled version of itself. It will recursively
     * traverse layer and state list drawables.
     */
private Drawable tileify(Drawable drawable, boolean clip) {
    if (drawable instanceof LayerDrawable) {
        LayerDrawable background = (LayerDrawable) drawable;
        final int N = background.getNumberOfLayers();
        Drawable[] outDrawables = new Drawable[N];
        for (int i = 0; i < N; i++) {
            int id = background.getId(i);
            outDrawables[i] = tileify(background.getDrawable(i), (id == R.id.progress || id == R.id.secondaryProgress));
        }
        LayerDrawable newBg = new LayerDrawable(outDrawables);
        for (int i = 0; i < N; i++) {
            newBg.setId(i, background.getId(i));
        }
        return newBg;
    } else if (drawable instanceof StateListDrawable) {
        StateListDrawable in = (StateListDrawable) drawable;
        StateListDrawable out = new StateListDrawable();
        int numStates = in.getStateCount();
        for (int i = 0; i < numStates; i++) {
            out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
        }
        return out;
    } else if (drawable instanceof BitmapDrawable) {
        final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
        if (mSampleTile == null) {
            mSampleTile = tileBitmap;
        }
        final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());
        final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
        shapeDrawable.getPaint().setShader(bitmapShader);
        return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL) : shapeDrawable;
    }
    return drawable;
}
Also used : Bitmap(android.graphics.Bitmap) LayerDrawable(android.graphics.drawable.LayerDrawable) LayerDrawable(android.graphics.drawable.LayerDrawable) AnimationDrawable(android.graphics.drawable.AnimationDrawable) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) ClipDrawable(android.graphics.drawable.ClipDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable) BitmapShader(android.graphics.BitmapShader) ClipDrawable(android.graphics.drawable.ClipDrawable)

Example 98 with StateListDrawable

use of android.graphics.drawable.StateListDrawable in project android_frameworks_base by ParanoidAndroid.

the class TargetDrawable method setState.

public void setState(int[] state) {
    if (mDrawable instanceof StateListDrawable) {
        StateListDrawable d = (StateListDrawable) mDrawable;
        d.setState(state);
    }
}
Also used : StateListDrawable(android.graphics.drawable.StateListDrawable)

Example 99 with StateListDrawable

use of android.graphics.drawable.StateListDrawable in project android_frameworks_base by ParanoidAndroid.

the class TargetDrawable method resizeDrawables.

/**
     * Makes drawables in a StateListDrawable all the same dimensions.
     * If not a StateListDrawable, then justs sets the bounds to the intrinsic size of the
     * drawable.
     */
private void resizeDrawables() {
    if (mDrawable instanceof StateListDrawable) {
        StateListDrawable d = (StateListDrawable) mDrawable;
        int maxWidth = 0;
        int maxHeight = 0;
        for (int i = 0; i < d.getStateCount(); i++) {
            Drawable childDrawable = d.getStateDrawable(i);
            maxWidth = Math.max(maxWidth, childDrawable.getIntrinsicWidth());
            maxHeight = Math.max(maxHeight, childDrawable.getIntrinsicHeight());
        }
        if (DEBUG)
            Log.v(TAG, "union of childDrawable rects " + d + " to: " + maxWidth + "x" + maxHeight);
        d.setBounds(0, 0, maxWidth, maxHeight);
        for (int i = 0; i < d.getStateCount(); i++) {
            Drawable childDrawable = d.getStateDrawable(i);
            if (DEBUG)
                Log.v(TAG, "sizing drawable " + childDrawable + " to: " + maxWidth + "x" + maxHeight);
            childDrawable.setBounds(0, 0, maxWidth, maxHeight);
        }
    } else if (mDrawable != null) {
        mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
    }
}
Also used : StateListDrawable(android.graphics.drawable.StateListDrawable) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable)

Example 100 with StateListDrawable

use of android.graphics.drawable.StateListDrawable in project android_frameworks_base by ParanoidAndroid.

the class IconUtilities method createIconDrawable.

public Drawable createIconDrawable(Drawable src) {
    Bitmap scaled = createIconBitmap(src);
    StateListDrawable result = new StateListDrawable();
    result.addState(new int[] { android.R.attr.state_focused }, new BitmapDrawable(createSelectedBitmap(scaled, false)));
    result.addState(new int[] { android.R.attr.state_pressed }, new BitmapDrawable(createSelectedBitmap(scaled, true)));
    result.addState(new int[0], new BitmapDrawable(scaled));
    result.setBounds(0, 0, mIconTextureWidth, mIconTextureHeight);
    return result;
}
Also used : Bitmap(android.graphics.Bitmap) BitmapDrawable(android.graphics.drawable.BitmapDrawable) StateListDrawable(android.graphics.drawable.StateListDrawable)

Aggregations

StateListDrawable (android.graphics.drawable.StateListDrawable)163 Drawable (android.graphics.drawable.Drawable)43 LayerDrawable (android.graphics.drawable.LayerDrawable)24 BitmapDrawable (android.graphics.drawable.BitmapDrawable)21 GradientDrawable (android.graphics.drawable.GradientDrawable)21 ColorDrawable (android.graphics.drawable.ColorDrawable)15 Bitmap (android.graphics.Bitmap)13 TextView (android.widget.TextView)12 ShapeDrawable (android.graphics.drawable.ShapeDrawable)11 View (android.view.View)11 AnimationDrawable (android.graphics.drawable.AnimationDrawable)9 ClipDrawable (android.graphics.drawable.ClipDrawable)9 SuppressLint (android.annotation.SuppressLint)8 Paint (android.graphics.Paint)8 ColorStateList (android.content.res.ColorStateList)7 RippleDrawable (android.graphics.drawable.RippleDrawable)7 TargetApi (android.annotation.TargetApi)6 TypedArray (android.content.res.TypedArray)6 TextPaint (android.text.TextPaint)5 Button (android.widget.Button)5