Android Matrix - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android Matrix class.

This code example shows how to use the following methods:mapRect, postScale, postTranslate, preTranslate, reset
 
        // use a rectangle at 0,0 to make sure we don't run into issues with scaling 
        r.set(0, 0, w, h);
 
        final Matrix m = mTempMatrix;
        m.reset();
        transformMatrix(m, view);
        mTempMatrix.mapRect(r);
 
        r.offset(view.getLeft(), view.getTop());
 
        // Straighten coords if rotations flipped them 
        if (r.right < r.left) {
            final float f = r.right;
            r.right = r.left;
            r.left = f;
        } 
        if (r.bottom < r.top) {
            final float f = r.top;
            r.top = r.bottom;

8
CodeRank
This code example shows how to use the following methods:
 
        Bitmap bitmap2 = Robolectric.newInstanceOf(Bitmap.class);
        shadowOf(bitmap2).appendDescription("Bitmap Two");
 
        Canvas canvas = new Canvas(bitmap1);
        canvas.drawBitmap(bitmap2, new Matrix(), null);
 
        assertEquals("Bitmap One\nBitmap Two transformed by matrix", shadowOf(bitmap1).getDescription());
    } 
 
    @Test 
    public void shouldReceiveDescriptionWhenDrawABitmapToCanvasWithAPaintEffect() throws Exception { 
        Bitmap bitmap1 = Robolectric.newInstanceOf(Bitmap.class);
        shadowOf(bitmap1).appendDescription("Bitmap One");
 
        Bitmap bitmap2 = Robolectric.newInstanceOf(Bitmap.class);
        shadowOf(bitmap2).appendDescription("Bitmap Two");
 
        Canvas canvas = new Canvas(bitmap1);
        Paint paint = new Paint();
8
CodeRank
This code example shows how to use the following methods:createBitmap, postScale
			int bmpHeight = bitmap.getHeight();
 
			scaleWidth = (float) (scaleWidth * ZOOM_IN_SCALE);
			scaleHeight = (float) (scaleHeight * ZOOM_IN_SCALE);
 
			Matrix matrix = new Matrix();
			matrix.postScale(scaleWidth, scaleHeight);
			zoomedBMP = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight,
					matrix, true);
			imgView.setImageBitmap(zoomedBMP);
		} catch (Exception e) {
			// can't zoom because of memory issue, just ignore, no big deal 
		} 
	} 
} 
9
CodeRank
This code example shows how to use the following methods:postTranslate, setScale
This code example shows how to use the following methods:postRotate, setScale
             
            mMatrix = new Matrix();
            mMatrix.setRotate(-45, 0.0f, 0.0f);
            mMatrix.postTranslate(100.0f, 100.0f);
 
            mMatrix2 = new Matrix();
            mMatrix2.setScale(1.0f, 2.0f);
            mMatrix2.postRotate(-45, 0.0f, 0.0f);
 
            mMatrix3 = new Matrix();
            mMatrix3.setTranslate(100.0f, 100.0f);            
             
            mPaint = new Paint();
        } 
 
        @Override 
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawRGB(255, 255, 255);
 
8
CodeRank
This code example shows how to use the following methods:mapRect, postTranslate, setRotate
This code example shows how to use the following methods:createBitmap, postRotate, postScale
 
	public static Bitmap decode(final InputStream is, final BitmapFactory.Options opts) {
		if (is == null) return null;
		final int orientation = Exif.getOrientation(is);
		final Bitmap bm = BitmapFactory.decodeStream(is, null, opts);
		final Matrix m = new Matrix();
		switch (orientation) {
			case 270: 
			case 90: 
				m.postRotate(orientation);
				return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
			case 180: 
				m.postRotate(orientation);
				m.postScale(bm.getWidth(), bm.getHeight());
				return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
		} 
		return bm;
	} 
 
	public static Bitmap decode(final String path, final BitmapFactory.Options opts) {
8
CodeRank
This code example shows how to use the following methods:mapRect, postScale, postTranslate, reset
 
        // use a rectangle at 0,0 to make sure we don't run into issues with scaling 
        r.set(0, 0, w, h);
 
        final Matrix m = mTempMatrix;
        m.reset();
        transformMatrix(m, view);
        mTempMatrix.mapRect(r);
 
        r.offset(view.getLeft(), view.getTop());
 
        // Straighten coords if rotations flipped them 
        if (r.right < r.left) {
            final float f = r.right;
            r.right = r.left;
            r.left = f;
        } 
        if (r.bottom < r.top) {
            final float f = r.top;
            r.top = r.bottom;

8
CodeRank
This code example shows how to use the following methods:getValues, postTranslate
	} 
	 
	private boolean checkImageScrollBeyondBorders(float dx, float dy)
	{ 
		float[] matrixValues = new float[9];
		Matrix m = new Matrix(changeMatrix);
		// Apply the translation 
		m.postTranslate(-dx, -dy);
		m.getValues(matrixValues);
		// Image can move only the extra width or height that is available 
		// after scaling from the original width or height 
		float scaledAdditionalHeight = imageView.getHeight() * (matrixValues[4] - 1);
		float scaledAdditionalWidth = imageView.getWidth() * (matrixValues[0] - 1);
		if (matrixValues[5] > -scaledAdditionalHeight && matrixValues[5] < 0 && matrixValues[2] > -scaledAdditionalWidth
			&& matrixValues[2] < 0) {
			return false; 
		} 
		return true; 
	} 
} 
8
CodeRank
This code example shows how to use the following methods:createBitmap, postRotate, postScale
			Bitmap output;
 
			if ( rotation == 0 ) {
				output = Bitmap.createScaledBitmap( input, dstWidth, dstHeight, true );
			} else { 
				Matrix matrix = new Matrix();
				matrix.postScale( (float) dstWidth / srcWidth, (float) dstHeight / srcHeight );
				matrix.postRotate( rotation );
				output = Bitmap.createBitmap( input, 0, 0, srcWidth, srcHeight, matrix, true );
			} 
			return output;
		} else 
			return input;
	} 
 
} 
8
CodeRank

Related Android Matrix Questions & Answers:

Other Android Matrix Examples

13:	public class TouchImageView extends ImageView {
14:	
15:		Matrix matrix;
16:	
17:		// We can be in one of these 3 states

Read More

See Code Examples for Android Matrix Methods: