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中实现在ImageView上随意画线涂鸦
我实现的思路: 1.继承ImageView类 2.重写onTouchEvent方法,在ACTION_MOVE(即移动时),记录下所经过的点坐标,在ACTION_UP时(即手指离开时,这时一条线已经画完 ...
- Android 使用Canvas在图片上绘制文字
一个小应用,在图片上绘制文字,以下是绘制文字的方法,并且能够实现自动换行,字体自动适配屏幕大小 private void drawNewBitmap(ImageView imageView, Stri ...
- android BadgeView的使用(图片上的文字提醒)
BadgeView主要是继承了TextView,所以实际上就是一个TextView,底层放了一个label,可以自定义背景图,自定义背景颜色,是否显示,显示进入的动画效果以及显示的位置等等: 这是Gi ...
- Android模仿QQ空间图片上传——原理
MainActivity.class package com.example.imagedemotext; import java.io.ByteArrayOutputStream; import j ...
- Android EditText中插入图片并响应点击事件
EditText中插入图片基本就是两种方法: ,通过Html.fromHtml(..)来实现 [mw_shl_code=java,true]eText.append(Html.fromHtml(&qu ...
- Formdata 图片上传 Ajax
/*图片上传*/ $("点击对象").bind("click", function(e){ $('#form-upload').remove(); $('bod ...
- CSS实现文字半透明显示在图片上方法
CSS实现文字半透明显示在图片上方法 在css中文字半透明我们会需要使用滤镜效果也就是css中的filter:alpha来实现了,下面来看两个文字显示在图片上并且半透明的例子. CSS让一行文字显示在 ...
- android 加载自定义图片并在图片上绘图
来源:毕设 关键词:Bitmap Canvas //毕设中需要自定义室内地图,并且在地图上绘制轨迹 //此处是一个测试Demo,实现图片的加载和记录手指在屏幕上的运动轨迹 图片的载入 使用系统提供的内 ...
- android拍照选择图片上传服务器自定义控件
做android项目的时候总免不了遇到图片上传功能,虽然就是调用android系统的拍照和相册选择功能,但是总面部了把一大推代码写在activity里,看上去一大推代码头都昏了.不如把这些功能都集成一 ...
随机推荐
- ECNUOJ 2616 游黄山
游黄山 Time Limit:1000MS Memory Limit:65536KB Total Submit:165 Accepted:52 Special Judge Description Po ...
- 洛谷—— P1926 小书童——刷题大军
https://www.luogu.org/problem/show?pid=1926#sub 题目背景 数学是火,点亮物理的灯:物理是灯,照亮化学的路:化学是路,通向生物的坑:生物是坑,埋葬学理的人 ...
- Qt之图形(绘制文本)
简述 前面我们讲解了Qt图形的基本绘制,其中包括: 绘制文本.直线.直线.矩形.弧线.椭圆.多边形.图片,以及其它一些高级用法,比如:渐变.转换等. 本节我们来详细讲解文字的绘制.主要通过QPaint ...
- sql暂时表的创建
create table #simple /*仅仅对当前用户有效.其它用户无法使用,断掉连接后马上销毁该表*/ ( id int not null ) select * from #simple c ...
- Zookeeper简单概念介绍
过去,每个应用都是一个CPU.一个主机上的单一系统.然而今天,随着大数据和云计算时代的到来,不论什么相互独立的程序都可以运行在多个计算机上.然而面临的问题是,协调这些集群的系统比在单一主机上要复杂的多 ...
- div动态加载页面
div动态加载页面 /* /// method 1 var url="<%=basePath%>/qne.do?p=pessegerCountSet"; $.post( ...
- nginx 实现跨域
nginx 添加头部跨域. location / { add_header 'Access-Control-Allow-Origin' '*'; //允许的域 add_header 'Access-C ...
- AJAX 前后端交互 验证信息是否正确
1.前段: function checkPtCode(obj){ $.ajax({ type: "post", url: "xxxxxxx", data: {& ...
- 【玲珑杯 round#18 B】图论你先敲完模板
[Link]:http://www.ifrog.cc/acm/problem/1146 [Description] [Solution] 设f[i]表示在第i个点休息的话最少需要的体力值; f[i]= ...
- 深入浅出WPF 第一部分(3)
3.2.3 属性元素 <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> ...