List of usage examples for android.graphics Canvas translate
public void translate(float dx, float dy)
From source file:ru.mahortov.listviewcustom.ListView.PinnedSectionListView.java
@Override protected void dispatchDraw(@NonNull Canvas canvas) { super.dispatchDraw(canvas); if (mPinnedSection != null) { // prepare variables int pLeft = getListPaddingLeft(); int pTop = getListPaddingTop(); View view = mPinnedSection.view; // draw child canvas.save();/*w ww. j ava2s .c om*/ int clipHeight = view.getHeight() + (mShadowDrawable == null ? 0 : Math.min(mShadowHeight, mSectionsDistanceY)); canvas.clipRect(pLeft, pTop, pLeft + view.getWidth(), pTop + clipHeight); canvas.translate(pLeft, pTop + mTranslateY); drawChild(canvas, mPinnedSection.view, getDrawingTime()); if (mShadowDrawable != null && mSectionsDistanceY > 0) { mShadowDrawable.setBounds(mPinnedSection.view.getLeft(), mPinnedSection.view.getBottom(), mPinnedSection.view.getRight(), mPinnedSection.view.getBottom() + mShadowHeight); mShadowDrawable.draw(canvas); } canvas.restore(); } }
From source file:org.chromium.chrome.browser.toolbar.ToolbarPhone.java
/** * Translates the canvas to ensure the specified view's coordinates are at 0, 0. * * @param from The view the canvas is currently translated to. * @param to The view to translate to./*from ww w.ja v a2s.c o m*/ * @param canvas The canvas to be translated. * * @throws IllegalArgumentException if {@code from} is not an ancestor of {@code to}. */ private static void translateCanvasToView(View from, View to, Canvas canvas) throws IllegalArgumentException { assert from != null; assert to != null; while (to != from) { canvas.translate(to.getLeft(), to.getTop()); if (!(to.getParent() instanceof View)) { throw new IllegalArgumentException("View 'to' was not a desendent of 'from'."); } to = (View) to.getParent(); } }
From source file:rosmi.acagild.alarmclock.ringing.ShareFragment.java
private static void drawStamp(Context context, Bitmap bitmap, String question) { Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(bitmap, 0, 0, null); float opacity = 0.7f; int horizontalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, context.getResources().getDisplayMetrics()); int verticalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, context.getResources().getDisplayMetrics()); int textSize = 16; // defined in SP int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, context.getResources().getDisplayMetrics()); LinearLayout layout = new LinearLayout(context); layout.setOrientation(LinearLayout.HORIZONTAL); layout.setBackgroundResource(R.drawable.rounded_corners); layout.getBackground().setAlpha((int) (opacity * 255)); layout.setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding); ImageView logo = new ImageView(context); logo.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.ic_launcher)); layout.addView(logo);/*from ww w. j a v a 2s . c o m*/ TextView textView = new TextView(context); textView.setVisibility(View.VISIBLE); if (question != null) { textView.setText(question); } else { textView.setText("Mimicker"); } textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize); textView.setPadding(horizontalPadding, 0, 0, 0); LinearLayout.LayoutParams centerInParent = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); centerInParent.gravity = Gravity.CENTER_VERTICAL; layout.addView(textView, centerInParent); layout.measure(canvas.getWidth(), height); layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight()); canvas.translate(horizontalPadding, (float) (canvas.getHeight() * 0.8 - height)); float scale = Math.min(1.0f, canvas.getWidth() / 1080f); canvas.scale(scale, scale); layout.draw(canvas); }
From source file:com.hippo.largeimageview.LargeImageView.java
private int transformCanvas(Canvas canvas) { final int saved; switch (mOrientation) { case ORIENTATION_0: saved = 0;// www . j av a 2 s . com break; case ORIENTATION_90: saved = canvas.save(Canvas.MATRIX_SAVE_FLAG); canvas.translate(getWidth(), 0); canvas.rotate(90); break; case ORIENTATION_180: saved = canvas.save(Canvas.MATRIX_SAVE_FLAG); canvas.translate(getWidth(), getHeight()); canvas.rotate(180); break; case ORIENTATION_270: saved = canvas.save(Canvas.MATRIX_SAVE_FLAG); canvas.translate(0, getHeight()); canvas.rotate(-90); break; default: throw new IllegalStateException("Unknown orientation: " + mOrientation); } return saved; }
From source file:com.appunite.scroll.ScaleImageView.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // Clips the next few drawing operations to the content area int clipRestoreCount = canvas.save(); canvas.clipRect(mContentRect);/*from w ww . j av a 2s.c om*/ if (mSrc != null) { getRealTranslation(mTranslation, mRealTranslation); canvas.translate(mRealTranslation.x, mRealTranslation.y); canvas.scale(mScale, mScale); Log.v(TAG, "scale=" + mScale + " translation=" + mTranslation.toString() + " realTranslation=" + mRealTranslation.toString()); mSrc.draw(canvas); } // Removes clipping rectangle canvas.restoreToCount(clipRestoreCount); drawEdgeEffectsUnclipped(canvas); }
From source file:com.microsoft.mimickeralarm.ringing.ShareFragment.java
private static void drawStamp(Context context, Bitmap bitmap, String question) { Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(bitmap, 0, 0, null); float opacity = 0.7f; int horizontalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, context.getResources().getDisplayMetrics()); int verticalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, context.getResources().getDisplayMetrics()); int textSize = 16; // defined in SP int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, context.getResources().getDisplayMetrics()); LinearLayout layout = new LinearLayout(context); layout.setOrientation(LinearLayout.HORIZONTAL); layout.setBackgroundResource(R.drawable.rounded_corners); layout.getBackground().setAlpha((int) (opacity * 255)); layout.setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding); ImageView logo = new ImageView(context); logo.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.ic_launcher_no_bg)); layout.addView(logo);// w ww . ja va 2 s. com TextView textView = new TextView(context); textView.setVisibility(View.VISIBLE); if (question != null) { textView.setText(question); } else { textView.setText("Mimicker"); } textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize); textView.setPadding(horizontalPadding, 0, 0, 0); LinearLayout.LayoutParams centerInParent = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); centerInParent.gravity = Gravity.CENTER_VERTICAL; layout.addView(textView, centerInParent); layout.measure(canvas.getWidth(), height); layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight()); canvas.translate(horizontalPadding, (float) (canvas.getHeight() * 0.8 - height)); float scale = Math.min(1.0f, canvas.getWidth() / 1080f); canvas.scale(scale, scale); layout.draw(canvas); }
From source file:android.widget.PinnedHeaderListView.java
private void drawHeader(final Canvas canvas, final PinnedHeader header, final long currentTime) { if (header.animating) { final int timeLeft = (int) (header.targetTime - currentTime); if (timeLeft <= 0) { header.y = header.targetY;/* www. j a v a2s. c o m*/ header.visible = header.targetVisible; header.animating = false; } else header.y = header.targetY + (header.sourceY - header.targetY) * timeLeft / mAnimationDuration; } if (header.visible) { final View view = header.view; final int saveCount = canvas.save(); final int translateX = ViewUtil.isViewLayoutRtl(this) ? getWidth() - mHeaderPaddingStart - view.getWidth() : mHeaderPaddingStart; canvas.translate(translateX, header.y); if (header.state == FADING) { mBounds.set(0, 0, view.getWidth(), view.getHeight()); canvas.saveLayerAlpha(mBounds, header.alpha, Canvas.ALL_SAVE_FLAG); } view.draw(canvas); canvas.restoreToCount(saveCount); } }
From source file:com.miuhouse.yourcompany.student.view.widget.date.datepicker.SimpleMonthView.java
@Override protected void onDraw(Canvas canvas) { Log.i("TAG", "onDraw"); if (Config.DEBUG) { Log.i(TAG, "onDraw(Canvas)"); }// w w w .j av a 2s . co m final int paddingLeft = getPaddingLeft(); final int paddingTop = getPaddingTop(); canvas.translate(paddingLeft, paddingTop); // drawMonth(canvas); drawDaysOfWeek(canvas); drawDays(canvas); canvas.translate(-paddingLeft, -paddingTop); }
From source file:com.example.view.wheel.WheelView.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (itemsLayout == null) { if (itemsWidth == 0) { calculateLayoutWidth(getWidth(), MeasureSpec.EXACTLY); } else {/* ww w . j a v a 2s .c o m*/ createLayouts(itemsWidth, labelWidth); } } if (itemsWidth > 0) { canvas.save(); // Skip padding space and hide a part of top and bottom items canvas.translate(PADDING, -ITEM_OFFSET); drawItems(canvas); drawValue(canvas); canvas.restore(); } drawCenterRect(canvas); // drawShadows(canvas); }
From source file:com.android.launcher3.BubbleTextView.java
@Override public void draw(Canvas canvas) { if (!mCustomShadowsEnabled) { super.draw(canvas); return;/*ww w. j ava2s . c o m*/ } final Drawable background = mBackground; if (background != null) { final int scrollX = getScrollX(); final int scrollY = getScrollY(); if (mBackgroundSizeChanged) { background.setBounds(0, 0, getRight() - getLeft(), getBottom() - getTop()); mBackgroundSizeChanged = false; } if ((scrollX | scrollY) == 0) { background.draw(canvas); } else { canvas.translate(scrollX, scrollY); background.draw(canvas); canvas.translate(-scrollX, -scrollY); } } // If text is transparent, don't draw any shadow if (getCurrentTextColor() == ContextCompat.getColor(getContext(), android.R.color.transparent)) { getPaint().clearShadowLayer(); super.draw(canvas); return; } // We enhance the shadow by drawing the shadow twice float density = getResources().getDisplayMetrics().density; getPaint().setShadowLayer(density * AMBIENT_SHADOW_RADIUS, 0, 0, AMBIENT_SHADOW_COLOR); super.draw(canvas); canvas.save(Canvas.CLIP_SAVE_FLAG); canvas.clipRect(getScrollX(), getScrollY() + getExtendedPaddingTop(), getScrollX() + getWidth(), getScrollY() + getHeight(), Region.Op.INTERSECT); getPaint().setShadowLayer(density * KEY_SHADOW_RADIUS, 0.0f, density * KEY_SHADOW_OFFSET, KEY_SHADOW_COLOR); super.draw(canvas); canvas.restore(); }