Example usage for android.graphics Matrix preTranslate

List of usage examples for android.graphics Matrix preTranslate

Introduction

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

Prototype

public boolean preTranslate(float dx, float dy) 

Source Link

Document

Preconcats the matrix with the specified translation.

Usage

From source file:Main.java

public static Bitmap rotateWithCanvas(Bitmap bitmap, int degrees) {

    int destWidth, destHeight;

    float centerX = bitmap.getWidth() / 2;
    float centerY = bitmap.getHeight() / 2;

    // We want to do the rotation at origin, but since the bounding
    // rectangle will be changed after rotation, so the delta values
    // are based on old & new width/height respectively.
    Matrix matrix = new Matrix();
    matrix.preTranslate(-centerX, -centerY);
    matrix.postRotate(degrees);//  ww w  .  j  a  va  2 s .c om
    if (degrees / 90 % 2 == 0) {
        destWidth = bitmap.getWidth();
        destHeight = bitmap.getHeight();
        matrix.postTranslate(centerX, centerY);
    } else {
        destWidth = bitmap.getHeight();
        destHeight = bitmap.getWidth();
        matrix.postTranslate(centerY, centerX);
    }
    Bitmap cropped = Bitmap.createBitmap(destWidth, destHeight, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(cropped);
    canvas.drawBitmap(bitmap, matrix, null);
    return cropped;
}

From source file:Main.java

public static Matrix getRotateMatrix(Bitmap bitmap, int rotation) {
    Matrix matrix = new Matrix();
    if (bitmap != null && rotation != 0) {
        int cx = bitmap.getWidth() / 2;
        int cy = bitmap.getHeight() / 2;
        matrix.preTranslate(-cx, -cy);
        matrix.postRotate(rotation);/*from  w  w  w.jav a2s . c om*/
        matrix.postTranslate(cx, cy);
    }
    return matrix;
}

From source file:Main.java

static void offsetDescendantMatrix(ViewParent target, View view, Matrix m) {
    final ViewParent parent = view.getParent();
    if (parent instanceof View && parent != target) {
        final View vp = (View) parent;
        offsetDescendantMatrix(target, vp, m);
        m.preTranslate(-vp.getScrollX(), -vp.getScrollY());
    }//w w w .  jav a  2  s.  c  o m

    m.preTranslate(view.getLeft(), view.getTop());

    if (!view.getMatrix().isIdentity()) {
        m.preConcat(view.getMatrix());
    }
}

From source file:Main.java

public static Bitmap rotateBitmapTranslate(Bitmap bitmap, float degrees) {
    Bitmap mBitmap = null;// www . j  a v  a2s  .  co  m
    int width;
    int height;
    try {
        Matrix matrix = new Matrix();
        if ((degrees / 90) % 2 != 0) {
            width = bitmap.getWidth();
            height = bitmap.getHeight();
        } else {
            width = bitmap.getHeight();
            height = bitmap.getWidth();
        }
        int cx = width / 2;
        int cy = height / 2;
        matrix.preTranslate(-cx, -cy);
        matrix.postRotate(degrees);
        matrix.postTranslate(cx, cy);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mBitmap;
}

From source file:android.support.transition.ChangeTransform.java

private void captureValues(TransitionValues transitionValues) {
    View view = transitionValues.view;
    if (view.getVisibility() == View.GONE) {
        return;//from  ww w.  j a  v a  2  s  .c  o m
    }
    transitionValues.values.put(PROPNAME_PARENT, view.getParent());
    Transforms transforms = new Transforms(view);
    transitionValues.values.put(PROPNAME_TRANSFORMS, transforms);
    Matrix matrix = view.getMatrix();
    if (matrix == null || matrix.isIdentity()) {
        matrix = null;
    } else {
        matrix = new Matrix(matrix);
    }
    transitionValues.values.put(PROPNAME_MATRIX, matrix);
    if (mReparent) {
        Matrix parentMatrix = new Matrix();
        ViewGroup parent = (ViewGroup) view.getParent();
        ViewUtils.transformMatrixToGlobal(parent, parentMatrix);
        parentMatrix.preTranslate(-parent.getScrollX(), -parent.getScrollY());
        transitionValues.values.put(PROPNAME_PARENT_MATRIX, parentMatrix);
        transitionValues.values.put(PROPNAME_INTERMEDIATE_MATRIX, view.getTag(R.id.transition_transform));
        transitionValues.values.put(PROPNAME_INTERMEDIATE_PARENT_MATRIX, view.getTag(R.id.parent_matrix));
    }
}

From source file:github.madmarty.madsonic.util.ImageLoader.java

private Bitmap createReflection(Bitmap originalImage) {

    //   int reflectionH = 80;

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

    // Height of reflection
    int reflectionHeight = height / 2;

    // The gap we want between the reflection and the original image
    final int reflectionGap = 4;

    // Create a new bitmap with same width but taller to fit reflection
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + reflectionHeight),
            Bitmap.Config.ARGB_8888);// www .j a  v  a  2 s  . c  o  m

    //// ----

    Bitmap reflection = Bitmap.createBitmap(width, reflectionHeight, Bitmap.Config.ARGB_8888);
    Bitmap blurryBitmap = Bitmap.createBitmap(originalImage, 0, height - reflectionHeight, height,
            reflectionHeight);

    // cheap and easy scaling algorithm; down-scale it, then
    // upscale it. The filtering during the scale operations
    // will blur the resulting image
    blurryBitmap = Bitmap
            .createScaledBitmap(
                    Bitmap.createScaledBitmap(blurryBitmap, blurryBitmap.getWidth() / 2,
                            blurryBitmap.getHeight() / 2, true),
                    blurryBitmap.getWidth(), blurryBitmap.getHeight(), true);

    // This shadier will hold a cropped, inverted,
    // blurry version of the original image
    BitmapShader bitmapShader = new BitmapShader(blurryBitmap, TileMode.CLAMP, TileMode.CLAMP);
    Matrix invertMatrix = new Matrix();
    invertMatrix.setScale(1f, -1f);
    invertMatrix.preTranslate(0, -reflectionHeight);
    bitmapShader.setLocalMatrix(invertMatrix);

    // This shader holds an alpha gradient
    Shader alphaGradient = new LinearGradient(0, 0, 0, reflectionHeight, 0x80ffffff, 0x00000000,
            TileMode.CLAMP);

    // This shader combines the previous two, resulting in a
    // blurred, fading reflection
    ComposeShader compositor = new ComposeShader(bitmapShader, alphaGradient, PorterDuff.Mode.DST_IN);

    Paint reflectionPaint = new Paint();
    reflectionPaint.setShader(compositor);

    // Draw the reflection into the bitmap that we will return
    Canvas canvas = new Canvas(reflection);
    canvas.drawRect(0, 0, reflection.getWidth(), reflection.getHeight(), reflectionPaint);

    /// -----

    // Create a new Canvas with the bitmap that's big enough for
    // the image plus gap plus reflection
    Canvas finalcanvas = new Canvas(bitmapWithReflection);

    // Draw in the original image
    finalcanvas.drawBitmap(originalImage, 0, 0, null);

    // Draw in the gap
    Paint defaultPaint = new Paint();

    // transparent gap
    defaultPaint.setColor(0);

    finalcanvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);

    // Draw in the reflection
    finalcanvas.drawBitmap(reflection, 0, height + reflectionGap, null);

    return bitmapWithReflection;
}

