Issue
I've an activity contains TextView
and a WallpaperService
where I draw some text on the canvas and display it as live wallpaper, I'm trying to draw a text on canvas
using a TextView
X and Y coordinates. Using the below code, I'm able to move the TextView
around the screen.
textView.setOnTouchListener((view, event) -> {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_MOVE:
view.setY(event.getRawY() + dY);
view.setX(event.getRawX() + dX);
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_UP:
x = (float) event.getRawX();
y = (float) event.getRawY();
break;
default:
return false;
}
return true;
});
Draw text based on the X and Y coordinates :
Rect rect = new Rect();
Paint timePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
timePaint.setTextSize(DigitalClockHelper.getScaledTextSize(115));
timePaint.setColor(Color.BLACK);
timePaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
timePaint.getTextBounds(time, 0, time.length(), rect);
timePaint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(time, x, y, timePaint);
My Issue:
When I draw the text on the canvas
it's drawn correctly but in a different place, just a little bit higher where it should be drawn.
**My question **
How I can draw text on WallpaperService
exactly where the TextView
placed in the activity.
Thank you.
Solution
Thanks to @MikeM. For helping me to fix my issue by providing an example
However, I've ended up using the following :
int y = view.getY() + view.getBaseline()
Also, since the status bar and the navigation bar are hidden in the live wallpaper preview, I've made the activity full screen to make it similar to the live wallpaper preview.
Answered By - Mouaad Abdelghafour AITALI
Answer Checked By - Senaida (JavaFixing Volunteer)