Example usage for android.graphics Canvas scale

List of usage examples for android.graphics Canvas scale

Introduction

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

Prototype

public void scale(float sx, float sy) 

Source Link

Document

Preconcat the current matrix with the specified scale.

Usage

From source file:com.coinomi.wallet.ui.ScanActivity.java

public void handleResult(final Result scanResult, final Bitmap thumbnailImage,
        final float thumbnailScaleFactor) {
    vibrator.vibrate(VIBRATE_DURATION);//  w  w w. j a va  2s.  c o  m

    // superimpose dots to highlight the key features of the qr code
    final ResultPoint[] points = scanResult.getResultPoints();
    if (points != null && points.length > 0) {
        final Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.scan_result_dots));
        paint.setStrokeWidth(10.0f);

        final Canvas canvas = new Canvas(thumbnailImage);
        canvas.scale(thumbnailScaleFactor, thumbnailScaleFactor);
        for (final ResultPoint point : points)
            canvas.drawPoint(point.getX(), point.getY(), paint);
    }

    scannerView.drawResultBitmap(thumbnailImage);

    final Intent result = new Intent();
    result.putExtra(INTENT_EXTRA_RESULT, scanResult.getText());
    setResult(RESULT_OK, result);

    // delayed finish
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            finish();
        }
    });
}

From source file:com.fillerino.wallet.ui.ScanActivity.java

public void handleResult(final Result scanResult, final Bitmap thumbnailImage,
        final float thumbnailScaleFactor) {
    vibrator.vibrate(VIBRATE_DURATION);/*from w w w.  j  a v a2s  . c  om*/

    // superimpose dots to highlight the key features of the qr code
    final ResultPoint[] points = scanResult.getResultPoints();
    if (points != null && points.length > 0) {
        final Paint paint = new Paint();
        paint.setColor(getResources().getColor(com.fillerino.wallet.R.color.scan_result_dots));
        paint.setStrokeWidth(10.0f);

        final Canvas canvas = new Canvas(thumbnailImage);
        canvas.scale(thumbnailScaleFactor, thumbnailScaleFactor);
        for (final ResultPoint point : points)
            canvas.drawPoint(point.getX(), point.getY(), paint);
    }

    scannerView.drawResultBitmap(thumbnailImage);

    final Intent result = new Intent();
    result.putExtra(INTENT_EXTRA_RESULT, scanResult.getText());
    setResult(RESULT_OK, result);

    // delayed finish
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            finish();
        }
    });
}

From source file:br.com.leoleal.swipetorefresh.SwipeProgressBar.java

/**
 * Draws a circle centered in the view.// w ww  . j a  va 2 s.co m
 *
 * @param canvas the canvas to draw on
 * @param cx the center x coordinate
 * @param cy the center y coordinate
 * @param color the color to draw
 * @param pct the percentage of the view that the circle should cover
 */
private void drawCircle(Canvas canvas, float cx, float cy, int color, float pct) {
    mPaint.setColor(color);
    canvas.save();
    canvas.translate(cx, cy);
    float radiusScale = INTERPOLATOR.getInterpolation(pct);
    canvas.scale(radiusScale, radiusScale);
    canvas.drawCircle(0, 0, cx, mPaint);
    canvas.restore();
}

From source file:danonino.danonino_the_game.Core.GamePanel.java

@Override
public void draw(Canvas canvas) {
    final float scaleFactorX = getWidth() / (WIDTH * 1.f);
    final float scaleFactorY = getHeight() / (HEIGHT * 1.f);
    if (canvas != null) {
        final int savedState = canvas.save();
        canvas.scale(scaleFactorX, scaleFactorY);
        this.bg.draw(canvas);
        this.progress.draw(canvas);

        //draw butterflies
        for (Butterfly b : this.butterflies) {
            b.draw(canvas);//from   w w w  .j a v  a2 s .  c  om
        }
        //draw enemies
        for (Fruit e : this.enemies) {
            e.draw(canvas);
        }
        this.player.draw(canvas);

        if (this.event != null) {
            this.event.draw(canvas);
        }

        if (!this.gameOver) {
            if (this.joystickEnabled) {
                this.joystick.draw(canvas);
            }

            if (this.powerUpLabel != null) {
                this.powerUpLabel.draw(canvas);
            }
        } else {
            GameOver.draw(canvas);
        }
        this.bgFront.draw(canvas);
        canvas.restoreToCount(savedState);
    }
}

From source file:Main.java

public static Bitmap cropMaxVisibleBitmap(Drawable drawable, int iconSize) {
    Bitmap tmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);/*  ww  w  .  j a v  a 2 s . c  om*/
    Canvas canvas = new Canvas(tmp);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    Rect crop = new Rect(tmp.getWidth(), tmp.getHeight(), -1, -1);
    for (int y = 0; y < tmp.getHeight(); y++) {
        for (int x = 0; x < tmp.getWidth(); x++) {
            int alpha = (tmp.getPixel(x, y) >> 24) & 255;
            if (alpha > 0) { // pixel is not 100% transparent
                if (x < crop.left)
                    crop.left = x;
                if (x > crop.right)
                    crop.right = x;
                if (y < crop.top)
                    crop.top = y;
                if (y > crop.bottom)
                    crop.bottom = y;
            }
        }
    }

    if (crop.width() <= 0 || crop.height() <= 0) {
        return Bitmap.createScaledBitmap(tmp, iconSize, iconSize, true);
    }

    // We want to crop a square region.
    float size = Math.max(crop.width(), crop.height());
    float xShift = (size - crop.width()) * 0.5f;
    crop.left -= Math.floor(xShift);
    crop.right += Math.ceil(xShift);

    float yShift = (size - crop.height()) * 0.5f;
    crop.top -= Math.floor(yShift);
    crop.bottom += Math.ceil(yShift);

    Bitmap finalImage = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(finalImage);
    float scale = iconSize / size;

    canvas.scale(scale, scale);
    canvas.drawBitmap(tmp, -crop.left, -crop.top, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
    canvas.setBitmap(null);
    return finalImage;
}

From source file:com.dk.view.FolderDrawerLayout.java

public static Bitmap drawViewToBitmap(Bitmap dest, View view, int width, int height, int downSampling,
        Drawable drawable) {//from  w  w w .j a  v a  2  s  . c o m
    float scale = 1f / downSampling;
    int heightCopy = view.getHeight();
    // view.layout(0, 0, width, height);
    int bmpWidth = (int) (width * scale);
    int bmpHeight = (int) (height * scale);
    if (dest == null || dest.getWidth() != bmpWidth || dest.getHeight() != bmpHeight) {
        dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas c = new Canvas(dest);
    drawable.setBounds(new Rect(0, 0, width, height));
    drawable.draw(c);
    if (downSampling > 1) {
        c.scale(scale, scale);
    }
    view.draw(c);
    // view.layout(0, 0, width, heightCopy);
    return dest;
}

From source file:android.support.v7.widget.FastScroller.java

private void drawVerticalScrollbar(Canvas canvas) {
    int viewWidth = mRecyclerViewWidth;

    int left = viewWidth - mVerticalThumbWidth;
    int top = mVerticalThumbCenterY - mVerticalThumbHeight / 2;
    mVerticalThumbDrawable.setBounds(0, 0, mVerticalThumbWidth, mVerticalThumbHeight);
    mVerticalTrackDrawable.setBounds(0, 0, mVerticalTrackWidth, mRecyclerViewHeight);

    if (isLayoutRTL()) {
        mVerticalTrackDrawable.draw(canvas);
        canvas.translate(mVerticalThumbWidth, top);
        canvas.scale(-1, 1);
        mVerticalThumbDrawable.draw(canvas);
        canvas.scale(1, 1);//from  ww w  .  j ava 2s .  c  om
        canvas.translate(-mVerticalThumbWidth, -top);
    } else {
        canvas.translate(left, 0);
        mVerticalTrackDrawable.draw(canvas);
        canvas.translate(0, top);
        mVerticalThumbDrawable.draw(canvas);
        canvas.translate(-left, -top);
    }
}

From source file:com.evilduck.piano.views.instrument.PianoView.java

@Override
protected void onDraw(Canvas canvas) {
    if (isInEditMode()) {
        canvas.drawColor(Color.GRAY);
        return;//www.j  a va  2  s.  c  o  m
    }

    if (measurementChanged) {
        measurementChanged = false;
        keyboard.initializeInstrument(getMeasuredHeight(), getContext());

        float oldInstrumentWidth = instrumentWidth;
        instrumentWidth = keyboard.getWidth();

        float ratio = (float) instrumentWidth / oldInstrumentWidth;
        xOffset = (int) (xOffset * ratio);
    }

    int localXOffset = getOffsetInsideOfBounds();

    canvas.save();
    canvas.scale(scaleX, 1.0f);
    canvas.translate(-localXOffset, 0);

    keyboard.updateBounds(localXOffset, canvasWidth + localXOffset);
    keyboard.draw(canvas);

    if (!notesToDraw.isEmpty()) {
        keyboard.drawOverlays(notesToDraw, canvas);
    }

    canvas.restore();
}

From source file:com.android.launcher3.folder.FolderIcon.java

private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
    canvas.save();/*from   ww w .  j  a v a  2s  .c o  m*/
    canvas.translate(params.transX, params.transY);
    canvas.scale(params.scale, params.scale);
    Drawable d = params.drawable;

    if (d != null) {
        mOldBounds.set(d.getBounds());
        d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
        if (d instanceof FastBitmapDrawable) {
            FastBitmapDrawable fd = (FastBitmapDrawable) d;
            float oldBrightness = fd.getBrightness();
            fd.setBrightness(params.overlayAlpha);
            d.draw(canvas);
            fd.setBrightness(oldBrightness);
        } else {
            d.setColorFilter(Color.argb((int) (params.overlayAlpha * 255), 255, 255, 255),
                    PorterDuff.Mode.SRC_ATOP);
            d.draw(canvas);
            d.clearColorFilter();
        }
        d.setBounds(mOldBounds);
    }
    canvas.restore();
}

