Android開發經驗之在圖片上隨意點擊移動文字

來源:互聯網
上載者:User

 

 

只要在圖片範圍之內,文字可隨意點擊移動。

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 windowWidth;private static float windowHeight;private static float left = 0;      //圖片在螢幕中位置X座標private static float top = 0;       //圖片在螢幕中位置Y座標private String str = "我愛你";private DisplayMetrics dm = new DisplayMetrics();  //用於擷取螢幕的高度和寬度private WindowManager windowManager;private Bitmap newbitmap;public GetTextImage(Context context){super(context);windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);//螢幕的寬度windowWidth = windowManager.getDefaultDisplay().getWidth();//螢幕的高度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);}/** * 傳回值: Bitmap 參數:原圖片,文字 功能: 根據給定的文字產生相應圖片 *  * @param originalMap  * @param text  文字 * @param x  點擊的X座標 * @param y  點擊的Y座標 * @return */public static Bitmap getTextImage(Bitmap originalMap, String text, float x,float y){float bitmapWidth = originalMap.getWidth();float bitmapHeight = originalMap.getHeight();// 定義畫布    Canvas canvas = new Canvas(originalMap);    // 定義畫筆Paint paint = new Paint();//獲得文本的長度(像素)float textWidth = paint.measureText(text); canvas.drawBitmap(originalMap, 0, 0, null);// 如果圖片寬度小於螢幕寬度if (left + bitmapWidth < windowWidth){// 右邊界if (x >= left + bitmapWidth - textWidth){x = left + bitmapWidth - textWidth;}// 左邊界else if (x <= left){x = left;}}else{// 右邊界if (x >= windowWidth - textWidth){x = windowWidth - textWidth;}// 左邊界else if (x <= 0){x = 0;}}// 如果圖片高度小於螢幕高度if (top + bitmapHeight < windowHeight){// 下if (y >= top + bitmapHeight){y = top + bitmapHeight;}// 上else if (y <= top + 10){y = top + 10;}}else{if (y >= windowHeight){y = windowHeight;}else if (y <= 0){y = 0;}}// 添加字    canvas.drawText(text, x, y, paint);return originalMap;}@Overridepublic boolean onTouchEvent(MotionEvent event){if (event.getAction() == MotionEvent.ACTION_DOWN){x = event.getX();y = event.getY();// 重繪invalidate();}return true;}}

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);    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.