Example usage for android.graphics Matrix setRotate

List of usage examples for android.graphics Matrix setRotate

Introduction

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

Prototype

public void setRotate(float degrees, float px, float py) 

Source Link

Document

Set the matrix to rotate by the specified number of degrees, with a pivot point at (px, py).

Usage

From source file:Main.java

public static Bitmap rotate(Bitmap b, int degrees, Matrix m) {
    if (degrees != 0 && b != null) {
        if (m == null) {
            m = new Matrix();
        }/*from w w  w . j  av  a  2  s.  c o m*/
        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        try {
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
            if (b != b2) {
                b.recycle();
                b = b2;
            }
        } catch (OutOfMemoryError ex) {
            // We have no memory to rotate. Return the original bitmap.
            Log.e(TAG, "Got oom exception ", ex);
        }
    }
    return b;
}

From source file:Main.java

public static Bitmap rotate(Bitmap b, int degrees) {
    if (degrees != 0 && b != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        try {//  w  w w.j av a 2 s  . c  om
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
            if (b != b2) {
                b.recycle();
                b = b2;
            }
        } catch (OutOfMemoryError ex) {
            // We have no memory to rotate. Return the original bitmap.
        }
    }
    return b;
}

From source file:com.yanzhenjie.album.task.LocalImageLoader.java

/**
 * Deposit in the province read images, width is high, the greater the picture clearer, but also the memory.
 *
 * @param imagePath pictures in the path of the memory card.
 * @param maxWidth  the highest limit value target width.
 * @param maxHeight the highest limit value target height.
 * @return Bitmap./*www  .  j av  a 2 s.  c  o m*/
 */
