1

I'm using drag and drop , I'm making matching concept , drag image from left to right, when answer will correct , at there i want to draw a line from left to right , i use many things as like

public class Dragtesting extends Activity implements OnTouchListener{
 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.drag);
        ViewGroup dlayout = (ViewGroup)findViewById(R.id.dlayout);
  dlayout.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
            // TODO Auto-generated method stub
            if(touchFlag==true){
 switch (paramMotionEvent.getActionMasked()) 
                {
case MotionEvent.ACTION_DOWN :
 //getting the positions
break;
  case MotionEvent.ACTION_UP:
break;

  Canvas canvas = new Canvas();
  Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.GREEN);
canvas.drawColor(Color.BLUE);

canvas.drawLine(offset_x, offset_y, currentX, currentY, paint); 
  break;
                default:
                    break;
                }

          }
            return true;
        }
    });
@Override
public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
    // TODO Auto-generated method stub
    switch (paramMotionEvent.getActionMasked()) 
    {
    case MotionEvent.ACTION_DOWN:
        touchFlag=true;
        offset_x = (int) paramMotionEvent.getX();
        offset_y = (int) paramMotionEvent.getY();
       // tagvalue = (Integer) paramView.getTag();
        selected_item =  paramView;
        imageParams=paramView.getLayoutParams();
        break;
    case MotionEvent.ACTION_UP:
        selected_item=null;
        touchFlag=false;
        break;
    default:
        break;
    }       
    return false;
}

this is not working , help me

4

3 回答 3

1

我扩展了相对布局并制作了一个设计时视图文件,用于在图像视图之间画一条线,感谢所有帮助我,但这不是正确的答案,我不接受其他答案,我很抱歉

于 2013-03-22T10:12:31.423 回答
0
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class LineView extends View {

    private Paint paint = new Paint();
    private float offset_x;
    private float offset_y;
    private float currentX;
    private float currentY;

    public LineView(Context context) {
        super(context);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            offset_x = currentX = event.getX();
            offset_y = currentY = event.getY();
        } else {
            currentX = event.getX();
            currentY = event.getY();
        }
        invalidate();
        return true;
    }

    @Override
    public void draw(Canvas canvas) {
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(Color.GREEN);
        canvas.drawColor(Color.BLUE);

        canvas.drawLine(offset_x, offset_y, currentX, currentY, paint);
    }
}

你去吧。应该是这样的。测试:)

于 2013-03-13T13:21:56.587 回答
0

你得到了什么?

invalidate(); 在绘画之前尝试写作 。

于 2013-03-13T13:15:40.403 回答