안녕하세요~ 바로 본론 들ㅇㅓ가겠습니다.
아래 소스에서 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;
}
}
감사합니다ㅠ