forked from facebook/fresco
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
drawee/src/main/java/com/facebook/drawee/drawable/OrientedBitmapDrawable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
drawee/src/test/java/com/facebook/drawee/drawable/OrientedBitmapDrawableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |