Установите анимацию для просмотра просмотра программно
-
11-12-2019 - |
Вопрос
Я устанавливаю анимацию в макете, как это:
<ViewSwitcher
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right" >
.
Как я могу сделать то же самое программно?
Решение
Пожалуйста, прочитайте документацию для класса ViewSwitcher
, у него есть два метода для установки анимации ввода / вывода:
// load the two animations
Animation animIn = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
Animation animOut = AnimationUtils.loadAnimation(context, android.R.anim.slide_out_right);
// set them on the ViewSwitcher
vs.setInAnimation(animIn);
vs.setOutAnimation(animOut);
. Другие советы
viewswitcher.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_in));
viewswitcher.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_out));
. Вы можете пропустить шаг загрузки анимации с AnimationUtils
и вместо этого передать ресурс Anim непосредственно:
switcher.setInAnimation(this, android.R.anim.slide_in_left);
switcher.setOutAnimation(this, android.R.anim.slide_out_right);
. Не связан с StackOverflow