下面列出了android.view.View#getScaleX ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
Transforms(View view) {
mTranslationX = view.getTranslationX();
mTranslationY = view.getTranslationY();
mTranslationZ = ViewCompat.getTranslationZ(view);
mScaleX = view.getScaleX();
mScaleY = view.getScaleY();
mRotationX = view.getRotationX();
mRotationY = view.getRotationY();
mRotationZ = view.getRotation();
}
public Transforms(View view) {
translationX = view.getTranslationX();
translationY = view.getTranslationY();
translationZ = view.getTranslationZ();
scaleX = view.getScaleX();
scaleY = view.getScaleY();
rotationX = view.getRotationX();
rotationY = view.getRotationY();
rotationZ = view.getRotation();
}
private boolean isViewOverlapping(View firstView, View secondView) {
int[] firstPosition = new int[2];
int[] secondPosition = new int[2];
firstView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
firstView.getLocationOnScreen(firstPosition);
secondView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
secondView.getLocationOnScreen(secondPosition);
float firstWidth = firstView.getMeasuredWidth() * firstView.getScaleX();
float secondWidth = secondView.getMeasuredWidth() * secondView.getScaleX();
return firstPosition[0] < secondPosition[0] + secondWidth
&& firstPosition[0] + firstWidth > secondPosition[0];
}
public void updateView(View v) {
if (v != null) {
this.alpha = v.getAlpha();
this.x = v.getX();
this.y = v.getY();
this.z = atLeastLollipop ? v.getZ() : 0;
this.width = v.getWidth();
this.height = v.getHeight();
this.expansionScaleX = v.getScaleX();
this.expansionScaleY = v.getScaleY();
this.dispositionAngle = v.getRotation();
this.dispositionAngleX = v.getRotationX();
this.dispositionAngleY = v.getRotationY();
}
}
/**
* 获取 View 水平方向缩放比例
* @param view View
* @return View 水平方向缩放比例
*/
public static float getScaleX(final View view) {
if (view != null) {
return view.getScaleX();
}
return 0f;
}
private void updateScaleAndAlpha(View view, float alpha, float scale) {
if(null == view) {
return;
}
if(alpha >= 0 && view.getAlpha() != alpha) {
view.setAlpha(alpha);
}
if(scale >= 0 && view.getScaleX() != scale) {
view.setScaleX(scale);
view.setScaleY(scale);
}
}
@Nullable
private Animator createAnimation(@NonNull final View view, float startScale, float endScale, @Nullable TransitionValues values) {
final float initialScaleX = view.getScaleX();
final float initialScaleY = view.getScaleY();
float startScaleX = initialScaleX * startScale;
float endScaleX = initialScaleX * endScale;
float startScaleY = initialScaleY * startScale;
float endScaleY = initialScaleY * endScale;
if (values != null) {
Float savedScaleX = (Float) values.values.get(PROPNAME_SCALE_X);
Float savedScaleY = (Float) values.values.get(PROPNAME_SCALE_Y);
// if saved value is not equal initial value it means that previous
// transition was interrupted and in the onTransitionEnd
// we've applied endScale. we should apply proper value to
// continue animation from the interrupted state
if (savedScaleX != null && savedScaleX != initialScaleX) {
startScaleX = savedScaleX;
}
if (savedScaleY != null && savedScaleY != initialScaleY) {
startScaleY = savedScaleY;
}
}
view.setScaleX(startScaleX);
view.setScaleY(startScaleY);
Animator animator = TransitionUtils.mergeAnimators(
ObjectAnimator.ofFloat(view, View.SCALE_X, startScaleX, endScaleX),
ObjectAnimator.ofFloat(view, View.SCALE_Y, startScaleY, endScaleY));
addListener(new TransitionListenerAdapter() {
@Override
public void onTransitionEnd(@NonNull Transition transition) {
view.setScaleX(initialScaleX);
view.setScaleY(initialScaleY);
transition.removeListener(this);
}
});
return animator;
}
public Transforms(View view) {
translationX = view.getTranslationX();
translationY = view.getTranslationY();
translationZ = ViewUtils.getTranslationZ(view);
scaleX = view.getScaleX();
scaleY = view.getScaleY();
rotationX = view.getRotationX();
rotationY = view.getRotationY();
rotationZ = view.getRotation();
}
public ReorderPreviewAnimation(View child, int mode, int cellX0, int cellY0, int cellX1,
int cellY1, int spanX, int spanY) {
regionToCenterPoint(cellX0, cellY0, spanX, spanY, mTmpPoint);
final int x0 = mTmpPoint[0];
final int y0 = mTmpPoint[1];
regionToCenterPoint(cellX1, cellY1, spanX, spanY, mTmpPoint);
final int x1 = mTmpPoint[0];
final int y1 = mTmpPoint[1];
final int dX = x1 - x0;
final int dY = y1 - y0;
finalDeltaX = 0;
finalDeltaY = 0;
int dir = mode == MODE_HINT ? -1 : 1;
if (dX == dY && dX == 0) {
} else {
if (dY == 0) {
finalDeltaX = - dir * Math.signum(dX) * mReorderPreviewAnimationMagnitude;
} else if (dX == 0) {
finalDeltaY = - dir * Math.signum(dY) * mReorderPreviewAnimationMagnitude;
} else {
double angle = Math.atan( (float) (dY) / dX);
finalDeltaX = (int) (- dir * Math.signum(dX) *
Math.abs(Math.cos(angle) * mReorderPreviewAnimationMagnitude));
finalDeltaY = (int) (- dir * Math.signum(dY) *
Math.abs(Math.sin(angle) * mReorderPreviewAnimationMagnitude));
}
}
this.mode = mode;
initDeltaX = child.getTranslationX();
initDeltaY = child.getTranslationY();
finalScale = getChildrenScale() - 4.0f / child.getWidth();
initScale = child.getScaleX();
this.child = child;
}
private static void move(View view, TransformInfo info) {
computeRenderOffset(view, info.pivotX, info.pivotY);
adjustTranslation(view, info.deltaX, info.deltaY);
float scale = view.getScaleX() * info.deltaScale;
scale = Math.max(info.minimumScale, Math.min(info.maximumScale, scale));
view.setScaleX(scale);
view.setScaleY(scale);
float rotation = adjustAngle(view.getRotation() + info.deltaAngle);
view.setRotation(rotation);
}
public static boolean isItemAtPoint(View view, float x, float y) {
return (view.getLeft() < x) && (view.getLeft() + view.getWidth() * view.getScaleX() > x)
&& (view.getTop() < y) && (view.getTop() + view.getHeight() * view.getScaleY() > y)
;
}
public void animateViewIntoPosition(DragView dragView, final View child, int duration,
final Runnable onFinishAnimationRunnable, View anchorView) {
ShortcutAndWidgetContainer parentChildren = (ShortcutAndWidgetContainer) child.getParent();
CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
parentChildren.measureChild(child);
Rect r = new Rect();
getViewRectRelativeToSelf(dragView, r);
int coord[] = new int[2];
float childScale = child.getScaleX();
coord[0] = lp.x + (int) (child.getMeasuredWidth() * (1 - childScale) / 2);
coord[1] = lp.y + (int) (child.getMeasuredHeight() * (1 - childScale) / 2);
// Since the child hasn't necessarily been laid out, we force the lp to be updated with
// the correct coordinates (above) and use these to determine the final location
float scale = getDescendantCoordRelativeToSelf((View) child.getParent(), coord);
// We need to account for the scale of the child itself, as the above only accounts for
// for the scale in parents.
scale *= childScale;
int toX = coord[0];
int toY = coord[1];
float toScale = scale;
if (child instanceof TextView) {
TextView tv = (TextView) child;
// Account for the source scale of the icon (ie. from AllApps to Workspace, in which
// the workspace may have smaller icon bounds).
toScale = scale / dragView.getIntrinsicIconScaleFactor();
// The child may be scaled (always about the center of the view) so to account for it,
// we have to offset the position by the scaled size. Once we do that, we can center
// the drag view about the scaled child view.
toY += Math.round(toScale * tv.getPaddingTop());
toY -= dragView.getMeasuredHeight() * (1 - toScale) / 2;
toX -= (dragView.getMeasuredWidth() - Math.round(scale * child.getMeasuredWidth())) / 2;
} else if (child instanceof FolderIcon) {
// Account for holographic blur padding on the drag view
toY += Math.round(scale * (child.getPaddingTop() - dragView.getDragRegionTop()));
toY -= scale * Workspace.DRAG_BITMAP_PADDING / 2;
toY -= (1 - scale) * dragView.getMeasuredHeight() / 2;
// Center in the x coordinate about the target's drawable
toX -= (dragView.getMeasuredWidth() - Math.round(scale * child.getMeasuredWidth())) / 2;
} else {
toY -= (Math.round(scale * (dragView.getHeight() - child.getMeasuredHeight()))) / 2;
toX -= (Math.round(scale * (dragView.getMeasuredWidth()
- child.getMeasuredWidth()))) / 2;
}
final int fromX = r.left;
final int fromY = r.top;
child.setVisibility(INVISIBLE);
Runnable onCompleteRunnable = new Runnable() {
public void run() {
child.setVisibility(VISIBLE);
if (onFinishAnimationRunnable != null) {
onFinishAnimationRunnable.run();
}
}
};
animateViewIntoPosition(dragView, fromX, fromY, toX, toY, 1, 1, 1, toScale, toScale,
onCompleteRunnable, ANIMATION_END_DISAPPEAR, duration, anchorView);
}
static float g(View view)
{
return view.getScaleX();
}
public static boolean isViewHiddenByScale(View v) {
return v.getScaleX() == 0 && v.getScaleY() == 0;
}
@Override
public Float get(View object) {
return object.getScaleX();
}
static float getScaleX(View view) {
return view.getScaleX();
}
@Override
protected float getProperty(View view) {
return view.getScaleX();
}
public void animateViewIntoPosition(DragView dragView, final View child, int duration,
final Runnable onFinishAnimationRunnable, View anchorView) {
ShortcutAndWidgetContainer parentChildren = (ShortcutAndWidgetContainer) child.getParent();
CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
parentChildren.measureChild(child);
Rect r = new Rect();
getViewRectRelativeToSelf(dragView, r);
int coord[] = new int[2];
float childScale = child.getScaleX();
coord[0] = lp.x + (int) (child.getMeasuredWidth() * (1 - childScale) / 2);
coord[1] = lp.y + (int) (child.getMeasuredHeight() * (1 - childScale) / 2);
// Since the child hasn't necessarily been laid out, we force the lp to be updated with
// the correct coordinates (above) and use these to determine the final location
float scale = getDescendantCoordRelativeToSelf((View) child.getParent(), coord);
// We need to account for the scale of the child itself, as the above only accounts for
// for the scale in parents.
scale *= childScale;
int toX = coord[0];
int toY = coord[1];
float toScale = scale;
if (child instanceof TextView) {
TextView tv = (TextView) child;
// Account for the source scale of the icon (ie. from AllApps to Workspace, in which
// the workspace may have smaller icon bounds).
toScale = scale / dragView.getIntrinsicIconScaleFactor();
// The child may be scaled (always about the center of the view) so to account for it,
// we have to offset the position by the scaled size. Once we do that, we can center
// the drag view about the scaled child view.
toY += Math.round(toScale * tv.getPaddingTop());
toY -= dragView.getMeasuredHeight() * (1 - toScale) / 2;
if (dragView.getDragVisualizeOffset() != null) {
toY -= Math.round(toScale * dragView.getDragVisualizeOffset().y);
}
toX -= (dragView.getMeasuredWidth() - Math.round(scale * child.getMeasuredWidth())) / 2;
} else if (child instanceof FolderIcon) {
// Account for holographic blur padding on the drag view
toY += Math.round(scale * (child.getPaddingTop() - dragView.getDragRegionTop()));
toY -= scale * Workspace.DRAG_BITMAP_PADDING / 2;
toY -= (1 - scale) * dragView.getMeasuredHeight() / 2;
// Center in the x coordinate about the target's drawable
toX -= (dragView.getMeasuredWidth() - Math.round(scale * child.getMeasuredWidth())) / 2;
} else {
toY -= (Math.round(scale * (dragView.getHeight() - child.getMeasuredHeight()))) / 2;
toX -= (Math.round(scale * (dragView.getMeasuredWidth()
- child.getMeasuredWidth()))) / 2;
}
final int fromX = r.left;
final int fromY = r.top;
child.setVisibility(INVISIBLE);
Runnable onCompleteRunnable = new Runnable() {
public void run() {
child.setVisibility(VISIBLE);
if (onFinishAnimationRunnable != null) {
onFinishAnimationRunnable.run();
}
}
};
animateViewIntoPosition(dragView, fromX, fromY, toX, toY, 1, 1, 1, toScale, toScale,
onCompleteRunnable, ANIMATION_END_DISAPPEAR, duration, anchorView);
}
private PushDownAnim( final View view ){
this.weakView = new WeakReference<>(view) ;
this.weakView.get().setClickable( true );
defaultScale = view.getScaleX();
}
/**
* cause elevation is not support below api 21,
* so you can set your elevation here for supporting it below api 21
* or you can just setElevation in {@link #setItemViewProperty(View, float)}
*/
protected float setViewElevation(View itemView, float targetOffset) {
return itemView.getScaleX() * 5;
}