如何在视图上滑动时更改背景颜色?

时间:2014-12-28 16:00:21

标签: android android-layout android-animation android-gesture

我有一个相对布局,在这个相对布局上,我使用以下代码成功处理了从右到左和从左到右事件的滑动。

    relativeLayout1.setOnTouchListener(new View.OnTouchListener() 
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gesture.onTouchEvent(event);
        }
    });


final GestureDetector gesture = new GestureDetector(getActivity(),
        new GestureDetector.SimpleOnGestureListener() {

            @Override
         public boolean onDown(MotionEvent e) {
                return true;
            }
         public boolean onDoubleTap(MotionEvent e) {return true;}
         public void onLongPress(MotionEvent e) {}
         public boolean onDoubleTapEvent(MotionEvent e) {return true;}
         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {return true;}
         public void onShowPress(MotionEvent e) {}
         public boolean onSingleTapConfirmed(MotionEvent e) {return true;}
         public boolean onSingleTapUp(MotionEvent e) {return true;}
            @Override
         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {

                final int SWIPE_MIN_DISTANCE = 5;
                final int SWIPE_MAX_OFF_PATH = 400;
                final int SWIPE_THRESHOLD_VELOCITY = 100;
                try {
                    if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                        return false;
                    if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE// user slide right to left
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        Log.i("Mhd", "Right to Left");

                    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE//user slide left to right
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        Log.i("Mhd", "Left to Right");
                    }
                } catch (Exception e) {

                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }  
        });

但我真的需要根据我的手指到达的位置改变相对布局的颜色,而我在这个相对布局上滑动,例如当你试图调用某个Android设备时,你将从左向右滑动你会注意到根据手指到达联系人视图的位置,颜色会发生变化。

2 个答案:

答案 0 :(得分:0)

使用像这样的选择器xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_background_normal"/>
    <item android:drawable="@drawable/button_background_touched" android:state_hovered="true"/>    
</selector>

答案 1 :(得分:0)

以下示例与三星设备完全相同,从左向右滑动,颜色会根据手指到达联系人视图的位置而变化。然后从右向左滑动以发送消息

首先,此部分的XML应如下所示:

<RelativeLayout
android:id="@+id/YourRelativeLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.27"
android:background="#234c5e"
android:orientation="horizontal"
android:focusable="true"
android:focusableInTouchMode="true">
<LinearLayout
    android:id="@+id/slideColorFromLiftToRightLayout"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="@drawable/color_green"
    android:orientation="horizontal">

</LinearLayout>

<LinearLayout
    android:id="@+id/slideColorFromRightToLiftLayout"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="@drawable/yalow"
    android:orientation="horizontal"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true">
</LinearLayout>

</RelativeLayout>

然后您可以使用此代码片段来检查幻灯片的方向并更改彩色布局的宽度。

    int x1,x2,x3,halfOfWidth = 0;
YourRelativeLayout.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                Log.d("Mhd", "ACTION_MOVE:event.getX() =============> " + event.getX());
                Log.d("Mhd", "ACTION_MOVE:event.getRawX() " + event.getRawX());

                x2 = (int) event.getRawX();

                if (x1 < halfOfWidth && x2 > x1) {
                    /// here check if there user slide from lift to write
                    slideColorFromLiftToRightLayout.requestLayout();
                    slideColorFromLiftToRightLayout.getLayoutParams().width = (int) event.getX();
                }

                if (x1 > halfOfWidth && x2 < x1) {
                    slideColorFromRightToLiftLayout.requestLayout();
                    slideColorFromRightToLiftLayout.getLayoutParams().width = (halfOfWidth * 2) - (int) event.getX();
                }
                return true;
            case MotionEvent.ACTION_UP:

                // this event occure after the user release his finger

                   x3 = (int) event.getX();
//                    slideColorFromLiftToRightLayout.getLayoutParams().width = 0; to clear the color of L to R
//                    slideColorFromRightToLiftLayout.getLayoutParams().width = 0; to clear the color of R to L
                    return true;

            case MotionEvent.ACTION_DOWN:
              // when user put his finger on screen
                x1 = (int) event.getX();
                halfOfWidth = AnswerRelativeLayout.getWidth() / 2;
                return true;
        }
        return false;
    }
});
相关问题