Android开发经验之在图片上随意点击移动文字
只要在图片范围之内,文字可随意点击移动。
- 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);
- }
- }
Android开发经验之在图片上随意点击移动文字相关推荐
- Android WebView 支持H5图片上传input type=file
2019独角兽企业重金招聘Python工程师标准>>> Android WebView 缓存处理 Android WebView 支持H5图片上传<input type=&qu ...
- css如何实现鼠标移至图片上显示遮罩层及文字
css如何实现鼠标移至图片上显示遮罩层及文字 问题:一张图片当鼠标移动到上方时,会显示一个遮罩层,并且显示一些提示文字 html: <div class="contentimg&quo ...
- 鼠标悬停 -css如何实现鼠标移至图片上显示遮罩层及文字
搜索这个效果的时候,很多人都是用css结合jQuery实现的,其实直接用css也可以实现哦~ 效果前: 效果后: 代码: <!DOCTYPE html> <html lang=&qu ...
- Android实现拍照相册图片上传功能
更改头像功能不像修改信息一样直接提交参数就可以,需要上传图片文件 我就直接贴代码了首先给出布局文件 <ImageViewandroid:id="@+id/iv"android ...
- android 自定义图片上传,android自定义ImageView仿图片上传示例
看下效果图 主要看下自定义view 代码 public class ProcessImageView extends ImageView{ private Context context; priva ...
- android 自定义图片上传,android自定义ImageView仿图片上传(示例代码)
Activity代码 1 public classMainActivity extends AppCompatActivity {2 ProcessImageView processImage ...
- android 队列上传图片,话说android端七牛图片上传
七牛图片上传业务流程如下图(这是官方的图): 由上图可知,要想实现图片上传,是要三端进行交互的(我刚刚开始以为只要七牛服务器跟客户端交互就行) 接下来步骤如下: 1.首先肯定是要有一个七牛的账号,并创 ...
- 话说android端七牛图片上传
七牛图片上传业务流程如下图(这是官方的图): 由上图可知,要想实现图片上传,是要三端进行交互的(我刚刚开始以为只要七牛服务器跟客户端交互就行) 接下来步骤如下: 1.首先肯定是要有一个七牛的账号,并创 ...
- HTML如何在图片上添加内容(如文字,菜单栏,按钮等)
HTML如何在图片上添加内容 演示效果 最近做的一个效果是网页头部有一张大图,菜单栏漂浮在图片之上.我看到网上讲的很简单,大致原理就是图片定为相对定位,文字定为绝对定位即可.不过我实现的并不顺利,花了 ...
最新文章
- ORA-24247: network access denied by access control list (ACL)
- 原创 | 数据资产确权浅议
- Paramiko: SSH and SFTP With Python
- 使用stream类型的Result实现Ajax
- 关于前后端交互的一些基础知识点
- 文献记录(part66)--一种基于交叉熵的社区发现算法
- 现在有一个map集合如下: Map<Integer,String> map = new HashMap<Integer, String>(); map.put(1, “
- 用c语言加密,求助:如何用C语言实现LFSR加密
- div和div之间画横线,如何在两个div之间画一条线?
- ASCII对应码表(键值)
- 理解苏宁:互联网转型之战
- 三菱a系列motion软体_三菱各类伺服电机标准参数一览表
- python改变当前工作目录_在python中更改当前工作目录
- hive中reduce类函数说明
- ปอเช็ตติโน่ เสียดายโอกาสของทีมในเกมฟัดหงส์
- 烤仔建工承建,著名画家孙天骄的元宇宙美术馆即将开门迎客
- 多款国产手机无一幸免:人脸识别被破解
- 引入第三方sdk错误提示
- 【OpenGL ES】立方体贴图(6张图)
- 测绘资质高性能数据服务器,测绘资质分级标准2020年
热门文章
- 华为ipd产品开发流程_华为集成产品开发(IPD)流程的解读
- 图像降噪算法——Variance Stabilizing Transform / Generalization Anscombe Transform算法
- 视觉SLAM总结——LSD SLAM中关键知识点总结
- 【每周CV论文推荐】 初学GAN必须要读的文章
- lazadashopee代运营服务有哪些,能帮商家解决哪些问题?
- 中国杀菌剂行业需求趋势与投资战略规划研究报告2021-2027年版
- 粤东农批项目座谈会 农业大健康·李喜贵:功能性农业差异化加工
- 世界农业发展史-国际农民丰收节贸易会:人类的发展史
- websocket与ajax的区别浅析
- 洛谷 P2197 nim游戏