gpt4 book ai didi

android - 如何在将内容 View 向下推的通知 View 中制作幻灯片动画

转载 作者:太空狗 更新时间:2023-10-29 16:40:12 24 4
gpt4 key购买 nike

我在屏幕上有两个 View

一个位于屏幕顶部,一个位于屏幕正下方

我需要绿色 View 从顶部滑出 - 并因此使蓝色 View 占据整个屏幕

enter image description here

这就是我想要做的:

-- 问题是,当动画结束时,蓝色 View 只是“跳”起来 - 我希望它随着绿色 View 的消失而缓和,我该怎么做?

slide_in_animation.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate
android:duration="1000"
android:fromYDelta="-100%"
android:toYDelta="0%" />
</set>

slide_out_animation.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate
android:duration="1000"
android:fromYDelta="0%"
android:toYDelta="-100%" />
</set>

主 Activity .java

slideInAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.slide_in_animation);
slideOutAnimation = AnimationUtils.loadAnimation(mActivity,R.anim.slide_out_animation);


slideInAnimation.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {
mGreenView.setVisibility(View.VISIBLE);

}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationEnd(Animation animation) {

}
});

slideOutAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {


}

@Override
public void onAnimationEnd(Animation animation) {
mGreenView.setVisibility(View.GONE);

}

@Override
public void onAnimationRepeat(Animation animation) {
// To change body of implemented methods use File | Settings
// | File Templates.
}
});

最佳答案

这对我有用

No notification Animating notification Notification visible Animating notification No notification

首先你必须导入这个库:http://nineoldandroids.com/

导入是通过将现有的 android 项目导入您的工作区来完成的,然后右键单击您的项目 -> 属性 -> Android。在这里您将看到一个库部分,单击“添加”按钮并添加 nineoldandroids 库。

首先,这是用于此工作的布局 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:divider="@android:color/transparent"
android:fastScrollEnabled="false"
android:listSelector="@android:color/transparent"
android:scrollbars="vertical"
android:smoothScrollbar="true" />

<View
android:id="@+id/greenView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#ff00ff00"
android:alpha="0"/>
</FrameLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:id="@+id/animate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickHandler"
android:text="animate" />

<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickHandler"
android:text="close" />
</LinearLayout>

注意:ListView和绿色View都可以是任何类型的布局,任何类型的内容。

接下来是概念验证 Activity 。

public class TestActivity extends Activity {

private View greenView;
private ListView listView;
private int greenHeight;

private boolean isShowingBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

// The animated view
greenView = (View)findViewById(R.id.greenView);
listView = (ListView)findViewById(R.id.listView1);


// Instanciating an array list (you don't need to do this, you already have yours)
ArrayList<String> your_array_list = new ArrayList<String>();
your_array_list.add("1");
your_array_list.add("2");
your_array_list.add("3");
your_array_list.add("4");
your_array_list.add("5");
your_array_list.add("6");
your_array_list.add("7");
your_array_list.add("8");
your_array_list.add("9");
your_array_list.add("10");
your_array_list.add("11");
your_array_list.add("12");
your_array_list.add("13");
your_array_list.add("14");
your_array_list.add("15");
// This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, your_array_list);
listView.setAdapter(arrayAdapter);

final LinearLayout layout = (LinearLayout)findViewById(R.id.parentLayout);

final ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

if (Build.VERSION.SDK_INT < 16) {
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}

greenHeight = greenView.getHeight();

}
});
}

public void clickHandler(View v) {
if (isShowingBox) {
isShowingBox = false;
slideOut(1500, 0);
} else {
isShowingBox = true;
slideIn(1500, 0);
}
}

private void slideIn(int duration, int delay) {
AnimatorSet set = new AnimatorSet();
set.playTogether(
// animate from off-screen in to screen
ObjectAnimator.ofFloat(greenView, "translationY", -greenHeight, 0),
ObjectAnimator.ofFloat(listView, "translationY", 0, greenHeight),
ObjectAnimator.ofFloat(greenView, "alpha", 0, 0.25f, 1)
// add other animations if you wish
);
set.setStartDelay(delay);
set.setDuration(duration).start();
}

private void slideOut(int duration, int delay) {
AnimatorSet set = new AnimatorSet();
set.playTogether(
// animate from on-screen and out
ObjectAnimator.ofFloat(greenView, "translationY", 0, -greenHeight),
ObjectAnimator.ofFloat(listView, "translationY", greenHeight, 0),
ObjectAnimator.ofFloat(greenView, "alpha", 1, 1, 1)
// add other animations if you wish
);
set.setStartDelay(delay);
set.setDuration(duration).start();
}

}

重要说明:请记住在您的类(class)中从 nineoldandroids 导入 AnimatorSet 和 ObjectAnimator,而不是从 Android SDK 导入!

关于android - 如何在将内容 View 向下推的通知 View 中制作幻灯片动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19098083/

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