Example usage for android.graphics Canvas getWidth

List of usage examples for android.graphics Canvas getWidth

Introduction

In this page you can find the example usage for android.graphics Canvas getWidth.

Prototype

public int getWidth() 

Source Link

Document

Returns the width of the current drawing layer

Usage

From source file:org.telegram.ui.Cells.ProfileSearchCell.java

@Override
protected void onDraw(Canvas canvas) {
    if (user == null && chat == null && encryptedChat == null) {
        return;//from w  w  w  .ja va2s .c  o m
    }

    if (useSeparator) {
        if (LocaleController.isRTL) {
            canvas.drawLine(0, getMeasuredHeight() - 1,
                    getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline),
                    getMeasuredHeight() - 1, linePaint);
        } else {
            canvas.drawLine(AndroidUtilities.dp(AndroidUtilities.leftBaseline), getMeasuredHeight() - 1,
                    getMeasuredWidth(), getMeasuredHeight() - 1, linePaint);
        }
    }

    if (drawAlpha != 1) {
        canvas.saveLayerAlpha(0, 0, canvas.getWidth(), canvas.getHeight(), (int) (255 * drawAlpha),
                Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
    }

    if (drawNameLock) {
        setDrawableBounds(lockDrawable, nameLockLeft, nameLockTop);
        lockDrawable.draw(canvas);
    } else if (drawNameGroup) {
        setDrawableBounds(groupDrawable, nameLockLeft, nameLockTop);
        groupDrawable.draw(canvas);
    } else if (drawNameBroadcast) {
        setDrawableBounds(broadcastDrawable, nameLockLeft, nameLockTop);
        broadcastDrawable.draw(canvas);
    } else if (drawNameBot) {
        setDrawableBounds(botDrawable, nameLockLeft, nameLockTop);
        botDrawable.draw(canvas);
    }

    if (nameLayout != null) {
        canvas.save();
        canvas.translate(nameLeft, nameTop);
        nameLayout.draw(canvas);
        canvas.restore();
        if (drawCheck) {
            if (LocaleController.isRTL) {
                setDrawableBounds(checkDrawable,
                        nameLeft - AndroidUtilities.dp(4) - checkDrawable.getIntrinsicWidth(), nameLockTop);
            } else {
                setDrawableBounds(checkDrawable,
                        nameLeft + (int) nameLayout.getLineWidth(0) + AndroidUtilities.dp(4), nameLockTop);
            }
            checkDrawable.draw(canvas);
        }
    }

    if (onlineLayout != null) {
        canvas.save();
        canvas.translate(onlineLeft, AndroidUtilities.dp(40));
        onlineLayout.draw(canvas);
        canvas.restore();
    }

    if (countLayout != null) {
        if (MessagesController.getInstance().isDialogMuted(dialog_id)) {
            setDrawableBounds(countDrawableGrey, countLeft - AndroidUtilities.dp(5.5f), countTop,
                    countWidth + AndroidUtilities.dp(11), countDrawableGrey.getIntrinsicHeight());
            countDrawableGrey.draw(canvas);
        } else {
            setDrawableBounds(countDrawable, countLeft - AndroidUtilities.dp(5.5f), countTop,
                    countWidth + AndroidUtilities.dp(11), countDrawable.getIntrinsicHeight());
            countDrawable.draw(canvas);
        }
        canvas.save();
        canvas.translate(countLeft, countTop + AndroidUtilities.dp(4));
        countLayout.draw(canvas);
        canvas.restore();
    }

    avatarImage.draw(canvas);
}

From source file:org.jraf.android.piclabel.app.form.FormActivity.java

