Сдвиньте макет снизу экрана
У меня есть макет, скрытый от представления. Нажав на кнопку, я хочу, чтобы она скользнула вниз, нажимая содержимое всего экрана вверх, очень похоже на то, как Whatsapp показывает панель смайликов на экране чата.
Я видел SlidingDrawer, который не работает для меня. Для этого требуется изображение как дескриптор, который отображается в центре экрана, я не хочу этого. Он также скользит по существующему содержимому экрана, я ищу способ перемещения существующего контента вверх.
Обновление 1:
Я попытался использовать анимации, как это предложил Санкет Кахела. Но скрытый макет никогда не отображается. Вот код.
Макет (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>
Любые идеи, как это можно сделать?
Спасибо.
Ответы
Ответ 1
Используйте эти анимации:
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);
Ответ 2
Ты был близок. Ключ состоит в том, чтобы скрытая компоновка раздувалась до 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"
Но это вопрос предпочтения, я полагаю.
Ответ 3
Вам просто нужно добавить строку в свое приложение, пожалуйста, найдите ее по ссылке ниже:
Показать и скрыть представление с анимацией слайдов вверх/вниз
Просто добавьте анимацию в свой макет, как это:
mLayoutTab.animate()
.translationYBy(120)
.translationY(0)
.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
Ответ 4
Вот то, что сработало для меня в конце.
Макеты
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>
Java: пакет 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);
}
}
}
Ответ 5
Используйте этот макет. Если вы хотите оживить сокращение основного вида, вам нужно добавить анимацию в высоту скрытой панели, купить ее может быть достаточно хорошим, чтобы использовать анимацию перевода на панели, а вместо анимации - прыжок с высоты обзора.
<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>
Ответ 6
Хорошо, есть два возможных подхода. Самый простой - это использовать библиотеку скользящее меню. Это позволяет создать нижнее скользящее меню, оно может анимировать верхний контейнер, чтобы сделать снизу видимым, он поддерживает как перетаскивание пальцем, так и анимацию с помощью кнопки (StaticDrawer).
Сложнее - если вы хотите использовать анимацию, как уже было предложено. С анимацией вы должны ПЕРВОЕ изменить макеты. Поэтому сначала попробуйте изменить свой макет в конечное состояние без каких-либо анимаций. Потому что очень вероятно, что вы не выложите свои взгляды должным образом в RelativeLayout, так что, хотя вы показываете свой снизу, он остается затененным верхним. После того, как вы достигли правильного изменения макета - все, что вам нужно сделать, - это запомнить переводы перед макетом и применить анимацию трансляции AFTER.
Ответ 7
Попробуйте этот код ниже, его очень короткий и простой.
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));
}
}
![bottom_up_navigation of image]()
Ответ 8
Мой код, чтобы сделать анимацию слайдом, слайд вниз без 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();
![введите описание изображения здесь]()
Ответ 9
Вы можете определить главный экран и другой экран, который вы хотите прокрутить вверх как фрагменты. Когда кнопка на главном экране будет нажата, фрагмент отправит сообщение в действие, которое затем заменит основной экран тем, который вы хотите прокрутить вверх и оживить замену.