use of android.graphics.Canvas in project platform_frameworks_base by android.
the class BitmapUtils method resizeBitmapByScale.
public static Bitmap resizeBitmapByScale(Bitmap bitmap, float scale, boolean recycle) {
int width = Math.round(bitmap.getWidth() * scale);
int height = Math.round(bitmap.getHeight() * scale);
if (width == bitmap.getWidth() && height == bitmap.getHeight())
return bitmap;
Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
Canvas canvas = new Canvas(target);
canvas.scale(scale, scale);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
canvas.drawBitmap(bitmap, 0, 0, paint);
if (recycle)
bitmap.recycle();
return target;
}
use of android.graphics.Canvas in project platform_frameworks_base by android.
the class BitmapUtils method resizeAndCropCenter.
public static Bitmap resizeAndCropCenter(Bitmap bitmap, int size, boolean recycle) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
if (w == size && h == size)
return bitmap;
// scale the image so that the shorter side equals to the target;
// the longer side will be center-cropped.
float scale = (float) size / Math.min(w, h);
Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
int width = Math.round(scale * bitmap.getWidth());
int height = Math.round(scale * bitmap.getHeight());
Canvas canvas = new Canvas(target);
canvas.translate((size - width) / 2f, (size - height) / 2f);
canvas.scale(scale, scale);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
canvas.drawBitmap(bitmap, 0, 0, paint);
if (recycle)
bitmap.recycle();
return target;
}
use of android.graphics.Canvas in project platform_frameworks_base by android.
the class Watermark method drawIfNeeded.
void drawIfNeeded() {
if (mDrawNeeded) {
final int dw = mLastDW;
final int dh = mLastDH;
mDrawNeeded = false;
Rect dirty = new Rect(0, 0, dw, dh);
Canvas c = null;
try {
c = mSurface.lockCanvas(dirty);
} catch (IllegalArgumentException e) {
} catch (Surface.OutOfResourcesException e) {
}
if (c != null) {
c.drawColor(0, PorterDuff.Mode.CLEAR);
int deltaX = mDeltaX;
int deltaY = mDeltaY;
// deltaX shouldn't be close to a round fraction of our
// x step, or else things will line up too much.
int div = (dw + mTextWidth) / deltaX;
int rem = (dw + mTextWidth) - (div * deltaX);
int qdelta = deltaX / 4;
if (rem < qdelta || rem > (deltaX - qdelta)) {
deltaX += deltaX / 3;
}
int y = -mTextHeight;
int x = -mTextWidth;
while (y < (dh + mTextHeight)) {
c.drawText(mText, x, y, mTextPaint);
x += deltaX;
if (x >= dw) {
x -= (dw + mTextWidth);
y += deltaY;
}
}
mSurface.unlockCanvasAndPost(c);
}
}
}
use of android.graphics.Canvas in project platform_frameworks_base by android.
the class PageAdapter method updatePreviewAreaPageSizeAndEmptyState.
private void updatePreviewAreaPageSizeAndEmptyState() {
if (mMediaSize == null) {
return;
}
final int availableWidth = mPreviewArea.getWidth();
final int availableHeight = mPreviewArea.getHeight();
// Page aspect ratio to keep.
final float pageAspectRatio = (float) mMediaSize.getWidthMils() / mMediaSize.getHeightMils();
// Make sure we have no empty columns.
final int columnCount = Math.min(mSelectedPageCount, mColumnCount);
mPreviewArea.setColumnCount(columnCount);
// Compute max page width.
final int horizontalMargins = 2 * columnCount * mPreviewPageMargin;
final int horizontalPaddingAndMargins = horizontalMargins + 2 * mPreviewListPadding;
final int pageContentDesiredWidth = (int) ((((float) availableWidth - horizontalPaddingAndMargins) / columnCount) + 0.5f);
// Compute max page height.
final int pageContentDesiredHeight = (int) ((pageContentDesiredWidth / pageAspectRatio) + 0.5f);
// If the page does not fit entirely in a vertical direction,
// we shirk it but not less than the minimal page width.
final int pageContentMinHeight = (int) (mPreviewPageMinWidth / pageAspectRatio + 0.5f);
final int pageContentMaxHeight = Math.max(pageContentMinHeight, availableHeight - 2 * (mPreviewListPadding + mPreviewPageMargin) - mFooterHeight);
mPageContentHeight = Math.min(pageContentDesiredHeight, pageContentMaxHeight);
mPageContentWidth = (int) ((mPageContentHeight * pageAspectRatio) + 0.5f);
final int totalContentWidth = columnCount * mPageContentWidth + horizontalMargins;
final int horizontalPadding = (availableWidth - totalContentWidth) / 2;
final int rowCount = mSelectedPageCount / columnCount + ((mSelectedPageCount % columnCount) > 0 ? 1 : 0);
final int totalContentHeight = rowCount * (mPageContentHeight + mFooterHeight + 2 * mPreviewPageMargin);
final int verticalPadding;
if (mPageContentHeight + mFooterHeight + mPreviewListPadding + 2 * mPreviewPageMargin > availableHeight) {
verticalPadding = Math.max(0, (availableHeight - mPageContentHeight - mFooterHeight) / 2 - mPreviewPageMargin);
} else {
verticalPadding = Math.max(mPreviewListPadding, (availableHeight - totalContentHeight) / 2);
}
mPreviewArea.setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);
// Now update the empty state drawable, as it depends on the page
// size and is reused for all views for better performance.
LayoutInflater inflater = LayoutInflater.from(mContext);
View loadingContent = inflater.inflate(R.layout.preview_page_loading, null, false);
loadingContent.measure(MeasureSpec.makeMeasureSpec(mPageContentWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mPageContentHeight, MeasureSpec.EXACTLY));
loadingContent.layout(0, 0, loadingContent.getMeasuredWidth(), loadingContent.getMeasuredHeight());
Bitmap loadingBitmap = Bitmap.createBitmap(mPageContentWidth, mPageContentHeight, Bitmap.Config.ARGB_8888);
loadingContent.draw(new Canvas(loadingBitmap));
// Do not recycle the old bitmap if such as it may be set as an empty
// state to any of the page views. Just let the GC take care of it.
mEmptyState = new BitmapDrawable(mContext.getResources(), loadingBitmap);
// Now update the empty state drawable, as it depends on the page
// size and is reused for all views for better performance.
View errorContent = inflater.inflate(R.layout.preview_page_error, null, false);
errorContent.measure(MeasureSpec.makeMeasureSpec(mPageContentWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mPageContentHeight, MeasureSpec.EXACTLY));
errorContent.layout(0, 0, errorContent.getMeasuredWidth(), errorContent.getMeasuredHeight());
Bitmap errorBitmap = Bitmap.createBitmap(mPageContentWidth, mPageContentHeight, Bitmap.Config.ARGB_8888);
errorContent.draw(new Canvas(errorBitmap));
// Do not recycle the old bitmap if such as it may be set as an error
// state to any of the page views. Just let the GC take care of it.
mErrorState = new BitmapDrawable(mContext.getResources(), errorBitmap);
}
use of android.graphics.Canvas in project platform_frameworks_base by android.
the class ChangeBounds method createAnimator.
@Override
public Animator createAnimator(final ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
if (startValues == null || endValues == null) {
return null;
}
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) {
view.setLeftTopRightBottom(startLeft, startTop, startRight, startBottom);
if (numChanges == 2) {
if (startWidth == endWidth && startHeight == endHeight) {
Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop);
anim = ObjectAnimator.ofObject(view, POSITION_PROPERTY, null, topLeftPath);
} else {
final ViewBounds viewBounds = new ViewBounds(view);
Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop);
ObjectAnimator topLeftAnimator = ObjectAnimator.ofObject(viewBounds, TOP_LEFT_PROPERTY, null, topLeftPath);
Path bottomRightPath = getPathMotion().getPath(startRight, startBottom, endRight, endBottom);
ObjectAnimator bottomRightAnimator = ObjectAnimator.ofObject(viewBounds, BOTTOM_RIGHT_PROPERTY, null, bottomRightPath);
AnimatorSet set = new AnimatorSet();
set.playTogether(topLeftAnimator, bottomRightAnimator);
anim = set;
set.addListener(new AnimatorListenerAdapter() {
// We need a strong reference to viewBounds until the
// animator ends.
private ViewBounds mViewBounds = viewBounds;
});
}
} else if (startLeft != endLeft || startTop != endTop) {
Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop);
anim = ObjectAnimator.ofObject(view, TOP_LEFT_ONLY_PROPERTY, null, topLeftPath);
} else {
Path bottomRight = getPathMotion().getPath(startRight, startBottom, endRight, endBottom);
anim = ObjectAnimator.ofObject(view, BOTTOM_RIGHT_ONLY_PROPERTY, null, bottomRight);
}
} else {
int maxWidth = Math.max(startWidth, endWidth);
int maxHeight = Math.max(startHeight, endHeight);
view.setLeftTopRightBottom(startLeft, startTop, startLeft + maxWidth, startTop + maxHeight);
ObjectAnimator positionAnimator = null;
if (startLeft != endLeft || startTop != endTop) {
Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop);
positionAnimator = ObjectAnimator.ofObject(view, POSITION_PROPERTY, null, topLeftPath);
}
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)) {
view.setClipBounds(startClip);
clipAnimator = ObjectAnimator.ofObject(view, "clipBounds", 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) {
view.setClipBounds(finalClip);
view.setLeftTopRightBottom(endLeft, endTop, endRight, endBottom);
}
}
});
}
anim = TransitionUtils.mergeAnimators(positionAnimator, clipAnimator);
}
if (view.getParent() instanceof ViewGroup) {
final ViewGroup parent = (ViewGroup) view.getParent();
parent.suppressLayout(true);
TransitionListener transitionListener = new TransitionListenerAdapter() {
boolean mCanceled = false;
@Override
public void onTransitionCancel(Transition transition) {
parent.suppressLayout(false);
mCanceled = true;
}
@Override
public void onTransitionEnd(Transition transition) {
if (!mCanceled) {
parent.suppressLayout(false);
}
}
@Override
public void onTransitionPause(Transition transition) {
parent.suppressLayout(false);
}
@Override
public void onTransitionResume(Transition transition) {
parent.suppressLayout(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(bitmap);
drawable.setBounds(startX, startY, startX + width, startY + height);
final float transitionAlpha = view.getTransitionAlpha();
view.setTransitionAlpha(0);
sceneRoot.getOverlay().add(drawable);
Path topLeftPath = getPathMotion().getPath(startX, startY, endX, endY);
PropertyValuesHolder origin = PropertyValuesHolder.ofObject(DRAWABLE_ORIGIN_PROPERTY, null, topLeftPath);
ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(drawable, origin);
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
sceneRoot.getOverlay().remove(drawable);
view.setTransitionAlpha(transitionAlpha);
}
});
return anim;
}
}
return null;
}
Aggregations