zoukankan      html  css  js  c++  java
  • Android开发经验之在图片上随意点击移动文字

    只要在图片范围之内,文字可随意点击移动。

    1. package xiaosi.GetTextImage;  
    2.   
    3.   
    4. import android.content.Context;  
    5. import android.content.res.Resources;  
    6. import android.graphics.Bitmap;  
    7. import android.graphics.BitmapFactory;  
    8. import android.graphics.Canvas;  
    9. import android.graphics.Paint;  
    10. import android.util.DisplayMetrics;  
    11. import android.view.MotionEvent;  
    12. import android.view.View;  
    13. import android.view.WindowManager;  
    14.   
    15. public class GetTextImage extends View  
    16. {  
    17.     private float x = 20, y = 40;  
    18.     private static float windowWidth;  
    19.     private static float windowHeight;  
    20.     private static float left = 0;      //图片在屏幕中位置X坐标  
    21.     private static float top = 0;       //图片在屏幕中位置Y坐标  
    22.     private String str = "我爱你";  
    23.     private DisplayMetrics dm = new DisplayMetrics();  //用于获取屏幕的高度和宽度  
    24.     private WindowManager windowManager;  
    25.     private Bitmap newbitmap;  
    26.   
    27.     public GetTextImage(Context context)  
    28.     {  
    29.         super(context);  
    30.         windowManager = (WindowManager) context  
    31.                 .getSystemService(Context.WINDOW_SERVICE);  
    32.         //屏幕的宽度  
    33.         windowWidth = windowManager.getDefaultDisplay().getWidth();  
    34.         //屏幕的高度  
    35.         windowHeight = windowManager.getDefaultDisplay().getHeight();  
    36.     }  
    37.   
    38.     public void onDraw(Canvas canvas)  
    39.     {  
    40.         Resources res = getResources();  
    41.         Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.b);  
    42.         newbitmap = getTextImage(bmp, str, x, y);  
    43.         canvas.drawBitmap(newbitmap, 0, 0, null);  
    44.     }  
    45.     /** 
    46.      * 返回值: Bitmap 参数:原图片,文字 功能: 根据给定的文字生成相应图片 
    47.      *  
    48.      * @param originalMap  
    49.      * @param text  文字 
    50.      * @param x  点击的X坐标 
    51.      * @param y  点击的Y坐标 
    52.      * @return 
    53.      */  
    54.     public static Bitmap getTextImage(Bitmap originalMap, String text, float x,  
    55.             float y)  
    56.     {  
    57.         float bitmapWidth = originalMap.getWidth();  
    58.         float bitmapHeight = originalMap.getHeight();  
    59.         // 定义画布  
    60.         Canvas canvas = new Canvas(originalMap);  
    61.         // 定义画笔  
    62.         Paint paint = new Paint();  
    63.         //获得文本的长度(像素)  
    64.         float textWidth = paint.measureText(text);   
    65.         canvas.drawBitmap(originalMap, 0, 0, null);  
    66.           
    67.         // 如果图片宽度小于屏幕宽度  
    68.         if (left + bitmapWidth < windowWidth)  
    69.         {  
    70.             // 右边界  
    71.             if (x >= left + bitmapWidth - textWidth)  
    72.             {  
    73.                 x = left + bitmapWidth - textWidth;  
    74.             }  
    75.             // 左边界  
    76.             else if (x <= left)  
    77.             {  
    78.                 x = left;  
    79.             }  
    80.         }  
    81.         else  
    82.         {  
    83.             // 右边界  
    84.             if (x >= windowWidth - textWidth)  
    85.             {  
    86.                 x = windowWidth - textWidth;  
    87.             }  
    88.             // 左边界  
    89.             else if (x <= 0)  
    90.             {  
    91.                 x = 0;  
    92.             }  
    93.         }  
    94.         // 如果图片高度小于屏幕高度  
    95.         if (top + bitmapHeight < windowHeight)  
    96.         {  
    97.             // 下  
    98.             if (y >= top + bitmapHeight)  
    99.             {  
    100.                 y = top + bitmapHeight;  
    101.             }  
    102.             // 上  
    103.             else if (y <= top + 10)  
    104.             {  
    105.                 y = top + 10;  
    106.             }  
    107.         }  
    108.         else  
    109.         {  
    110.             if (y >= windowHeight)  
    111.             {  
    112.                 y = windowHeight;  
    113.             }  
    114.             else if (y <= 0)  
    115.             {  
    116.                 y = 0;  
    117.             }  
    118.         }  
    119.           
    120.         // 添加字  
    121.         canvas.drawText(text, x, y, paint);  
    122.         return originalMap;  
    123.     }  
    124.     @Override  
    125.     public boolean onTouchEvent(MotionEvent event)  
    126.     {  
    127.         if (event.getAction() == MotionEvent.ACTION_DOWN)  
    128.         {  
    129.             x = event.getX();  
    130.             y = event.getY();  
    131.             // 重绘  
    132.             invalidate();  
    133.         }  
    134.         return true;  
    135.     }  
    136. }  


    1. package xiaosi.GetTextImage;  
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5.   
    6. public class GetTextImageActivity extends Activity {  
    7.     /** Called when the activity is first created. */  
    8.     private GetTextImage get;  
    9.     @Override  
    10.     public void onCreate(Bundle savedInstanceState) {  
    11.         super.onCreate(savedInstanceState);  
    12.         get = new GetTextImage(this);  
    13.         setContentView(get);  
    14.     }  
    15. }  


  • 相关阅读:
    (CodeForces 548B 暴力) Mike and Fun
    (BestCoder Round #64 (div.2))Array
    (2015 杭电校赛 )玩骰子
    js基础
    frame框架标签
    html
    div嵌套div标签 里层的div透明属性
    div嵌套div标签
    盒子标签div嵌套
    divspan盒子模型
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6722199.html
Copyright © 2011-2022 走看看