简体   繁体   中英

Draw text on canvas using a TextView X and Y positions

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.

Don't use the raw version of the coordinates the docs say

Returns the X coordinate of the pointer referenced by pointerIndex for this motion event. The coordinate is in the coordinate space of the device display, irrespective of system decorations and whether or not the system is in multi-window mode. If the app spans multiple screens in a multiple-screen environment, the coordinate space includes all of the spanned screens.

use the event's getX and getY instead as thes are

Returns the X coordinate of the pointer referenced by pointerIndex for this motion event. The coordinate is in the coordinate space of the view that received this motion event.

So the these would be relative to the view time and would be the same coordinates as time 's canvas

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM