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

The following examples show how to use android.graphics.Canvas#translate() . 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: XulWorker.java    From starcor.xul with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static Bitmap toRoundCornerMutableBitmap(Canvas canvas, Paint paintClear, Bitmap srcBitmap, float[] roundRadius) {
	canvas.setBitmap(srcBitmap);
	RectF inset = new RectF(1, 1, 1, 1);
	if (roundRadius.length == 2) {
		float[] tmpRoundRadius = new float[8];
		for (int i = 0; i < 4; ++i) {
			tmpRoundRadius[i * 2 + 0] = roundRadius[0];
			tmpRoundRadius[i * 2 + 1] = roundRadius[1];
			roundRadius = tmpRoundRadius;
		}
	}
	canvas.save();
	canvas.translate(-1, -1);
	RoundRectShape roundRectShape = new RoundRectShape(null, inset, roundRadius);
	roundRectShape.resize(srcBitmap.getWidth() + 2, srcBitmap.getHeight() + 2);
	roundRectShape.draw(canvas, paintClear);
	canvas.restore();
	canvas.setBitmap(null);
	return srcBitmap;
}
 
Example 2
Source File: KeyboardView.java    From openboard with GNU General Public License v3.0 6 votes vote down vote up
private void onDrawKey(@Nonnull final Key key, @Nonnull final Canvas canvas,
        @Nonnull final Paint paint) {
    final int keyDrawX = key.getDrawX() + getPaddingLeft();
    final int keyDrawY = key.getY() + getPaddingTop();
    canvas.translate(keyDrawX, keyDrawY);

    final KeyVisualAttributes attr = key.getVisualAttributes();
    final KeyDrawParams params = mKeyDrawParams.mayCloneAndUpdateParams(key.getHeight(), attr);
    params.mAnimAlpha = Constants.Color.ALPHA_OPAQUE;

    if (!key.isSpacer()) {
        final Drawable background = key.selectBackgroundDrawable(
                mKeyBackground, mFunctionalKeyBackground, mSpacebarBackground);
        if (background != null) {
            onDrawKeyBackground(key, canvas, background);
        }
    }
    onDrawKeyTopVisuals(key, canvas, paint, params);

    canvas.translate(-keyDrawX, -keyDrawY);
}
 
Example 3
Source File: BallBeatIndicator.java    From LRecyclerView with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas, Paint paint) {
    float circleSpacing=4;
    float radius=(getWidth()-circleSpacing*2)/6;
    float x = getWidth()/ 2-(radius*2+circleSpacing);
    float y=getHeight() / 2;
    for (int i = 0; i < 3; i++) {
        canvas.save();
        float translateX=x+(radius*2)*i+circleSpacing*i;
        canvas.translate(translateX, y);
        canvas.scale(scaleFloats[i], scaleFloats[i]);
        paint.setAlpha(alphas[i]);
        canvas.drawCircle(0, 0, radius, paint);
        canvas.restore();
    }
}
 
Example 4
Source File: CharBox.java    From AndroidMathKeyboard with Apache License 2.0 6 votes vote down vote up
public void draw(Canvas g2, float x, float y) {
	drawDebug(g2, x, y);
	g2.save();
	g2.translate(x, y);
	Typeface font = FontInfo.getFont(cf.fontId);
	if (size != 1) {
		g2.scale(size, size);
	}
	Paint st = AjLatexMath.getPaint();
	st.setTextSize(TeXFormula.PIXELS_PER_POINT);
	st.setTypeface(font);
	st.setStyle(Style.FILL);
	st.setAntiAlias(true);
	st.setStrokeWidth(0);
	arr[0] = cf.c;
	g2.drawText(arr, 0, 1, 0, 0, st);
	g2.restore();
}
 
Example 5
Source File: ParallaxViewPager.java    From FamilyChat with Apache License 2.0 5 votes vote down vote up
private void drawLeftShadow(Canvas canvas)
{
    canvas.save();
    float translate = (getScrollX() / getWidth() + 1) * getWidth() - mShadowWidth;
    canvas.translate(translate, 0);
    mLeftShadow.setBounds(0, 0, mShadowWidth, getHeight());
    mLeftShadow.draw(canvas);
    canvas.restore();
}
 
Example 6
Source File: VerticalImageSpan.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    Drawable drawable = getDrawable();
    canvas.save();
    int transY = ((bottom - top) - drawable.getBounds().bottom) / 2 + top;
    canvas.translate(x, transY);
    drawable.draw(canvas);
    canvas.restore();
}
 
Example 7
Source File: SwitchButton.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
	super.onDraw(canvas);

	canvas.getClipBounds(mBounds);
	if (mBounds != null && mConf.needShrink()) {
		mBounds.inset(mConf.getInsetX(), mConf.getInsetY());
		canvas.clipRect(mBounds, Region.Op.REPLACE);
		canvas.translate(mConf.getInsetBounds().left, mConf.getInsetBounds().top);
	}

	boolean useGeneralDisableEffect = !isEnabled() && this.notStatableDrawable();
	if (useGeneralDisableEffect) {
		canvas.saveLayerAlpha(mSaveLayerZone, 255 / 2, Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
				| Canvas.CLIP_TO_LAYER_SAVE_FLAG);
	}

	mConf.getOffDrawable().draw(canvas);
	mConf.getOnDrawable().setAlpha(calcAlpha());
	mConf.getOnDrawable().draw(canvas);
	mConf.getThumbDrawable().draw(canvas);

	if (useGeneralDisableEffect) {
		canvas.restore();
	}

	if (SHOW_RECT) {
		mRectPaint.setColor(Color.parseColor("#AA0000"));
		canvas.drawRect(mBackZone, mRectPaint);
		mRectPaint.setColor(Color.parseColor("#00FF00"));
		canvas.drawRect(mSafeZone, mRectPaint);
		mRectPaint.setColor(Color.parseColor("#0000FF"));
		canvas.drawRect(mThumbZone, mRectPaint);
	}
}
 
Example 8
Source File: RoundRectDrawableWithShadow.java    From Slice with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    if (mDirty) {
        buildComponents(getBounds());
        mDirty = false;
    }
    canvas.translate(0, mRawShadowSize / 2);
    drawShadow(canvas);
    canvas.translate(0, -mRawShadowSize / 2);
    sRoundRectHelper.drawRoundRect(canvas, mCardBounds, mCornerRadius, mPaint);
}
 
Example 9
Source File: VerticalSeekBar.java    From Augendiagnose with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected final void onDraw(@NonNull final Canvas c) {
	c.rotate(ROTATION_ANGLE);
	c.translate(-getHeight(), 0);

	super.onDraw(c);
}
 
Example 10
Source File: WheelView.java    From CoolClock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws items
 * @param canvas the canvas for drawing
 */
private void drawItems(Canvas canvas) {
	canvas.save();
	
	int top = (currentItem - firstItem) * getItemHeight() + (getItemHeight() - getHeight()) / 2;
	canvas.translate(PADDING, - top + scrollingOffset);
	
	itemsLayout.draw(canvas);

	canvas.restore();
}
 
Example 11
Source File: LoadingView.java    From Common with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mWidth == 0 || mHeight == 0 || mTempRect == null) {
        return;
    }
    canvas.translate(mWidth / 2, mHeight / 2);
    mJ++;
    mJ %= mCount;
    int alpha;
    for (int i = 0; i < mCount; i++) {
        canvas.rotate(360f / mCount);
        alpha = (i - mJ + mCount) % mCount;
        alpha = (int) (((alpha) * (255f - mMinAlpha) / mCount + mMinAlpha));
        mPaint.setAlpha(alpha);
        switch (mType) {
            case TYPE_DAISY:
                /** Daisy rotation **/
                canvas.drawRoundRect(mTempRect, mRectWidth / 2, mRectWidth / 2, mPaint);
                break;
            case TYPE_DOT:
                /** Dot rotation **/
                canvas.drawCircle((mTempRect.left + mTempRect.right) / 2, (mTempRect.top + mTempRect.bottom) / 2, mRectWidth * 2 / 3, mPaint);
                break;
        }
    }
    if (mIsFirst) {
        mIsFirst = false;
        mHandler.postDelayed(mRunnable, mDuration / mCount);
    }
}
 
Example 12
Source File: VideoView.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    super.draw(canvas);

    if (mSubtitleWidget != null) {
        final int saveCount = canvas.save();
        canvas.translate(getPaddingLeft(), getPaddingTop());
        mSubtitleWidget.draw(canvas);
        canvas.restoreToCount(saveCount);
    }
}
 
