我正在处理动画,而我是新手。我在XML文件中有此结构:
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:padding="10dp"
android:background="@color/colorAccent"
android:layout_gravity="center">
<TextView
android:id="@+id/textViewT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Some Text"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/colorPrimaryDark"
android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Something"
android:textSize="12sp"
android:textColor="@color/colorPrimary"
android:gravity="center"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:scrollbars="vertical" />
动画:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<translate
android:duration="300"
android:fromYDelta="0"
android:toYDelta="-100%"/>
而且我的MainActivity看起来:
public class MainActivity extends AppCompatActivity {
MyRecyclerViewAdapter adapter;
boolean animationOnOff;
LinearLayout ll;
Animation animScroll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> animalNames = new ArrayList<>();
animalNames.add("Horse");
....
animationOnOff = true;
ll = findViewById(R.id.linearLayout1);
animScroll = AnimationUtils.loadAnimation(this, R.anim.total_gasto_hide);
animScroll.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
ll.animate().alpha(0).setDuration(0).withEndAction(new Runnable() {
@Override
public void run() {
ll.setVisibility(View.GONE);
}
});
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
RecyclerView recyclerView = findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MyRecyclerViewAdapter(this, animalNames);
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(dy > 0) {
if(animationOnOff) {
ll.startAnimation(animScroll);
animationOnOff = false;
}
} else {
}
}
});
}
}
此代码应将linearLayout1
抬起,并在300ms后以visibility(view.GONE)
消失,到目前为止,效果很好。问题在于,recyclerview不会被该动画所吸引,并且当linearLayout1
消失时,recycler会立即使视图的位置消失(进行某种闪烁效果)。上面的Gif:
所以我的问题是,当linearLayout1
消失时,如何消除闪烁效果并实现平滑的插值效果?
Android默认情况下可以为您处理动画。只需将android:animateLayoutChanges="true"
添加到您的父项LinearLayout
中,可见性就会变成动画。
我建议您将CoordinatorLayout