我必须将一些文本视图的可见性更改为消失,而将其他一些文本视图更改为可见,我正在尝试对此进行动画处理。
这是我编写的代码:
public class SearchBookTest extends AppCompatActivity {
private final static int INTRO_VIEW = 0;
private final static int OFFLINE_VIEW = 1;
private boolean[] currentView = {true, false};
private FloatingSearchView mSearchView;
private TextView mIntroTextViewTop;
private TextView mIntroTextViewBottom;
private TextView mNoConnectionTextViewTop;
private TextView mNoConnectionTextViewBottom;
private AppCompatButton mNoConnectionButton;
private int mShortAnimationDuration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_test);
mSearchView = findViewById(R.id.floating_search_view);
mIntroTextViewTop = findViewById(R.id.search_book_intro_top);
mIntroTextViewBottom = findViewById(R.id.search_book_intro_bottom);
mNoConnectionTextViewTop = findViewById(R.id.search_book_no_connection_top);
mNoConnectionTextViewBottom = findViewById(R.id.search_book_no_connection_bottom);
mNoConnectionButton = findViewById(R.id.search_book_no_connection_try_again);
mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
setupQueryChangeListener();
}
private void setupQueryChangeListener() {
mSearchView.setOnQueryChangeListener(new FloatingSearchView.OnQueryChangeListener() {
@Override
public void onSearchTextChanged(String oldQuery, String newQuery) {
switch (newQuery.length()) {
case 1:
showIntroView();
break;
case 2:
showOfflineView();
break;
default:
break;
}
}
});
}
private void showOfflineView() {
if (currentView[INTRO_VIEW]) {
currentView[INTRO_VIEW] = false;
fadeOutViews(mIntroTextViewTop, mIntroTextViewBottom, null, mNoConnectionTextViewTop, mNoConnectionTextViewBottom, mNoConnectionButton);
}
currentView[OFFLINE_VIEW] = true;
}
private void showIntroView() {
if (currentView[OFFLINE_VIEW]) {
currentView[OFFLINE_VIEW] = false;
fadeOutViews(mNoConnectionTextViewTop, mNoConnectionTextViewBottom, mNoConnectionButton, mIntroTextViewTop, mIntroTextViewBottom, null);
}
currentView[INTRO_VIEW] = true;
}
private void fadeOutViews(final View vOut1, final View vOut2, final View vOut3, final View vIn1, final View vIn2, final View vIn3) {
vOut1.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
if (vOut2 != null) {
vOut2.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
vOut2.setVisibility(View.GONE);
if (vOut3 == null)
fadeInViews(vIn1, vIn2, vIn3);
}
});
}
if (vOut3 != null) {
vOut3.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
vOut3.setVisibility(View.GONE);
fadeInViews(vIn1, vIn2, vIn3);
}
});
}
}
@Override
public void onAnimationEnd(Animator animation) {
vOut1.setVisibility(View.GONE);
if (vOut2 == null)
fadeInViews(vIn1, vIn2, vIn3);
}
});
}
private void fadeInViews(final View v1, final View v2, final View v3) {
v1.setAlpha(0f);
v1.setVisibility(View.VISIBLE);
v1.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
if (v2 != null) {
v2.setAlpha(0f);
v2.setVisibility(View.VISIBLE);
v2.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
if (v3 != null) {
v3.setAlpha(0f);
v3.setVisibility(View.VISIBLE);
v3.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration);
}
}
});
}
}
});
}
}
这是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/parent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@color/grey"
tools:context=".ui.searchbooks.SearchBookActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/search_book_intro_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="200dp"
android:text="@string/search_intro_top"
android:textColor="@color/grayed_button"
android:fontFamily="sans-serif-light"
android:textSize="22sp"/>
<TextView
android:id="@+id/search_book_intro_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:text="@string/search_intro_botttom"
android:textColor="@color/grayed_button"
android:fontFamily="sans-serif-light"
android:textSize="16sp"/>
<TextView
android:id="@+id/search_book_no_connection_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="200dp"
android:text="@string/search_no_connection_1"
android:textColor="@color/black"
android:fontFamily="sans-serif-bold"
android:textSize="18sp"
android:visibility="gone"/>
<TextView
android:id="@+id/search_book_no_connection_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:text="@string/search_no_connection_2"
android:textColor="@color/black"
android:fontFamily="sans-serif-light"
android:textSize="14sp"
android:visibility="gone"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/search_book_no_connection_try_again"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_marginTop="20dp"
android:elevation="2dp"
android:text="@string/try_again"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:background="@color/colorSecondaryAccent"
android:visibility="gone"/>
</LinearLayout>
<com.arlib.floatingsearchview.FloatingSearchView
android:id="@+id/floating_search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:floatingSearch_close_search_on_keyboard_dismiss="true"
app:floatingSearch_dismissOnOutsideTouch="true"
app:floatingSearch_leftActionMode="showHome"
app:floatingSearch_menu="@menu/menu_search_books"
app:floatingSearch_dimBackground="false"
app:floatingSearch_searchBarMarginLeft="5dp"
app:floatingSearch_searchBarMarginRight="5dp"
app:floatingSearch_searchBarMarginTop="5dp"
app:floatingSearch_searchHint="@string/search_books_hint"
app:floatingSearch_showSearchKey="true"/>
</RelativeLayout>
当我第一次写第二个字符时,视图切换得很好,使 3 个"noConnection"视图动画化并显示正常。
然后我删除了一个 char,介绍视图再次显示正常(在无连接视图消失后(,但是当我再次编写第二个 char 时,视图会搞砸,使整个介绍视图与无连接视图一起显示(按钮除外(。
我确定了出了什么问题,但我不明白为什么会这样:
当第二次写入第二个字符时,"vOut3"变量不为空,但包含 mNoConnectionButton,使其消失并在两个介绍视图上调用 fadeInView。我尝试记录值并且 vOut3 为空,但它无论如何都会进入 if 语句!
有人可以帮助我摆脱困境吗?
您可以使用Transition
框架来实现所需的结果。以下代码适用于 API 级别 19+。
基本上,您调用TransitionManager.beginDelayedTransition(sceneRoot, transitionSet);
然后立即设置View
所需的状态。
sceneRoot 应该是一个包含所有要转换的View
的ViewGroup
。就您而言,我认为LinearLayout
是一个不错的选择。所以我让它有一个属性android:id="@+id/sceneRoot"
,并在Activity
中声明了一个变量private ViewGroup sceneRoot;
,并在onCreate()
中为其分配了LinearLayout
。
对于Transition
,我使用了一个由Fade
过渡和ChangeBounds
过渡组成的TransitionSet
,因为您更改了 alpha 值以及整体布局(在View.VISIBLE
和View.GONE
之间切换(。
private void fadeOutViews(final View vOut1, final View vOut2, final View vOut3, final View vIn1, final View vIn2, final View vIn3) {
// if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)return;
TransitionSet transitionSet = new TransitionSet();
transitionSet.addTransition(new Fade());
transitionSet.addTransition(new ChangeBounds());
TransitionManager.beginDelayedTransition(sceneRoot, transitionSet);
vOut1.setVisibility(View.GONE);
if(vOut2 != null) {
vOut2.setVisibility(View.GONE);
}
if(vOut3 != null) {
vOut3.setVisibility(View.GONE);
}
vIn1.setVisibility(View.VISIBLE);
if(vIn2 != null) {
vIn2.setVisibility(View.VISIBLE);
}
if(vIn3 != null) {
vIn3.setVisibility(View.VISIBLE);
}
}