Code example for Drawable

Methods: drawgetIntrinsicWidthsetBounds

0
        if (mNeedBoundsUpdate) {
            updateBounds(); 
            mNeedBoundsUpdate = false;
        } 
        mLeftCapDrawable.draw(canvas);
        mBottomAnchorDrawable.draw(canvas);
        mRightCapDrawable.draw(canvas);
    } 
 
    @Override 
    public void setAlpha(int alpha) {
        mLeftCapDrawable.setAlpha(alpha);
        mBottomAnchorDrawable.setAlpha(alpha);
        mRightCapDrawable.setAlpha(alpha);
        invalidateSelf();
    } 
 
    @Override 
    public void setColorFilter(ColorFilter cf) {
        mLeftCapDrawable.setColorFilter(cf);
        mBottomAnchorDrawable.setColorFilter(cf);
        mRightCapDrawable.setColorFilter(cf);
        invalidateSelf();
    } 
 
    @Override 
    public int getOpacity() { 
        return Drawable.resolveOpacity(mLeftCapDrawable.getOpacity(),
                Drawable.resolveOpacity(mBottomAnchorDrawable.getOpacity(), mRightCapDrawable.getOpacity()));
    } 
 
    @Override 
    public Drawable mutate() {
        if (!mMutated && super.mutate() == this) {
            mLeftCapDrawable.mutate();
            mBottomAnchorDrawable.mutate();
            mRightCapDrawable.mutate();
            mMutated = true;
        } 
        return this;
    } 
 
    @Override 
    public boolean getPadding(Rect padding) {
        // The padding for this Drawable is a composition of some of the 
        // paddings of the underlying Drawables. 
        final Rect rect = mTempRect;
 
        mLeftCapDrawable.getPadding(rect);
        int pLeft = rect.left;
 
        mBottomAnchorDrawable.getPadding(rect);
        int pTop = rect.top;
        int pBottom = rect.bottom;
 
        mRightCapDrawable.getPadding(rect);
        int pRight = rect.right;
 
        padding.set(pLeft, pTop, pRight, pBottom);
 
        return pLeft != 0 && pTop != 0 && pRight != 0 && pBottom != 0;
    } 
 
    @Override 
    public boolean isStateful() { 
        return mLeftCapDrawable.isStateful() || mBottomAnchorDrawable.isStateful() || mRightCapDrawable.isStateful();
    } 
 
    @Override 
    protected boolean onStateChange(int[] state) {
        boolean result = false;
        result |= mLeftCapDrawable.setState(state);
        result |= mBottomAnchorDrawable.setState(state);
        result |= mRightCapDrawable.setState(state);
        if (result) {
            // It's weird but we need to invalidate on our own. The framework is 
            // not doing it by itself depending on the returned value here. 
            invalidateSelf();
        } 
        return result;
    } 
 
    @Override 
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        mNeedBoundsUpdate = true;
    } 
 
    @Override 
    public int getIntrinsicHeight() { 
        return Math.max(mLeftCapDrawable.getIntrinsicHeight(),
                Math.max(mBottomAnchorDrawable.getIntrinsicHeight(), mRightCapDrawable.getIntrinsicHeight()));
    } 
 
    @Override 
    public int getIntrinsicWidth() { 
        //@formatter:off 
        return mLeftCapDrawable.getIntrinsicWidth() 
                + mBottomAnchorDrawable.getIntrinsicWidth() 
                + mRightCapDrawable.getIntrinsicWidth();
        //@formatter:on 
    } 
 
    @Override 
    public int getChangingConfigurations() { 
        //@formatter:off 
        return super.getChangingConfigurations() 
                | mLeftCapDrawable.getChangingConfigurations()
                | mBottomAnchorDrawable.getChangingConfigurations()
                | mRightCapDrawable.getChangingConfigurations();
        //@formatter:on 
    } 
 
    private void updateBounds() { 
 
        // The anchor offset may be out of range. Let's clamp it to the 
        // authorized set of values. 
        final int anchorOffset;
        switch (mAnchorOffset) {
            case ANCHOR_POSITION_CENTER:
                anchorOffset = getBounds().width() / 2;
                break; 
 
            default: 
                anchorOffset = Math.min(Math.max(mLeftMargin, mAnchorOffset), getBounds().width() - mRightMargin);
                break; 
        } 
 
        final Rect selfBounds = getBounds();
        final int leftCapWidth = anchorOffset - mBottomAnchorDrawable.getIntrinsicWidth() / 2;
 
        //@formatter:off 
        mLeftCapDrawable.setBounds(
                selfBounds.left,
                selfBounds.top,
                selfBounds.left + leftCapWidth,
                selfBounds.bottom);
         
        mBottomAnchorDrawable.setBounds(
                selfBounds.left + leftCapWidth,
                selfBounds.top,
                selfBounds.left + leftCapWidth + mBottomAnchorDrawable.getIntrinsicWidth(),
                selfBounds.bottom);
         
        mRightCapDrawable.setBounds(
                selfBounds.left + leftCapWidth + mBottomAnchorDrawable.getIntrinsicWidth(),
                selfBounds.top,
                selfBounds.right,
                selfBounds.bottom);
        //@formatter:on 
    }