下面列出了android.graphics.drawable.BitmapDrawable#getIntrinsicHeight() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Get the Bitmap from the Drawable.
*
* If the Bitmap needed to be scaled up to account for density, BitmapDrawable
* handles this at draw time. But this class doesn't actually draw the Bitmap;
* it is just a holder for native code to access its SkBitmap. So this needs to
* get a version that is scaled to account for density.
*/
private Bitmap getBitmapFromDrawable(BitmapDrawable bitmapDrawable) {
Bitmap bitmap = bitmapDrawable.getBitmap();
final int scaledWidth = bitmapDrawable.getIntrinsicWidth();
final int scaledHeight = bitmapDrawable.getIntrinsicHeight();
if (scaledWidth == bitmap.getWidth() && scaledHeight == bitmap.getHeight()) {
return bitmap;
}
Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF dst = new RectF(0, 0, scaledWidth, scaledHeight);
Bitmap scaled = Bitmap.createBitmap(scaledWidth, scaledHeight, bitmap.getConfig());
Canvas canvas = new Canvas(scaled);
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(bitmap, src, dst, paint);
return scaled;
}
/**
* Constructor
*
* @param res {@link Resources} instance.
* @param defaultStateBitmap {@link Bitmap} of the default state.
* @param pressedOneDirectionStateBitmap {@link Bitmap} of the pressed state in one direction.
* @param pressedTwoDirectionsStateBitmap {@link Bitmap} of the pressed state in two direction.
* @param buttonUp Identifier for the up button.
* @param buttonDown Identifier for the down button.
* @param buttonLeft Identifier for the left button.
* @param buttonRight Identifier for the right button.
*/
public InputOverlayDrawableDpad(Resources res,
Bitmap defaultStateBitmap,
Bitmap pressedOneDirectionStateBitmap,
Bitmap pressedTwoDirectionsStateBitmap,
int buttonUp, int buttonDown,
int buttonLeft, int buttonRight)
{
mDefaultStateBitmap = new BitmapDrawable(res, defaultStateBitmap);
mPressedOneDirectionStateBitmap = new BitmapDrawable(res, pressedOneDirectionStateBitmap);
mPressedTwoDirectionsStateBitmap = new BitmapDrawable(res, pressedTwoDirectionsStateBitmap);
mWidth = mDefaultStateBitmap.getIntrinsicWidth();
mHeight = mDefaultStateBitmap.getIntrinsicHeight();
mButtonType[0] = buttonUp;
mButtonType[1] = buttonDown;
mButtonType[2] = buttonLeft;
mButtonType[3] = buttonRight;
}
/**
* make a drawable to a bitmap
* @param drawable drawable you want convert
* @return converted bitmap
*/
public static Bitmap drawableToBitmap(int size, Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
bitmap = bitmapDrawable.getBitmap();
if (bitmap != null && bitmap.getHeight() > 0) {
Matrix matrix = new Matrix();
float scaleHeight = size * 1.0f / bitmapDrawable.getIntrinsicHeight();
matrix.postScale(scaleHeight, scaleHeight);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return bitmap;
}
}
bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
/**
* Constructor
*
* @param res {@link Resources} instance.
* @param defaultStateBitmap {@link Bitmap} to use with the default state Drawable.
* @param pressedStateBitmap {@link Bitmap} to use with the pressed state Drawable.
* @param buttonType Identifier for this type of button.
*/
public InputOverlayDrawableButton(Resources res, Bitmap defaultStateBitmap,
Bitmap pressedStateBitmap, int buttonType)
{
mDefaultStateBitmap = new BitmapDrawable(res, defaultStateBitmap);
mPressedStateBitmap = new BitmapDrawable(res, pressedStateBitmap);
mButtonType = buttonType;
mWidth = mDefaultStateBitmap.getIntrinsicWidth();
mHeight = mDefaultStateBitmap.getIntrinsicHeight();
}
private void updateScale() {
Drawable d = getDrawable();
if(d instanceof BitmapDrawable) {
BitmapDrawable bd = (BitmapDrawable) d;
// Don't upscale if more than 2x larger
if (getWidth() > mMaxScale * bd.getIntrinsicWidth()
&& getHeight() > mMaxScale * bd.getIntrinsicHeight()) {
setScaleType(ScaleType.MATRIX);
Matrix trans = new Matrix();
Matrix scale = new Matrix();
trans.setTranslate((getWidth() - mMaxScale * bd.getIntrinsicWidth())/2, (getHeight() - mMaxScale * bd.getIntrinsicHeight())/2);
scale.setScale(mMaxScale, mMaxScale);
Matrix m = new Matrix();
if(isInEditMode()) {
// WTF? Edit mode consider inversed matrix??
m.setConcat(scale, trans);
}else {
m.setConcat(trans, scale);
}
setImageMatrix(m);
}else {
setScaleType(ScaleType.CENTER_CROP);
}
}
}
protected void setUp() throws Exception
{
super.setUp();
// map = new MapWidget(getContext(), "map", 11);
drawable1 = new BitmapDrawable(BitmapFactory.decodeResource(getContext().getResources(), R.drawable.presence_online));
drawable2 = new BitmapDrawable(BitmapFactory.decodeResource(getContext().getResources(), R.drawable.presence_online));
originalDrawableSize = new Size(drawable1.getIntrinsicWidth(), drawable1.getIntrinsicHeight());
// layer = map.createLayer(1);
scalableObject = new FakeMapObject("1", drawable1, 10, 10, 0, 0, true, true);
nonScalableObject = new FakeMapObject("2", drawable2, 100, 100, 0, 0, true, false);
}