gpt4 book ai didi

android - 从特定点动画 View 以填充整个屏幕并将其重新调整回该点

转载 作者:太空狗 更新时间:2023-10-29 16:18:07 25 4
gpt4 key购买 nike

我知道如何将 View 动画化到整个屏幕并动画化回其原始大小。这是执行此操作的链接 Re sizing through animation

但问题是这种技术只有在我的 View 位于屏幕开始时才有效。

我想要的是,如果我的高度和宽度 View (50,50) 放置在屏幕中心的某个按钮下方,并且我单击该 View ,它应该会动画以填充整个屏幕,当再次单击时它会动画回到它的原始大小(50,50)

最佳答案

如果您使用的是 LinearLayout,请尝试为此动画设置 AnimationListener 并适本地切换按钮的可见性。将按钮的可见性设置为 View.GONE onAnimationStart当“扩展” View 并查看时。VISIBLE onAnimationEnd当“折叠”它时。使用 RelativeLayout 也可以解决这个问题。

对于动画:

public class ExpandCollapseViewAnimation extends Animation {
int targetWidth;
int targetHeight;
int initialWidth;
int initialHeight;
boolean expand;
View view;

public ExpandCollapseViewAnimation(View view, int targetWidth, int targetHeight ,boolean expand) {
this.view = view;
this.targetWidth = targetWidth;
this.targetHeight = targetHeight;
this.initialWidth = view.getWidth();
this.initialHeight = view.getHeight();
this.expand = expand;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newWidth, newHeight;
if (expand) {
newWidth = this.initialWidth
+ (int) ((this.targetWidth - this.initialWidth) * interpolatedTime);
newHeight = this.initialHeight
+ (int) ((this.targetHeight - this.initialHeight) * interpolatedTime);
} else {
newWidth = this.initialWidth
- (int) ((this.initialWidth - this.targetWidth) * interpolatedTime);
newHeight = this.initialHeight
- (int) ((this.initialHeight - this.targetHeight) * interpolatedTime);
}

view.getLayoutParams().width = newWidth;
view.getLayoutParams().height = newHeight;
view.requestLayout();
}

@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}

@Override
public boolean willChangeBounds() {
return true;
}

}

和布局 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button android:id="@+id/btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:text="Test"/>


<View android:id="@+id/centered_view"
android:layout_width="50dp"
android:layout_height="50dp"
android:clickable="true"
android:layout_gravity="center_horizontal"
android:background="#FF0000"/>

</LinearLayout>

此代码有效:

animatedView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
if (!expand) expandView();
else collapseView();
}
});

private void expandView() {
expand = true;
animatedView.clearAnimation();
Display display = this.getWindowManager().getDefaultDisplay();
int maxWidth = display.getWidth();
int maxHeight = display.getHeight();
ExpandCollapseViewAnimation animation = new ExpandCollapseViewAnimation(animatedView, maxWidth,maxHeight, expand);
animation.setDuration(500);
animation.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {
btn.setVisibility(View.GONE);
}

@Override
public void onAnimationRepeat(Animation animation) {
}

@Override
public void onAnimationEnd(Animation animation) {
}
});
animatedView.startAnimation(animation);
animatedView.invalidate();
}

private void collapseView() {
expand = false;
animatedView.clearAnimation();
ExpandCollapseViewAnimation animation = new ExpandCollapseViewAnimation(
animatedView, dpToPx(this, 50),dpToPx(this, 50), expand);
animation.setDuration(500);
animation.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {
btn.setVisibility(View.VISIBLE);
}
});
animatedView.startAnimation(animation);
animatedView.invalidate();
}

关于android - 从特定点动画 View 以填充整个屏幕并将其重新调整回该点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21907287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com