Example 13
Source File: SettingsSearchCell.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
    Drawable drawable = getDrawable();
    canvas.save();
    Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
    int fontHeight = fmPaint.descent - fmPaint.ascent;
    int centerY = y + fmPaint.descent - fontHeight / 2;
    int transY = centerY - (drawable.getBounds().bottom - drawable.getBounds().top) / 2;
    canvas.translate(x, transY);
    if (LocaleController.isRTL) {
        canvas.scale(-1, 1, drawable.getIntrinsicWidth() / 2, drawable.getIntrinsicHeight() / 2);
    }
    drawable.draw(canvas);
    canvas.restore();
}
 
Example 14
Source File: SmoothProgressDrawable.java    From Klyph with MIT License 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    mBounds = getBounds();
    canvas.clipRect(mBounds);

    int boundsWidth = mBounds.width();

    if (mReversed) {
        canvas.translate(boundsWidth, 0);
        canvas.scale(-1, 1);
    }

    drawStrokes(canvas);
}
 
Example 15
Source File: CollapsingTitleLayout.java    From android-proguards with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    if (lineCount == 1) {
        collapsingText.draw(canvas);
    } else {
        float x = titleInsetStart;
        float y = Math.max(textTop - scrollOffset, titleInsetTop);
        canvas.translate(x, y);
        canvas.clipRect(0, 0,
                getWidth() - titleInsetStart - titleInsetEnd,
                Math.max(getHeight() - scrollOffset, collapsedHeight) - y);
        layout.draw(canvas);
    }
}
 
Example 16
Source File: BallClipRotatePulseIndicator.java    From LRecyclerView with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas, Paint paint) {
    float circleSpacing=12;
    float x=getWidth()/2;
    float y=getHeight()/2;

    //draw fill circle
    canvas.save();
    canvas.translate(x, y);
    canvas.scale(scaleFloat1, scaleFloat1);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(0, 0, x / 2.5f, paint);

    canvas.restore();

    canvas.translate(x, y);
    canvas.scale(scaleFloat2, scaleFloat2);
    canvas.rotate(degrees);

    paint.setStrokeWidth(3);
    paint.setStyle(Paint.Style.STROKE);

    //draw two arc
    float[] startAngles=new float[]{225,45};
    for (int i = 0; i < 2; i++) {
        RectF rectF=new RectF(-x+circleSpacing,-y+circleSpacing,x-circleSpacing,y-circleSpacing);
        canvas.drawArc(rectF, startAngles[i], 90, false, paint);
    }
}
 
Example 17
Source File: WebPlayerView.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    if (drawImage) {
        if (firstFrameRendered && currentAlpha != 0) {
            long newTime = System.currentTimeMillis();
            long dt = newTime - lastUpdateTime;
            lastUpdateTime = newTime;
            currentAlpha -= dt / 150.0f;
            if (currentAlpha < 0) {
                currentAlpha = 0.0f;
            }
            invalidate();
        }
        imageReceiver.setAlpha(currentAlpha);
        imageReceiver.draw(canvas);
    }
    if (videoPlayer.isPlayerPrepared() && !isStream) {
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        if (!isInline) {
            if (durationLayout != null) {
                canvas.save();
                canvas.translate(width - AndroidUtilities.dp(58) - durationWidth, height - AndroidUtilities.dp(29 + (inFullscreen ? 6 : 10)));
                durationLayout.draw(canvas);
                canvas.restore();
            }

            if (progressLayout != null) {
                canvas.save();
                canvas.translate(AndroidUtilities.dp(18), height - AndroidUtilities.dp(29 + (inFullscreen ? 6 : 10)));
                progressLayout.draw(canvas);
                canvas.restore();
            }
        }

        if (duration != 0) {
            int progressLineY;
            int progressLineX;
            int progressLineEndX;
            int cy;

            if (isInline) {
                progressLineY = height - AndroidUtilities.dp(3);
                progressLineX = 0;
                progressLineEndX = width;
                cy = height - AndroidUtilities.dp(7);
            } else if (inFullscreen) {
                progressLineY = height - AndroidUtilities.dp(26 + 3);
                progressLineX = AndroidUtilities.dp(18 + 18) + durationWidth;
                progressLineEndX = width - AndroidUtilities.dp(58 + 18) - durationWidth;
                cy = height - AndroidUtilities.dp(7 + 21);
            } else {
                progressLineY = height - AndroidUtilities.dp(10 + 3);
                progressLineX = 0;
                progressLineEndX = width;
                cy = height - AndroidUtilities.dp(2 + 10);
            }
            if (inFullscreen) {
                canvas.drawRect(progressLineX, progressLineY, progressLineEndX, progressLineY + AndroidUtilities.dp(3), progressInnerPaint);
            }
            int progressX;
            if (progressPressed) {
                progressX = currentProgressX;
            } else {
                progressX = progressLineX + (int) ((progressLineEndX - progressLineX) * (progress / (float) duration));
            }
            if (bufferedPosition != 0 && duration != 0) {
                canvas.drawRect(progressLineX, progressLineY, progressLineX + (progressLineEndX - progressLineX) * (bufferedPosition / (float) duration), progressLineY + AndroidUtilities.dp(3), inFullscreen ? progressBufferedPaint : progressInnerPaint);
            }
            canvas.drawRect(progressLineX, progressLineY, progressX, progressLineY + AndroidUtilities.dp(3), progressPaint);
            if (!isInline) {
                canvas.drawCircle(progressX, cy, AndroidUtilities.dp(progressPressed ? 7 : 5), progressPaint);
            }
        }
    }
}
 
