Example usage for android.graphics Canvas getHeight

List of usage examples for android.graphics Canvas getHeight

Introduction

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

Prototype

public int getHeight() 

Source Link

Document

Returns the height of the current drawing layer

Usage

From source file:piuk.blockchain.android.ui.zxing.ViewfinderView.java

@Override
public void onDraw(Canvas canvas) {
    if (cameraManager == null) {
        return; // not ready yet, early draw before done configuring
    }/* www  . j  a va  2  s.c  o m*/
    cameraManager.setFramingViewSize(new Point(this.getWidth(), this.getHeight()));
    Rect frame = cameraManager.getFramingRect();
    if (frame == null) {
        return;
    }
    int width = canvas.getWidth();
    int height = canvas.getHeight();

    // Draw the exterior (i.e. outside the framing rect) darkened
    paint.setColor(resultBitmap != null ? resultColor : maskColor);
    canvas.drawRect(0, 0, width, frame.top, paint);
    canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
    canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
    canvas.drawRect(0, frame.bottom + 1, width, height, paint);

    if (resultBitmap != null) {
        // Draw the opaque result bitmap over the scanning rectangle
        paint.setAlpha(CURRENT_POINT_OPACITY);
        canvas.drawBitmap(resultBitmap, null, frame, paint);
    } else {

        // Draw a red "laser scanner" line through the middle to show decoding is active
        paint.setColor(laserColor);
        paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
        scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
        int middle = frame.height() / 2 + frame.top;
        canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint);

        Rect previewFrame = cameraManager.getFramingRectInPreview();
        assert previewFrame != null;
        float scaleX = frame.width() / (float) previewFrame.width();
        float scaleY = frame.height() / (float) previewFrame.height();

        List<ResultPoint> currentPossible = possibleResultPoints;
        List<ResultPoint> currentLast = lastPossibleResultPoints;
        int frameLeft = frame.left;
        int frameTop = frame.top;
        if (currentPossible.isEmpty()) {
            lastPossibleResultPoints = null;
        } else {
            possibleResultPoints = new ArrayList<>(5);
            lastPossibleResultPoints = currentPossible;
            paint.setAlpha(CURRENT_POINT_OPACITY);
            paint.setColor(resultPointColor);
            synchronized (currentPossible) {
                for (ResultPoint point : currentPossible) {
                    canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
                            frameTop + (int) (point.getY() * scaleY), POINT_SIZE, paint);
                }
            }
        }
        if (currentLast != null) {
            paint.setAlpha(CURRENT_POINT_OPACITY / 2);
            paint.setColor(resultPointColor);
            synchronized (currentLast) {
                float radius = POINT_SIZE / 2.0f;
                for (ResultPoint point : currentLast) {
                    canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
                            frameTop + (int) (point.getY() * scaleY), radius, paint);
                }
            }
        }

        // Request another update at the animation interval, but only repaint the laser line,
        // not the entire viewfinder mask.
        postInvalidateDelayed(ANIMATION_DELAY, frame.left - POINT_SIZE, frame.top - POINT_SIZE,
                frame.right + POINT_SIZE, frame.bottom + POINT_SIZE);
    }
}

From source file:com.mwm.loyal.libs.zxing.ViewfinderView.java

@SuppressLint("DrawAllocation")
@Override/*from w  w  w  .  ja v  a2 s  . c om*/
public void onDraw(Canvas canvas) {
    if (cameraManager == null) {
        return; // not ready yet, early draw before done configuring
    }
    Rect frame = cameraManager.getFramingRect();
    Rect previewFrame = cameraManager.getFramingRectInPreview();
    if (frame == null || previewFrame == null) {
        return;
    }
    int width = canvas.getWidth();
    int height = canvas.getHeight();

    // Draw the exterior (i.e. outside the framing rect) darkened
    paint.setColor(resultBitmap != null ? resultColor : maskColor);
    canvas.drawRect(0, 0, width, frame.top, paint);
    canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
    canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
    canvas.drawRect(0, frame.bottom + 1, width, height, paint);

    if (resultBitmap != null) {
        // Draw the opaque result bitmap over the scanning rectangle
        paint.setAlpha(CURRENT_POINT_OPACITY);
        canvas.drawBitmap(resultBitmap, null, frame, paint);
    } else {

        // Draw a red "laser scanner" line through the middle to permissionDialog decoding is active
        paint.setColor(laserColor);
        paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
        scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
        int middle = frame.height() / 2 + frame.top;
        canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint);

        float scaleX = frame.width() / (float) previewFrame.width();
        float scaleY = frame.height() / (float) previewFrame.height();

        List<ResultPoint> currentPossible = possibleResultPoints;
        List<ResultPoint> currentLast = lastPossibleResultPoints;
        int frameLeft = frame.left;
        int frameTop = frame.top;
        if (currentPossible.isEmpty()) {
            lastPossibleResultPoints = null;
        } else {
            possibleResultPoints = new ArrayList<>(5);
            lastPossibleResultPoints = currentPossible;
            paint.setAlpha(CURRENT_POINT_OPACITY);
            paint.setColor(resultPointColor);
            synchronized (currentPossible) {
                for (ResultPoint point : currentPossible) {
                    canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
                            frameTop + (int) (point.getY() * scaleY), POINT_SIZE, paint);
                }
            }
        }
        if (currentLast != null) {
            paint.setAlpha(CURRENT_POINT_OPACITY / 2);
            paint.setColor(resultPointColor);
            synchronized (currentLast) {
                float radius = POINT_SIZE / 2.0f;
                for (ResultPoint point : currentLast) {
                    canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
                            frameTop + (int) (point.getY() * scaleY), radius, paint);
                }
            }
        }

        // Request another update at the animation interval, but only repaint the laser line,
        // not the entire viewfinder mask.
        postInvalidateDelayed(ANIMATION_DELAY, frame.left - POINT_SIZE, frame.top - POINT_SIZE,
                frame.right + POINT_SIZE, frame.bottom + POINT_SIZE);
    }
}

