我想从一个图像到另一个图像绘制一条简单的线,拖放图像

时间:2013-03-13 13:09:46

标签: android

我正在使用拖放,我正在制作匹配的概念,从左到右拖动图像,当答案纠正时,在那里我想从左到右画一条线,我使用很多东西像< / p>

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;
}

这不起作用,帮帮我

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你得到了什么?

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

答案 2 :(得分: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);
    }
}

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