Text can be moved as long as it is within the image range.
[Java]
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 = 20, y = 40;
Private static float implements wwidth;
Private static float implements wheight;
Private static float left = 0; // The Position of the image in the screen X coordinate
Private static float top = 0; // Y coordinate of the image on the screen
Private String str = "I Love You ";
Private DisplayMetrics dm = new DisplayMetrics (); // gets the screen height and width.
Private WindowManager windowManager;
Private Bitmap newbitmap;
Public GetTextImage (Context context)
{
Super (context );
WindowManager = (WindowManager) context
. GetSystemService (Context. WINDOW_SERVICE );
// Screen width
Required wwidth = windowManager. getdefadisplay display (). getWidth ();
// Screen height
Required wheight = 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 image, text function: generate the corresponding image based on the given text
*
* @ Param originalMap
* @ Param text
* @ Param x X coordinate of the clicked x
* @ Param y Y y coordinate clicked
* @ Return
* // Www.2cto.com
Public static Bitmap getTextImage (Bitmap originalMap, String text, float x,
Float y)
{
Float bitmapWidth = originalMap. getWidth ();
Float bitmapHeight = originalMap. getHeight ();
// Define the canvas
Canvas canvas = new Canvas (originalMap );
// Define the paint brush
Paint paint = new Paint ();
// Obtain the text length (in pixels)
Float textWidth = paint. measureText (text );
Canvas. drawBitmap (originalMap, 0, 0, null );
// If the image width is smaller than the screen width
If (left + bitmapWidth <descriwwidth)
{
// Right border
If (x> = left + bitmapWidth-textWidth)
{
X = left + bitmapWidth-textWidth;
}
// Left boundary
Else if (x <= left)
{
X = left;
}
}
Else
{
// Right border
If (x> = descriwwidth-textWidth)
{
X = required wwidth-textWidth;
}
// Left boundary
Else if (x <= 0)
{
X = 0;
}
}
// If the image height is smaller than the screen height
If (top + bitmapHeight <windowHeight)
{
// Lower
If (y> = top + bitmapHeight)
{
Y = top + bitmapHeight;
}
// Upload
Else if (y <= top + 10)
{
Y = top + 10;
}
}
Else
{
If (y> = running wheight)
{
Y = running wheight;
}
Else if (y <= 0)
{
Y = 0;
}
}
// Add a 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]
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 );
}
}
From sunset hut