Android开发经验之在图片上随意点击移动文字
来源:互联网 发布:甲子网络域名 编辑:程序博客网 时间:2024/05/22 09:42
只要在图片范围之内,文字可随意点击移动。
- 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;
- }
- @Override
- public 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);
- }
- }
0 0
- Android开发经验之在图片上随意点击移动文字
- Android开发经验之在图片上随意点击移动文字
- Android开发经验之点击图片判断是否在图片范围之内
- Android开发经验之点击图片判断是否在图片范围之内
- Jquery之鼠标移动到图片上显示文字并且文字可以点击
- Android在界面中随意移动一个图片
- 关于在eclipse开发web时鼠标移动到图片上显示文字问题
- Android ViewBadger --- 在图片上添加文字
- android在图片上添加文字
- 随意移动图片!
- iPhone开发之在图片上显示文字(Add text to UIImage)
- c#图片处理之:在图片上打上文字
- c#图片处理之:在图片上打上文字
- c#图片处理之:在图片上打上文字
- android开发 之 Canvas绘制文字,图片
- iOS开发 在图片上添加文字,图片合成文字,图片上添加富文本,美图秀秀,美颜相机文字编辑
- Android实现 通过手势随意缩放、移动ImageView图片
- Android实现 通过手势随意缩放、移动ImageView图片
- Oracle重做日志文件
- SSH:Struts2框架(拦截器)
- Delphi 更改文件时间
- android中的http通讯----(5)客户端解析xml文件
- 数据挖掘十大算法翻译——3SVM
- Android开发经验之在图片上随意点击移动文字
- iOS消息推送机制
- Oracle directory 目录
- 使用Java正则表达式匹配、替换HTML内容
- lincode刷题--最长单词
- 机器学习(Machine Learning)心得体会(1)线性回归
- 特征向量相似度和距离的计算
- web入门第10天
- Linux grub 引导区修复记录