如何从拖放操作的源到目标绘制一条线?

时间:2013-03-18 06:41:25

标签: android drag-and-drop line draw

我已经实现了一个拖放系统,现在我想从源到目标绘制一条线。我怎么画这条线?我已经使用了View类的扩展,但是它没有用。这是我的代码:

public class TempDDActivity extends Activity implements OnTouchListener {
    /** Called when the activity is first created. */
    private View selected_item = null;
    private int offset_x = 0;
    private int offset_y = 0;
    Boolean touchFlag=false;
    boolean dropFlag=false;
    LayoutParams imageParams;
    ImageView imageDrop,image1,image2;
    int crashX,crashY;
    Drawable dropDrawable,selectDrawable;
    Rect dropRect,selectRect;
    int topy,leftX,rightX,bottomY;

    int dropArray[];    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        ViewGroup container = (ViewGroup) findViewById(R.id.container);
        imageDrop=(ImageView) findViewById(R.id.ImgDrop);       
        image1=(ImageView) findViewById(R.id.img);      
        image2=(ImageView) findViewById(R.id.img2);
        container.setOnTouchListener(new View.OnTouchListener() 
        {
            public boolean onTouch(View v, MotionEvent event) 
            {
                if(touchFlag==true)
                {
                    System.err.println("Display If  Part ::->"+touchFlag);
                    switch (event.getActionMasked()) 
                    {
                    case MotionEvent.ACTION_DOWN :

                         topy=imageDrop.getTop();
                         leftX=imageDrop.getLeft();
                         rightX=imageDrop.getRight();   
                         bottomY=imageDrop.getBottom();
                        System.err.println("Display Top-->"+topy);      
                        System.err.println("Display Left-->"+leftX);
                        System.err.println("Display Right-->"+rightX);
                        System.err.println("Display Bottom-->"+bottomY);                


                        //opRect.
                        break;
                    case MotionEvent.ACTION_MOVE:
                        crashX=(int) event.getX();
                        crashY=(int) event.getY();
                        System.err.println("Display Here X Value-->"+crashX);
                        System.err.println("Display Here Y Value-->"+crashY);

                        int x = (int) event.getX() - offset_x;
                        int y = (int) event.getY() - offset_y;                                          
                        //int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
                        //int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
                        int w = getWindowManager().getDefaultDisplay().getWidth() - 50;
                        int h = getWindowManager().getDefaultDisplay().getHeight() - 10;
                        if (x > w)
                            x = w;
                        if (y > h)
                            y = h;                      
                        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams(  RelativeLayout.LayoutParams.WRAP_CONTENT,   RelativeLayout.LayoutParams.WRAP_CONTENT));
                        lp.setMargins(x, y, 0, 0);                  

                        //Drop Image Here                       
                        if(crashX > leftX && crashX < rightX && crashY > topy && crashY < bottomY )                     
                        {                           
                            Drawable temp=selected_item.getBackground();                            
                            imageDrop.setBackgroundDrawable(temp);
                            imageDrop.bringToFront();                           
                            dropFlag=true;
                            selected_item.setVisibility(View.INVISIBLE);
                        }
                        //Drop Image Here                       
                        selected_item.setLayoutParams(lp);
                        break;  
                    case MotionEvent.ACTION_UP:
                        //                      
                        touchFlag=false;
                        if(dropFlag==true)
                        {
                            dropFlag=false;
                        }
                        else
                        {
                            selected_item.setLayoutParams(imageParams);
                        }                       
                        break;
                    default:
                        break;
                    }
                }else
                {
                    System.err.println("Display Else Part ::->"+touchFlag);
                }               
                return true;
            }
        });

        image1.setOnTouchListener(this);
        image2.setOnTouchListener(this);
    }

    public boolean onTouch(View v, MotionEvent event) 
    {   
        switch (event.getActionMasked()) 
        {
        case MotionEvent.ACTION_DOWN:
            touchFlag=true;
            offset_x = (int) event.getX();
            offset_y = (int) event.getY();
            selected_item = v;
            imageParams=v.getLayoutParams();
            break;
        case MotionEvent.ACTION_UP:
            selected_item=null;
            touchFlag=false;
            break;
        default:
            break;
        }       
        return false;
    }
}

1 个答案:

答案 0 :(得分:1)

  

现在我想从源到目的地画一​​条线

首先,您需要有一个自定义视图来实际绘制线条。这将是包裹ImageViews的布局,在我看来,我认为是RelativeLayout。那个班级会是这样的:

public class DragObserverLayout extends RelativeLayout {

    float startX, startY, stopX, stopY;
    private Paint mPaint = new Paint();
    private List<Rect> lines = new ArrayList<Rect>();

    public DragObserverLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint.setColor(Color.GREEN);
        mPaint.setStrokeWidth(2.0f);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        final int count = lines.size();
        for (int i = 0; i < count; i++) {
            final Rect r = lines.get(i);
            canvas.drawLine(r.left, r.top, r.right, r.bottom, mPaint);
        }
    }

    public void addLine(Rect r) {
        lines.add(r);
        invalidate();
    }

}

然后在您进行拖动操作的容器的OnTouchListener中,您只需执行以下操作:

    final int[] location = new int[2];
    final DragObserverLayout container = (DragObserverLayout ) findViewById(R.id.container);       
    container.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if(touchFlag==true) {
                switch (event.getActionMasked()) 
                {
                case MotionEvent.ACTION_DOWN :
                    //...
                    selected_item.getLocationOnScreen(location);
        container.startX = location[0];
        container.startY = location[1];  
                    break;
                case MotionEvent.ACTION_MOVE:
                  //...
                    selected_item.getLocationOnScreen(location);            
                    break;  
                case MotionEvent.ACTION_UP:
                    // ...
                    // .. the item was dragged on the target
                    if (selected_item.getVisibility() != View.VISIBLE) {
                        Rect r = new Rect();
                        r.left = (int) container.startX;
                        r.top = (int) container.startY;
                        imageDrop.getLocationInWindow(location);
                        r.right = location[0];
                        r.bottom = location[1];
                        container.addLine(r);
                    } 

这将从拖动的ImageView(视图的左上角)的初始位置到当前位置绘制线,直到ImageView被“删除” ”。如果您希望通过onTouch方法对ImageViews进行一些简单计算,可以将起始线的位置偏移到指向实际触摸位置。这也适用于覆盖整个屏幕的全屏应用程序,否则您必须将getLocationOnScreen()的回报抵消以考虑状态栏。如果要在拖动操作完成后将线保留在屏幕上,则需要将其存储在容器中。