From source file:kr.wdream.storyshop.AndroidUtilities.java

private static Intent createShortcutIntent(long did, boolean forDelete) {
    Intent shortcutIntent = new Intent(ApplicationLoader.applicationContext, OpenChatReceiver.class);

    int lower_id = (int) did;
    int high_id = (int) (did >> 32);

    TLRPC.User user = null;//  w w w . j a  va 2s.c o m
    TLRPC.Chat chat = null;
    if (lower_id == 0) {
        shortcutIntent.putExtra("encId", high_id);
        TLRPC.EncryptedChat encryptedChat = MessagesController.getInstance().getEncryptedChat(high_id);
        if (encryptedChat == null) {
            return null;
        }
        user = MessagesController.getInstance().getUser(encryptedChat.user_id);
    } else if (lower_id > 0) {
        shortcutIntent.putExtra("userId", lower_id);
        user = MessagesController.getInstance().getUser(lower_id);
    } else if (lower_id < 0) {
        chat = MessagesController.getInstance().getChat(-lower_id);
        shortcutIntent.putExtra("chatId", -lower_id);
    } else {
        return null;
    }
    if (user == null && chat == null) {
        return null;
    }

    String name;
    TLRPC.FileLocation photo = null;

    if (user != null) {
        name = ContactsController.formatName(user.first_name, user.last_name);
        if (user.photo != null) {
            photo = user.photo.photo_small;
        }
    } else {
        name = chat.title;
        if (chat.photo != null) {
            photo = chat.photo.photo_small;
        }
    }

    shortcutIntent.setAction("com.tmessages.openchat" + did);
    shortcutIntent.addFlags(0x4000000);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    addIntent.putExtra("duplicate", false);
    if (!forDelete) {
        Bitmap bitmap = null;
        if (photo != null) {
            try {
                File path = FileLoader.getPathToAttach(photo, true);
                bitmap = BitmapFactory.decodeFile(path.toString());
                if (bitmap != null) {
                    int size = AndroidUtilities.dp(58);
                    Bitmap result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
                    result.eraseColor(Color.TRANSPARENT);
                    Canvas canvas = new Canvas(result);
                    BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
                            Shader.TileMode.CLAMP);
                    if (roundPaint == null) {
                        roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                        bitmapRect = new RectF();
                    }
                    float scale = size / (float) bitmap.getWidth();
                    canvas.save();
                    canvas.scale(scale, scale);
                    roundPaint.setShader(shader);
                    bitmapRect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
                    canvas.drawRoundRect(bitmapRect, bitmap.getWidth(), bitmap.getHeight(), roundPaint);
                    canvas.restore();
                    Drawable drawable = ApplicationLoader.applicationContext.getResources()
                            .getDrawable(R.drawable.book_logo);
                    int w = AndroidUtilities.dp(15);
                    int left = size - w - AndroidUtilities.dp(2);
                    int top = size - w - AndroidUtilities.dp(2);
                    drawable.setBounds(left, top, left + w, top + w);
                    drawable.draw(canvas);
                    try {
                        canvas.setBitmap(null);
                    } catch (Exception e) {
                        //don't promt, this will crash on 2.x
                    }
                    bitmap = result;
                }
            } catch (Throwable e) {
                FileLog.e("tmessages", e);
            }
        }
        if (bitmap != null) {
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
        } else {
            if (user != null) {
                if (user.bot) {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_bot));
                } else {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_user));
                }
            } else if (chat != null) {
                if (ChatObject.isChannel(chat) && !chat.megagroup) {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_channel));
                } else {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_group));
                }
            }
        }
    }
    return addIntent;
}