从屏幕外部到内部的TranslateAnimation被剪裁

时间:2014-07-22 11:07:32

标签: android animation

我试图"滑入" ListView的底部和顶部的两个视图。

我的列表中有一个"特色"项目。当此特色项目向下滚动ListView的可见部分时,底部会显示一个按钮以滚动到特色项目位置,同样,当它向上滚动ListView的可见部分时{1}},顶部会显示一个按钮。

问题是"滑入"之前屏幕外的部分按钮?动画在动画结束时被剪裁。我尝试在所有父视图上设置android:clipChildren="false",但它仍然被裁剪。

我怎样才能做到这一点?

特色商品为黄色

如果特色项目滚动超出屏幕顶部,则会出现顶部按钮,但它会被剪裁(仅显示与标题大小相对应的部分)

如果特色项目滚动到屏幕底部以外,则会显示底部按钮,但它会被剪裁(仅显示与页脚大小相对应的部分)

TestActivity.java

public class TestActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        Fragment fragment = new TestFragment();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        transaction.replace(R.id.container, fragment);
        transaction.addToBackStack(null);

        transaction.commit();
    }
}

activity_test.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:clipToPadding="false">

    <FrameLayout
        android:id="@+id/container"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

TestFragment.java

public class TestFragment extends Fragment {

    public TestFragment() {}

    private ListView listView;

    private int featuredItemPosition = 10;

    /* Scroll buttons */

    private TextView scrollUpBtn, scrollDownBtn;

    private enum FEATURED_ITEM_RELATIVE_POS {
        UP,
        DOWN,
        VISIBLE
    }
    private FEATURED_ITEM_RELATIVE_POS featuredItemRelativePos;

    TranslateAnimation hideScrollDownBtnAnim, showScrollDownBtnAnim,
            showScrollUpBtnAnim, hideScrollUpBtnAnim;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_test, container, false);
    }

    public void onViewCreated(View v, Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);

        scrollUpBtn = (TextView) getView().findViewById(R.id.scroll_up_btn);
        scrollDownBtn = (TextView) getView().findViewById(R.id.scroll_down_btn);

        // Create animation
        float density = getActivity().getResources().getDisplayMetrics().density;
        showScrollDownBtnAnim = new TranslateAnimation(0, 0, 0, 50 * density);
        hideScrollDownBtnAnim = new TranslateAnimation(0, 0, 50 * density, 0);
        showScrollUpBtnAnim = new TranslateAnimation(0, 0, 0, -50 * density);
        hideScrollUpBtnAnim = new TranslateAnimation(0, 0, -50 * density, 0);

        showScrollDownBtnAnim.setFillAfter(true);
        hideScrollDownBtnAnim.setFillAfter(true);
        hideScrollUpBtnAnim.setFillAfter(true);
        showScrollUpBtnAnim.setFillAfter(true);

        showScrollDownBtnAnim.setDuration(500);
        hideScrollDownBtnAnim.setDuration(500);
        hideScrollUpBtnAnim.setDuration(500);
        showScrollUpBtnAnim.setDuration(500);

        listView = (ListView) getView().findViewById(R.id.listview);

        List<String> items = new ArrayList<String>();
        for(int i=0; i<20; i++) items.add("row " + i);
        String[] array = new String[items.size()];

        listView.setAdapter(new TestAdapter(getActivity(), items.toArray(array)));

        // Scroll to current match
        listView.setSelection(featuredItemPosition);
        featuredItemRelativePos = FEATURED_ITEM_RELATIVE_POS.VISIBLE;

        listView.setOnScrollListener(onScrollListener);

    }

    class TestAdapter extends ArrayAdapter<String> {

        private LayoutInflater layoutInflater;
        private Context context;

        private class ViewHolder {
            TextView text;
        }

        public TestAdapter(Context context, String[] items) {
            super(context, 0, items);
            layoutInflater = ((Activity)context).getLayoutInflater();
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            View v = convertView;

            if (v == null) {
                v = layoutInflater.inflate(R.layout.test_row, parent, false);
                holder = new ViewHolder();
                holder.text = (TextView)v.findViewById(R.id.text);
                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }

            holder.text.setText(getItem(position));
            if(position==featuredItemPosition) holder.text.setBackgroundColor(Color.YELLOW);
            else holder.text.setBackgroundColor(Color.WHITE);

            return v;
        }
    }

    private AbsListView.OnScrollListener onScrollListener =
            new AbsListView.OnScrollListener() {

                public void onScrollStateChanged(AbsListView view, int scrollState) {}

                public void onScroll(AbsListView view, int firstVisibleItem,
                                     int visibleItemCount, int totalItemCount) {


                    if(featuredItemRelativePos==FEATURED_ITEM_RELATIVE_POS.VISIBLE &&
                            firstVisibleItem > featuredItemPosition){
                        featuredItemRelativePos = FEATURED_ITEM_RELATIVE_POS.UP;
                        scrollDownBtn.setVisibility(View.VISIBLE);
                        scrollDownBtn.startAnimation(showScrollDownBtnAnim);
                        Log.d("TestFragment", "Show scroll btn at top");
                    }
                    else if (featuredItemRelativePos==FEATURED_ITEM_RELATIVE_POS.UP &&
                            firstVisibleItem <= featuredItemPosition){
                        featuredItemRelativePos = FEATURED_ITEM_RELATIVE_POS.VISIBLE;
                        scrollDownBtn.startAnimation(hideScrollDownBtnAnim);
                        Log.d("TestFragment", "Hide scroll btn at top");
                    }
                    else if(featuredItemRelativePos==FEATURED_ITEM_RELATIVE_POS.VISIBLE &&
                            firstVisibleItem + visibleItemCount -1 < featuredItemPosition){
                        featuredItemRelativePos = FEATURED_ITEM_RELATIVE_POS.DOWN;
                        scrollUpBtn.setVisibility(View.VISIBLE);
                        scrollUpBtn.startAnimation(showScrollUpBtnAnim);
                        Log.d("TestFragment", "Show scroll btn at bottom");
                    }
                    else if (featuredItemRelativePos==FEATURED_ITEM_RELATIVE_POS.DOWN &&
                            firstVisibleItem + visibleItemCount -1 >= featuredItemPosition){
                        featuredItemRelativePos = FEATURED_ITEM_RELATIVE_POS.VISIBLE;
                        scrollUpBtn.startAnimation(hideScrollUpBtnAnim);
                        Log.d("TestFragment", "Hide scroll btn at bottom");
                    }
                }
            };
}

fragment_test.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:clipToPadding="false">

    <ListView
        android:id="@+id/listview"
        android:layout_below="@+id/header"
        android:layout_above="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView android:id="@+id/scroll_down_btn"
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:layout_above="@id/listview"
        android:text="scroll_down_btn"
        android:gravity="center"
        android:background="#987987"/>

    <TextView android:id="@+id/scroll_up_btn"
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:layout_below="@id/listview"
        android:text="scroll_up_btn"
        android:gravity="center"
        android:background="#987987"/>

    <TextView android:id="@+id/header"
        android:layout_height="30dp"
        android:layout_width="match_parent"
        android:background="#646464"
        android:text="HEADER"
        android:gravity="center"/>

    <TextView android:id="@+id/footer"
        android:layout_height="20dp"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:background="#646464"
        android:text="FOOTER"
        android:gravity="center"/>

</RelativeLayout>

test_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"/>

</RelativeLayout>

0 个答案:

没有答案