Example usage for android.graphics Canvas drawRect

List of usage examples for android.graphics Canvas drawRect

Introduction

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

Prototype

public void drawRect(float left, float top, float right, float bottom, @NonNull Paint paint) 

Source Link

Document

Draw the specified Rect using the specified paint.

Usage

From source file:com.lizhi.library.widget.WechatTab.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (isInEditMode() || tabCount == 0) {
        return;// w w w .  j  a v  a 2  s .  co  m
    }

    final int height = getHeight();

    // draw underline
    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);

    // draw indicator line
    rectPaint.setColor(indicatorColor);

    // default: line below current tab
    View currentTab = tabsContainer.getChildAt(currentPosition);
    float lineLeft = currentTab.getLeft();
    float lineRight = currentTab.getRight();

    // if there is an offset, start interpolating left and right coordinates between current and next tab
    if (currentPositionOffset > 0f && currentPosition < tabCount - 1) {

        View nextTab = tabsContainer.getChildAt(currentPosition + 1);
        final float nextTabLeft = nextTab.getLeft();
        final float nextTabRight = nextTab.getRight();

        lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);
        lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);
    }

    //        TextView tv = ((TextView) currentTab);
    //        String s = tv.getText().toString();
    //        float widths[] = new float[s.length()];
    //        tv.getPaint().getTextWidths(s, widths);
    //        float StringWidth = 0;
    //        for (float item : widths) {
    //            StringWidth += item;
    //        }
    //        int padding = (int) ((tv.getWidth() - StringWidth) / 2 - mMyUnderlinePadding);
    //        if (padding < 0) {
    //            padding = 0;
    //        }

    if (!underLine) {
        canvas.drawRect(lineLeft, 0, lineRight, height, rectPaint);
    } else {
        canvas.drawRect(lineLeft + mMyUnderlinePadding, height - indicatorHeight,
                lineRight - mMyUnderlinePadding, height, rectPaint);
    }

    // draw divider

    dividerPaint.setColor(dividerColor);
    for (int i = 0; i < tabCount - 1; i++) {
        View tab = tabsContainer.getChildAt(i);
        canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint);
    }
}

From source file:com.abewy.android.apps.klyph.messenger.widget.BezelImageView.java

@Override
protected void onDraw(Canvas canvas) {
    if (mBounds == null) {
        return;/*from  w  ww  . ja va  2 s. c o m*/
    }

    int width = mBounds.width();
    int height = mBounds.height();

    if (width == 0 || height == 0) {
        return;
    }

    if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) {
        // Need to redraw the cache
        if (width == mCachedWidth && height == mCachedHeight) {
            // Have a correct-sized bitmap cache already allocated. Just erase it.
            mCacheBitmap.eraseColor(0);
        } else {
            // Allocate a new bitmap with the correct dimensions.
            mCacheBitmap.recycle();
            // noinspection AndroidLintDrawAllocation
            mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mCachedWidth = width;
            mCachedHeight = height;
        }

        Canvas cacheCanvas = new Canvas(mCacheBitmap);
        if (mMaskDrawable != null && mMaskEnabled == true) {
            int sc = cacheCanvas.save();
            mMaskDrawable.draw(cacheCanvas);
            mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed()) ? mDesaturateColorFilter : null);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else if (mDesaturateOnPress && isPressed()) {
            int sc = cacheCanvas.save();
            cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
            mMaskedPaint.setColorFilter(mDesaturateColorFilter);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint,
                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else {
            super.onDraw(cacheCanvas);
        }

        if (mBorderDrawable != null) {
            mBorderDrawable.draw(cacheCanvas);
        }
    }

    // Draw from cache
    canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);
}

From source file:com.merlin.view.recycler.RecyclerListDecoration.java