private void drawText(Canvas canvas) {
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setAntiAlias(true);//from  w  w w  .j  a v  a  2 s.c  o m
    paint.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/" + getSelectedFontName()));

    int textSize = canvas.getHeight() / 35;
    paint.setTextSize(textSize);
    int margin = textSize / 5;

    // Measure date/time
    String dateTime = mEdtDateTime.getText().toString();
    Rect boundsDateTime = new Rect();
    paint.getTextBounds(dateTime, 0, dateTime.length(), boundsDateTime);

    // Measure location
    String location = mEdtLocation.getText().toString();
    Rect boundsLocation = new Rect();
    paint.getTextBounds(location, 0, location.length(), boundsLocation);

    int totalWidth = boundsDateTime.width() + textSize * 2 + boundsLocation.width();
    if (totalWidth > canvas.getWidth()) {
        // Draw on 2 lines

        // Draw a rectangle
        paint.setColor(Color.argb(180, 0, 0, 0));
        canvas.drawRect(0, 0, canvas.getWidth(), -boundsDateTime.top + boundsDateTime.bottom
                + -boundsLocation.top + boundsLocation.bottom + margin * 3, paint);

        // Draw date/time
        paint.setColor(Color.WHITE);
        canvas.drawText(dateTime, margin, margin + -boundsDateTime.top, paint);

        // Draw location
        canvas.drawText(location, canvas.getWidth() - boundsLocation.right - boundsLocation.left - margin,
                margin + -boundsDateTime.top + boundsDateTime.bottom + margin + -boundsLocation.top, paint);

    } else {
        // Draw on 1 line

        // Draw a rectangle
        paint.setColor(Color.argb(180, 0, 0, 0));
        canvas.drawRect(0, 0, canvas.getWidth(),
                margin + Math.max(boundsDateTime.height(), boundsLocation.height()) + margin, paint);

        // Draw date/time
        paint.setColor(Color.WHITE);
        canvas.drawText(dateTime, margin, margin + -boundsDateTime.top, paint);

        // Draw location
        canvas.drawText(location, canvas.getWidth() - boundsLocation.right - boundsLocation.left - margin,
                margin + -boundsLocation.top, paint);
    }
}

From source file:com.mjhram.geodata.GpsMainActivity.java

public Bitmap drawMultilineTextToBitmap(Context gContext, Bitmap bitmap, String gText) {
    // prepare canvas
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    //Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }/*from  w  ww.  jav  a2 s.c o  m*/
    // resource bitmaps are imutable,
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);

    // new antialiased Paint
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(Color.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (20 * scale));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
    //canvas.drawText("This is", 100, 100, paint);
    //canvas.drawText("multi-line", 100, 150, paint);
    //canvas.drawText("text", 100, 200, paint);

    // set text width to canvas width minus 16dp padding
    int textWidth = canvas.getWidth() - (int) (16 * scale);

    // init StaticLayout for text
    StaticLayout textLayout = new StaticLayout(gText, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f,
            0.0f, false);

    // get height of multiline text
    int textHeight = textLayout.getHeight();

    // get position of text's top left corner
    float x = (bitmap.getWidth() - textWidth) / 2;
    float y = bitmap.getHeight() - textHeight;

    // draw text to the Canvas center
    canvas.save();
    canvas.translate(x, y);
    textLayout.draw(canvas);
    canvas.restore();

    return bitmap;
}

From source file:com.tbse.mywearapplication.WatchFaceDrawer.java

