Skip to content

Commit

Permalink
Added OrientedBitmapDrawable class
Browse files Browse the repository at this point in the history
  • Loading branch information
lukkm authored and tyronen committed Jul 20, 2015
1 parent aa8686e commit 9d7cced
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.drawee.drawable;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;

import com.facebook.common.internal.Preconditions;
import com.facebook.common.internal.VisibleForTesting;

/**
* Drawable that automatically rotates the underlying drawable with a pivot in the center of the
* drawable bounds based on a rotation angle.
*/
public class OrientedBitmapDrawable extends BitmapDrawable {

private static final int UNKNOWN_ROTATION_ANGLE = -1;

@VisibleForTesting final Matrix mRotationMatrix;
private int mRotationAngle;

/**
* Creates a new OrientedBitmapDrawable. The only rotation angles allowed are multiples of 90 or
* -1 if the angle is unknown.
*/
public OrientedBitmapDrawable(Resources res, Bitmap bitmap, int rotationAngle) {
super(res, bitmap);
Preconditions.checkArgument(rotationAngle == UNKNOWN_ROTATION_ANGLE || rotationAngle % 90 == 0);
mRotationMatrix = new Matrix();
mRotationAngle = rotationAngle;
}

@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
if (mRotationAngle <= 0) {
super.draw(canvas);
return;
}
mRotationMatrix.setRotate(mRotationAngle, bounds.centerX(), bounds.centerY());
int saveCount = canvas.save();
canvas.concat(mRotationMatrix);
super.draw(canvas);
canvas.restoreToCount(saveCount);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.drawee.drawable;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;

/**
* Tests for {@link OrientedBitmapDrawable}
*/
@RunWith(RobolectricTestRunner.class)
public class OrientedBitmapDrawableTest {

private Resources mResources;
private Bitmap mBitmap;
private Canvas mCanvas;

@Before
public void setUp() {
mResources = Resources.getSystem();
mBitmap = mock(Bitmap.class);
mCanvas = mock(Canvas.class);
}

@Test
public void testCreation_invalidAngle() {
try {
new OrientedBitmapDrawable(mResources, mBitmap, 20);
fail();
} catch (IllegalArgumentException e) {
// Do nothing, expected.
}
}

@Test
public void testCreation_unknownAngle() {
OrientedBitmapDrawable drawable = new OrientedBitmapDrawable(mResources, mBitmap, -1);
drawable.draw(mCanvas);
assertTrue(drawable.mRotationMatrix.isIdentity());
}

@Test
public void testCreation_zeroDegrees() {
OrientedBitmapDrawable drawable = new OrientedBitmapDrawable(mResources, mBitmap, 0);
drawable.draw(mCanvas);
assertTrue(drawable.mRotationMatrix.isIdentity());

}

@Test
public void testCreation_nintyDegrees() {
OrientedBitmapDrawable drawable = new OrientedBitmapDrawable(mResources, mBitmap, 90);
drawable.draw(mCanvas);

Matrix expectedMatrix = new Matrix();
expectedMatrix.setRotate(90, drawable.getBounds().centerX(), drawable.getBounds().centerY());
assertFalse(drawable.mRotationMatrix.isIdentity());
AndroidGraphicsTestUtils.assertEquals(expectedMatrix, drawable.mRotationMatrix);
}

@Test
public void testCreation_hundredAndEightyDegrees() {
OrientedBitmapDrawable drawable = new OrientedBitmapDrawable(mResources, mBitmap, 180);
drawable.draw(mCanvas);

Matrix expectedMatrix = new Matrix();
expectedMatrix.setRotate(180, drawable.getBounds().centerX(), drawable.getBounds().centerY());
assertFalse(drawable.mRotationMatrix.isIdentity());
AndroidGraphicsTestUtils.assertEquals(expectedMatrix, drawable.mRotationMatrix);
}

@Test
public void testCreation_twoHundredAndSeventyDegrees() {
OrientedBitmapDrawable drawable = new OrientedBitmapDrawable(mResources, mBitmap, 270);
drawable.draw(mCanvas);

Matrix expectedMatrix = new Matrix();
expectedMatrix.setRotate(270, drawable.getBounds().centerX(), drawable.getBounds().centerY());
assertFalse(drawable.mRotationMatrix.isIdentity());
AndroidGraphicsTestUtils.assertEquals(expectedMatrix, drawable.mRotationMatrix);
}

}

0 comments on commit 9d7cced

Please sign in to comment.