gpt4 book ai didi

Android动画下拉/向上 View 正确

转载 作者:IT老高 更新时间:2023-10-28 23:26:47 28 4
gpt4 key购买 nike

我正在尝试制作正确的向下滑动动画。向下滑动的 View 应将其下方的所有 View 以一种平滑的方式向下推,当它向上滑动时,所有 View 应以一种平滑的方式跟随。

我的尝试:

在代码中:

LinearLayout lin = (LinearLayout)findViewById(R.id.user_list_container);
setLayoutAnimSlidedownfromtop(lin, this);
lin.addView(getLayoutInflater().inflate(R.layout.user_panel,null),0);

还有:

public static void setLayoutAnimSlidedownfromtop(ViewGroup panel, Context ctx) {

AnimationSet set = new AnimationSet(true);

Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(100);
set.addAnimation(animation);

animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(500);
set.addAnimation(animation);

LayoutAnimationController controller =
new LayoutAnimationController(set, 0.25f);
panel.setLayoutAnimation(controller);

}

我的user_panel.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="vertical" >
<ImageView
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/icon" />
</LinearLayout>

主要 XML 的顶部:

<LinearLayout
android:id="@+id/user_list_container"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/container"
android:layout_below="@+id/user_list_container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

上述方法的问题是,当我首先启动动画时,会创建 View 的空白空间,然后 View 会向下滑动。我希望它可以慢慢地将所有其他 View 向下推,而不是一气呵成。

最佳答案

所以我最终在 this answer 的帮助下自己完成了这项工作。 .如果是 Android 3.0 ,我本可以使用属性动画,但不是我必须自己这样做。

这是我最终得到的结果:

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
* Class for handling collapse and expand animations.
* @author Esben Gaarsmand
*
*/
public class ExpandCollapseAnimation extends Animation {
private View mAnimatedView;
private int mEndHeight;
private int mType;

/**
* Initializes expand collapse animation, has two types, collapse (1) and expand (0).
* @param view The view to animate
* @param duration
* @param type The type of animation: 0 will expand from gone and 0 size to visible and layout size defined in XML.
* 1 will collapse view and set to gone
*/
public ExpandCollapseAnimation(View view, int duration, int type) {
setDuration(duration);
mAnimatedView = view;
mEndHeight = mAnimatedView.getLayoutParams().height;
mType = type;
if(mType == 0) {
mAnimatedView.getLayoutParams().height = 0;
mAnimatedView.setVisibility(View.VISIBLE);
}
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
if(mType == 0) {
mAnimatedView.getLayoutParams().height = (int) (mEndHeight * interpolatedTime);
} else {
mAnimatedView.getLayoutParams().height = mEndHeight - (int) (mEndHeight * interpolatedTime);
}
mAnimatedView.requestLayout();
} else {
if(mType == 0) {
mAnimatedView.getLayoutParams().height = mEndHeight;
mAnimatedView.requestLayout();
} else {
mAnimatedView.getLayoutParams().height = 0;
mAnimatedView.setVisibility(View.GONE);
mAnimatedView.requestLayout();
mAnimatedView.getLayoutParams().height = mEndHeight;
}
}
}
}

示例用法:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AnimationTestActivity extends Activity {
private boolean mActive = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button animatedButton = (Button) findViewById(R.id.animatedButton);

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
ExpandCollapseAnimation animation = null;
if(mActive) {
animation = new ExpandCollapseAnimation(animatedButton, 1000, 1);
mActive = false;
} else {
animation = new ExpandCollapseAnimation(animatedButton, 1000, 0);
mActive = true;
}
animatedButton.startAnimation(animation);
}
});
}
}

XML:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/animatedButton"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="@string/hello"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
</LinearLayout>

编辑

测量 wrap_content 高度:

因此,为了使 wrap_content 能够正常工作,我在开始动画之前测量了 View 的高度,然后使用这个测量的高度作为实际高度。下面是测量 View 高度并将其设置为新高度的代码(我假设 View 使用屏幕宽度,根据您自己的需要更改):

/**
* This method can be used to calculate the height and set it for views with wrap_content as height.
* This should be done before ExpandCollapseAnimation is created.
* @param activity
* @param view
*/
public static void setHeightForWrapContent(Activity activity, View view) {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

int screenWidth = metrics.widthPixels;

int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(screenWidth, MeasureSpec.EXACTLY);

view.measure(widthMeasureSpec, heightMeasureSpec);
int height = view.getMeasuredHeight();
view.getLayoutParams().height = height;
}

关于Android动画下拉/向上 View 正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9248930/

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