Someone Somewhere Someone Somewhere - 3 months ago 17
Android Question

Custom view isn't drawing text where I touch it

I'm writing a custom view that should draw text wherever the user touches it:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class TouchTextView extends View
{
private Paint mPaint;
private Path path = new Path();
final String text = "Touched";

public TouchTextView(Context context)
{
super(context);
initialize();
}

/**
* Constructor for XML Inflation
* @param context
* @param attrs
*/
public TouchTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
initialize();
}

public TouchTextView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
initialize();
}

public TouchTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
initialize();
}

private void initialize()
{

mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.BLACK);
mPaint.setTypeface(Typeface.DEFAULT);
mPaint.setTextSize(50);
mPaint.setSubpixelText(true);//Enabling this flag causes glyph advances to be computed with subpixel accuracy.
}

@Override
public void draw(Canvas canvas)
{
super.draw(canvas);
canvas.drawPath(path, mPaint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPaint.getTextPath(text, 0, text.length(), eventX, eventY, path);
return true;
case MotionEvent.ACTION_MOVE:
// nothing to do
break;
case MotionEvent.ACTION_UP:
// nothing to do
break;
default:
return false;
}

// Schedules a repaint.
invalidate();
return true;
}
}


my XML layout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="android.test_app.MainActivity">

<android.test_app.TouchTextView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>


PROBLEM: no text is drawn when I touch the view. Is there anything I'm doing wrong ?

Answer

Your function call invalidate(); is never called because you are leaving the function by return statement.

So change your code to the following:

case MotionEvent.ACTION_DOWN:
                mPaint.getTextPath(text, 0, text.length(), eventX, eventY, path);
                path.close();
                invalidate();
                return true;
Comments