As long as within the image range, the text can be arbitrarily clicked to move.
[Java]View PlainCopy
- Package Xiaosi. Gettextimage;
- Import Android.content.Context;
- Import android.content.res.Resources;
- Import Android.graphics.Bitmap;
- Import Android.graphics.BitmapFactory;
- Import Android.graphics.Canvas;
- Import Android.graphics.Paint;
- Import Android.util.DisplayMetrics;
- Import android.view.MotionEvent;
- Import Android.view.View;
- Import Android.view.WindowManager;
- Public class Gettextimage extends View
- {
- private Float x = , y = 40;
- private static float windowwidth;
- private static float windowheight;
- private static float left = 0; //Picture in the screen position x coordinate
- private static float top = 0; //Picture in the screen position y coordinate
- private String str = "I love You";
- private displaymetrics DM = new Displaymetrics (); //To get the height and width of the screen
- private WindowManager WindowManager;
- private Bitmap Newbitmap;
- Public Gettextimage (context context)
- {
- super (context);
- WindowManager = (WindowManager) context
- . Getsystemservice (Context.window_service);
- //width of the screen
- WindowWidth = Windowmanager.getdefaultdisplay (). GetWidth ();
- //height of screen
- WindowHeight = Windowmanager.getdefaultdisplay (). GetHeight ();
- }
- public void OnDraw (canvas canvas)
- {
- Resources res = getresources ();
- Bitmap BMP = Bitmapfactory.decoderesource (res, r.drawable.b);
- Newbitmap = gettextimage (BMP, str, x, y);
- Canvas.drawbitmap (Newbitmap, 0, 0, null);
- }
- /**
- * Return value: Bitmap parameter: Original picture, text function: generate the corresponding picture according to the given text
- *
- * @param originalmap
- * @param text text
- * @param x Click X-coordinate
- * y-coordinate @param y-click
- * @return
- */
- public static Bitmap gettextimage (Bitmap originalmap, String text, float x,
- float y)
- {
- float bitmapwidth = Originalmap.getwidth ();
- float bitmapheight = Originalmap.getheight ();
- //define Canvas
- Canvas canvas = new Canvas (ORIGINALMAP);
- //define brushes
- Paint paint = new paint ();
- //Get the length of the text (in pixels)
- float textWidth = paint.measuretext (text);
- Canvas.drawbitmap (Originalmap, 0, 0, null);
- //If the picture width is less than the screen width
- if (left + bitmapwidth < windowwidth)
- {
- //Right Border
- if (x >= left + bitmapwidth-textwidth)
- {
- x = left + bitmapwidth-textwidth;
- }
- //Left Border
- Else if (x <= left)
- {
- x = left;
- }
- }
- Else
- {
- //Right Border
- if (x >= windowwidth-textwidth)
- {
- x = Windowwidth-textwidth;
- }
- //Left Border
- Else if (x <= 0)
- {
- x = 0;
- }
- }
- //If the picture height is less than the screen height
- if (top + bitmapheight < WindowHeight)
- {
- //Down
- if (y >= top + bitmapheight)
- {
- y = top + bitmapheight;
- }
- //On
- Else if (y <= top + Ten)
- {
- y = top + 10;
- }
- }
- Else
- {
- if (y >= windowheight)
- {
- y = windowheight;
- }
- Else if (y <= 0)
- {
- y = 0;
- }
- }
- //Add word
- Canvas.drawtext (text, x, y, paint);
- return originalmap;
- }
- @Override
- Public Boolean ontouchevent (Motionevent event)
- {
- if (event.getaction () = = Motionevent.action_down)
- {
- x = Event.getx ();
- y = event.gety ();
- // Redraw
- Invalidate ();
- }
- return true;
- }
- }
[Java]View PlainCopy
- Package Xiaosi. Gettextimage;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Public class Gettextimageactivity extends Activity {
- /** Called when the activity is first created. * /
- private Gettextimage get;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- get = new Gettextimage (this);
- Setcontentview (get);
- }
- }
Android Development experience at random click on the picture to move the text