From source file:com.landenlabs.all_UiDemo.frag.ImageScalesFrag.java

private Bitmap fillLowerLeft(final Drawable drawable, ImageView imageView, Bitmap prevBm) {

    // Compute  matrix to fill viewer with drawable starting with upper left
    // Stretching till 2nd edge is crossed (filling screen) with correct aspect ratio.
    Matrix m = new Matrix();
    m.reset();/*from   ww w .  ja v  a 2s .  c o  m*/
    int imgWidth = drawable.getIntrinsicWidth();
    int imgHeight = drawable.getIntrinsicHeight();
    int viewWidth = imageView.getWidth();
    int viewHeight = imageView.getHeight();
    float xScale = (float) viewWidth / imgWidth;
    float yScale = (float) viewHeight / imgHeight;
    float maxScale = Math.max(xScale, yScale);

    float dy = (imgHeight * maxScale - viewHeight) / maxScale;
    // dy = imgHeight / 2;
    m.preTranslate(0, -dy);
    m.postScale(maxScale, maxScale);

    imageView.setScaleType(ImageView.ScaleType.MATRIX);
    imageView.setImageMatrix(m);
    imageView.setImageDrawable(drawable);
    return prevBm;
}

From source file:de.tlabs.ssr.g1.client.SourcesView.java

private void recalculateViewportTransformation(Matrix dstMatrix, float centerRotation, float scaling,
        float[] translation) {
    dstMatrix.reset();/*  w ww . j  av  a 2 s . c  om*/
    // translate to center
    dstMatrix.preTranslate((float) getWidth() / 2.0f, (float) getHeight() / 2.0f);
    // rotate around center
    dstMatrix.preRotate(-centerRotation); // negative rotation, because y axis not yet inverted
    // translate
    dstMatrix.preTranslate(translation[0], translation[1]);
    // scale and invert y axis
    dstMatrix.preScale(scaling, -scaling);
    // rotate to look north always (instead of east)
    dstMatrix.preRotate(90.0f);
}