From source file:au.com.zacher.popularmovies.activity.layout.CollapsingTitleLayout.java

@Override
public void draw(@NonNull Canvas canvas) {
    final int saveCount = canvas.save();

    final int toolbarHeight = this.mToolbar.getHeight();
    canvas.clipRect(0, 0, canvas.getWidth(),
            interpolate(canvas.getHeight(), toolbarHeight, this.mScrollOffset));

    // Now call super and let it draw the background, etc
    super.draw(canvas);

    if (this.mTitleToDraw != null) {
        float x = this.mTextLeft;
        float y = this.mTextTop;

        final float ascent = this.mTextPaint.ascent() * this.mScale;
        final float descent = this.mTextPaint.descent() * this.mScale;
        final float h = descent - ascent;

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(this.mTextLeft, y - h + descent, this.mTextRight, y + descent, DEBUG_DRAW_PAINT);
        }/*from   w  ww. j a  v a2 s . c o m*/

        if (this.mUseTexture) {
            y = y - h + descent;
        }

        if (this.mScale != 1f) {
            canvas.scale(this.mScale, this.mScale, x, y);
        }

        if (this.mUseTexture && this.mExpandedTitleTexture != null) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(this.mExpandedTitleTexture, x, y, this.mTexturePaint);
        } else {
            canvas.drawText(this.mTitleToDraw, x, y, this.mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:org.cicadasong.samples.tubestatus.TubeStatus.java

protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTypeface(Typeface.DEFAULT_BOLD);

    // We've centered the output vertically, so it works with the reduced canvas height in
    // widget mode.
    int y = canvas.getHeight() / 2;
    int x = canvas.getWidth() / 2;

    paint.setTypeface(Typeface.DEFAULT);
    paint.setTextSize(11);/* ww  w .  j av  a2s. c  o m*/
    canvas.drawText(TubeLine.allLines.get(selectionIndex).name, x, y - paint.descent() - 1, paint);

    paint.setTextSize(11); // TODO dynamically adjust font size depending on length of status string?
    canvas.drawText(status, x, y + (int) -paint.ascent() + 1, paint);
}

From source file:me.test.zxing.ViewfinderView.java

@SuppressLint("DrawAllocation")
@Override/*ww w  .jav a2 s .  co m*/
public void onDraw(Canvas canvas) {
    if (cameraManager == null) {
        return; // not ready yet, early draw before done configuring
    }
    Rect frame = cameraManager.getFramingRect();
    Rect previewFrame = cameraManager.getFramingRectInPreview();
    if (frame == null || previewFrame == null) {
        return;
    }
    int width = canvas.getWidth();
    int height = canvas.getHeight();

    if (!isFirst) {
        isFirst = true;
        slideTop = frame.top;
    }

    //    // Draw the exterior (i.e. outside the framing rect) darkened
    paint.setColor(resultBitmap != null ? resultColor : maskColor);
    canvas.drawRect(0, 0, width, frame.top, paint);
    canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
    canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
    canvas.drawRect(0, frame.bottom + 1, width, height, paint);

    if (resultBitmap != null) {
        // Draw the opaque result bitmap over the scanning rectangle
        paint.setAlpha(CURRENT_POINT_OPACITY);
        canvas.drawBitmap(resultBitmap, null, frame, paint);
    } else {

        //
        paint.setColor(ContextCompat.getColor(getContext(), android.R.color.holo_green_dark));
        //
        canvas.drawRect(frame.left, frame.top, frame.left + 50, frame.top + 10, paint);
        canvas.drawRect(frame.left, frame.top, frame.left + 10, frame.top + 50, paint);
        //?
        canvas.drawRect(frame.right - 50, frame.top, frame.right, frame.top + 10, paint);
        canvas.drawRect(frame.right - 10, frame.top, frame.right, frame.top + 50, paint);
        //
        canvas.drawRect(frame.left, frame.bottom - 10, frame.left + 50, frame.bottom, paint);
        canvas.drawRect(frame.left, frame.bottom - 50, frame.left + 10, frame.bottom, paint);
        //?
        canvas.drawRect(frame.right - 50, frame.bottom - 10, frame.right, frame.bottom, paint);
        canvas.drawRect(frame.right - 10, frame.bottom - 50, frame.right, frame.bottom, paint);

        //,??SPEEN_DISTANCE
        slideTop += 5;
        if (slideTop >= frame.bottom) {
            slideTop = frame.top;
        }
        canvas.drawRect(frame.left + 5, slideTop - 6 / 2, frame.right - 5, slideTop + 6 / 2, paint);
        float scaleX = frame.width() / (float) previewFrame.width();
        float scaleY = frame.height() / (float) previewFrame.height();

        List<ResultPoint> currentPossible = possibleResultPoints;
        List<ResultPoint> currentLast = lastPossibleResultPoints;
        int frameLeft = frame.left;
        int frameTop = frame.top;
        if (currentPossible.isEmpty()) {
            lastPossibleResultPoints = null;
        } else {
            possibleResultPoints = new ArrayList<>(5);
            lastPossibleResultPoints = currentPossible;
            paint.setAlpha(CURRENT_POINT_OPACITY);
            paint.setColor(resultPointColor);
            synchronized (currentPossible) {
                for (ResultPoint point : currentPossible) {
                    canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
                            frameTop + (int) (point.getY() * scaleY), POINT_SIZE, paint);
                }
            }
        }
        if (currentLast != null) {
            paint.setAlpha(CURRENT_POINT_OPACITY / 2);
            paint.setColor(resultPointColor);
            synchronized (currentLast) {
                float radius = POINT_SIZE / 2.0f;
                for (ResultPoint point : currentLast) {
                    canvas.drawCircle(frameLeft + (int) (point.getX() * scaleX),
                            frameTop + (int) (point.getY() * scaleY), radius, paint);
                }
            }
        }

        // Request another update at the animation interval, but only repaint the laser line,
        // not the entire viewfinder mask.
        postInvalidateDelayed(ANIMATION_DELAY, frame.left - POINT_SIZE, frame.top - POINT_SIZE,
                frame.right + POINT_SIZE, frame.bottom + POINT_SIZE);
    }
}