void onDraw(Context context, IWatchFaceConfig config, Canvas canvas, Rect bounds) {
    final Calendar calendar = config.getCalendar();
    final boolean isAmbient = config.isAmbient();
    final boolean isRound = config.isRound();
    final boolean useLightTheme = !isAmbient && config.isLightTheme();

    mBackgroundPaint.setColor(ContextCompat.getColor(context,
            useLightTheme ? R.color.watchface_background_light : R.color.watchface_background));

    /////////////////////////////////////////////////////////////////////
    // Draw your watch face here, using the provided canvas and bounds //
    /////////////////////////////////////////////////////////////////////

    final int width = bounds.width();
    final int height = bounds.height();

    // Find the center. Ignore the window insets so that, on round
    // watches with a "chin", the watch face is centered on the entire
    // screen, not just the usable portion.
    final float centerX = width / 2f;
    final float centerY = height / 2f;

    // Draw the background.
    if (mIsMobilePreview) {
        if (isRound) {
            canvas.drawCircle(centerX, centerY, centerX, mPreviewBorderPaint);
        } else {/*w  w w.j  a  v  a 2 s  .c om*/
            final float radius = mPreviewSquareRadius;
            final RectF rectF = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, mPreviewBorderPaint);
        }

        final float translateXY = width * 0.05f;
        canvas.translate(translateXY, translateXY);
        canvas.scale(0.9f, 0.9f);

        if (isRound) {
            canvas.drawCircle(centerX, centerY, centerX, mBackgroundPaint);
        } else {
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
        }
    } else {
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
    }

    // Draw weather icon
    if (image != null) {
        Matrix matrix = new Matrix();
        matrix.setTranslate(50, 90);
        matrix.preScale(0.2f, 0.2f);
        canvas.drawBitmap(image, matrix, null);
    }

    final float secRot = calendar.get(Calendar.SECOND) / 30f * (float) Math.PI;
    final int minutes = calendar.get(Calendar.MINUTE);
    final float minRot = minutes / 30f * (float) Math.PI;
    final float hrRot = ((calendar.get(Calendar.HOUR) + (minutes / 60f)) / 6f) * (float) Math.PI;

    final float secLength = centerX - mSecondOuterOffset;
    final float minLength = centerX - mMinuteOuterOffset;
    final float hrLength = centerX - mHourOuterOffset;

    if (!isAmbient) {
        final float secX = (float) Math.sin(secRot) * secLength;
        final float secY = (float) -Math.cos(secRot) * secLength;
        canvas.drawLine(centerX, centerY, centerX + secX, centerY + secY, mSecondHandPaint);
    }

    final float minX = (float) Math.sin(minRot) * minLength;
    final float minY = (float) -Math.cos(minRot) * minLength;
    canvas.drawLine(centerX, centerY, centerX + minX, centerY + minY, mMinuteHandPaint);

    final float hrX = (float) Math.sin(hrRot) * hrLength;
    final float hrY = (float) -Math.cos(hrRot) * hrLength;
    canvas.drawLine(centerX, centerY, centerX + hrX, centerY + hrY, mHourHandPaint);

    // Draw weather text
    canvas.drawText(day, 50, 50, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(low, 50, 65, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(high, 80, 65, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(weatherDescription, 50, 80, isAmbient ? ambientTextPaint : textPaint);

}

From source file:com.mylikes.likes.etchasketch.Slate.java

private void drawStrokeDebugInfo(Canvas c) {
    final int ROW_HEIGHT = 24;
    final int ROW_MARGIN = 6;
    final int COLUMN_WIDTH = 55;

    final float FIRM_PRESSURE_LOW = 0.85f;
    final float FIRM_PRESSURE_HIGH = 1.25f;

    if (mStrokeDebugGraph == null) {
        final int width = c.getWidth() - 128;
        final int height = ROW_HEIGHT * mStrokes.length + 2 * ROW_MARGIN;
        mStrokeDebugGraph = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        if (mStrokeDebugGraph == null) {
            throw new RuntimeException(
                    "drawStrokeDebugInfo: couldn't create debug bitmap (" + width + "x" + height + ")");
        }//from   w  w w. j av a2  s. c  o m
        mGraphPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    Canvas graph = new Canvas(mStrokeDebugGraph);
    graph.save();
    graph.clipRect(new Rect(0, 0, COLUMN_WIDTH, graph.getHeight()));
    graph.drawColor(0, PorterDuff.Mode.CLEAR);
    graph.restore();

    int left = 4;
    int bottom = graph.getHeight() - ROW_MARGIN;
    final int STEP = 4;
    for (MarkersPlotter st : mStrokes) {
        float r = st.getLastPressure();

        if (r >= FIRM_PRESSURE_LOW && r <= FIRM_PRESSURE_HIGH)
            mGraphPaint1.setColor(0xFF33FF33);
        else if (r < FIRM_PRESSURE_LOW)
            mGraphPaint1.setColor(0xFF808080);
        else
            mGraphPaint1.setColor(0xFFFF8000);

        String s = (r < 0) ? "--"
                : String.format("%s %.4f", ((st.getLastTool() == MotionEvent.TOOL_TYPE_STYLUS) ? "S" : "F"), r);

        graph.drawText(s, left, bottom - 2, mGraphPaint1);

        if (mGraphX + COLUMN_WIDTH > graph.getWidth()) {
            mGraphX = 0;
            graph.save();
            graph.clipRect(new Rect(30, 0, graph.getWidth(), graph.getHeight()));
            graph.drawColor(0, PorterDuff.Mode.CLEAR);
            graph.restore();
        }

        if (r >= 0) {
            int barsize = (int) (r * ROW_HEIGHT);
            graph.drawRect(mGraphX + COLUMN_WIDTH, bottom - barsize, mGraphX + COLUMN_WIDTH + STEP, bottom,
                    mGraphPaint1);
        } else {
            graph.drawPoint(mGraphX + COLUMN_WIDTH + STEP, bottom, mGraphPaint1);
        }
        bottom -= (ROW_HEIGHT + ROW_MARGIN);
    }

    mGraphX += STEP;

    final int x = 96;
    final int y = 64;

    c.drawBitmap(mStrokeDebugGraph, x, y, null);
    invalidate(new Rect(x, y, x + c.getWidth(), y + c.getHeight()));
}

From source file:com.dragon4.owo.ar_trace.ARCore.MixView.java

@Override
protected void onDraw(Canvas canvas) {
    try {// www  . j ava  2 s  .c  o m

        app.killOnError(); // ?  ?

        // ? ??  (? ???  ?)
        MixView.dWindow.setWidth(canvas.getWidth());
        MixView.dWindow.setHeight(canvas.getHeight());
        MixView.dWindow.setCanvas(canvas); //  

        // ??   ? ?   
        if (!MixView.getDataView().isInited()) {
            MixView.getDataView().init(MixView.dWindow.getWidth(), MixView.dWindow.getHeight());
        }

        // ?? ? ??? ?? 
        MixView.getDataView().draw(MixView.dWindow);

    } catch (Exception ex) {
        app.doError(ex);
    }
}

From source file:com.jjoe64.graphview.GridLabelRenderer.java

/**
 * draws the horizontal axis title if/*from   w w w . ja v a 2 s  .  com*/
 * it is set
 * @param canvas canvas
 */
protected void drawHorizontalAxisTitle(Canvas canvas) {
    if (mHorizontalAxisTitle != null && mHorizontalAxisTitle.length() > 0) {
        mPaintAxisTitle.setColor(getHorizontalAxisTitleColor());
        mPaintAxisTitle.setTextSize(getHorizontalAxisTitleTextSize());
        float x = canvas.getWidth() / 2;
        float y = canvas.getHeight() - mStyles.padding;
        canvas.drawText(mHorizontalAxisTitle, x, y, mPaintAxisTitle);
    }
}

From source file:in.sc9.discreteslider.DiscreteSlider.java

@Override
protected synchronized void onDraw(Canvas canvas) {

    int thumbWidth = mThumb.getIntrinsicWidth();
    int thumbHeight = mThumb.getIntrinsicHeight();
    int addedThumb = mAddedTouchBounds;
    int halfThumb = thumbWidth / 2;
    int paddingLeft = getPaddingLeft() + addedThumb;
    int paddingRight = getPaddingRight();
    int bottom = getHeight() - getPaddingBottom() - addedThumb;
    int trackHeight = Math.max(mTrackHeight / 2, 1);
    int paddingTop = getPaddingTop();
    float margin = (canvas.getWidth() - (paddingLeft + getPaddingRight())) / 4;
    float halfHeight = (halfThumb + (trackHeight * .5f) + mAddedTouchBounds + paddingTop);
    float h = paddingTop + mAddedTouchBounds + halfThumb;
    int scrubberHeight = Math.max(mScrubberHeight / 2, 2);

    if (!isLollipopOrGreater) {
        mRipple.draw(canvas);/*  ww w .  j a va  2  s  .c o  m*/
    }

    mTrack.draw(canvas);
    mScrubber.draw(canvas);

    //float interval = (((getWidth() - halfThumb - paddingRight) - (paddingLeft + halfThumb)) / (mMax - mMin));
    //float interval = ((getWidth() - halfThumb - paddingRight - addedThumb - paddingLeft - halfThumb) / (mMax - mMin));
    //float interval = ((getMeasuredWidth()-halfThumb-paddingLeft-paddingRight-addedThumb) / (mMax - mMin));
    float interval = (((float) mTrack.getBounds().right - (float) mTrack.getBounds().left)
            / (float) (mMax - mMin));
    for (int i = mMin; i <= mMax; i++) {
        /* position.set(paddingLeft+(i*margin)-halfSize,halfHeight-halfSize,
        paddingLeft+(i*margin)+halfSize, halfHeight+halfSize);  */

        position.set(mTrack.getBounds().left + ((i - mMin) * interval) - halfSize, h - halfSize,
                mTrack.getBounds().left + ((i - mMin) * interval) + halfSize, h + halfSize);

        if (mDiscretePointsShowAlways) {
            canvas.drawOval(position, mDiscretePoints);
        } else if (mDiscretePointsShowOnTouch && mDiscretePointsEnabled) {
            canvas.drawOval(position, mDiscretePoints);
        } else {
            canvas.drawOval(position, mDiscretePointsTran);
        }
    }

    mThumb.draw(canvas);
    super.onDraw(canvas);
    if (textStyle == NONE) {

    } else {
        drawTextArray(canvas, thumbWidth, halfThumb, mThumb.getBounds().bottom + mTextPaddingTop + mTextSize,
                mTrack.getBounds().left, interval);
    }
}

From source file:com.pdftron.pdf.tools.Tool.java

@Override
public boolean onDrawEdgeEffects(Canvas canvas, int width, int verticalOffset) {
    boolean needsInvalidate = false;

    if (!mEdgeEffectLeft.isFinished()) {
        canvas.save();//w  w  w  .  j av a2 s  .  com
        try {
            canvas.translate(0, canvas.getHeight() + verticalOffset);
            canvas.rotate(-90, 0, 0);
            mEdgeEffectLeft.setSize(canvas.getHeight(), canvas.getWidth());
            if (mEdgeEffectLeft.draw(canvas)) {
                needsInvalidate = true;
            }
        } finally {
            canvas.restore();
        }
    }

    if (!mEdgeEffectRight.isFinished()) {
        canvas.save();
        try {
            canvas.translate(width, verticalOffset);
            canvas.rotate(90, 0, 0);
            mEdgeEffectRight.setSize(canvas.getHeight(), canvas.getWidth());
            if (mEdgeEffectRight.draw(canvas)) {
                needsInvalidate = true;
            }
        } finally {
            canvas.restore();
        }
    }
    return needsInvalidate;
}

From source file:com.gigamole.library.ntb.NavigationTabBar.java

@SuppressLint("DrawAllocation")
@Override/*w  w  w .  java2s. c  om*/
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    // Get measure size
    final int width = MeasureSpec.getSize(widthMeasureSpec);
    final int height = MeasureSpec.getSize(heightMeasureSpec);

    if (mModels.isEmpty() || width == 0 || height == 0)
        return;

    // Detect orientation and calculate icon size
    if (width > height) {
        mIsHorizontalOrientation = true;

        // Get model size
        mModelSize = (float) width / (float) mModels.size();

        // Get smaller side
        float side = mModelSize > height ? height : mModelSize;
        if (mIsBadged)
            side -= side * TITLE_SIZE_FRACTION;

        mIconSize = side * (mIsTitled ? TITLE_ICON_SIZE_FRACTION : ICON_SIZE_FRACTION);
        mModelTitleSize = side * TITLE_SIZE_FRACTION;
        mTitleMargin = side * TITLE_MARGIN_FRACTION;

        // If is badged mode, so get vars and set paint with default bounds
        if (mIsBadged) {
            mBadgeTitleSize = mModelTitleSize * BADGE_TITLE_SIZE_FRACTION;

            final Rect badgeBounds = new Rect();
            mBadgePaint.setTextSize(mBadgeTitleSize);
            mBadgePaint.getTextBounds(PREVIEW_BADGE, 0, 1, badgeBounds);
            mBadgeMargin = (badgeBounds.height() * 0.5F)
                    + (mBadgeTitleSize * BADGE_HORIZONTAL_FRACTION * BADGE_VERTICAL_FRACTION);
        }
    } else {
        // Disable vertical translation in coordinator layout
        mBehaviorEnabled = false;
        // Disable other features
        mIsHorizontalOrientation = false;
        mIsTitled = false;
        mIsBadged = false;

        mModelSize = (float) height / (float) mModels.size();
        mIconSize = (int) ((mModelSize > width ? width : mModelSize) * ICON_SIZE_FRACTION);
    }

    // Set bounds for NTB
    mBounds.set(0.0F, 0.0F, width, height - mBadgeMargin);

    // Set main bitmap
    mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    mCanvas.setBitmap(mBitmap);

    // Set pointer canvas
    mPointerBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    mPointerCanvas.setBitmap(mPointerBitmap);

    // Set icons canvas
    mIconsBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    mIconsCanvas.setBitmap(mIconsBitmap);

    // Set titles canvas
    if (mIsTitled) {
        mTitlesBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        mTitlesCanvas.setBitmap(mTitlesBitmap);
    } else
        mTitlesBitmap = null;

    // Set scale fraction for icons
    for (Model model : mModels) {
        final float originalIconSize = model.mIcon.getWidth() > model.mIcon.getHeight() ? model.mIcon.getWidth()
                : model.mIcon.getHeight();
        model.mInactiveIconScale = mIconSize / originalIconSize;
        model.mActiveIconScaleBy = model.mInactiveIconScale
                * (mIsTitled ? TITLE_ACTIVE_ICON_SCALE_BY : ACTIVE_ICON_SCALE_BY);
    }

    // Set start position of pointer for preview or on start
    if (isInEditMode() || !mIsViewPagerMode) {
        mIsSetIndexFromTabBar = true;

        // Set random in preview mode
        if (isInEditMode()) {
            mIndex = new Random().nextInt(mModels.size());

            if (mIsBadged)
                for (int i = 0; i < mModels.size(); i++) {
                    final Model model = mModels.get(i);

                    if (i == mIndex) {
                        model.mBadgeFraction = MAX_FRACTION;
                        model.showBadge();
                    } else {
                        model.mBadgeFraction = MIN_FRACTION;
                        model.hideBadge();
                    }
                }
        }

        mStartPointerX = mIndex * mModelSize;
        mEndPointerX = mStartPointerX;
        updateIndicatorPosition(MAX_FRACTION);
    }

    //The translation behavior has to be set up after the super.onMeasure has been called
    if (!mIsBehaviorSet) {
        setBehaviorEnabled(mBehaviorEnabled);
        mIsBehaviorSet = true;
    }

    if (mBackground == null || mNeedInvalidateBackground) {
        if (getBackground() != null) {
            if (getBackground() instanceof BitmapDrawable)
                mBackground = ((BitmapDrawable) getBackground()).getBitmap();
            else {
                mBackground = Bitmap.createBitmap((int) mBounds.width(), (int) mBounds.height(),
                        Bitmap.Config.ARGB_8888);

                final Canvas backgroundCanvas = new Canvas(mBackground);
                getBackground().setBounds(0, 0, backgroundCanvas.getWidth(), backgroundCanvas.getHeight());
                getBackground().draw(backgroundCanvas);
            }

            setBackgroundDrawable(null);
            mNeedInvalidateBackground = false;
        }
    }
}