Example 18
Source File: SmartTable.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
/**
 * 绘制
 * 首先通过计算的table大小,计算table title大小
 * 再通过 matrixHelper getZoomProviderRect计算实现缩放和位移的Rect
 * 再绘制背景
 * 绘制XY序号列
 * 最后绘制内容
 *
 * @param canvas
 */
@Override
protected void onDraw(Canvas canvas) {
    if (!isNotifying.get()) {
        setScrollY(0);
        showRect.set(getPaddingLeft(), getPaddingTop(),
                getWidth() - getPaddingRight(),
                getHeight() - getPaddingBottom());
        if (tableData != null) {
            //表格Rect
            Rect rect = tableData.getTableInfo().getTableRect();
            if (rect != null) {
                if (config.isShowTableTitle()) {
                    measurer.measureTableTitle(tableData, tableTitle, showRect);
                }
                tableRect.set(rect);
                Rect scaleRect = matrixHelper.getZoomProviderRect(showRect, tableRect,
                        tableData.getTableInfo());
                if (config.isShowTableTitle()) {
                    tableTitle.onMeasure(scaleRect, showRect, config);
                    tableTitle.onDraw(canvas, showRect, tableData.getTableName(), config);
                }
                if (config.isShowYSequence()) {
                    yAxis.onMeasure(scaleRect, showRect, config);
                    if (isYSequenceRight) {
                        canvas.save();
                        canvas.translate(showRect.width(), 0);
                        yAxis.onDraw(canvas, showRect, tableData, config);
                        canvas.restore();
                    } else {
                        yAxis.onDraw(canvas, showRect, tableData, config);
                    }
                }
                if (isYSequenceRight) {
                    canvas.save();
                    canvas.translate(-yAxis.getWidth(), 0);
                    provider.onDraw(canvas, scaleRect, showRect, tableData);
                    canvas.restore();
                } else {
                    provider.onDraw(canvas, scaleRect, showRect, tableData);
                }
            }
        }
    }
}
 