From source file:github.madmarty.madsonic.util.Util.java

public static Bitmap createBitmapFromDrawable(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }// w w  w.  j  a v  a2  s.  c om

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

From source file:foam.starwisp.DrawableMap.java

public Marker AddText(final LatLng location, final String text, final int padding, final int fontSize,
        int colour) {
    Marker marker = null;//from www .j av  a 2 s . co m

    final TextView textView = new TextView(m_Context);
    textView.setText(text);
    textView.setTextSize(fontSize);
    textView.setTypeface(m_Context.m_Typeface);

    final Paint paintText = textView.getPaint();

    final Rect boundsText = new Rect();
    paintText.getTextBounds(text, 0, textView.length(), boundsText);
    paintText.setTextAlign(Paint.Align.CENTER);

    final Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    final Bitmap bmpText = Bitmap.createBitmap(boundsText.width() + 2 * padding,
            boundsText.height() + 2 * padding, conf);

    final Canvas canvasText = new Canvas(bmpText);
    paintText.setColor(Color.BLACK);

    canvasText.drawText(text, (canvasText.getWidth() / 2) + 3,
            (canvasText.getHeight() - padding - boundsText.bottom) + 3, paintText);

    paintText.setColor(colour);

    canvasText.drawText(text, canvasText.getWidth() / 2, canvasText.getHeight() - padding - boundsText.bottom,
            paintText);

    final MarkerOptions markerOptions = new MarkerOptions().position(location)
            .icon(BitmapDescriptorFactory.fromBitmap(bmpText)).anchor(0.5f, 1);

    marker = map.addMarker(markerOptions);

    return marker;
}

From source file:de.hs_bremen.aurora_hunter.ui.views.PredictionGraphView.java

public void onDraw(Canvas canvas) {
    if (mPoints.size() == 0) {
        return;/* w w  w .j  a  v a 2  s .c  o  m*/
    }
    int yOffsetForLabels = 0;

    if (mMax * mScaleFactor > 0.8) {
        yOffsetForLabels = 15;
    }
    Path path = new Path();
    int height = canvas.getHeight();
    int width = canvas.getWidth();

    for (Point point : mPoints) {
        point.x = point.percentageX * width + 60;
        // Log.i("mScaleFactor", " : " + mScaleFactor);
        //Log.i("percent", " : " + percent);

        point.y = (float) ((1 - point.percentageY * mScaleFactor) * height) + yOffsetForLabels * 3;
    }

    if (mPoints.size() > 1) {
        //calcuate x/y based on size of canvas
        for (int i = 0; i < mPoints.size(); i++) {
            if (i >= 0) {
                Point point = mPoints.get(i);
                //  Log.i("dx",point.x + " - " + point.y );
                if (i == 0) {
                    Point next = mPoints.get(i + 1);
                    point.dx = ((next.x - point.x) / 5);
                    point.dy = ((next.y - point.y) / 5);
                } else if (i == mPoints.size() - 1) {
                    Point prev = mPoints.get(i - 1);
                    point.dx = ((point.x - prev.x) / 5);
                    point.dy = ((point.y - prev.y) / 5);
                } else {
                    Point next = mPoints.get(i + 1);
                    Point prev = mPoints.get(i - 1);
                    point.dx = ((next.x - prev.x) / 5);
                    point.dy = ((next.y - prev.y) / 5);
                }
            }
        }
    }

    if (mPoints.size() > 0) {
        path.moveTo(0, (float) (canvas.getHeight() * 0.8));
        path.lineTo(0, mPoints.get(0).y);

    }
    boolean first = true;

    for (int i = 0; i < mPoints.size(); i++) {
        Point point = mPoints.get(i);
        if (first) {
            first = false;
            path.cubicTo(point.x - point.x * 2, point.y - point.y / 5, point.x - point.dx, point.y - point.dy,
                    point.x, point.y);
        } else {
            Point prev = mPoints.get(i - 1);
            //  Log.i("Draw", point.dx  + " " + point.dy);
            path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x - point.dx, point.y - point.dy, point.x,
                    point.y);
        }
    }

    if (mPoints.size() > 0) {
        path.lineTo(width, mPoints.get(mPoints.size() - 1).y);
    }
    path.lineTo(width, height);
    path.lineTo(0, height);

    canvas.drawPath(path, mGraphPaint);

    int detlaY = 30;

    for (Point p : mPoints) {

        int val = (int) Math.round(p.percentageY * 100);
        //if last element

        if (mPoints.indexOf(p) == mPoints.size() - 1) {
            //Log.i("last", p.toString());
            if (val == 0 || p.y > getHeight()) {
                canvas.drawText(val + "%", p.x - 150, getHeight() - detlaY, mTextPaint);
            } else {
                canvas.drawText(val + "%", p.x - 150, p.y - detlaY + yOffsetForLabels, mTextPaint);
            }

        } else {
            if (val == 0 || p.y > getHeight()) {
                canvas.drawText(val + "%", p.x - 20, getHeight() - detlaY, mTextPaint);
            } else {
                canvas.drawText(val + "%", p.x - 20, p.y - detlaY + yOffsetForLabels, mTextPaint);
            }

        }
        //Log.i("point", p.toString());
    }
    // Log.i("Lenght", mPoints.size() + " ");

    float levelStart = (float) (canvas.getHeight() - (mNotificationLevel * canvas.getHeight()));
    float lineHeight = DpToPixelUtil.convertDpToPixel(2, getContext());

    if (mViewMode == PredictionFragment.NOTIFICATION_MODE.SET_NOTIFICATION_LEVEL) {
        mNotificationLevelPaint.setAlpha(150);
        float leftOffset = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            float arcHeight = DpToPixelUtil.convertDpToPixel(30, getContext());
            leftOffset = arcHeight;
            RectF rectF = new RectF(getWidth() - arcHeight, levelStart - arcHeight / 2, getWidth(),
                    levelStart + arcHeight / 2);
            canvas.drawArc(rectF, 0, 360, true, mNotificationLevelPaint);

        }
        canvas.drawRect(0, levelStart, canvas.getWidth() - leftOffset, levelStart + lineHeight,
                mNotificationLevelPaint);

        mNotificationLevelPaint.setAlpha(20);
        canvas.drawRect(0, levelStart + lineHeight, canvas.getWidth(), canvas.getHeight(),
                mNotificationLevelPaint);
        String text = Math.round(mNotificationLevel * 100) + "%";
        canvas.drawText(text, DpToPixelUtil.convertDpToPixel(10, getContext()),
                levelStart + DpToPixelUtil.convertDpToPixel(25, getContext()), mNotificationLevelTextPaint);
    } else {
        mNotificationLevelPaint.setAlpha(30);
        canvas.drawRect(0, levelStart, canvas.getWidth(), levelStart + lineHeight, mNotificationLevelPaint);
    }

}

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  va  2  s. co  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);
    }
}