Android拖动imageview时拉伸背景图像

时间:2015-02-05 11:26:43

标签: android scrollview drag ontouchlistener

我尝试实施图片控件垂直滑块。图像位于ScrollView内。当涉及ImageView的垂直拖动时,相对布局的上边距总是提供不同的读数。

如果在向下拖动时大于600,则相对布局的背景图像与我拖动的图像位置一起垂直延伸。

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:fillViewport="true" >
...


   <RelativeLayout
                android:id="@+id/relativeLayouyt6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/rTop"
                android:background="@drawable/plain" >

                <ImageView
                    android:id="@+id/dragImage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/arrow_one"
                    android:visibility="gone" />
            </RelativeLayout>

请你告诉我如何

  1. 获取我在我的滚动视图中滚动的偏移量?
  2. 我知道触摸位置减去滚动位置加上图像Y位置,完成拖动后如何实现此参数?
  3. 如何在相对布局中设置imageview的相对边界?将此背景图像作为图像视图更明智吗?
  4. 如果我以编程方式重新定位6个相对布局但是达到相同的宽度,是否会影响滚动位置和scollview总高度?如果是这样,如何计算重新定位的偏移量Y?
  5. 以下是截至2月11日的代码:

    dragImage.setOnTouchListener(new OnTouchListener(){
                    //private int _xDelta;
                    private int _yDelta;
                @Override
                public boolean onTouch(View v, MotionEvent event) {
    
                    v.getParent().requestDisallowInterceptTouchEvent(true);
    
    
    
                    final float y = event.getY();
    
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //_yDelta = Y - lParams.topMargin;
                        mOldY2 = y;
                        break;
                    case MotionEvent.ACTION_UP:
                        mViewPager.setPagingEnabled(true);
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        btn4.setBackgroundResource(R.drawable.fbl02);
                        final float dy = y - mOldY2;
                        mNewY2 += dy;
                        mOldY2 = y;
                        System.out.println(mNewY2);                 
    
                        while(mNewY2 > 224){
                            mNewY2 -= 224;
                        }
    
                        while(mNewY2 < 157){
                            mNewY2 += 157;
                        }
    
    
                        if(mNewY2 < 157 || mNewY2 > 224)
                            break;
    
                        v.setTranslationY((int)mNewY2);
    
                        v.invalidate();
    
                        float power = (float) ( 51.5/67 -(0.2/67) * mNewY2) ;
                        System.out.println(power);
    
                        Float roundF = new Float( Math.round(power));
                        midBandStick = roundF;
                        btn4.setText(String.valueOf(midBandStick) );
                        //}
                        //break;
                    }
                    return true;
    
                }
    

    以下是我的代码:

    public static void setRLBelowAnother(RelativeLayout rA , RelativeLayout rB   ){         
            RelativeLayout.LayoutParams rparam4 =  
                    (android.widget.RelativeLayout.LayoutParams) rB.getLayoutParams();
            rparam4.addRule(RelativeLayout.BELOW, rA.getId());              
            rB.setLayoutParams(rparam4);             
        } 
    
    
    
                setRLBelowAnother(rTop , r1);
                setRLBelowAnother(r1 , r2);
                setRLBelowAnother(r2 , r6  );
                setRLBelowAnother(r6 , r3  );
                setRLBelowAnother(r3 , r4 );
                setRLBelowAnother(r4 , r5  );
        dragImage.setVisibility(View.VISIBLE);
            dragImage.setImageResource(R.drawable.slide_lshort);
    
            dragImage.setX((float) (0.15*screenWidth));
            dragImage.setY((float) (0.05*screenHeight));
            dragImage.setOnTouchListener(new OnTouchListener(){
                //private int _xDelta;
                private int _yDelta;
                @Override
                public boolean onTouch(View v, MotionEvent event) {
    
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    final int X = (int) event.getX();
                    final int Y = (int) event.getY();
                    switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
    
    
    
                        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) dragImage
                        .getLayoutParams();
    
                        //_xDelta = X - lParams.leftMargin;
                        _yDelta = Y - lParams.topMargin;
                        break;
                    case MotionEvent.ACTION_UP:
                        mViewPager.setPagingEnabled(true);
    
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
    
                        midStick = 0.2f;
                        btn4.setBackgroundResource(R.drawable.fbl02);
    
                        RelativeLayout.LayoutParams ParamsA = (RelativeLayout.LayoutParams) dragImage
                        .getLayoutParams();
                        //ParamsA.leftMargin = X - _xDelta;
                        ParamsA.topMargin = Y - _yDelta;
                        //ParamsA.rightMargin = -250;
                        ParamsA.bottomMargin = -250;
                        mViewPager.setPagingEnabled(false);
    
    
                        int offYb = 0;
                        int pos = ParamsA.topMargin +  offYb ;
                        if(pos > -52 &&  pos < 582 ){
                            dragImage.setLayoutParams(ParamsA);
                            System.out.println(ParamsA.topMargin);
                            float power = (float) (100 + (900/634) * ParamsA.topMargin) ;
                            Float roundF = new Float( Math.round(power));
                            midStick = roundF;
                            btn4.setText(String.valueOf(midStick));
    
                        }
                        break;
                    }
                    return true;
    
                }
    
            });
    

1 个答案:

答案 0 :(得分:0)

  1. getScrollY()调用ScrollView方法获取Y索引(滚动索引)

  2. 如果我理解(否则请纠正我)你可以在ImageView和RelativeLayout之间获得一个框架(边界),为ImageView添加填充或边距。你只需要调用android:padding =&#34;&#34;或者android:margin =&#34;&#34;

  3. 如果新添加的RelativeLayout / ImageView不完全适合可用空间,则ScrollView的高度会发生变化,scrollY也会发生变化。添加新布局后,您可以从ScrollView获取scrollY并查看ScrollView滚动到的位置。

  4. 你能改进问题2,3和4吗?这很令人困惑。