下面列出了android.view.View#setScaleY ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void transformItem(GalleryLayoutManager layoutManager, View item, float fraction) {
if (layoutManager.getOrientation() == GalleryLayoutManager.VERTICAL) {
return;
}
int width = item.getWidth();
int height = item.getHeight();
item.setPivotX(width / 2.f);
item.setPivotY(height);
float scale = 1 - 0.1f * Math.abs(fraction);
item.setScaleX(scale);
item.setScaleY(scale);
item.setRotation(ROTATE_ANGEL * fraction);
item.setTranslationY((float) (Math.sin(2 * Math.PI * ROTATE_ANGEL * Math.abs(fraction) / 360.f) * width / 2.0f));
item.setTranslationX((float) ((1 - scale) * width / 2.0f / Math.cos(2 * Math.PI * ROTATE_ANGEL * fraction / 360.f)));
if (fraction > 0) {
item.setTranslationX(-item.getTranslationX());
}
item.setAlpha(1 - 0.2f * Math.abs(fraction));
}
/**
* Show the the touch interceptor.
*/
private void showTouchInterceptor() {
if (findViewWithTag(TAG_OVERLAY) == null) {
addTouchInterceptor();
}
if (mContentView == null) {
mContentView = findViewWithTag(TAG_CONTENT);
}
float offset = map(mContentView.getLeft(), 0, DuoDrawerLayout.this.getWidth() * mMarginFactor, 0, 1);
float scaleFactorContent = map(offset, 0, 1, mContentScaleClosed, mClickToCloseScale);
View interceptor = findViewWithTag(TAG_OVERLAY);
if (interceptor != null) {
interceptor.setTranslationX(mContentView.getLeft());
interceptor.setTranslationY(mContentView.getTop());
interceptor.setScaleX(scaleFactorContent);
interceptor.setScaleY(scaleFactorContent);
interceptor.setVisibility(VISIBLE);
}
}
/**
* Called each {@link #transformPage(View, float)} before {{@link #onTransform(View, float)}.
* <p>
* The default implementation attempts to reset all view properties. This is useful when toggling transforms that do
* not modify the same page properties. For instance changing from a transformation that applies rotation to a
* transformation that fades can inadvertently leave a fragment stuck with a rotation or with some degree of applied
* alpha.
*
* @param page
* Apply the transformation to this page
* @param position
* Position of page relative to the current front-and-center position of the pager. 0 is front and
* center. 1 is one full page position to the right, and -1 is one page position to the left.
*/
protected void onPreTransform(View page, float position) {
final float width = page.getWidth();
page.setRotationX(0);
page.setRotationY(0);
page.setRotation(0);
page.setScaleX(1);
page.setScaleY(1);
page.setPivotX(0);
page.setPivotY(0);
page.setTranslationY(0);
page.setTranslationX(isPagingEnabled() ? 0f : -width * position);
if (hideOffscreenPages()) {
page.setAlpha(position <= -1f || position >= 1f ? 0f : 1f);
// page.setEnabled(false);
} else {
// page.setEnabled(true);
page.setAlpha(1f);
}
}
/**
* 竖向方向上的缩放
*/
private void scaleVerticalChildView(){
float mid = getHeight() / 2.0f;
for (int i = 0; i < getChildCount(); i++) {
View childView = getChildAt(i);
if (childView != null) {
float childMid = (getDecoratedTop(childView) + getDecoratedBottom(childView)) / 2.0f;
float scale = 1.0f + (-1 * (1 - mScale)) * (Math.min(mid, Math.abs(mid - childMid))) / mid;
childView.setScaleX(scale);
childView.setScaleY(scale);
if (mAlpha) {
childView.setAlpha(scale);
}
}
}
}
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);
} else if (position <= 1) { // (0,1]
// Fade the page out.
view.setAlpha(1 - position);
// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);
// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
private static void clear(View v) {
v.setAlpha(1);
v.setScaleY(1);
v.setScaleX(1);
v.setTranslationY(0);
v.setTranslationX(0);
v.setRotation(0);
v.setRotationY(0);
v.setRotationX(0);
v.setPivotY(v.getMeasuredHeight() / 2);
v.setPivotX(v.getMeasuredWidth() / 2);
v.animate().setInterpolator(null).setStartDelay(0);
}
private void transformPageDepth(View view, float position) {
final float MIN_SCALE = 0.75f;
int pageWidth = view.getWidth();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);
} else if (position <= 1) { // (0,1]
// Fade the page out.
view.setAlpha(1 - position);
// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);
// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
@Override
protected void onTransform(View view, float position) {
if (position >= -1 || position <= 1) {
// Modify the default slide transition to shrink the page as well
final float height = view.getHeight();
final float width = view.getWidth();
final float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
final float vertMargin = height * (1 - scaleFactor) / 2;
final float horzMargin = width * (1 - scaleFactor) / 2;
// Center vertically
view.setPivotY(0.5f * height);
view.setPivotX(0.5f * width);
if (position < 0) {
view.setTranslationX(horzMargin - vertMargin / 2);
} else {
view.setTranslationX(-horzMargin + vertMargin / 2);
}
// Scale the page down (between MIN_SCALE and 1)
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
// Fade the page relative to its size.
view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
}
}
/** Reset the transform on a view. */
public static void reset(View v) {
v.setTranslationX(0f);
v.setTranslationY(0f);
v.setScaleX(1f);
v.setScaleY(1f);
v.setAlpha(1f);
}
private void resetSubButton(Point center, SubButton subButton, View subButtonView) {
if (subButton != null) {
int floatingMenuButtonWidth = floatingMenuButton.getWidth();
int floatingMenuButtonHeight = floatingMenuButton.getHeight();
int xResetPos = center.x + floatingMenuButtonWidth / 2;
int yResetPos = center.y - floatingMenuButtonHeight / 2;
subButtonView.setX(xResetPos);
subButtonView.setY(yResetPos);
subButtonView.setScaleX(0);
subButtonView.setScaleY(0);
subButton.setX(xResetPos);
subButton.setY(yResetPos);
subButton.setAlpha(0);
}
}
@Test
public void testCaptureViewStatus() {
ActivityController<NavigationSourceUtility.TestActivity> controller = Robolectric.buildActivity(NavigationSourceUtility.TestActivity.class).create().start().resume();
NavigationSourceUtility.TestActivity testActivity = controller.get();
View view = new View(testActivity);
view.setTranslationX(1);
view.setTranslationY(1);
view.setScaleX(2.0f);
view.setScaleY(2.0f);
view.setRotation(1.0f);
view.setRotationX(1.0f);
view.setRotationY(1.0f);
view.setAlpha(0.5f);
AnimatorUtility.AnimatorInfo animatorInfo = AnimatorUtility.captureViewStatus(view);
assertEquals(1.0f, animatorInfo.translationX, 0.0f);
assertEquals(1.0f, animatorInfo.translationY, 0.0f);
assertEquals(2.0f, animatorInfo.scaleX, 0.0f);
assertEquals(2.0f, animatorInfo.scaleY, 0.0f);
assertEquals(1.0f, animatorInfo.rotation, 0.0f);
assertEquals(1.0f, animatorInfo.rotationX, 0.0f);
assertEquals(1.0f, animatorInfo.rotationY, 0.0f);
assertEquals(0.5f, animatorInfo.alpha, 0.0f);
View view2 = new View(testActivity);
AnimatorUtility.resetViewStatus(view2, animatorInfo);
assertEquals(1.0f, view2.getTranslationX(), 0.0f);
assertEquals(1.0f, view2.getTranslationY(), 0.0f);
assertEquals(2.0f, view2.getScaleX(), 0.0f);
assertEquals(2.0f, view2.getScaleY(), 0.0f);
assertEquals(1.0f, view2.getRotation(), 0.0f);
assertEquals(1.0f, view2.getRotationX(), 0.0f);
assertEquals(1.0f, view2.getRotationY(), 0.0f);
assertEquals(0.5f, view2.getAlpha(), 0.0f);
}
@Override
public void onHideLabel(View label) {
final float shift = label.getWidth() / 2;
label.setScaleX(SCALE_X_SHOWN);
label.setScaleY(SCALE_Y_SHOWN);
label.setX(0f);
label.animate().alpha(0).scaleX(SCALE_X_HIDDEN).scaleY(SCALE_Y_HIDDEN).x(shift);
}
@Override
public void onHideLabel(View label) {
final float shift = label.getWidth() / 2;
label.setScaleX(SCALE_X_SHOWN);
label.setScaleY(SCALE_Y_SHOWN);
label.setX(0f);
label.animate().alpha(0).scaleX(SCALE_X_HIDDEN).scaleY(SCALE_Y_HIDDEN).x(shift);
}
@Override
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);
} else if (position <= 1) { // (0,1]
// Fade the page out.
view.setAlpha(1 - position);
// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);
// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE
+ (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
@Override
protected void resetLogoViewSize(int hintLocation, View logoView) {
logoView.clearAnimation();
logoView.setTranslationX(0);
logoView.setScaleX(1);
logoView.setScaleY(1);
}
@Override
public void transformPage(View view, float arg1) {
view.setAlpha(1);
view.setTranslationX(0);
view.setTranslationY(0);
view.setPivotX(view.getWidth() / 2);
view.setPivotY(view.getHeight() / 2);
view.setScaleX(1);
view.setScaleY(1);
view.setRotation(0);
}
@Override
public void transformPage(View page, float position) {
final float normalizedposition = Math.abs(Math.abs(position) - 1);
page.setScaleX(normalizedposition / 2 + 0.5f);
page.setScaleY(normalizedposition / 2 + 0.5f);
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
protected void expandToFitScreenWidth(int adWidth, int adHeight, View view) {
//Determine the width of the screen
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
@SuppressWarnings("UnusedAssignment") int width = -1;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Point p = new Point();
display.getSize(p);
width = p.x;
} else {
width = display.getWidth();
}
float ratio_delta = ((float) width) / ((float) adWidth);
int new_height = (int) Math.floor(adHeight * ratio_delta);
if(getLayoutParams() != null) {
oldH = getLayoutParams().height;
oldW = getLayoutParams().width;
//Adjust width of container
if (getLayoutParams().width > 0 || getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT) {
getLayoutParams().width = width;
}
//Adjust height of container
getLayoutParams().height = new_height;
}
if(view instanceof WebView) {
//Adjust height of webview
if (view.getLayoutParams() == null) {
view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
} else {
view.getLayoutParams().width = FrameLayout.LayoutParams.MATCH_PARENT;
view.getLayoutParams().height = FrameLayout.LayoutParams.MATCH_PARENT;
}
((WebView)view).setInitialScale((int) Math.ceil(ratio_delta * 100));
}else{
int adWidthInPixel = ViewUtil.getValueInPixel(getContext(), adWidth);
float widthRatio = (float) width / (float) adWidthInPixel;
view.setScaleX(widthRatio);
view.setScaleY(widthRatio);
}
view.invalidate();
shouldResetContainer = true;
}
@Override
public void setValue(View view, float value) {
view.setScaleY(value);
}
/**
* This method handles setting the property values directly in the View object's fields.
* propertyConstant tells it which property should be set, value is the value to set
* the property to.
*
* @param propertyConstant The property to be set
* @param value The value to set the property to
*/
private void setValue(int propertyConstant, float value) {
//final View.TransformationInfo info = mView.mTransformationInfo;
View v = mView.get();
if (v != null) {
switch (propertyConstant) {
case TRANSLATION_X:
//info.mTranslationX = value;
v.setTranslationX(value);
break;
case TRANSLATION_Y:
//info.mTranslationY = value;
v.setTranslationY(value);
break;
case ROTATION:
//info.mRotation = value;
v.setRotation(value);
break;
case ROTATION_X:
//info.mRotationX = value;
v.setRotationX(value);
break;
case ROTATION_Y:
//info.mRotationY = value;
v.setRotationY(value);
break;
case SCALE_X:
//info.mScaleX = value;
v.setScaleX(value);
break;
case SCALE_Y:
//info.mScaleY = value;
v.setScaleY(value);
break;
case X:
//info.mTranslationX = value - v.mLeft;
v.setX(value);
break;
case Y:
//info.mTranslationY = value - v.mTop;
v.setY(value);
break;
case ALPHA:
//info.mAlpha = value;
v.setAlpha(value);
break;
}
}
}