From source file:com.inmobi.nativead.sample.photopages.CustomViewPager.java

@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
    // We can cast here because CustomPagerAdapter only creates wrappers.
    CustomViewPagerItemWrapper item = (CustomViewPagerItemWrapper) child;

    // Since Jelly Bean children won't get invalidated automatically,
    // needs to be added for the smooth coverflow animation
    if (android.os.Build.VERSION.SDK_INT >= 16) {
        item.invalidate();// ww  w.ja v  a 2  s  .c om
    }

    final int coverFlowWidth = this.getWidth();
    final int coverFlowCenter = coverFlowWidth / 2;
    final int childWidth = item.getWidth();
    final int childHeight = item.getHeight();
    final int childCenter = item.getLeft() + childWidth / 2;

    // Use coverflow width when its defined as automatic.
    final int actionDistance = (this.actionDistance == ACTION_DISTANCE_AUTO)
            ? (int) ((coverFlowWidth + childWidth) / 2.0f)
            : this.actionDistance;

    // Calculate the abstract amount for all effects.
    final float effectsAmount = Math.min(1.0f,
            Math.max(-1.0f, (1.0f / actionDistance) * (childCenter - coverFlowCenter)));

    // Clear previous transformations and set transformation type (matrix + alpha).
    t.clear();
    t.setTransformationType(Transformation.TYPE_BOTH);

    // Alpha
    if (this.unselectedAlpha != 1) {
        final float alphaAmount = (this.unselectedAlpha - 1) * Math.abs(effectsAmount) + 1;
        t.setAlpha(alphaAmount);
    }

    // Saturation
    if (this.unselectedSaturation != 1) {
        // Pass over saturation to the wrapper.
        final float saturationAmount = (this.unselectedSaturation - 1) * Math.abs(effectsAmount) + 1;
        item.setSaturation(saturationAmount);
    }

    final Matrix imageMatrix = t.getMatrix();

    // Apply rotation.
    if (this.maxRotation != 0) {
        final int rotationAngle = (int) (-effectsAmount * this.maxRotation);
        this.transformationCamera.save();
        this.transformationCamera.rotateY(rotationAngle);
        this.transformationCamera.getMatrix(imageMatrix);
        this.transformationCamera.restore();
    }

    // Zoom.
    if (this.unselectedScale != 1) {
        final float zoomAmount = (this.unselectedScale - 1) * Math.abs(effectsAmount) + 1;
        // Calculate the scale anchor (y anchor can be altered)
        final float translateX = childWidth / 2.0f;
        final float translateY = childHeight * this.scaleDownGravity;
        imageMatrix.preTranslate(-translateX, -translateY);
        imageMatrix.postScale(zoomAmount, zoomAmount);
        imageMatrix.postTranslate(translateX, translateY);
    }

    return true;
}

From source file:com.example.PagerCoverFlow.PagerContainer.java

private void transformImageBitmap(ImageView child, Matrix imageMatrix, int rotationAngle) {
    mCamera.save();/* w w w.j  a va2 s .  c  o  m*/
    //        final Matrix imageMatrix = t.getMatrix();;
    final int imageHeight = child.getLayoutParams().height;
    final int imageWidth = child.getLayoutParams().width;
    final int rotation = Math.abs(rotationAngle);

    //      mCamera.translate(0.0f, 0.0f, 100.0f);
    Log.i("Select", "imageWidth:" + imageWidth + " " + "imageHeight:" + imageHeight);
    //As the angle of the view gets less, zoom in
    //      if ( rotation < mMaxRotationAngle )
    {
        float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
        Log.i("Select", "rotation:" + rotation + " zoomAmount:" + zoomAmount);
        mCamera.translate(0.0f, 0.0f, zoomAmount);
    }

    //Alpha
    int alphaVal = 255 - rotation * 3;
    //      child.setAlpha(alphaVal); //[0,255]
    child.getDrawable().setAlpha(alphaVal);
    //      mCamera.rotateY(rotationAngle);
    mCamera.getMatrix(imageMatrix);
    imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
    imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
    mCamera.restore();

    float[] values = new float[9];
    imageMatrix.getValues(values);
    float globalX = values[Matrix.MTRANS_X];
    float globalY = values[Matrix.MTRANS_Y];
    //      float width = values[Matrix.MSCALE_X]*CommonValue.menuWidth ;
    //      float height = values[Matrix.MSCALE_Y]*CommonValue.menuWidth;
    values[Matrix.MSCALE_X] = 2;
    values[Matrix.MSCALE_Y] = 2;
    imageMatrix.setValues(values);
}