Example usage for android.graphics Matrix setTranslate

List of usage examples for android.graphics Matrix setTranslate

Introduction

In this page you can find the example usage for android.graphics Matrix setTranslate.

Prototype

public void setTranslate(float dx, float dy) 

Source Link

Document

Set the matrix to translate by (dx, dy).

Usage

From source file:Main.java

public static Bitmap overlayToDownCenter(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    Matrix matrix = new Matrix();
    matrix.setTranslate(((float) bmp1.getWidth() - bmp2.getWidth()) / 2, bmp1.getHeight() - bmp2.getHeight());
    canvas.drawBitmap(bmp2, matrix, null);
    return bmOverlay;
}

From source file:Main.java

public static Bitmap overlayToDownRightCorner(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    Matrix matrix = new Matrix();
    matrix.setTranslate(bmp1.getWidth() - bmp2.getWidth(), bmp1.getHeight() - bmp2.getHeight());
    canvas.drawBitmap(bmp2, matrix, null);
    return bmOverlay;
}

From source file:com.example.android.camera.CameraActivity.java

public static void prepareMatrix(Matrix matrix, int displayOrientation, int viewWidth, int viewHeight) {

    Log.d("face CameraPreview", "Width: " + viewWidth + " Height: " + viewHeight);

    // This is the value for android.hardware.Camera.setDisplayOrientation.
    matrix.postRotate(displayOrientation);

    // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
    // UI coordinates range from (0, 0) to (width, height).
    // Log.d("face View", "Width: " + viewWidth + " x Height: " + viewHeight);
    matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
    //        matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
    matrix.setTranslate(viewWidth / 2f, viewHeight / 2f);
}

From source file:br.com.viniciuscr.notification2android.mediaPlayer.MusicUtils.java

static void setBackground(View v, Bitmap bm) {

    if (bm == null) {
        v.setBackgroundResource(0);/*from   w  ww  .  ja v a2 s  .  co m*/
        return;
    }

    int vwidth = v.getWidth();
    int vheight = v.getHeight();
    int bwidth = bm.getWidth();
    int bheight = bm.getHeight();
    float scalex = (float) vwidth / bwidth;
    float scaley = (float) vheight / bheight;
    float scale = Math.max(scalex, scaley) * 1.3f;

    Bitmap.Config config = Bitmap.Config.ARGB_8888;
    Bitmap bg = Bitmap.createBitmap(vwidth, vheight, config);
    Canvas c = new Canvas(bg);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    ColorMatrix greymatrix = new ColorMatrix();
    greymatrix.setSaturation(0);
    ColorMatrix darkmatrix = new ColorMatrix();
    darkmatrix.setScale(.3f, .3f, .3f, 1.0f);
    greymatrix.postConcat(darkmatrix);
    ColorFilter filter = new ColorMatrixColorFilter(greymatrix);
    paint.setColorFilter(filter);
    Matrix matrix = new Matrix();
    matrix.setTranslate(-bwidth / 2, -bheight / 2); // move bitmap center to origin
    matrix.postRotate(10);
    matrix.postScale(scale, scale);
    matrix.postTranslate(vwidth / 2, vheight / 2); // Move bitmap center to view center
    c.drawBitmap(bm, matrix, paint);
    v.setBackgroundDrawable(new BitmapDrawable(bg));
}

From source file:com.tbse.mywearapplication.WatchFaceDrawer.java

void onDraw(Context context, IWatchFaceConfig config, Canvas canvas, Rect bounds) {
    final Calendar calendar = config.getCalendar();
    final boolean isAmbient = config.isAmbient();
    final boolean isRound = config.isRound();
    final boolean useLightTheme = !isAmbient && config.isLightTheme();

    mBackgroundPaint.setColor(ContextCompat.getColor(context,
            useLightTheme ? R.color.watchface_background_light : R.color.watchface_background));

    /////////////////////////////////////////////////////////////////////
    // Draw your watch face here, using the provided canvas and bounds //
    /////////////////////////////////////////////////////////////////////

    final int width = bounds.width();
    final int height = bounds.height();

    // Find the center. Ignore the window insets so that, on round
    // watches with a "chin", the watch face is centered on the entire
    // screen, not just the usable portion.
    final float centerX = width / 2f;
    final float centerY = height / 2f;

    // Draw the background.
    if (mIsMobilePreview) {
        if (isRound) {
            canvas.drawCircle(centerX, centerY, centerX, mPreviewBorderPaint);
        } else {/*from www.j av  a 2 s  .c  o m*/
            final float radius = mPreviewSquareRadius;
            final RectF rectF = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, mPreviewBorderPaint);
        }

        final float translateXY = width * 0.05f;
        canvas.translate(translateXY, translateXY);
        canvas.scale(0.9f, 0.9f);

        if (isRound) {
            canvas.drawCircle(centerX, centerY, centerX, mBackgroundPaint);
        } else {
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
        }
    } else {
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
    }

    // Draw weather icon
    if (image != null) {
        Matrix matrix = new Matrix();
        matrix.setTranslate(50, 90);
        matrix.preScale(0.2f, 0.2f);
        canvas.drawBitmap(image, matrix, null);
    }

    final float secRot = calendar.get(Calendar.SECOND) / 30f * (float) Math.PI;
    final int minutes = calendar.get(Calendar.MINUTE);
    final float minRot = minutes / 30f * (float) Math.PI;
    final float hrRot = ((calendar.get(Calendar.HOUR) + (minutes / 60f)) / 6f) * (float) Math.PI;

    final float secLength = centerX - mSecondOuterOffset;
    final float minLength = centerX - mMinuteOuterOffset;
    final float hrLength = centerX - mHourOuterOffset;

    if (!isAmbient) {
        final float secX = (float) Math.sin(secRot) * secLength;
        final float secY = (float) -Math.cos(secRot) * secLength;
        canvas.drawLine(centerX, centerY, centerX + secX, centerY + secY, mSecondHandPaint);
    }

    final float minX = (float) Math.sin(minRot) * minLength;
    final float minY = (float) -Math.cos(minRot) * minLength;
    canvas.drawLine(centerX, centerY, centerX + minX, centerY + minY, mMinuteHandPaint);

    final float hrX = (float) Math.sin(hrRot) * hrLength;
    final float hrY = (float) -Math.cos(hrRot) * hrLength;
    canvas.drawLine(centerX, centerY, centerX + hrX, centerY + hrY, mHourHandPaint);

    // Draw weather text
    canvas.drawText(day, 50, 50, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(low, 50, 65, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(high, 80, 65, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(weatherDescription, 50, 80, isAmbient ? ambientTextPaint : textPaint);

}

From source file:com.journeyapps.barcodescanner.WXViewfinderView.java

@SuppressLint("DrawAllocation")
@Override//from ww  w  . j a v a2s. c om
public void onDraw(Canvas canvas) {
    refreshSizes();
    if (framingRect == null || previewFramingRect == null) {
        return;
    }

    Rect frame = framingRect;
    Rect previewFrame = previewFramingRect;

    int width = canvas.getWidth();
    int height = canvas.getHeight();

    maskPaint.setColor(maskColor);
    canvas.drawRect(0, 0, width, frame.top, maskPaint);
    canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, maskPaint);
    canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, maskPaint);
    canvas.drawRect(0, frame.bottom + 1, width, height, maskPaint);
    //drawable the border
    canvas.drawRect(frame.left + 1, frame.top + 1, frame.right, frame.bottom, borderPaint);
    int halfWidth = (int) (cornerWidth / 2);
    //draw four corner
    Path corner1 = new Path();
    corner1.moveTo(frame.left, frame.top + cornerLength);
    corner1.lineTo(frame.left, frame.top);
    corner1.lineTo(frame.left + cornerLength, frame.top);
    Matrix translate1 = new Matrix();
    translate1.setTranslate(halfWidth, halfWidth);
    corner1.transform(translate1);
    canvas.drawPath(corner1, cornerPaint);

    Path corner2 = new Path();
    corner2.moveTo(frame.right + 1 - cornerLength, frame.top);
    corner2.lineTo(frame.right + 1, frame.top);
    corner2.lineTo(frame.right + 1, frame.top + cornerLength);
    Matrix translate2 = new Matrix();
    translate2.setTranslate(-halfWidth, halfWidth);
    corner2.transform(translate2);
    canvas.drawPath(corner2, cornerPaint);

    Path corner3 = new Path();
    corner3.moveTo(frame.left, frame.bottom + 1 - cornerLength);
    corner3.lineTo(frame.left, frame.bottom + 1);
    corner3.lineTo(frame.left + cornerLength, frame.bottom + 1);
    Matrix translate3 = new Matrix();
    translate3.setTranslate(halfWidth, -halfWidth);
    corner3.transform(translate3);
    canvas.drawPath(corner3, cornerPaint);

    Path corner4 = new Path();
    corner4.moveTo(frame.right + 1 - cornerLength, frame.bottom + 1);
    corner4.lineTo(frame.right + 1, frame.bottom + 1);
    corner4.lineTo(frame.right + 1, frame.bottom + 1 - cornerLength);
    Matrix translate4 = new Matrix();
    translate4.setTranslate(-halfWidth, -halfWidth);
    corner4.transform(translate4);
    canvas.drawPath(corner4, cornerPaint);

    offset += speed;
    if (offset >= frame.bottom - frame.top) {
        offset = 0;
    }
    Rect rect = new Rect();
    rect.left = frame.left + 1 + laserPadding;
    rect.top = frame.top + 1 + offset;
    rect.right = frame.right - laserPadding;
    rect.bottom = frame.top + 1 + offset + 3;

    Bitmap laserBitmap = ((BitmapDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.scan_laser,
            null)).getBitmap();
    canvas.drawBitmap(laserBitmap, null, rect, linePaint);

    textPaint.setTextAlign(Paint.Align.CENTER);

    canvas.drawText(statusText, (frame.right + frame.left) / 2,
            frame.bottom + statusTextPadding + statusTextSize, textPaint);

    postInvalidateDelayed(animationDelay, frame.left, frame.top, frame.right, frame.bottom);

}