Example 19
Source File: MenuDrawable.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    if (currentRotation != finalRotation) {
        long newTime = SystemClock.elapsedRealtime();
        if (lastFrameTime != 0) {
            long dt = newTime - lastFrameTime;

            currentAnimationTime += dt;
            if (currentAnimationTime >= 200) {
                currentRotation = finalRotation;
            } else {
                if (currentRotation < finalRotation) {
                    currentRotation = interpolator.getInterpolation(currentAnimationTime / 200.0f) * finalRotation;
                } else {
                    currentRotation = 1.0f - interpolator.getInterpolation(currentAnimationTime / 200.0f);
                }
            }
        }
        lastFrameTime = newTime;
        invalidateSelf();
    }

    canvas.save();
    canvas.translate(getIntrinsicWidth() / 2, getIntrinsicHeight() / 2);
    float endYDiff;
    float endXDiff;
    float startYDiff;
    float startXDiff;
    int color1 = Theme.getColor(Theme.key_actionBarDefaultIcon);
    if (rotateToBack) {
        canvas.rotate(currentRotation * (reverseAngle ? -180 : 180));
        paint.setColor(color1);
        canvas.drawLine(-AndroidUtilities.dp(9), 0, AndroidUtilities.dp(9) - AndroidUtilities.dp(3.0f) * currentRotation, 0, paint);
        endYDiff = AndroidUtilities.dp(5) * (1 - Math.abs(currentRotation)) - AndroidUtilities.dp(0.5f) * Math.abs(currentRotation);
        endXDiff = AndroidUtilities.dp(9) - AndroidUtilities.dp(2.5f) * Math.abs(currentRotation);
        startYDiff = AndroidUtilities.dp(5) + AndroidUtilities.dp(2.0f) * Math.abs(currentRotation);
        startXDiff = -AndroidUtilities.dp(9) + AndroidUtilities.dp(7.5f) * Math.abs(currentRotation);
    } else {
        canvas.rotate(currentRotation * (reverseAngle ? -225 : 135));
        int color2 = Theme.getColor(Theme.key_actionBarActionModeDefaultIcon);
        paint.setColor(AndroidUtilities.getOffsetColor(color1, color2, currentRotation, 1.0f));
        canvas.drawLine(-AndroidUtilities.dp(9) + AndroidUtilities.dp(1) * currentRotation, 0, AndroidUtilities.dp(9) - AndroidUtilities.dp(1) * currentRotation, 0, paint);
        endYDiff = AndroidUtilities.dp(5) * (1 - Math.abs(currentRotation)) - AndroidUtilities.dp(0.5f) * Math.abs(currentRotation);
        endXDiff = AndroidUtilities.dp(9) - AndroidUtilities.dp(9) * Math.abs(currentRotation);
        startYDiff = AndroidUtilities.dp(5) + AndroidUtilities.dp(3.0f) * Math.abs(currentRotation);
        startXDiff = -AndroidUtilities.dp(9) + AndroidUtilities.dp(9) * Math.abs(currentRotation);
    }
    canvas.drawLine(startXDiff, -startYDiff, endXDiff, -endYDiff, paint);
    canvas.drawLine(startXDiff, startYDiff, endXDiff, endYDiff, paint);
    canvas.restore();
}
 
Example 20
Source File: Sensors.java    From codeexamples-android with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    synchronized (this) {
        if (mBitmap != null) {
            final Paint paint = mPaint;
            final Path path = mPath;
            final int outer = 0xFFC0C0C0;
            final int inner = 0xFFff7010;

            if (mLastX >= mMaxX) {
                mLastX = 0;
                final Canvas cavas = mCanvas;
                final float yoffset = mYOffset;
                final float maxx = mMaxX;
                final float oneG = SensorManager.STANDARD_GRAVITY * mScale[0];
                paint.setColor(0xFFAAAAAA);
                cavas.drawColor(0xFFFFFFFF);
                cavas.drawLine(0, yoffset,      maxx, yoffset,      paint);
                cavas.drawLine(0, yoffset+oneG, maxx, yoffset+oneG, paint);
                cavas.drawLine(0, yoffset-oneG, maxx, yoffset-oneG, paint);
            }
            canvas.drawBitmap(mBitmap, 0, 0, null);

            float[] values = mOrientationValues;
            if (mWidth < mHeight) {
                float w0 = mWidth * 0.333333f;
                float w  = w0 - 32;
                float x = w0*0.5f;
                for (int i=0 ; i<3 ; i++) {
                    canvas.save(Canvas.MATRIX_SAVE_FLAG);
                    canvas.translate(x, w*0.5f + 4.0f);
                    canvas.save(Canvas.MATRIX_SAVE_FLAG);
                    paint.setColor(outer);
                    canvas.scale(w, w);
                    canvas.drawOval(mRect, paint);
                    canvas.restore();
                    canvas.scale(w-5, w-5);
                    paint.setColor(inner);
                    canvas.rotate(-values[i]);
                    canvas.drawPath(path, paint);
                    canvas.restore();
                    x += w0;
                }
            } else {
                float h0 = mHeight * 0.333333f;
                float h  = h0 - 32;
                float y = h0*0.5f;
                for (int i=0 ; i<3 ; i++) {
                    canvas.save(Canvas.MATRIX_SAVE_FLAG);
                    canvas.translate(mWidth - (h*0.5f + 4.0f), y);
                    canvas.save(Canvas.MATRIX_SAVE_FLAG);
                    paint.setColor(outer);
                    canvas.scale(h, h);
                    canvas.drawOval(mRect, paint);
                    canvas.restore();
                    canvas.scale(h-5, h-5);
                    paint.setColor(inner);
                    canvas.rotate(-values[i]);
                    canvas.drawPath(path, paint);
                    canvas.restore();
                    y += h0;
                }
            }

        }
    }
}