use of android.graphics.drawable.BitmapDrawable in project Transitions-Everywhere by andkulikov.
the class ChangeBounds method createAnimator.
@Override
public Animator createAnimator(final ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
if (startValues == null || endValues == null) {
return null;
}
if (sRectEvaluator == null) {
sRectEvaluator = new RectEvaluator();
}
Map<String, Object> startParentVals = startValues.values;
Map<String, Object> endParentVals = endValues.values;
ViewGroup startParent = (ViewGroup) startParentVals.get(PROPNAME_PARENT);
ViewGroup endParent = (ViewGroup) endParentVals.get(PROPNAME_PARENT);
if (startParent == null || endParent == null) {
return null;
}
final View view = endValues.view;
if (parentMatches(startParent, endParent)) {
Rect startBounds = (Rect) startValues.values.get(PROPNAME_BOUNDS);
Rect endBounds = (Rect) endValues.values.get(PROPNAME_BOUNDS);
final int startLeft = startBounds.left;
final int endLeft = endBounds.left;
final int startTop = startBounds.top;
final int endTop = endBounds.top;
final int startRight = startBounds.right;
final int endRight = endBounds.right;
final int startBottom = startBounds.bottom;
final int endBottom = endBounds.bottom;
final int startWidth = startRight - startLeft;
final int startHeight = startBottom - startTop;
final int endWidth = endRight - endLeft;
final int endHeight = endBottom - endTop;
Rect startClip = (Rect) startValues.values.get(PROPNAME_CLIP);
Rect endClip = (Rect) endValues.values.get(PROPNAME_CLIP);
int numChanges = 0;
if ((startWidth != 0 && startHeight != 0) || (endWidth != 0 && endHeight != 0)) {
if (startLeft != endLeft || startTop != endTop)
++numChanges;
if (startRight != endRight || startBottom != endBottom)
++numChanges;
}
if ((startClip != null && !startClip.equals(endClip)) || (startClip == null && endClip != null)) {
++numChanges;
}
if (numChanges > 0) {
Animator anim;
if (!mResizeClip || (startClip == null && endClip == null)) {
ViewUtils.setLeftTopRightBottom(view, startLeft, startTop, startRight, startBottom);
if (numChanges == 2) {
if (startWidth == endWidth && startHeight == endHeight) {
anim = AnimatorUtils.ofPointF(view, POSITION_PROPERTY, getPathMotion(), startLeft, startTop, endLeft, endTop);
} else {
ViewBounds viewBounds = new ViewBounds(view);
Animator topLeftAnimator = AnimatorUtils.ofPointF(viewBounds, TOP_LEFT_PROPERTY, getPathMotion(), startLeft, startTop, endLeft, endTop);
Animator bottomRightAnimator = AnimatorUtils.ofPointF(viewBounds, BOTTOM_RIGHT_PROPERTY, getPathMotion(), startRight, startBottom, endRight, endBottom);
AnimatorSet set = new AnimatorSet();
set.playTogether(topLeftAnimator, bottomRightAnimator);
set.addListener(viewBounds);
anim = set;
}
} else if (startLeft != endLeft || startTop != endTop) {
anim = AnimatorUtils.ofPointF(view, TOP_LEFT_ONLY_PROPERTY, getPathMotion(), startLeft, startTop, endLeft, endTop);
} else {
anim = AnimatorUtils.ofPointF(view, BOTTOM_RIGHT_ONLY_PROPERTY, getPathMotion(), startRight, startBottom, endRight, endBottom);
}
} else {
int maxWidth = Math.max(startWidth, endWidth);
int maxHeight = Math.max(startHeight, endHeight);
ViewUtils.setLeftTopRightBottom(view, startLeft, startTop, startLeft + maxWidth, startTop + maxHeight);
Animator positionAnimator = null;
if (startLeft != endLeft || startTop != endTop) {
positionAnimator = AnimatorUtils.ofPointF(view, POSITION_PROPERTY, getPathMotion(), startLeft, startTop, endLeft, endTop);
}
final Rect finalClip = endClip;
if (startClip == null) {
startClip = new Rect(0, 0, startWidth, startHeight);
}
if (endClip == null) {
endClip = new Rect(0, 0, endWidth, endHeight);
}
ObjectAnimator clipAnimator = null;
if (!startClip.equals(endClip)) {
ViewUtils.setClipBounds(view, startClip);
clipAnimator = ObjectAnimator.ofObject(view, ChangeClipBounds.VIEW_CLIP_BOUNDS, sRectEvaluator, startClip, endClip);
clipAnimator.addListener(new AnimatorListenerAdapter() {
private boolean mIsCanceled;
@Override
public void onAnimationCancel(Animator animation) {
mIsCanceled = true;
}
@Override
public void onAnimationEnd(Animator animation) {
if (!mIsCanceled) {
ViewUtils.setClipBounds(view, finalClip);
ViewUtils.setLeftTopRightBottom(view, endLeft, endTop, endRight, endBottom);
}
}
});
}
anim = TransitionUtils.mergeAnimators(positionAnimator, clipAnimator);
}
if (view.getParent() instanceof ViewGroup) {
final ViewGroup parent = (ViewGroup) view.getParent();
ViewGroupUtils.suppressLayout(parent, true);
TransitionListener transitionListener = new TransitionListenerAdapter() {
boolean mCanceled = false;
@Override
public void onTransitionCancel(Transition transition) {
ViewGroupUtils.suppressLayout(parent, false);
mCanceled = true;
}
@Override
public void onTransitionEnd(Transition transition) {
if (!mCanceled) {
ViewGroupUtils.suppressLayout(parent, false);
}
}
@Override
public void onTransitionPause(Transition transition) {
ViewGroupUtils.suppressLayout(parent, false);
}
@Override
public void onTransitionResume(Transition transition) {
ViewGroupUtils.suppressLayout(parent, true);
}
};
addListener(transitionListener);
}
return anim;
}
} else {
sceneRoot.getLocationInWindow(tempLocation);
int startX = (Integer) startValues.values.get(PROPNAME_WINDOW_X) - tempLocation[0];
int startY = (Integer) startValues.values.get(PROPNAME_WINDOW_Y) - tempLocation[1];
int endX = (Integer) endValues.values.get(PROPNAME_WINDOW_X) - tempLocation[0];
int endY = (Integer) endValues.values.get(PROPNAME_WINDOW_Y) - tempLocation[1];
// TODO: also handle size changes: check bounds and animate size changes
if (startX != endX || startY != endY) {
final int width = view.getWidth();
final int height = view.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
final BitmapDrawable drawable = new BitmapDrawable(sceneRoot.getContext().getResources(), bitmap);
drawable.setBounds(startX, startY, startX + width, startY + height);
Animator anim;
anim = AnimatorUtils.ofPointF(drawable, DRAWABLE_ORIGIN_PROPERTY, getPathMotion(), startX, startY, endX, endY);
if (anim != null) {
final float alpha = view.getAlpha();
view.setAlpha(0);
ViewOverlayUtils.addOverlay(sceneRoot, drawable);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
ViewOverlayUtils.removeOverlay(sceneRoot, drawable);
view.setAlpha(alpha);
}
});
}
return anim;
}
}
return null;
}
use of android.graphics.drawable.BitmapDrawable in project platform_frameworks_base by android.
the class PointerIcon method loadResource.
private void loadResource(Context context, Resources resources, @XmlRes int resourceId) {
final XmlResourceParser parser = resources.getXml(resourceId);
final int bitmapRes;
final float hotSpotX;
final float hotSpotY;
try {
XmlUtils.beginDocument(parser, "pointer-icon");
final TypedArray a = resources.obtainAttributes(parser, com.android.internal.R.styleable.PointerIcon);
bitmapRes = a.getResourceId(com.android.internal.R.styleable.PointerIcon_bitmap, 0);
hotSpotX = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotX, 0);
hotSpotY = a.getDimension(com.android.internal.R.styleable.PointerIcon_hotSpotY, 0);
a.recycle();
} catch (Exception ex) {
throw new IllegalArgumentException("Exception parsing pointer icon resource.", ex);
} finally {
parser.close();
}
if (bitmapRes == 0) {
throw new IllegalArgumentException("<pointer-icon> is missing bitmap attribute.");
}
Drawable drawable;
if (context == null) {
drawable = resources.getDrawable(bitmapRes);
} else {
drawable = context.getDrawable(bitmapRes);
}
if (drawable instanceof AnimationDrawable) {
// Extract animation frame bitmaps.
final AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
final int frames = animationDrawable.getNumberOfFrames();
drawable = animationDrawable.getFrame(0);
if (frames == 1) {
Log.w(TAG, "Animation icon with single frame -- simply treating the first " + "frame as a normal bitmap icon.");
} else {
// Assumes they have the exact duration.
mDurationPerFrame = animationDrawable.getDuration(0);
mBitmapFrames = new Bitmap[frames - 1];
final int width = drawable.getIntrinsicWidth();
final int height = drawable.getIntrinsicHeight();
for (int i = 1; i < frames; ++i) {
Drawable drawableFrame = animationDrawable.getFrame(i);
if (!(drawableFrame instanceof BitmapDrawable)) {
throw new IllegalArgumentException("Frame of an animated pointer icon " + "must refer to a bitmap drawable.");
}
if (drawableFrame.getIntrinsicWidth() != width || drawableFrame.getIntrinsicHeight() != height) {
throw new IllegalArgumentException("The bitmap size of " + i + "-th frame " + "is different. All frames should have the exact same size and " + "share the same hotspot.");
}
mBitmapFrames[i - 1] = ((BitmapDrawable) drawableFrame).getBitmap();
}
}
}
if (!(drawable instanceof BitmapDrawable)) {
throw new IllegalArgumentException("<pointer-icon> bitmap attribute must " + "refer to a bitmap drawable.");
}
// Set the properties now that we have successfully loaded the icon.
mBitmap = ((BitmapDrawable) drawable).getBitmap();
mHotSpotX = hotSpotX;
mHotSpotY = hotSpotY;
}
use of android.graphics.drawable.BitmapDrawable in project platform_frameworks_base by android.
the class WallpaperManager method getBuiltInDrawable.
/**
* Returns a drawable for the built-in static wallpaper of the specified type. Based on the
* parameters, the drawable can be cropped and scaled.
*
* @param outWidth The width of the returned drawable
* @param outWidth The height of the returned drawable
* @param scaleToFit If true, scale the wallpaper down rather than just cropping it
* @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
* 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
* @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
* 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
* @param which The {@code FLAG_*} identifier of a valid wallpaper type. Throws
* IllegalArgumentException if an invalid wallpaper is requested.
* @return A Drawable presenting the built-in default wallpaper image of the given type,
* or {@code null} if no default image of that type is defined on this device.
*/
public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit, float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which) {
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
throw new RuntimeException(new DeadSystemException());
}
if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
}
Resources resources = mContext.getResources();
horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment));
verticalAlignment = Math.max(0, Math.min(1, verticalAlignment));
InputStream wpStream = openDefaultWallpaper(mContext, which);
if (wpStream == null) {
if (DEBUG) {
Log.w(TAG, "default wallpaper stream " + which + " is null");
}
return null;
} else {
InputStream is = new BufferedInputStream(wpStream);
if (outWidth <= 0 || outHeight <= 0) {
Bitmap fullSize = BitmapFactory.decodeStream(is, null, null);
return new BitmapDrawable(resources, fullSize);
} else {
int inWidth;
int inHeight;
// Just measure this time through...
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
if (options.outWidth != 0 && options.outHeight != 0) {
inWidth = options.outWidth;
inHeight = options.outHeight;
} else {
Log.e(TAG, "default wallpaper dimensions are 0");
return null;
}
}
// Reopen the stream to do the full decode. We know at this point
// that openDefaultWallpaper() will return non-null.
is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
RectF cropRectF;
outWidth = Math.min(inWidth, outWidth);
outHeight = Math.min(inHeight, outHeight);
if (scaleToFit) {
cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight, horizontalAlignment, verticalAlignment);
} else {
float left = (inWidth - outWidth) * horizontalAlignment;
float right = left + outWidth;
float top = (inHeight - outHeight) * verticalAlignment;
float bottom = top + outHeight;
cropRectF = new RectF(left, top, right, bottom);
}
Rect roundedTrueCrop = new Rect();
cropRectF.roundOut(roundedTrueCrop);
if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) {
Log.w(TAG, "crop has bad values for full size image");
return null;
}
// See how much we're reducing the size of the image
int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth, roundedTrueCrop.height() / outHeight);
// Attempt to open a region decoder
BitmapRegionDecoder decoder = null;
try {
decoder = BitmapRegionDecoder.newInstance(is, true);
} catch (IOException e) {
Log.w(TAG, "cannot open region decoder for default wallpaper");
}
Bitmap crop = null;
if (decoder != null) {
// Do region decoding to get crop bitmap
BitmapFactory.Options options = new BitmapFactory.Options();
if (scaleDownSampleSize > 1) {
options.inSampleSize = scaleDownSampleSize;
}
crop = decoder.decodeRegion(roundedTrueCrop, options);
decoder.recycle();
}
if (crop == null) {
// BitmapRegionDecoder has failed, try to crop in-memory. We know at
// this point that openDefaultWallpaper() will return non-null.
is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
Bitmap fullSize = null;
BitmapFactory.Options options = new BitmapFactory.Options();
if (scaleDownSampleSize > 1) {
options.inSampleSize = scaleDownSampleSize;
}
fullSize = BitmapFactory.decodeStream(is, null, options);
if (fullSize != null) {
crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left, roundedTrueCrop.top, roundedTrueCrop.width(), roundedTrueCrop.height());
}
}
if (crop == null) {
Log.w(TAG, "cannot decode default wallpaper");
return null;
}
// Scale down if necessary
if (outWidth > 0 && outHeight > 0 && (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) {
Matrix m = new Matrix();
RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight());
RectF returnRect = new RectF(0, 0, outWidth, outHeight);
m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL);
Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(), (int) returnRect.height(), Bitmap.Config.ARGB_8888);
if (tmp != null) {
Canvas c = new Canvas(tmp);
Paint p = new Paint();
p.setFilterBitmap(true);
c.drawBitmap(crop, m, p);
crop = tmp;
}
}
return new BitmapDrawable(resources, crop);
}
}
}
use of android.graphics.drawable.BitmapDrawable in project platform_frameworks_base by android.
the class ApplicationPackageManager method getBadgedDrawable.
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable, Rect badgeLocation, boolean tryBadgeInPlace) {
final int badgedWidth = drawable.getIntrinsicWidth();
final int badgedHeight = drawable.getIntrinsicHeight();
final boolean canBadgeInPlace = tryBadgeInPlace && (drawable instanceof BitmapDrawable) && ((BitmapDrawable) drawable).getBitmap().isMutable();
final Bitmap bitmap;
if (canBadgeInPlace) {
bitmap = ((BitmapDrawable) drawable).getBitmap();
} else {
bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
if (!canBadgeInPlace) {
drawable.setBounds(0, 0, badgedWidth, badgedHeight);
drawable.draw(canvas);
}
if (badgeLocation != null) {
if (badgeLocation.left < 0 || badgeLocation.top < 0 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
throw new IllegalArgumentException("Badge location " + badgeLocation + " not in badged drawable bounds " + new Rect(0, 0, badgedWidth, badgedHeight));
}
badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
canvas.save();
canvas.translate(badgeLocation.left, badgeLocation.top);
badgeDrawable.draw(canvas);
canvas.restore();
} else {
badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
badgeDrawable.draw(canvas);
}
if (!canBadgeInPlace) {
BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
}
return mergedDrawable;
}
return drawable;
}
use of android.graphics.drawable.BitmapDrawable in project platform_frameworks_base by android.
the class ProgressBar method tileify.
/**
* Converts a drawable to a tiled version of itself. It will recursively
* traverse layer and state list drawables.
*/
private Drawable tileify(Drawable drawable, boolean clip) {
if (drawable instanceof LayerDrawable) {
final LayerDrawable orig = (LayerDrawable) drawable;
final int N = orig.getNumberOfLayers();
final Drawable[] outDrawables = new Drawable[N];
for (int i = 0; i < N; i++) {
final int id = orig.getId(i);
outDrawables[i] = tileify(orig.getDrawable(i), (id == R.id.progress || id == R.id.secondaryProgress));
}
final LayerDrawable clone = new LayerDrawable(outDrawables);
for (int i = 0; i < N; i++) {
clone.setId(i, orig.getId(i));
clone.setLayerGravity(i, orig.getLayerGravity(i));
clone.setLayerWidth(i, orig.getLayerWidth(i));
clone.setLayerHeight(i, orig.getLayerHeight(i));
clone.setLayerInsetLeft(i, orig.getLayerInsetLeft(i));
clone.setLayerInsetRight(i, orig.getLayerInsetRight(i));
clone.setLayerInsetTop(i, orig.getLayerInsetTop(i));
clone.setLayerInsetBottom(i, orig.getLayerInsetBottom(i));
clone.setLayerInsetStart(i, orig.getLayerInsetStart(i));
clone.setLayerInsetEnd(i, orig.getLayerInsetEnd(i));
}
return clone;
}
if (drawable instanceof StateListDrawable) {
final StateListDrawable in = (StateListDrawable) drawable;
final StateListDrawable out = new StateListDrawable();
final int N = in.getStateCount();
for (int i = 0; i < N; i++) {
out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
}
return out;
}
if (drawable instanceof BitmapDrawable) {
final Drawable.ConstantState cs = drawable.getConstantState();
final BitmapDrawable clone = (BitmapDrawable) cs.newDrawable(getResources());
clone.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
if (mSampleWidth <= 0) {
mSampleWidth = clone.getIntrinsicWidth();
}
if (clip) {
return new ClipDrawable(clone, Gravity.LEFT, ClipDrawable.HORIZONTAL);
} else {
return clone;
}
}
return drawable;
}
Aggregations