1

我有一个显示图像(ImageView)的应用程序。我希望当我触摸图像上的屏幕时,它会在我触摸的同一点画一条线。

使用此代码我无法做到,但我认为它有一个合乎逻辑的想法。

请问你能帮帮我吗?

谢谢

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class Cronograma extends Activity{

    public boolean touched=false;
    public float touched_x=0, touched_y=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cronograma);
    }

    public void onDraw(Canvas canvas){

        Paint pincel = new Paint();
        pincel.setColor(Color.BLUE);
        pincel.setStrokeWidth(8);
        pincel.setStyle(Style.STROKE);

        if (touched==true)
            canvas.drawLine(touched_x, touched_y, touched_x+5, touched_y, pincel);

    }

    public boolean onTouchEvent (MotionEvent event){

        touched_x = event.getX();
        touched_y = event.getY();

        int action = event.getAction();

        switch (action){

        case MotionEvent.ACTION_DOWN:
            Toast toast = Toast.makeText(getApplicationContext(), "La coordenada x es " + touched_x + " y la coordenada y es " + touched_y , Toast.LENGTH_SHORT);
            toast.show();
            touched=true;
            break;
        }

        return false;

    }

}
4

0 回答 0