Java Code Examples for android.graphics.Canvas#isHardwareAccelerated()

The following examples show how to use android.graphics.Canvas#isHardwareAccelerated() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeyboardView.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    if (canvas.isHardwareAccelerated()) {
        onDrawKeyboard(canvas);
        return;
    }

    final boolean bufferNeedsUpdates = mInvalidateAllKeys || !mInvalidatedKeys.isEmpty();
    if (bufferNeedsUpdates || mOffscreenBuffer == null) {
        if (maybeAllocateOffscreenBuffer()) {
            mInvalidateAllKeys = true;
            // TODO: Stop using the offscreen canvas even when in software rendering
            mOffscreenCanvas.setBitmap(mOffscreenBuffer);
        }
        onDrawKeyboard(mOffscreenCanvas);
    }
    canvas.drawBitmap(mOffscreenBuffer, 0.0f, 0.0f, null);
}
 
Example 2
Source File: KeyboardView.java    From LokiBoard-Android-Keylogger with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    if (canvas.isHardwareAccelerated()) {
        onDrawKeyboard(canvas);
        return;
    }

    final boolean bufferNeedsUpdates = mInvalidateAllKeys || !mInvalidatedKeys.isEmpty();
    if (bufferNeedsUpdates || mOffscreenBuffer == null) {
        if (maybeAllocateOffscreenBuffer()) {
            mInvalidateAllKeys = true;
            // TODO: Stop using the offscreen canvas even when in software rendering
            mOffscreenCanvas.setBitmap(mOffscreenBuffer);
        }
        onDrawKeyboard(mOffscreenCanvas);
    }
    canvas.drawBitmap(mOffscreenBuffer, 0.0f, 0.0f, null);
}
 
Example 3
Source File: TextureView.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Subclasses of TextureView cannot do their own rendering
 * with the {@link Canvas} object.
 *
 * @param canvas The Canvas to which the View is rendered.
 */
@Override
public final void draw(Canvas canvas) {
    // NOTE: Maintain this carefully (see View#draw)
    mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;

    /* Simplify drawing to guarantee the layer is the only thing drawn - so e.g. no background,
    scrolling, or fading edges. This guarantees all drawing is in the layer, so drawing
    properties (alpha, layer paint) affect all of the content of a TextureView. */

    if (canvas.isHardwareAccelerated()) {
        DisplayListCanvas displayListCanvas = (DisplayListCanvas) canvas;

        TextureLayer layer = getTextureLayer();
        if (layer != null) {
            applyUpdate();
            applyTransformMatrix();

            mLayer.setLayerPaint(mLayerPaint); // ensure layer paint is up to date
            displayListCanvas.drawTextureLayer(layer);
        }
    }
}
 
Example 4
Source File: NumberSpan.java    From html-textview with Apache License 2.0 6 votes vote down vote up
@Override
public void drawLeadingMargin(@NonNull Canvas c, @NonNull Paint p, int x, int dir,
                              int top, int baseline, int bottom, @NonNull CharSequence text,
                              int start, int end, boolean first, @Nullable Layout l) {
    if (((Spanned) text).getSpanStart(this) == start) {
        Paint.Style style = p.getStyle();

        p.setStyle(Paint.Style.FILL);

        if (c.isHardwareAccelerated()) {
            c.save();
            c.drawText(mNumber, x + dir, baseline, p);
            c.restore();
        } else {
            c.drawText(mNumber, x + dir, (top + bottom) / 2.0f, p);
        }

        p.setStyle(style);
    }
}
 
Example 5
Source File: ChartView.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 6 votes vote down vote up
/**
 * draw all the stuff on canvas
 *
 * @param canvas
 */
protected void drawGraphElements(Canvas canvas) {
    if (android.os.Build.VERSION.SDK_INT >= 11 && !canvas.isHardwareAccelerated()) {
        Log.d("ChartView","use android:hardwareAccelerated=\"true\" for better performance");
    }

    try {
        drawTitle(canvas);
        mViewport.drawFirst(canvas);
        mGridLabelRenderer.draw(canvas);
        for (Series s : mSeries) {
            s.draw(this, canvas);
        }
        mViewport.draw(canvas);
        mLegendRenderer.draw(canvas);
    }catch (Exception e) {
        Log.d("ChartView", e.getMessage());
    }
}
 
Example 6
Source File: KeyboardView.java    From simple-keyboard with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    if (canvas.isHardwareAccelerated()) {
        onDrawKeyboard(canvas);
        return;
    }

    final boolean bufferNeedsUpdates = mInvalidateAllKeys || !mInvalidatedKeys.isEmpty();
    if (bufferNeedsUpdates || mOffscreenBuffer == null) {
        if (maybeAllocateOffscreenBuffer()) {
            mInvalidateAllKeys = true;
            // TODO: Stop using the offscreen canvas even when in software rendering
            mOffscreenCanvas.setBitmap(mOffscreenBuffer);
        }
        onDrawKeyboard(mOffscreenCanvas);
    }
    canvas.drawBitmap(mOffscreenBuffer, 0.0f, 0.0f, null);
}
 
Example 7
Source File: WXViewUtils.java    From weex-uikit with MIT License 6 votes vote down vote up
public static void clipCanvasWithinBorderBox(View targetView, Canvas canvas) {
  Drawable drawable;
  /* According to https://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported
    API 18 or higher supports clipPath to canvas based on hardware acceleration.
   */
  if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ||
       !canvas.isHardwareAccelerated()) &&
      ((drawable = targetView.getBackground()) instanceof BorderDrawable)) {
    BorderDrawable borderDrawable = (BorderDrawable) drawable;
    if(borderDrawable.isRounded()) {
      Path path = borderDrawable.getContentPath(
          new RectF(0, 0, targetView.getWidth(), targetView.getHeight()));
      canvas.clipPath(path);
    }
  }
}
 
Example 8
Source File: KeyboardView.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    if (canvas.isHardwareAccelerated()) {
        onDrawKeyboard(canvas);
        return;
    }

    final boolean bufferNeedsUpdates = mInvalidateAllKeys || !mInvalidatedKeys.isEmpty();
    if (bufferNeedsUpdates || mOffscreenBuffer == null) {
        if (maybeAllocateOffscreenBuffer()) {
            mInvalidateAllKeys = true;
            // TODO: Stop using the offscreen canvas even when in software rendering
            mOffscreenCanvas.setBitmap(mOffscreenBuffer);
        }
        onDrawKeyboard(mOffscreenCanvas);
    }
    canvas.drawBitmap(mOffscreenBuffer, 0.0f, 0.0f, null);
}
 
Example 9
Source File: ContentViewRenderView.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * This method could be subclassed optionally to provide a custom SurfaceView object to
 * this ContentViewRenderView.
 * @param context The context used to create the SurfaceView object.
 * @return The created SurfaceView object.
 */
protected SurfaceView createSurfaceView(Context context) {
    return new SurfaceView(context) {
        @Override
        public void onDraw(Canvas canvas) {
            // We only need to draw to software canvases, which are used for taking screenshots.
            if (canvas.isHardwareAccelerated()) return;
            Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
                    Bitmap.Config.ARGB_8888);
            if (nativeCompositeToBitmap(mNativeContentViewRenderView, bitmap)) {
                canvas.drawBitmap(bitmap, 0, 0, null);
            }
        }
    };
}
 
Example 10
Source File: ContentViewRenderView.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * This method could be subclassed optionally to provide a custom SurfaceView object to
 * this ContentViewRenderView.
 * @param context The context used to create the SurfaceView object.
 * @return The created SurfaceView object.
 */
protected SurfaceView createSurfaceView(Context context) {
    return new SurfaceView(context) {
        @Override
        public void onDraw(Canvas canvas) {
            // We only need to draw to software canvases, which are used for taking screenshots.
            if (canvas.isHardwareAccelerated()) return;
            Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
                    Bitmap.Config.ARGB_8888);
            if (nativeCompositeToBitmap(mNativeContentViewRenderView, bitmap)) {
                canvas.drawBitmap(bitmap, 0, 0, null);
            }
        }
    };
}
 
Example 11
Source File: HighlightView.java    From HaiNaBaiChuan with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private boolean isClipPathSupported(Canvas canvas) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return false;
    } else if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        || Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        return true;
    } else {
        return !canvas.isHardwareAccelerated();
    }
}
 
Example 12
Source File: HighlightView.java    From CloudPan with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private boolean isClipPathSupported(Canvas canvas) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return false;
    } else if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        || Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        return true;
    } else {
        return !canvas.isHardwareAccelerated();
    }
}
 
Example 13
Source File: MarkDownBulletSpan.java    From Markdown with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout l) {
    if (((Spanned) text).getSpanStart(this) != start) {
        return;
    }

    int oldColor = p.getColor();
    p.setColor(mBulletColor);

    if (mIndex != null) {
        c.drawText(mIndex, x + mMargin - GAP_WIDTH - 2 * BULLET_RADIUS, baseline, p);
    } else {
        float dy = (p.getFontMetricsInt().descent - p.getFontMetricsInt().ascent) * 0.5f + top;

        Paint.Style oldStyle = p.getStyle();
        p.setStyle(mLevel == 1 ? Paint.Style.STROKE : Paint.Style.FILL);

        if (!c.isHardwareAccelerated()) {
            Path path = mLevel >= 2 ? RECT_BULLET_PATH : CIRCLE_BULLET_PATH;

            c.save();
            c.translate(x + mMargin - GAP_WIDTH, dy);
            c.drawPath(path, p);
            c.restore();
        } else {
            c.drawCircle(x + mMargin - GAP_WIDTH, dy, BULLET_RADIUS, p);
        }

        p.setStyle(oldStyle);
    }

    p.setColor(oldColor);
}
 
Example 14
Source File: HighlightView.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private boolean isClipPathSupported(Canvas canvas) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return false;
    } else if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        || Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        return true;
    } else {
        return !canvas.isHardwareAccelerated();
    }
}
 
Example 15
Source File: ImprovedBulletSpan.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
public void drawLeadingMargin(Canvas canvas, Paint paint, int x, int dir,
                              int top, int baseline, int bottom, CharSequence text,
                              int start, int end, boolean first, Layout layout) {

    if (((Spanned)text).getSpanStart(this) == start) {
        Style style = paint.getStyle();
        paint.setStyle(Style.FILL);

        float yPosition;
        if (layout != null) {
            int line = layout.getLineForOffset(start);
            yPosition = (float)layout.getLineBaseline(line) - (float)this.bulletRadius * 2.0F;
        } else {
            yPosition = (float)(top + bottom) / 2.0F;
        }

        float xPosition = (float)(x + dir * this.bulletRadius);

        if (canvas.isHardwareAccelerated()) {
            if (this.mBulletPath == null) {
                this.mBulletPath = new Path();
                this.mBulletPath.addCircle(0.0F, 0.0F, (float)this.bulletRadius, Direction.CW);
            }

            canvas.save();
            canvas.translate(xPosition, yPosition);
            canvas.drawPath(this.mBulletPath, paint);
            canvas.restore();
        } else {
            canvas.drawCircle(xPosition, yPosition, (float)this.bulletRadius, paint);
        }

        paint.setStyle(style);
    }

}
 
Example 16
Source File: CropView.java    From ImagePicker with Apache License 2.0 5 votes vote down vote up
private boolean isClipPathSupported(Canvas canvas)
{
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1)
    {
        return false;
    } else if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
            || Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
    {
        return true;
    } else
    {
        return !canvas.isHardwareAccelerated();
    }
}
 
Example 17
Source File: HighlightView.java    From LockDemo with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private boolean isClipPathSupported(Canvas canvas) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return false;
    } else if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        || Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        return true;
    } else {
        return !canvas.isHardwareAccelerated();
    }
}
 