private static Bitmap readImage(String imagePath, int maxWidth, int maxHeight) {
    File imageFile = new File(imagePath);
    if (imageFile.exists()) {
        BufferedInputStream inputStream = null;
        try {
            inputStream = new BufferedInputStream(new FileInputStream(imageFile));
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(inputStream, null, options);
            inputStream.close();

            options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
            options.inJustDecodeBounds = false;

            Bitmap decodeSampledBitmap = null;

            boolean decodeAttemptSuccess = false;
            while (!decodeAttemptSuccess) {
                inputStream = new BufferedInputStream(new FileInputStream(imageFile));
                try {
                    decodeSampledBitmap = BitmapFactory.decodeStream(inputStream, null, options);
                    decodeAttemptSuccess = true;
                } catch (Exception e) {
                    options.inSampleSize *= 2;
                }
                inputStream.close();
            }

            if (imagePath.endsWith(".jpg") || imagePath.endsWith(".JPG") || imagePath.endsWith(".jpeg")
                    || imagePath.endsWith(".JPEG")) {
                int degrees = readDegree(imagePath);
                if (degrees > 0) {
                    Matrix matrix = new Matrix();
                    matrix.setRotate(degrees, decodeSampledBitmap.getWidth() / 2,
                            decodeSampledBitmap.getHeight() / 2);
                    decodeSampledBitmap = Bitmap.createBitmap(decodeSampledBitmap, 0, 0,
                            decodeSampledBitmap.getWidth(), decodeSampledBitmap.getHeight(), matrix, true);
                }
            }
            return decodeSampledBitmap;
        } catch (Exception ignored) {
        } finally {
            if (inputStream != null)
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    return null;
}

From source file:com.almalence.googsharing.Thumbnail.java

private static Bitmap rotateImage(Bitmap bitmap, int orientation) {
    if (orientation != 0) {
        // We only rotate the thumbnail once even if we get OOM.
        Matrix m = new Matrix();
        m.setRotate(orientation, bitmap.getWidth() * 0.5f, bitmap.getHeight() * 0.5f);

        try {/*  ww  w .ja  v a 2s .co m*/
            Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
            // If the rotated bitmap is the original bitmap, then it
            // should not be recycled.
            if (rotated != bitmap)
                bitmap.recycle();
            return rotated;
        } catch (Exception t) {
            Log.w(TAG, "Failed to rotate thumbnail", t);
        }
    }

    return bitmap;
}

From source file:com.linhnv.apps.maxim.utils.ImageWorker.java

public static Bitmap rotate(Bitmap b, int degrees) {
    if (degrees != 0 && b != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        try {/*from ww w.  ja v a  2s  .  c  o  m*/
            Bitmap b1 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);

            // Bitmap b2 =Bitmap.c(b1, 1280, 1024, true);

            b.recycle();
            return b1;
        } catch (OutOfMemoryError ex) {
            ex.printStackTrace();
        }
    }
    return b;
}

From source file:cn.djangoogle.pull2load.internal.IndicatorLayout.java

public IndicatorLayout(Context context, Pull2LoadBase.Mode mode) {
    super(context);
    mArrowImageView = new ImageView(context);

    Drawable arrowD = ContextCompat.getDrawable(context, R.drawable.indicator_arrow);
    mArrowImageView.setImageDrawable(arrowD);

    final int padding = getResources().getDimensionPixelSize(R.dimen.indicator_internal_padding);
    mArrowImageView.setPadding(padding, padding, padding, padding);
    addView(mArrowImageView);/*from   ww w  . ja  va2s  . com*/

    int inAnimResId, outAnimResId;
    switch (mode) {
    case PULL_FROM_END:
        inAnimResId = R.anim.slide_in_from_bottom;
        outAnimResId = R.anim.slide_out_to_bottom;
        setBackgroundResource(R.drawable.indicator_bg_bottom);

        // Rotate Arrow so it's pointing the correct way
        mArrowImageView.setScaleType(ScaleType.MATRIX);
        Matrix matrix = new Matrix();
        matrix.setRotate(180f, arrowD.getIntrinsicWidth() / 2f, arrowD.getIntrinsicHeight() / 2f);
        mArrowImageView.setImageMatrix(matrix);
        break;
    default:
    case PULL_FROM_START:
        inAnimResId = R.anim.slide_in_from_top;
        outAnimResId = R.anim.slide_out_to_top;
        setBackgroundResource(R.drawable.indicator_bg_top);
        break;
    }

    mInAnim = AnimationUtils.loadAnimation(context, inAnimResId);
    mInAnim.setAnimationListener(this);

    mOutAnim = AnimationUtils.loadAnimation(context, outAnimResId);
    mOutAnim.setAnimationListener(this);

    final Interpolator interpolator = new LinearInterpolator();
    mRotateAnimation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    mRotateAnimation.setInterpolator(interpolator);
    mRotateAnimation.setDuration(DEFAULT_ROTATION_ANIMATION_DURATION);
    mRotateAnimation.setFillAfter(true);

    mResetRotateAnimation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    mResetRotateAnimation.setInterpolator(interpolator);
    mResetRotateAnimation.setDuration(DEFAULT_ROTATION_ANIMATION_DURATION);
    mResetRotateAnimation.setFillAfter(true);

}

From source file:library.internal.IndicatorLayout.java

public IndicatorLayout(Context context, PullToRefreshBase.Mode mode) {
    super(context);
    mArrowImageView = new ImageView(context);

    Drawable arrowD = ContextCompat.getDrawable(context, R.drawable.indicator_arrow);
    mArrowImageView.setImageDrawable(arrowD);

    final int padding = getResources().getDimensionPixelSize(R.dimen.indicator_internal_padding);
    mArrowImageView.setPadding(padding, padding, padding, padding);
    addView(mArrowImageView);/* www. j a v a 2  s  .c  om*/

    int inAnimResId, outAnimResId;
    switch (mode) {
    case PULL_FROM_END:
        inAnimResId = R.anim.slide_in_from_bottom;
        outAnimResId = R.anim.slide_out_to_bottom;
        setBackgroundResource(R.drawable.indicator_bg_bottom);

        // Rotate Arrow so it's pointing the correct way
        mArrowImageView.setScaleType(ScaleType.MATRIX);
        Matrix matrix = new Matrix();
        matrix.setRotate(180f, arrowD.getIntrinsicWidth() / 2f, arrowD.getIntrinsicHeight() / 2f);
        mArrowImageView.setImageMatrix(matrix);
        break;
    default:
    case PULL_FROM_START:
        inAnimResId = R.anim.slide_in_from_top;
        outAnimResId = R.anim.slide_out_to_top;
        setBackgroundResource(R.drawable.indicator_bg_top);
        break;
    }

    mInAnim = AnimationUtils.loadAnimation(context, inAnimResId);
    mInAnim.setAnimationListener(this);

    mOutAnim = AnimationUtils.loadAnimation(context, outAnimResId);
    mOutAnim.setAnimationListener(this);

    final Interpolator interpolator = new LinearInterpolator();
    mRotateAnimation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    mRotateAnimation.setInterpolator(interpolator);
    mRotateAnimation.setDuration(DEFAULT_ROTATION_ANIMATION_DURATION);
    mRotateAnimation.setFillAfter(true);

    mResetRotateAnimation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    mResetRotateAnimation.setInterpolator(interpolator);
    mResetRotateAnimation.setDuration(DEFAULT_ROTATION_ANIMATION_DURATION);
    mResetRotateAnimation.setFillAfter(true);

}

From source file:de.vanita5.twittnuker.view.ShapedImageView.java

private void updateBorderShader() {
    final int[] colors = mBorderColors;
    if (colors == null || colors.length == 0) {
        mBorderPaint.setShader(null);//from  w w w.j  a va2 s . c o m
        return;
    }
    mDestination.set(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(),
            getHeight() - getPaddingBottom());
    final float cx = mDestination.centerX(), cy = mDestination.centerY();
    final int[] sweepColors = new int[colors.length * 2];
    final float[] positions = new float[colors.length * 2];
    for (int i = 0, j = colors.length; i < j; i++) {
        sweepColors[i * 2] = sweepColors[i * 2 + 1] = colors[i];
        positions[i * 2] = i == 0 ? 0 : i / (float) j;
        positions[i * 2 + 1] = i == j - 1 ? 1 : (i + 1) / (float) j;
    }
    final SweepGradient shader = new SweepGradient(cx, cy, sweepColors, positions);
    final Matrix matrix = new Matrix();
    matrix.setRotate(90, cx, cy);
    shader.setLocalMatrix(matrix);
    mBorderPaint.setShader(shader);
}

From source file:org.getlantern.firetweet.view.ShapedImageView.java

private void updateBorderShader() {
    final int[] colors = mBorderColors;
    if (colors == null || colors.length == 0) {
        mBorderAlpha = 0;//from   w ww  . j  av a 2s  .co m
        return;
    }
    mDestination.set(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(),
            getHeight() - getPaddingBottom());
    final float cx = mDestination.centerX(), cy = mDestination.centerY();
    final int[] sweepColors = new int[colors.length * 2];
    final float[] positions = new float[colors.length * 2];
    for (int i = 0, j = colors.length; i < j; i++) {
        sweepColors[i * 2] = sweepColors[i * 2 + 1] = colors[i];
        positions[i * 2] = i == 0 ? 0 : i / (float) j;
        positions[i * 2 + 1] = i == j - 1 ? 1 : (i + 1) / (float) j;
    }
    final SweepGradient shader = new SweepGradient(cx, cy, sweepColors, positions);
    final Matrix matrix = new Matrix();
    matrix.setRotate(90, cx, cy);
    shader.setLocalMatrix(matrix);
    mBorderPaint.setShader(shader);
}

From source file:com.scigames.registration.Registration4PhotoActivity.java

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST) {
        or.setVisibility(View.VISIBLE);
        retakeButton.setVisibility(View.VISIBLE);
        saveButton.setVisibility(View.VISIBLE);
        takePhotoButton.setVisibility(View.INVISIBLE);

        if (data.hasExtra("data")) {
            //change to new view
            photo = (Bitmap) data.getExtras().get("data");
            //photoUri = data.getExtras().get("data");
            Log.d(TAG, "photoDensity: ");
            Log.d(TAG, String.valueOf(photo.getDensity()));
            Log.d(TAG, "photo getHeight:");
            Log.d(TAG, String.valueOf(photo.getHeight()));
            Log.d(TAG, "photo getWidth");
            Log.d(TAG, String.valueOf(photo.getWidth()));
            Log.d(TAG, "photo config:");
            if (photo.getHeight() < photo.getWidth()) {
                Log.d(TAG, "height < width");

                Log.d(TAG, photo.getConfig().toString());
                photoToSend = Bitmap.createBitmap(120, 160, photo.getConfig());
                //photoToSend = Bitmap.createBitmap(photo);
                Canvas canvas = new Canvas(photoToSend);
                Log.d(TAG, "photo getScaledHeight:");
                Log.d(TAG, String.valueOf(photo.getScaledHeight(canvas)));
                Log.d(TAG, "photo getScaledWidth");
                Log.d(TAG, String.valueOf(photo.getScaledWidth(canvas)));
                Matrix matrix = new Matrix();
                //matrix.preScale(-1.0f, 1.0f);
                //Bitmap mirroredBitmap = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight());
                matrix.setRotate(270, photo.getWidth() / 2, photo.getHeight() / 2);

                //matrix.postTranslate(photo.getWidth(),photo.getHeight());
                Log.d(TAG, matrix.toString());
                canvas.drawBitmap(photo, matrix, new Paint());
            } else {
                Log.d(TAG, photo.getConfig().toString());
                photoToSend = Bitmap.createBitmap(120, 160, photo.getConfig());
                //photoToSend = Bitmap.createBitmap(photo); 
                Canvas canvas = new Canvas(photoToSend);
                Log.d(TAG, "photo getScaledHeight:");
                Log.d(TAG, String.valueOf(photo.getScaledHeight(canvas)));
                Log.d(TAG, "photo getScaledWidth");
                Log.d(TAG, String.valueOf(photo.getScaledWidth(canvas)));
                Matrix matrix = new Matrix();
                //matrix.preScale(-1.0f, 1.0f);
                //Bitmap mirroredBitmap = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight());
                //matrix.setRotate(0, photo.getWidth()/2, photo.getHeight()/2);
                //matrix.setScale(0.9f, 0.9f);
                Log.d(TAG, matrix.toString());
                //matrix.postTranslate(photo.getWidth(),photo.getHeight());

                canvas.drawBitmap(photo, matrix, new Paint());
                avatarPhoto.setScaleX(0.95f);
                avatarPhoto.setScaleY(0.95f);
                avatarPhoto.setX(180f);/*from   w w w  . j  a va 2  s. c  o m*/
                avatarPhoto.setY(440f);
            }
            avatarPhoto.setImageBitmap(photoToSend);

            Log.d(TAG, "photoToSendDensity: ");
            Log.d(TAG, String.valueOf(photoToSend.getDensity()));
            Log.d(TAG, "photoToSend getScaledHeight:");
            Log.d(TAG, String.valueOf(photoToSend.getHeight()));
            Log.d(TAG, "photoToSend getScaledWidth");
            Log.d(TAG, String.valueOf(photoToSend.getWidth()));
        } else {
            or.setVisibility(View.INVISIBLE);
            saveButton.setVisibility(View.INVISIBLE);
            retakeButton.setVisibility(View.INVISIBLE);
            takePhotoButton.setVisibility(View.VISIBLE);
            //instruction.setText("");
        }
    }
}