1
我有一個畫布文本使用drawText方法繪製旋轉-45度,我需要的是一個觸摸動作的文本部分單獨,參考圖像下面, Android Canvas OnDraw觸摸偵聽器的旋轉文本
我的代碼如下
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint newpaint=new Paint();
newpaint.setColor(Color.RED);
canvas.drawLine(10,10,getMeasuredWidth(),10,newpaint);
if(lines.size()>0)
{
Paint BluePaint=new Paint();
BluePaint.setColor(Color.BLUE);
BluePaint.setStrokeWidth(1.5f);
for (Line l : lines)
{
canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY+50, BluePaint);
canvas.save();
canvas.rotate((float) (-45), l.startX, l.stopY+160);
canvas.drawText("Add Location",l.startX-10,l.stopY+90,BluePaint);
canvas.restore();
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
float x = event.getX();
float y = event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
//Check if the x and y position of the touch is inside the bitmap
if(x > bitmapXPosition && x < bitmapXPosition + getMeasuredWidth() && y > bitmapYPosition && y < bitmapYPosition + 5)
{
lines.add(new Line(event.getX(), event.getY()));
invalidate();
return true;
}
}
return false;
}
class Line {
float startX, startY, stopX, stopY;
public Line(float startX, float startY, float stopX, float stopY) {
this.startX = startX;
this.startY = startY;
this.stopX = stopX;
this.stopY = stopY;
}
public Line(float startX, float startY) { // for convenience
this(startX, startY, startX, startY);
}
}
我已經檢測線觸摸事件,但不能能夠檢測到文本的觸摸事件。幫助解決這個問題。