Example 18
Source File: VirtualViewUtils.java    From Virtualview-Android with MIT License 4 votes vote down vote up
public static void clipCanvas(View view, Canvas canvas, int width, int height, int borderWidth,
    int borderTopLeftRadius, int borderTopRightRadius, int borderBottomLeftRadius, int borderBottomRightRadius) {
    if (canvas == null) {
        return;
    }

    if (!enableBorderRadius) {
        borderTopLeftRadius = 0;
        borderTopRightRadius = 0;
        borderBottomLeftRadius = 0;
        borderBottomRightRadius = 0;
    }

    if (!isRounded(borderTopLeftRadius, borderTopRightRadius, borderBottomLeftRadius, borderBottomRightRadius)) {
        return;
    }
    sPath.reset();
    //start point
    sPath.moveTo((borderTopLeftRadius > 0 ? borderTopLeftRadius : 0), 0);
    //line top edge
    sPath.lineTo(width - (borderTopRightRadius > 0 ? borderTopRightRadius : 0), 0);
    //arc to right edge
    if (borderTopRightRadius > 0) {
        oval.set(width - 2 * borderTopRightRadius, 0, width, 2 * borderTopRightRadius);
        sPath.arcTo(oval, 270, 90);
    }
    //line right edge
    sPath.lineTo(width, height - (borderBottomRightRadius > 0 ? borderBottomRightRadius : 0));
    //arc to bottom edge
    if (borderBottomRightRadius > 0) {
        oval.set(width - 2 * borderBottomRightRadius, height - 2 * borderBottomRightRadius, width, height);
        sPath.arcTo(oval, 0, 90);
    }
    //line bottom edge
    sPath.lineTo((borderBottomLeftRadius > 0 ? borderBottomLeftRadius : 0), height);
    //arc to left edge
    if (borderBottomLeftRadius > 0) {
        oval.set(0, height - 2 * borderBottomLeftRadius, 2 * borderBottomLeftRadius, height);
        sPath.arcTo(oval, 90, 90);
    }
    //line left edge
    sPath.lineTo(0, (borderTopLeftRadius > 0 ? borderTopLeftRadius : 0));
    //arc to top edge
    if (borderTopLeftRadius > 0) {
        oval.set(0, 0, 2 * borderTopLeftRadius, 2 * borderTopLeftRadius);
        sPath.arcTo(oval, 180, 90);
    }
    if (canvas.isHardwareAccelerated() && (Build.VERSION.SDK_INT < 18) && view != null) {
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    canvas.clipPath(sPath);
}
 
Example 19
Source File: FolderIcon.java    From LaunchEnr with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);

    if (mReferenceDrawable != null) {
        computePreviewDrawingParams(mReferenceDrawable);
    }

    if (!mBackground.drawingDelegated()) {
        mBackground.drawBackground(mContext, canvas);
    }

    if (mFolder == null) return;
    if (mFolder.getItemCount() == 0 && !mAnimating) return;

    final int saveCount;

    if (canvas.isHardwareAccelerated()) {
        saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null,
                Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
    } else {
        saveCount = canvas.save(Canvas.CLIP_SAVE_FLAG);
        if (mPreviewLayoutRule.clipToBackground()) {
            mBackground.clipCanvasSoftware(canvas, Region.Op.INTERSECT);
        }
    }

    // The items are drawn in coordinates relative to the preview offset
    canvas.translate(mBackground.basePreviewOffsetX, mBackground.basePreviewOffsetY);

    // The first item should be drawn last (ie. on top of later items)
    for (int i = mDrawingParams.size() - 1; i >= 0; i--) {
        PreviewItemDrawingParams p = mDrawingParams.get(i);
        if (!p.hidden) {
            drawPreviewItem(canvas, p);
        }
    }
    canvas.translate(-mBackground.basePreviewOffsetX, -mBackground.basePreviewOffsetY);

    if (mPreviewLayoutRule.clipToBackground() && canvas.isHardwareAccelerated()) {
        mBackground.clipCanvasHardware(canvas);
    }
    canvas.restoreToCount(saveCount);

    if (mPreviewLayoutRule.clipToBackground() && !mBackground.drawingDelegated()) {
        mBackground.drawBackgroundStroke(mContext, canvas);
    }

    if ((mBadgeInfo != null && mBadgeInfo.hasBadge()) || mBadgeScale > 0) {
        int offsetX = mBackground.getOffsetX();
        int offsetY = mBackground.getOffsetY();
        int previewSize = (int) (mBackground.previewSize * mBackground.mScale);
        mTempBounds.set(offsetX, offsetY, offsetX + previewSize, offsetY + previewSize);

        // If we are animating to the accepting state, animate the badge out.
        float badgeScale = Math.max(0, mBadgeScale - mBackground.getScaleProgress());
        mTempSpaceForBadgeOffset.set(getWidth() - mTempBounds.right, mTempBounds.top);
        IconPalette badgePalette = IconPalette.getFolderBadgePalette(getResources());
        mBadgeRenderer.draw(canvas, badgePalette, mBadgeInfo, mTempBounds,
                badgeScale, mTempSpaceForBadgeOffset);
    }
}
 
