0

我正在尝试在 android 上的图像内拖动和调整大小的 textview。我能够在触摸 TextView 时拖动,但它会拖动到屏幕之外。你能帮我在图像中拖动 textview。代码在这里

  public void drag(MotionEvent event, View v){

    RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) v.getLayoutParams();

    switch(event.getAction())
    {
       case MotionEvent.ACTION_MOVE:
       {
           Log.v("touch","move");
         params.topMargin = (int)event.getRawY() - (textView.getHeight());
         params.leftMargin = (int)event.getRawX() - (textView.getWidth()/2);
        int y= (int)event.getRawY();
        int x=(int)event.getRawX();

         DisplayMetrics dm = new DisplayMetrics();
         getWindowManager().getDefaultDisplay().getMetrics(dm);
         if(params.topMargin>=0 && params.leftMargin>=0  )
             textView.setLayoutParams(params);
         break;
       }
       case MotionEvent.ACTION_UP:
       {
           Log.v("touch","up");

         params.topMargin = (int)event.getRawY() - (textView.getHeight());
         params.leftMargin = (int)event.getRawX() - (textView.getWidth()/2);

         Log.v("touch", "params.topMargin"+params.topMargin);
         Log.v("touch", " params.leftMargin"+ params.leftMargin);

         if( ( params.topMargin>=0) && (params.leftMargin>=0))

                 textView.setLayoutParams(params);

        // textView.setLayoutParams(params);
         break;
       }
       case MotionEvent.ACTION_DOWN:
       {
           textView.setLayoutParams(params);
        break;
       }
    }
}
4

2 回答 2

0

你真的需要拖动来移动TextView吗?你可以扩展TextView,重新定义public boolean onTouch(View v, MotionEvent event),在里面计算offsetX和offsetY,添加接口,比如OnPositionChangeListener(int offsetX, int offsetY),让包含这个RelativeLayout的activity实现这个接口。它对我有用。

于 2013-02-27T10:46:16.477 回答
0
private void initDragView() {
        // text view drag
        _root = (ViewGroup) findViewById(R.id.tblDragView);

        _root.addView(txtContent);
        _root.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                case MotionEvent.ACTION_MOVE:
                    int x = (int) event.getX() - _xDelta;
                    int y = (int) event.getY() - _yDelta;
                    int w = backgroundImageWidth;// getWindowManager().getDefaultDisplay().getWidth();
                    int h = backgroundImageHeight;// getWindowManager().getDefaultDisplay().getHeight();
                    if (x < 0 || y < 0 || x > w || y > h)
                        return false;

                    if (x > w) {

                        x = w;
                        return true;
                    }
                    if (y > h) {
                        // Log.d("TAG", "Ouch i am out side height");
                        y = h;
                        return true;
                    }

                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                            new ViewGroup.MarginLayoutParams(
                                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                                    RelativeLayout.LayoutParams.WRAP_CONTENT));
                    if (lp.leftMargin < x && lp.topMargin < y)
                        lp.setMargins(x, y, 0, 0);
                    // txtContent.setBackgroundColor(getResources().getColor(
                    // R.color.blue));
                    txtContent.setLayoutParams(lp);

                    break;
                default:
                    break;
                }
                return true;
            }
        });

        txtContent.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) event.getX();
                    _yDelta = (int) event.getY();

                    txtContent = (TextView) v;
                    break;
                default:
                    break;
                }
                return false;
            }
        });

    }
于 2013-03-11T11:14:25.257 回答