public void drawHorizontal(Canvas c, RecyclerView parent) {
    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        if (isHideDivider(parent, parent.getChildLayoutPosition(child), parent.getAdapter().getItemCount())) {
            continue;
        }/*  ww  w .j a v a  2 s  .  co  m*/
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
        final int top = child.getTop() - params.topMargin;
        final int bottom = child.getBottom() + params.bottomMargin;
        final int left = child.getRight() + params.rightMargin + Math.round(ViewCompat.getTranslationX(child));
        final int right = left + mHeight;

        if (mDivider == null) {
            c.drawRect(left, top, right, bottom, dividerPaint);
        } else {
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * /*  w ww. j  av a 2 s.  com*/
 * 
 * @param bitmap
 *            ?Bitmap
 * @return Bitmap
 */
public static Bitmap createReflectionBitmap(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:com.android.contacts.common.ContactPhotoManager.java

/**
 * If necessary, decodes bytes stored in the holder to Bitmap.  As long as the
 * bitmap is held either by {@link #mBitmapCache} or by a soft reference in
 * the holder, it will not be necessary to decode the bitmap.
 *///from w  w w. ja v a 2s. c om
private static void inflateBitmap(BitmapHolder holder, int requestedExtent) {
    final int sampleSize = BitmapUtil.findOptimalSampleSize(holder.originalSmallerExtent, requestedExtent);
    byte[] bytes = holder.bytes;
    if (bytes == null || bytes.length == 0) {
        return;
    }

    if (sampleSize == holder.decodedSampleSize) {
        // Check the soft reference.  If will be retained if the bitmap is also
        // in the LRU cache, so we don't need to check the LRU cache explicitly.
        if (holder.bitmapRef != null) {
            holder.bitmap = holder.bitmapRef.get();
            if (holder.bitmap != null) {
                return;
            }
        }
    }

    try {
        Bitmap bitmap = BitmapUtil.decodeBitmapFromBytes(bytes, sampleSize);

        // TODO: As a temporary workaround while framework support is being added to
        // clip non-square bitmaps into a perfect circle, manually crop the bitmap into
        // into a square if it will be displayed as a thumbnail so that it can be cropped
        // into a circle.
        final int height = bitmap.getHeight();
        final int width = bitmap.getWidth();

        // The smaller dimension of a scaled bitmap can range from anywhere from 0 to just
        // below twice the length of a thumbnail image due to the way we calculate the optimal
        // sample size.
        if (height != width && Math.min(height, width) <= mThumbnailSize * 2) {
            final int dimension = Math.min(height, width);
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension);
        }
        // make bitmap mutable and draw size onto it
        if (DEBUG_SIZES) {
            Bitmap original = bitmap;
            bitmap = bitmap.copy(bitmap.getConfig(), true);
            original.recycle();
            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint();
            paint.setTextSize(16);
            paint.setColor(Color.BLUE);
            paint.setStyle(Style.FILL);
            canvas.drawRect(0.0f, 0.0f, 50.0f, 20.0f, paint);
            paint.setColor(Color.WHITE);
            paint.setAntiAlias(true);
            canvas.drawText(bitmap.getWidth() + "/" + sampleSize, 0, 15, paint);
        }

        holder.decodedSampleSize = sampleSize;
        holder.bitmap = bitmap;
        holder.bitmapRef = new SoftReference<Bitmap>(bitmap);
        if (DEBUG) {
            Log.d(TAG, "inflateBitmap " + btk(bytes.length) + " -> " + bitmap.getWidth() + "x"
                    + bitmap.getHeight() + ", " + btk(bitmap.getByteCount()));
        }
    } catch (OutOfMemoryError e) {
        // Do nothing - the photo will appear to be missing
    }
}

From source file:com.little.pager.PagerSlidingTabStrip.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (isInEditMode() || tabCount == 0) {
        return;//from  w  ww  .  j  a v a2  s  .  c  om
    }

    final int height = getHeight();

    // 
    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);

    // 
    rectPaint.setColor(indicatorColor);

    // default: line below current tab
    View currentTab = tabsContainer.getChildAt(currentPosition);
    float lineLeft = currentTab.getLeft();
    float lineRight = currentTab.getRight();

    // if there is an offset, start interpolating left and right coordinates
    // between current and next tab
    if (currentPositionOffset > 0f && currentPosition < tabCount - 1) {

        View nextTab = tabsContainer.getChildAt(currentPosition + 1);
        final float nextTabLeft = nextTab.getLeft();
        final float nextTabRight = nextTab.getRight();

        lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);
        lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);
    }

    canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);

    // draw divider

    //
    if (hasDivier) {
        dividerPaint.setColor(dividerColor);
        for (int i = 0; i < tabCount - 1; i++) {
            View tab = tabsContainer.getChildAt(i);
            canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding,
                    dividerPaint);
        }
    }
}

From source file:com.merlin.view.recycler.RecyclerListDecoration.java

public void drawVertical(Canvas c, RecyclerView parent) {
    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        if (isHideDivider(parent, parent.getChildLayoutPosition(child), parent.getAdapter().getItemCount())) {
            continue;
        }/* w  w  w.  j  a  va 2  s.  c  om*/
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
        final int left = child.getLeft() - params.leftMargin;
        final int right = child.getRight() + params.rightMargin + mHeight;
        final int top = child.getBottom() + params.bottomMargin + Math.round(ViewCompat.getTranslationY(child));
        final int bottom = top + mHeight;

        if (mDivider == null) {
            c.drawRect(left, top, right, bottom, dividerPaint);
        } else {
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}

From source file:com.quran.labs.androidquran.widgets.IconPageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int count = mIconsLayout.getChildCount();
    ImageView v = (ImageView) mIconsLayout.getChildAt(mSelectedIndex);
    final int bottom = v.getHeight();
    final int top = bottom - mIndicatorHeight;
    int left = v.getLeft();
    int right = v.getRight();

    if (mSelectedIndex + 1 < count) {
        View nextIcon = mIconsLayout.getChildAt(mSelectedIndex + 1);
        left = (int) (mSelectionOffset * nextIcon.getLeft() + (1.0f - mSelectionOffset) * left);
        right = (int) (mSelectionOffset * nextIcon.getRight() + (1.0f - mSelectionOffset) * right);
    }//w w w .  j  av  a 2  s. c  om

    mIndicatorPaint.setColor(mIndicatorColor);
    canvas.drawRect(left, top, right, bottom, mIndicatorPaint);
}

From source file:com.crs4.roodin.moduletester.CustomView.java

/**
 * @return//from  w  w w.  j  a v a2 s .  co m
 */
private Picture drawBarreds() {
    Picture pictureContent = new Picture();
    Canvas canvasContent = pictureContent.beginRecording(getWidth(), getHeight());
    Paint paintContent = new Paint(Paint.ANTI_ALIAS_FLAG);
    canvasContent.save();

    paintContent.setColor(Color.BLUE);
    paintContent.setStyle(Style.STROKE);
    paintContent.setStrokeWidth(2);
    paintContent.setAlpha(50);

    try {
        cellDimension = block.getCellDimension();
        JSONArray barred = block.getBarred();

        int rows = block.getShape().getInt(0);
        int cols = block.getShape().getInt(1);

        // this code draws all the shape:
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {

                canvasContent.drawRect(cellDimension * j, cellDimension * i, cellDimension * j + cellDimension,
                        cellDimension * i + cellDimension, paintContent);
            }
        }
        paintContent.setColor(Color.RED);
        paintContent.setStyle(Style.FILL);
        paintContent.setAlpha(50);

        for (int i = 0; i < barred.length(); i++) {
            JSONArray pos = barred.getJSONArray(i);
            canvasContent.drawRect(cellDimension * pos.getInt(1), cellDimension * pos.getInt(0),
                    cellDimension * pos.getInt(1) + cellDimension,
                    cellDimension * pos.getInt(0) + cellDimension, paintContent);
        }

        this.paintBoxList(canvasContent, paintContent);

    } catch (JSONException e) {
        e.printStackTrace();
        Log.e("Exception in DrawComponents.drawBarreds", "" + e.getMessage());
    }

    canvasContent.restore();
    pictureContent.endRecording();
    return pictureContent;

}

From source file:com.hgdendi.contactslist.common.FloatingBarItemDecoration.java

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);
    final int position = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
    if (position == RecyclerView.NO_POSITION) {
        return;/*from   w w  w  . j a  va 2 s  .  c om*/
    }
    View child = parent.findViewHolderForAdapterPosition(position).itemView;
    String initial = getTag(position);
    if (initial == null) {
        return;
    }

    boolean flag = false;
    if (getTag(position + 1) != null && !initial.equals(getTag(position + 1))) {
        if (child.getHeight() + child.getTop() < mTitleHeight) {
            c.save();
            flag = true;
            c.translate(0, child.getHeight() + child.getTop() - mTitleHeight);
        }
    }

    c.drawRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(),
            parent.getPaddingTop() + mTitleHeight, mBackgroundPaint);
    c.drawText(initial, child.getPaddingLeft() + mTextStartMargin,
            parent.getPaddingTop() + mTitleHeight - (mTitleHeight - mTextHeight) / 2 - mTextBaselineOffset,
            mTextPaint);

    if (flag) {
        c.restore();
    }
}