我在视图中隐藏了一个布局。在按钮上单击,我希望它从底部向上滑动,向上推动整个屏幕内容,非常类似于whatsapp在聊天屏幕中显示表情符号面板的方式。
我见过SlidingDrawer,这对我不起作用。它需要一个图像作为一个手柄,显示在屏幕的中心,我不希望这样。它还会滑动现有的屏幕内容,我正在寻找一种向上移动现有内容的方法。
更新1:
我尝试使用Sanket Kachhela建议的动画。但隐藏的布局从未显示过。这是代码。
布局(activity_main.xml):
<RelativeLayout
android:id="@+id/main_screen"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_alignParentTop="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_centerInParent="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Slide up / down"
android:layout_alignParentBottom="true"
android:onClick="slideUpDown"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/hidden_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/main_screen">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
</RelativeLayout>
活动(MainActivity.java):
package com.example.slideuplayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
public class MainActivity extends Activity {
private ViewGroup hiddenPanel;
private boolean isPanelShown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hiddenPanel = (ViewGroup)findViewById(R.id.hidden_panel);
hiddenPanel.setVisibility(View.INVISIBLE);
isPanelShown = false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void slideUpDown(final View view) {
if(!isPanelShown) {
// Show the panel
Animation bottomUp = AnimationUtils.loadAnimation(this,
R.anim.bottom_up);
hiddenPanel.startAnimation(bottomUp);
hiddenPanel.setVisibility(View.VISIBLE);
isPanelShown = true;
}
else {
// Hide the Panel
Animation bottomDown = AnimationUtils.loadAnimation(this,
R.anim.bottom_down);
hiddenPanel.startAnimation(bottomDown);
hiddenPanel.setVisibility(View.INVISIBLE);
isPanelShown = false;
}
}
}
动画:
bottom_up.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="75%p"
android:toYDelta="0%p"
android:fillAfter="true"
android:duration="500" />
</set>
bottom_down.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0%p"
android:toYDelta="100%p"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator"
android:duration="500" />
</set>
任何想法如何做到这一点?
感谢。
答案 0 :(得分:138)
使用这些动画:
<强> bottom_up.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="75%p" android:toYDelta="0%p"
android:fillAfter="true"
android:duration="500"/>
</set>
<强> bottom_down.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%p" android:toYDelta="100%p" android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator"
android:duration="500" />
</set>
在您的活动中使用此代码隐藏/动画您的视图:
Animation bottomUp = AnimationUtils.loadAnimation(getContext(),
R.anim.bottom_up);
ViewGroup hiddenPanel = (ViewGroup)findViewById(R.id.hidden_panel);
hiddenPanel.startAnimation(bottomUp);
hiddenPanel.setVisibility(View.VISIBLE);
答案 1 :(得分:41)
你很亲密。关键是让隐藏的布局在高度和重量上都膨胀到match_parent
。只需将其作为View.GONE
启动即可。这样,使用动画师中的百分比就可以正常工作。
布局(activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_screen"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="slideUpDown"
android:text="Slide up / down" />
<RelativeLayout
android:id="@+id/hidden_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:visibility="gone" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:layout_centerInParent="true"
android:onClick="slideUpDown" />
</RelativeLayout>
</RelativeLayout>
活动(MainActivity.java):
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
public class OffscreenActivity extends Activity {
private View hiddenPanel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
hiddenPanel = findViewById(R.id.hidden_panel);
}
public void slideUpDown(final View view) {
if (!isPanelShown()) {
// Show the panel
Animation bottomUp = AnimationUtils.loadAnimation(this,
R.anim.bottom_up);
hiddenPanel.startAnimation(bottomUp);
hiddenPanel.setVisibility(View.VISIBLE);
}
else {
// Hide the Panel
Animation bottomDown = AnimationUtils.loadAnimation(this,
R.anim.bottom_down);
hiddenPanel.startAnimation(bottomDown);
hiddenPanel.setVisibility(View.GONE);
}
}
private boolean isPanelShown() {
return hiddenPanel.getVisibility() == View.VISIBLE;
}
}
我改变的其他事情只在bottom_up.xml
。而不是
android:fromYDelta="75%p"
我用过:
android:fromYDelta="100%p"
但我认为这是一个偏好问题。
答案 2 :(得分:7)
您只需在应用中添加一些内容,请从以下链接中找到:
Show and hide a View with a slide up/down animation
只需在您的布局中添加动画,如下所示:
mLayoutTab.animate()
.translationYBy(120)
.translationY(0)
.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
答案 3 :(得分:6)
这对我来说最终有用。
<强>布局:强>
activity_main.xml中
<RelativeLayout
android:id="@+id/main_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_alignParentTop="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:layout_centerInParent="true" />
<Button
android:id="@+id/slideButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Slide up / down"
android:layout_alignParentBottom="true"
android:onClick="slideUpDown"/>
</RelativeLayout>
hidden_panel.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hidden_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test" />
</LinearLayout>
<强>爪哇:强> package com.example.slideuplayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
public class MainActivity extends Activity {
private ViewGroup hiddenPanel;
private ViewGroup mainScreen;
private boolean isPanelShown;
private ViewGroup root;
int screenHeight = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainScreen = (ViewGroup)findViewById(R.id.main_screen);
ViewTreeObserver vto = mainScreen.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
screenHeight = mainScreen.getHeight();
mainScreen.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
root = (ViewGroup)findViewById(R.id.root);
hiddenPanel = (ViewGroup)getLayoutInflater().inflate(R.layout.hidden_panel, root, false);
hiddenPanel.setVisibility(View.INVISIBLE);
root.addView(hiddenPanel);
isPanelShown = false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void slideUpDown(final View view) {
if(!isPanelShown) {
// Show the panel
mainScreen.layout(mainScreen.getLeft(),
mainScreen.getTop() - (screenHeight * 25/100),
mainScreen.getRight(),
mainScreen.getBottom() - (screenHeight * 25/100));
hiddenPanel.layout(mainScreen.getLeft(), mainScreen.getBottom(), mainScreen.getRight(), screenHeight);
hiddenPanel.setVisibility(View.VISIBLE);
Animation bottomUp = AnimationUtils.loadAnimation(this,
R.anim.bottom_up);
hiddenPanel.startAnimation(bottomUp);
isPanelShown = true;
}
else {
isPanelShown = false;
// Hide the Panel
Animation bottomDown = AnimationUtils.loadAnimation(this,
R.anim.bottom_down);
bottomDown.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation arg0) {
isPanelShown = false;
mainScreen.layout(mainScreen.getLeft(),
mainScreen.getTop() + (screenHeight * 25/100),
mainScreen.getRight(),
mainScreen.getBottom() + (screenHeight * 25/100));
hiddenPanel.layout(mainScreen.getLeft(), mainScreen.getBottom(), mainScreen.getRight(), screenHeight);
}
});
hiddenPanel.startAnimation(bottomDown);
}
}
}
答案 4 :(得分:4)
使用此布局。如果你想要缩小主视图的动画,你需要将动画添加到隐藏栏的高度,购买它可能足以使用条形图上的平移动画,并使主视图高度跳跃而不是动画。
<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" >
<RelativeLayout
android:id="@+id/main_screen"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="slideUpDown"
android:text="Slide up / down" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/hidden_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#fcc"
android:visibility="visible" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
</RelativeLayout>
</LinearLayout>
答案 5 :(得分:3)
好的,有两种可能的方法。最简单的 - 是使用sliding menu库。它允许创建一个底部滑动菜单,它可以设置顶部容器的动画以使底部可见,它支持用手指拖动它,或通过按钮(StaticDrawer)以编程方式设置动画。
更难的方式 - 如果你想使用Animations,就像已经建议的那样。使用动画,您必须首先更改布局。因此,首先尝试将布局更改为最终状态,而无需任何动画。因为很可能你没有在RelativeLayout中正确地布置你的视图,所以即使你显示你的底部视图,它仍然被顶部视图遮挡。一旦你实现了适当的布局改变 - 你需要做的就是在布局之前记住翻译并在布局后应用翻译动画。
答案 6 :(得分:2)
我的代码使动画向上滑动,向下滑动而不用XML
private static ObjectAnimator createBottomUpAnimation(View view,
AnimatorListenerAdapter listener, float distance) {
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", -distance);
// animator.setDuration(???)
animator.removeAllListeners();
if (listener != null) {
animator.addListener(listener);
}
return animator;
}
public static ObjectAnimator createTopDownAnimation(View view, AnimatorListenerAdapter listener,
float distance) {
view.setTranslationY(-distance);
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", 0);
animator.removeAllListeners();
if (listener != null) {
animator.addListener(listener);
}
return animator;
}
是强> 向下滑动
createTopDownAnimation(myYellowView, null, myYellowView.getHeight()).start();
向上滑动
createBottomUpAnimation(myYellowView, null, myYellowView.getHeight()).start();
答案 7 :(得分:2)
尝试以下代码,它非常简短。
<强> transalate_anim.xml 强>
<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="4000"
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="infinite"
android:toXDelta="0"
android:toYDelta="-90%p" />
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="4000"
android:fromAlpha="0.0"
android:repeatCount="infinite"
android:toAlpha="1.0" />
</set>
<强> activity_main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.naveen.congratulations.MainActivity">
<ImageView
android:id="@+id/image_1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:srcCompat="@drawable/balloons" />
</android.support.constraint.ConstraintLayout>
<强> MainActivity.java 强>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageView1 = (ImageView) findViewById(R.id.image_1);
imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startBottomToTopAnimation(imageView1);
}
});
}
private void startBottomToTopAnimation(View view) {
view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.translate_anim));
}
}
答案 8 :(得分:0)
您可以定义主屏幕和要作为片段向上滚动的其他屏幕。当按下主屏幕上的按钮时,片段会向活动发送一条消息,然后将主屏幕替换为您想要向上滚动的主屏幕并为替换设备设置动画。
答案 9 :(得分:0)
以下是[https://stackoverflow.com/a/46644736/10249774]
的扩展方案底部面板是向上推送主要内容
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<Button
android:id="@+id/my_button"
android:layout_marginTop="10dp"
android:onClick="onSlideViewButtonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="main "
android:textSize="70dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="main "
android:textSize="70dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="main "
android:textSize="70dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="main"
android:textSize="70dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="main"
android:textSize="70dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/footer_view"
android:background="#a6e1aa"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="footer content"
android:textSize="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="footer content"
android:textSize="40dp" />
</LinearLayout>
</RelativeLayout>
MainActivity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button myButton;
private View footerView;
private View mainView;
private boolean isUp;
private int anim_duration = 700;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
footerView = findViewById(R.id.footer_view);
mainView = findViewById(R.id.main_view);
myButton = findViewById(R.id.my_button);
// initialize as invisible (could also do in xml)
footerView.setVisibility(View.INVISIBLE);
myButton.setText("Slide up");
isUp = false;
}
public void slideUp(View mainView , View footer_view){
footer_view.setVisibility(View.VISIBLE);
TranslateAnimation animate_footer = new TranslateAnimation(
0, // fromXDelta
0, // toXDelta
footer_view.getHeight(), // fromYDelta
0); // toYDelta
animate_footer.setDuration(anim_duration);
animate_footer.setFillAfter(true);
footer_view.startAnimation(animate_footer);
mainView.setVisibility(View.VISIBLE);
TranslateAnimation animate_main = new TranslateAnimation(
0, // fromXDelta
0, // toXDelta
0, // fromYDelta
(0-footer_view.getHeight())); // toYDelta
animate_main.setDuration(anim_duration);
animate_main.setFillAfter(true);
mainView.startAnimation(animate_main);
}
public void slideDown(View mainView , View footer_view){
TranslateAnimation animate_footer = new TranslateAnimation(
0, // fromXDelta
0, // toXDelta
0, // fromYDelta
footer_view.getHeight()); // toYDelta
animate_footer.setDuration(anim_duration);
animate_footer.setFillAfter(true);
footer_view.startAnimation(animate_footer);
TranslateAnimation animate_main = new TranslateAnimation(
0, // fromXDelta
0, // toXDelta
(0-footer_view.getHeight()), // fromYDelta
0); // toYDelta
animate_main.setDuration(anim_duration);
animate_main.setFillAfter(true);
mainView.startAnimation(animate_main);
}
public void onSlideViewButtonClick(View view) {
if (isUp) {
slideDown(mainView , footerView);
myButton.setText("Slide up");
} else {
slideUp(mainView , footerView);
myButton.setText("Slide down");
}
isUp = !isUp;
}
}