官方的缩放视图教程使用 AnimatorSet
放大 View
. 当视图扩展时,它会产生向下移动的错觉。后来 AnimatorSet
只是简单地向后重放以产生缩小的错觉。
我需要实现的恰恰相反。我需要从一个展开的视图开始,将其缩小为一个向上移动的较小视图:
在这个例子中,我似乎不能使用反转代码。该示例假设您首先放大视图并展开它,然后将其缩小回原始缩略图图标。
这是我到目前为止试过的。我的xml布局是
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#1999da">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:layout_gravity="center"
android:gravity="center">
<!-- The final shrunk image -->
<ImageView
android:id="@+id/thumb_button_1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginRight="1dp"
android:visibility="invisible"/>
</LinearLayout>
</LinearLayout>
<!-- The initial expanded image that needs to be shrunk -->
<ImageView
android:id="@+id/expanded_image"
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_gravity="center"
android:src="@drawable/title_logo_expanded"
android:scaleType="centerCrop"/>
</FrameLayout>
下面是执行缩小操作的方法。我在教程中基本上尝试了反转过程:
private void zoomImageFromThumbReverse(final View expandedImageView, int imageResId, final int duration) {
// If there's an animation in progress, cancel it immediately and proceed with this one.
if (mCurrentAnimator != null) {
mCurrentAnimator.cancel();
}
// Load the low-resolution "zoomed-out" image.
final ImageView thumbView = (ImageView) findViewById(R.id.thumb_button_1);
thumbView.setImageResource(imageResId);
// Calculate the starting and ending bounds for the zoomed-in image. This step
// involves lots of math. Yay, math.
final Rect startBounds = new Rect();
final Rect finalBounds = new Rect();
final Point globalOffset = new Point();
// The start bounds are the global visible rectangle of the container view (i.e. the FrameLayout), and the
// final bounds are the global visible rectangle of the thumbnail. Also
// set the container view's offset as the origin for the bounds, since that's
// the origin for the positioning animation properties (X, Y).
findViewById(R.id.container).getGlobalVisibleRect(startBounds, globalOffset);
thumbView.getGlobalVisibleRect(finalBounds);
startBounds.offset(-globalOffset.x, -globalOffset.y);
finalBounds.offset(-globalOffset.x, -globalOffset.y);
// Adjust the start bounds to be the same aspect ratio as the final bounds using the
// "center crop" technique. This prevents undesirable stretching during the animation.
// Also calculate the start scaling factor (the end scaling factor is always 1.0).
float startScale;
if ((float) finalBounds.width() / finalBounds.height()
> (float) startBounds.width() / startBounds.height()) {
// Extend start bounds horizontally
startScale = (float) startBounds.height() / finalBounds.height();
float startWidth = startScale * finalBounds.width();
float deltaWidth = (startWidth - startBounds.width()) / 2;
startBounds.left -= deltaWidth;
startBounds.right += deltaWidth;
} else {
// Extend start bounds vertically
startScale = (float) startBounds.width() / finalBounds.width();
float startHeight = startScale * finalBounds.height();
float deltaHeight = (startHeight - startBounds.height()) / 2;
startBounds.top -= deltaHeight;
startBounds.bottom += deltaHeight;
}
// Hide the expanded-image and show the zoomed-out, thumbnail view. When the animation begins,
// it will position the zoomed-in view in the place of the thumbnail.
expandedImageView.setAlpha(0f);
thumbView.setVisibility(View.VISIBLE);
// Set the pivot point for SCALE_X and SCALE_Y transformations to the top-left corner of
// the zoomed-in view (the default is the center of the view).
thumbView.setPivotX(0f);
thumbView.setPivotY(0f);
// Construct and run the parallel animation of the four translation and scale properties
// (X, Y, SCALE_X, and SCALE_Y).
AnimatorSet set = new AnimatorSet();
set
.play(ObjectAnimator.ofFloat(thumbView, View.X, startBounds.left,
finalBounds.left))
.with(ObjectAnimator.ofFloat(thumbView, View.Y, startBounds.top,
finalBounds.top))
.with(ObjectAnimator.ofFloat(thumbView, View.SCALE_X, startScale, 1f))
.with(ObjectAnimator.ofFloat(thumbView, View.SCALE_Y, startScale, 1f));
//set.setDuration(mShortAnimationDuration);
set.setDuration(duration);
set.setInterpolator(new DecelerateInterpolator());
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mCurrentAnimator = null;
}
@Override
public void onAnimationCancel(Animator animation) {
mCurrentAnimator = null;
}
});
set.start();
mCurrentAnimator = set;
// Upon clicking the zoomed-out image, it should zoom back down to the original bounds
// and show the thumbnail instead of the expanded image.
final float startScaleFinal = startScale;
thumbView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mCurrentAnimator != null) {
mCurrentAnimator.cancel();
}
// Animate the four positioning/sizing properties in parallel, back to their
// original values.
AnimatorSet set = new AnimatorSet();
set
.play(ObjectAnimator.ofFloat(thumbView, View.X, startBounds.left))
.with(ObjectAnimator.ofFloat(thumbView, View.Y, startBounds.top))
.with(ObjectAnimator
.ofFloat(thumbView, View.SCALE_X, startScaleFinal))
.with(ObjectAnimator
.ofFloat(thumbView, View.SCALE_Y, startScaleFinal));
//set.setDuration(mShortAnimationDuration);
set.setDuration(duration);
set.setInterpolator(new DecelerateInterpolator());
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
expandedImageView.setAlpha(1f);
thumbView.setVisibility(View.GONE);
mCurrentAnimator = null;
}
@Override
public void onAnimationCancel(Animator animation) {
expandedImageView.setAlpha(1f);
thumbView.setVisibility(View.GONE);
mCurrentAnimator = null;
}
});
set.start();
mCurrentAnimator = set;
}
});
}
我在中调用这个方法 onCreate()
具体如下:
final View expandedImageView = findViewById(R.id.expanded_image);
new Handler().postDelayed(new Runnable(){
public void run() {
zoomImageFromThumbReverse(expandedImageView, R.drawable.title_logo_min, 1000);
}}, 1000);
好了,就这样,伙计们。它不起作用。我不知道为什么。演示示例运行得很好,那么为什么这不起作用呢?喝一杯告诉我我是不是疯了。
有人能识别错误吗?或者给我指出正确的方向?所有的帮助将不胜感激。
2条答案
按热度按时间v1l68za41#
这是我最终使用的解决方案:
这里的关键因素是
toYDelta
在TranslateAnimation
参数:最主要的是理解为什么这样做。剩下的大部分都很简单。
2ul0zpep2#
好吧,我想你想从你的图片和描述中放大并向上移动。我不能理解你的代码,它似乎太复杂了(我是一个noob)。现在我已经用下面的代码完成了你想要的。首先我用一个图像视图声明一个相对布局,这个相对布局就是容器。我设置了初始宽度和高度,但我们稍后将从代码中更改它。
现在在activity中,我为布局更改设置了一个侦听器,以便获得容器的实际大小。然后设置imageview的布局。
我们必须在这里动画三件事,宽度,高度和顶边(定位)。所以,我声明了三个变量作为动画师的初始位置,并在初始布局设置中计算它们。现在我们需要同时设置这三个变量的动画,这很简单。