package text.htss.com.demo_shuxing_anim; import android.annotation.SuppressLint; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewPropertyAnimator; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.ListView; /** * 属性动画 */ public class MainActivity extends AppCompatActivity { private boolean ismove = true; private ViewPropertyAnimator animatebottom, animatetop; private ListView mListView; private LinearLayout tabbottom; private LinearLayout tabtop; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); //属性动画 animatebottom = tabbottom.animate(); animatetop = tabtop.animate(); ArrayAdapter<String> adapter = initDatas(); mListView.setAdapter(adapter); setListener(); } private void setListener() { mListView.setOnTouchListener(new OnTouchListener() { private float y; private boolean down = true; private float lasty; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: y = event.getY(); if (down) { lasty = y; } down = false; break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: down = true; if (lasty - event.getY() < -5) { onScrollReset(); } else if (lasty - event.getY() > 5) { onScroll(); } break; } return false; } }); } @NonNull private ArrayAdapter<String> initDatas() { ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1); for (int i = 0; i < 100; i++) { adapter.add(i + ""); } return adapter; } private void initViews() { mListView = (ListView) findViewById(R.id.my_listview); tabbottom = (LinearLayout) findViewById(R.id.tabbottom); tabtop = (LinearLayout) findViewById(R.id.tabtop); } @SuppressLint("NewApi") public void onScroll() { if (ismove) { animatebottom.setDuration(500).translationY(200); animatetop.setDuration(500).translationY(-200); ismove = false; } } @SuppressLint("NewApi") public void onScrollReset() { if (!ismove) { animatebottom.setDuration(500).translationY(0); animatetop.setDuration(500).translationY(0); ismove = true; } } }
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/my_listview" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/tabbottom" android:layout_width="match_parent" android:layout_height="50dip" android:layout_alignParentBottom="true" android:background="#880090D9" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" android:textColor="#ffffff" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:id="@+id/tabtop" android:layout_width="match_parent" android:layout_height="50dip" android:layout_alignParentTop="true" android:background="#880090D9" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" android:textColor="#ffffff" android:textSize="18sp" /> </LinearLayout> </RelativeLayout> </FrameLayout>