From source file:com.zzti.fyg.widgets.SwipeProgressBar.java

private void drawBar(Canvas canvas, int height) {
    mPaint.setAntiAlias(true);// w  ww . j a  va 2  s . com
    Bitmap bitmap = null;
    if (state == FINISHED) {
        bitmap = getFinishedBitmap();
    } else {
        bitmap = getBitmap();
    }

    Matrix matrix = new Matrix();
    matrix.setTranslate(bitmap.getWidth() >> 1, bitmap.getHeight() >> 1);

    if (mRunning) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        matrix.postRotate((now - mStartTime) % 360);
    } else {
        if (type == ROTATE) {
            if (state != FINISHED) {
                matrix.postRotate(height % 360);
            }
        } else {
            if (direction == 0) {
                if (state == RELEASE_TO_REFRESH) {
                    matrix.postRotate(180);
                }

            } else if (direction == 1) {
                if (state == PULL_TO_REFRESH) {
                    matrix.postRotate(180);
                }
            }
        }

    }

    dstbmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    int originalWidth = bitmap.getWidth();
    int originalHeight = bitmap.getHeight();

    int dstWidth = dstbmp.getWidth();
    int dstHeight = dstbmp.getHeight();

    int fontHeight = (int) DisplayUtil.getFontHeight(textSize);
    mPaint.setTextSize(textSize);
    mPaint.setColor(textColor);
    if (direction == 0) {
        //canvas.drawBitmap(dstbmp, (width >> 1) - ((int)mPaint.measureText(latestRefreshTime) >> 1) - drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)), (mBounds.bottom - (originalHeight >> 1)) + ((originalHeight >> 1) - (dstHeight >> 1)) - (fontHeight << 1) , mPaint);
        canvas.drawBitmap(dstbmp, drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)),
                (mBounds.bottom - (originalHeight >> 1)) + ((originalHeight >> 1) - (dstHeight >> 1))
                        - (fontHeight << 1),
                mPaint);
        mPaint.setFakeBoldText(true);

        canvas.drawText(tips, (width >> 1) - ((int) mPaint.measureText(tips) >> 1),
                (mBounds.bottom - (fontHeight << 1)), mPaint);
        mPaint.setFakeBoldText(false);
        canvas.drawText(latestRefreshTime, (width >> 1) - ((int) mPaint.measureText(latestRefreshTime) >> 1),
                (mBounds.bottom - fontHeight), mPaint);
    } else {
        float y = mBounds.top + ((textMarginTop + (fontHeight << 1)) >> 1) + textMarginTop - (dstHeight >> 1);
        //          canvas.drawBitmap(dstbmp, (width >> 1) - ((int)mPaint.measureText(latestRefreshTime) >> 1) - drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)), y , mPaint);
        canvas.drawBitmap(dstbmp, drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)), y, mPaint);
        mPaint.setFakeBoldText(true);
        canvas.drawText(tips, (width >> 1) - ((int) mPaint.measureText(tips) >> 1),
                (mBounds.top + textMarginTop + fontHeight), mPaint);
        mPaint.setFakeBoldText(false);
        canvas.drawText(latestRefreshTime, (width >> 1) - ((int) mPaint.measureText(latestRefreshTime) >> 1),
                (mBounds.top + (fontHeight << 1) + (textMarginTop << 1)), mPaint);
    }

}

From source file:com.limxing.library.PullToRefresh.SwipeProgressBar.java

private void drawBar(Canvas canvas, int height) {
    mPaint.setAntiAlias(true);//w  w  w  .  j  a  va 2  s.c om
    Bitmap bitmap = null;
    if (state == FINISHED) {
        bitmap = getFinishedBitmap();
    } else {
        bitmap = getBitmap();
    }

    Matrix matrix = new Matrix();
    matrix.setTranslate(bitmap.getWidth() >> 1, bitmap.getHeight() >> 1);

    if (mRunning) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        matrix.postRotate(((now - mStartTime) % 360) / 2);
    } else {
        if (type == ROTATE) {
            if (state != FINISHED) {
                matrix.postRotate(height % 360);
            }
        } else {
            if (direction == 0) {
                if (state == RELEASE_TO_REFRESH) {
                    matrix.postRotate(180);
                }

            } else if (direction == 1) {
                if (state == PULL_TO_REFRESH) {
                    matrix.postRotate(180);
                }
            }
        }

    }

    dstbmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    int originalWidth = bitmap.getWidth();
    int originalHeight = bitmap.getHeight();

    int dstWidth = dstbmp.getWidth();
    int dstHeight = dstbmp.getHeight();

    int fontHeight = (int) DisplayUtil.getFontHeight(textSize);
    mPaint.setTextSize(textSize);
    mPaint.setColor(textColor);
    if (direction == 0) {
        // canvas.drawBitmap(dstbmp, (width >> 1) -
        // ((int)mPaint.measureText(latestRefreshTime) >> 1) -
        // drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)),
        // (mBounds.bottom - (originalHeight >> 1)) + ((originalHeight >> 1)
        // - (dstHeight >> 1)) - (fontHeight << 1) , mPaint);
        canvas.drawBitmap(dstbmp, drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)),
                (mBounds.bottom - (originalHeight >> 1)) + ((originalHeight >> 1) - (dstHeight >> 1))
                        - (fontHeight << 1),
                mPaint);
        mPaint.setFakeBoldText(true);

        canvas.drawText(tips, (width >> 1) - ((int) mPaint.measureText(tips) >> 1),
                (mBounds.bottom - (fontHeight << 1)), mPaint);
        mPaint.setFakeBoldText(false);
        canvas.drawText(latestRefreshTime, (width >> 1) - ((int) mPaint.measureText(latestRefreshTime) >> 1),
                (mBounds.bottom - fontHeight), mPaint);
    } else {
        float y = mBounds.top + ((textMarginTop + (fontHeight << 1)) >> 1) + textMarginTop - (dstHeight >> 1);
        // canvas.drawBitmap(dstbmp, (width >> 1) -
        // ((int)mPaint.measureText(latestRefreshTime) >> 1) -
        // drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)),
        // y , mPaint);
        canvas.drawBitmap(dstbmp, drawable_margin_left - ((dstWidth >> 1) - (originalWidth >> 1)), y, mPaint);
        mPaint.setFakeBoldText(true);
        canvas.drawText(tips, (width >> 1) - ((int) mPaint.measureText(tips) >> 1),
                (mBounds.top + textMarginTop + fontHeight), mPaint);
        mPaint.setFakeBoldText(false);
        canvas.drawText(latestRefreshTime, (width >> 1) - ((int) mPaint.measureText(latestRefreshTime) >> 1),
                (mBounds.top + (fontHeight << 1) + (textMarginTop << 1)), mPaint);
    }

}

From source file:com.artioml.practice.activities.LicenseActivity.java

public BitmapDrawable drawParallelogramLine(int width) {
    Matrix matrix = new Matrix();
    Path path = new Path();
    path.addRect(0, 0, (4 * width) / 40, (4 * width) / 200, Path.Direction.CW);
    Path pathStamp = new Path();
    Paint p;//w  w  w  .  ja v a  2 s.  com
    Bitmap bitmap;

    p = new Paint(Paint.ANTI_ALIAS_FLAG);
    p.setStyle(Paint.Style.FILL);

    bitmap = Bitmap.createBitmap(width / 4, width / 80 * 2 + (4 * width) / 200, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    p.setColor(ContextCompat.getColor(this, R.color.colorPrimaryLight));

    matrix.reset();
    matrix.setTranslate(0, 0);
    matrix.postSkew(-1f, 0.0f, 0, (4 * width) / 200);
    path.transform(matrix, pathStamp);
    canvas.drawPath(pathStamp, p);

    p.setColor(ContextCompat.getColor(this, R.color.colorAccent));

    matrix.reset();
    matrix.setTranslate(width / 8, 0);
    matrix.postSkew(-1f, 0.0f, width / 8, (4 * width) / 200);
    path.transform(matrix, pathStamp);
    canvas.drawPath(pathStamp, p);

    BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
    bitmapDrawable.setTileModeX(Shader.TileMode.REPEAT);

    return bitmapDrawable;
}

From source file:com.ferdi2005.secondgram.AndroidUtilities.java

public static void setRectToRect(Matrix matrix, RectF src, RectF dst, int rotation, Matrix.ScaleToFit align) {
    float tx, sx;
    float ty, sy;
    if (rotation == 90 || rotation == 270) {
        sx = dst.height() / src.width();
        sy = dst.width() / src.height();
    } else {/*from  w w  w.  j av  a2s.  c  om*/
        sx = dst.width() / src.width();
        sy = dst.height() / src.height();
    }
    if (align != Matrix.ScaleToFit.FILL) {
        if (sx > sy) {
            sx = sy;
        } else {
            sy = sx;
        }
    }
    tx = -src.left * sx;
    ty = -src.top * sy;

    matrix.setTranslate(dst.left, dst.top);
    if (rotation == 90) {
        matrix.preRotate(90);
        matrix.preTranslate(0, -dst.width());
    } else if (rotation == 180) {
        matrix.preRotate(180);
        matrix.preTranslate(-dst.width(), -dst.height());
    } else if (rotation == 270) {
        matrix.preRotate(270);
        matrix.preTranslate(-dst.height(), 0);
    }

    matrix.preScale(sx, sy);
    matrix.preTranslate(tx, ty);
}