Example usage for android.graphics Canvas rotate

List of usage examples for android.graphics Canvas rotate

Introduction

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

Prototype

public final void rotate(float degrees, float px, float py) 

Source Link

Document

Preconcat the current matrix with the specified rotation.

Usage

From source file:Main.java

public static Bitmap rotateDrawable(Context context, @DrawableRes int resId, int angle) {
    Bitmap bmpOriginal = BitmapFactory.decodeResource(context.getResources(), resId);
    Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(),
            Bitmap.Config.ARGB_8888);//from  ww  w  .  jav  a  2 s.  c  o  m
    Canvas tempCanvas = new Canvas(bmResult);
    tempCanvas.rotate(angle - 90, bmpOriginal.getWidth() / 2, bmpOriginal.getHeight() / 2);
    tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
    return bmResult;
}

From source file:Main.java

/**
 * Retrieves a copy of the specified drawable resource, rotated by a specified angle.
 *
 * @param resources The current resources.
 * @param resourceId The resource ID of the drawable to rotate.
 * @param angle The angle of rotation./* ww w .ja  v a  2 s  . co m*/
 * @return Rotated drawable.
 */
public static Drawable getRotatedDrawable(android.content.res.Resources resources, int resourceId,
        float angle) {

    // Get the original drawable and make a copy which will be rotated.
    Bitmap original = BitmapFactory.decodeResource(resources, resourceId);
    Bitmap rotated = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);

    // Perform the rotation.
    Canvas tempCanvas = new Canvas(rotated);
    tempCanvas.rotate(angle, original.getWidth() / 2, original.getHeight() / 2);
    tempCanvas.drawBitmap(original, 0, 0, null);

    return new BitmapDrawable(resources, rotated);
}

From source file:Main.java

public static Drawable getRotateDrawable(Resources res, final Bitmap bitmap, final float angle) {
    final BitmapDrawable drawable = new BitmapDrawable(res, bitmap) {
        @Override/*w  w w.j  a  v a 2 s . c o m*/
        public void draw(final Canvas canvas) {
            canvas.save();
            canvas.rotate(angle, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
            super.draw(canvas);
            canvas.restore();
        }
    };
    return drawable;
}

From source file:Main.java

public static Bitmap rotateAndFrame(Bitmap bitmap) {
    final boolean positive = sRandom.nextFloat() >= 0.5f;
    final float angle = (ROTATION_ANGLE_MIN + sRandom.nextFloat() * ROTATION_ANGLE_EXTRA)
            * (positive ? 1.0f : -1.0f);
    final double radAngle = Math.toRadians(angle);

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    final double cosAngle = Math.abs(Math.cos(radAngle));
    final double sinAngle = Math.abs(Math.sin(radAngle));

    final int strokedWidth = (int) (bitmapWidth + 2 * PHOTO_BORDER_WIDTH);
    final int strokedHeight = (int) (bitmapHeight + 2 * PHOTO_BORDER_WIDTH);

    final int width = (int) (strokedHeight * sinAngle + strokedWidth * cosAngle);
    final int height = (int) (strokedWidth * sinAngle + strokedHeight * cosAngle);

    final float x = (width - bitmapWidth) / 2.0f;
    final float y = (height - bitmapHeight) / 2.0f;

    final Bitmap decored = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(decored);

    canvas.rotate(angle, width / 2.0f, height / 2.0f);
    canvas.drawBitmap(bitmap, x, y, sPaint);
    canvas.drawRect(x, y, x + bitmapWidth, y + bitmapHeight, sStrokePaint);

    return decored;
}

From source file:Main.java

public static void drawText(Canvas canvas, String text, float x, float y, Paint paint, float angle) {
    if (angle != 0F)
        canvas.rotate(angle, x, y);

    canvas.drawText(text, x, y, paint);//from  www.java 2  s .c  o m
    if (angle != 0F)
        canvas.rotate(-angle, x, y);
}

From source file:Main.java

/**
 * Draws a string that is aligned by one anchor point and rotated about
 * another anchor point./*from w w  w .  ja  v  a  2s .  com*/
 * 
 * @param text
 *            the text.
 * @param g2
 *            the graphics device.
 * @param x
 *            the location of the text anchor.
 * @param y
 *            the location of the text anchor.
 * @param textAnchor
 *            the text anchor.
 * @param rotationX
 *            the x-coordinate for the rotation anchor point.
 * @param rotationY
 *            the y-coordinate for the rotation anchor point.
 * @param angle
 *            the rotation angle.
 */
public static void drawRotatedString(final String text, final Canvas g2, final float x, final float y,
        final Paint paint, float angle) {

    //g2.drawCircle(x, y, 2, paint);
    g2.save();
    if (angle == 0.0)
        g2.drawText(text, x, y, paint);
    else {
        g2.rotate((float) Math.toDegrees(angle), x, y);
        g2.drawText(text, x, y, paint);
    }
    g2.restore();
}

From source file:com.nadmm.airports.utils.UiUtils.java

public static Drawable getRotatedDrawable(Context context, int resid, float rotation) {
    String key = String.format(Locale.US, "%d:%d", resid, (int) rotation);
    Drawable d = getDrawableFromCache(key);
    if (d == null) {
        Resources res = context.getResources();
        Bitmap bmp = BitmapFactory.decodeResource(res, resid);
        Bitmap rotated = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(rotated);
        canvas.setDensity(Bitmap.DENSITY_NONE);
        canvas.rotate(rotation, bmp.getWidth() / 2, bmp.getHeight() / 2);
        canvas.drawBitmap(bmp, 0, 0, sPaint);
        d = new BitmapDrawable(res, rotated);
        putDrawableIntoCache(key, d);/*from  w w  w.ja  va2  s .  co  m*/
    }
    return d;
}

From source file:com.crs4.roodin.moduletester.CustomView.java

@Override
public void onDraw(Canvas canvas, Matrix matrix) {

    canvas.rotate(currentRotation, translateX, translateY);
    canvas.drawBitmap(blockImg, 0, 0, null);
    drawBarreds().draw(canvas);/*from   www  .j av a 2 s .  c o m*/
    drawRadar(canvas);

    canvas.restore();
}

From source file:foam.jellyfish.StarwispCanvas.java

public void DrawText(Canvas canvas, JSONArray prim, float sx, float sy) {
    try {/*from ww w.ja v  a 2 s . c om*/
        canvas.save();
        if (prim.getString(6).equals("vertical"))
            canvas.rotate(-90, prim.getInt(2) * sx, prim.getInt(3) * sy);

        Paint myPaint = new Paint();
        JSONArray c = prim.getJSONArray(4);
        myPaint.setColor(Color.rgb(c.getInt(0), c.getInt(1), c.getInt(2)));
        myPaint.setTextSize(prim.getInt(5));
        myPaint.setTypeface(m_Typeface);
        canvas.drawText(prim.getString(1), prim.getInt(2) * sx, prim.getInt(3) * sy, myPaint);
        canvas.restore();
    } catch (JSONException e) {
        Log.e("starwisp", "Error parsing data " + e.toString());
    }
}

From source file:com.tr4android.support.extension.drawable.IndeterminateProgressDrawable.java

@Override
public void draw(Canvas canvas) {
    int saveCount = canvas.save();
    canvas.rotate(mRotation, mArcRect.centerX(), mArcRect.centerY());

    float startAngle = -90 + 360 * (mOffset + mStart);
    float sweepAngle = 360 * (mEnd - mStart);
    canvas.drawArc(mArcRect, startAngle, sweepAngle, false, mArcPaint);

    canvas.restoreToCount(saveCount);//from w  w  w .java 2s. c o m
}