视差在android中无法正常工作

时间:2015-03-27 20:07:00

标签: android android-5.0-lollipop

下面的代码是快速移动图像视图,但我想从顶部和底部缩小图像并在其上滑动线性布局。但它不能正常工作。我应该改变什么以使其像google playstore一样工作imageview / scrollview? BAsically如何减少imageview的大小,并允许linearlayout重复它?

public class Parallax extends Activity {

    ImageView im1;
    ScrollView sv1;

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

        im1=(ImageView)findViewById(R.id.imageView12);

        sv1=(ScrollView)findViewById(R.id.scrollView);

        ll=(LinearLayout)findViewById(R.id.linear);


        final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener = new
                ViewTreeObserver.OnScrollChangedListener() {

                    @Override
                    public void onScrollChanged() {
                        //do stuff here


                        Rect scrollBounds = new Rect();
                        sv1.getHitRect(scrollBounds);
                        if (im1.getLocalVisibleRect(scrollBounds)) {


                            im1.setTranslationY(   im1.getScrollY() -sv1.getScrollY() );


                        } else {

                        }
                    }
                };
        sv1.setOnTouchListener(new View.OnTouchListener() {
            private ViewTreeObserver observer;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (observer == null) {
                    observer = sv1.getViewTreeObserver();
                    observer.addOnScrollChangedListener(onScrollChangedListener);
                }
                else if (!observer.isAlive()) {
                    observer.removeOnScrollChangedListener(onScrollChangedListener);
                    observer = sv1.getViewTreeObserver();
                    observer.addOnScrollChangedListener(onScrollChangedListener);
                }

                return false;
            }
        });



    }

//// XML

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="1500dp"

        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:id="@+id/imageView12"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher"
            />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button20"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="65dp" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="300dp"
            android:background="#329053"
            android:id="@+id/linear"
            android:layout_below="@+id/imageView12"

            android:layout_centerHorizontal="true"></LinearLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:id="@+id/imageView13"
            android:layout_below="@+id/linear"
            android:layout_centerHorizontal="true"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher"
            android:adjustViewBounds="false"
            />


    </RelativeLayout>

</ScrollView>

0 个答案:

没有答案
相关问题