마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

WebView와 Canvas와의 listener 질문입니다~ [closed]

0 추천

안녕하세요~ 바로 본론 들ㅇㅓ가겠습니다.

아래 소스에서 canvas의 ondraw가 return true이면 webview에 ontouch까지 못오고

                                             return이 false면 webview의 ontouch로 오는데 그리기가 안되더라구요..

제가 필요한건 그림도 그려주고 webview의 ontouch도 하고 싶은 것입니다. 어떠한 방법이 있을까요?

 

또한, 혹시 webview의 터치했을 당시의 webview의 html element를 알 수 있을까요?(hitTextResult를 이용하려하는데 이해를 못하는건지 잘 안먹더라구요..ㅎ)

 

소스입니다 

>Main

public class MainActivity extends Activity {

    private CanvasView customCanvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
        WebView wv = new WebView(getBaseContext());
        wv.setLayoutParams(new LinearLayout.LayoutParams(1000, 1000));
        wv.setBackgroundColor(Color.GRAY);

        wv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d("TAG","onTouc" +
                        "h in WebView");
                return false;
            }
        });
        customCanvas = new CanvasView(getBaseContext());
        wv.addView(customCanvas);

        mainLayout.addView(wv);
    }
}

>Canvas

public class CanvasView extends View  {

    public int width;
    public int height;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    Context context;
    private Paint mPaint;
    private float mX, mY;
    private static final float TOLERANCE = 5;

    public CanvasView(Context c) {
        super(c);
        //super.setBackgroundColor(Color.YELLOW);
        context = c;

        // we set a new Path
        mPath = new Path();

        // and we set a new Paint with the desired attributes
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeWidth(4f);
    }

    // override onSizeChanged
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        // your Canvas will draw onto the defined Bitmap
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    // override onDraw
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d("TAG","onDraw in Canvas");
        // draw the mPath with the mPaint on the canvas when onDraw
        canvas.drawPath(mPath, mPaint);
    }

    // when ACTION_DOWN start touch according to the x,y values
    private void startTouch(float x, float y) {
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    // when ACTION_MOVE move touch according to the x,y values
    private void moveTouch(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOLERANCE || dy >= TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
    }

    public void clearCanvas() {
        mPath.reset();
        invalidate();
    }

    // when ACTION_UP stop touch
    private void upTouch() {
        mPath.lineTo(mX, mY);
    }

    //override the onTouchEvent
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d("TAG","onTouch in Canvas");

        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startTouch(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                moveTouch(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                upTouch();
                invalidate();
                break;
        }
        return true;
        //return false;
    }


}

 

감사합니다ㅠ

질문을 종료한 이유: 기간 지남
행인28 (480 포인트) 님이 2016년 5월 21일 질문
행인28님이 2016년 5월 22일 closed
...