Example 20
Source File: SinglePointRenderer.java    From mil-sym-android with Apache License 2.0 4 votes vote down vote up
public Bitmap getTestSymbol()
 {
     Bitmap temp = null;
     try
     {
         temp = Bitmap.createBitmap(70, 70, Config.ARGB_8888);

         Canvas canvas = new Canvas(temp);

         if (canvas.isHardwareAccelerated())
         {
             System.out.println("HW acceleration supported");
         }
//canvas.drawColor(Color.WHITE);

         //Typeface tf = Typeface.createFromAsset(_am, "fonts/unitfonts.ttf");
         Typeface tf = _tfUnits;

         Paint fillPaint = new Paint();
         fillPaint.setStyle(Paint.Style.FILL);
         fillPaint.setColor(Color.CYAN.toInt());
         fillPaint.setTextSize(50);
         fillPaint.setAntiAlias(true);
         fillPaint.setTextAlign(Align.CENTER);
         fillPaint.setTypeface(tf);

         Paint framePaint = new Paint();
         framePaint.setStyle(Paint.Style.FILL);
         framePaint.setColor(Color.BLACK.toInt());
         framePaint.setTextSize(50);
         framePaint.setAntiAlias(true);
         framePaint.setTextAlign(Align.CENTER);
         framePaint.setTypeface(tf);

         Paint symbolPaint = new Paint();
         symbolPaint.setStyle(Paint.Style.FILL);
         symbolPaint.setColor(Color.BLACK.toInt());
         symbolPaint.setTextSize(50);
         symbolPaint.setAntiAlias(true);
         symbolPaint.setTextAlign(Align.CENTER);
         symbolPaint.setTypeface(tf);

         String strFill = String.valueOf((char) 800);
         String strFrame = String.valueOf((char) 801);
         String strSymbol = String.valueOf((char) 1121);

         canvas.drawText(strFill, 35, 35, fillPaint);
         canvas.drawText(strFrame, 35, 35, framePaint);
         canvas.drawText(strSymbol, 35, 35, symbolPaint);

         FontMetrics mf = framePaint.getFontMetrics();
         float height = mf.bottom - mf.top;
         float width = fillPaint.measureText(strFrame);

         Log.i(TAG, "top: " + String.valueOf(mf.top));
         Log.i(TAG, "bottom: " + String.valueOf(mf.bottom));
         Log.i(TAG, "ascent: " + String.valueOf(mf.ascent));
         Log.i(TAG, "descent: " + String.valueOf(mf.descent));
         Log.i(TAG, "leading: " + String.valueOf(mf.leading));
         Log.i(TAG, "width: " + String.valueOf(width));
         Log.i(TAG, "height: " + String.valueOf(height));

     }
     catch (Exception exc)
     {
         Log.e(TAG, exc.getMessage());
         Log.e(TAG, getStackTrace(exc));
     }

     return temp;
 }