android中拖动文字实现功能,Android:图片中叠加文字,支持拖动改变位置

之所以做了这么一个Demo,是因为最近项目中有一个奇葩的需求:用户拍摄照片后,分享到微信的同时添加备注,想获取用户在微信的弹出框输入的内容,保存在自己的服务器上。而事实上,这个内容程序是无法获取的,因此采取了一个折衷方案,将文字直接写在图片上。

首先上Demo效果图:

21053093T_0.png

功能:

1.用户自由输入内容,可手动换行,并且行满也会自动换行。

2.可拖动改变图片中文本位置(文字不会超出图片区域)。

3.点击“生成图片”按钮之后,生成一张带有文字的图片文件。

代码不多,直接全部贴上了:

Activity:

/**

* 将文字写在图片中(截图方式),支持拖动文字。

* 说明:很明显,截图方式会降低图片的质量。如果需要保持图片质量可以使用canvas的方式,将文字直接绘制在图片之上(不过,使用此方式要实现文字拖动较为复杂)。

*/

public class MainActivity extends AppCompatActivity {

//图片组件

private ImageView imageView;

//位于图片中的文本组件

private TextView tvInImage;

//图片和文本的父组件

private View containerView;

//父组件的尺寸

private float imageWidth, imageHeight, imagePositionX, imagePositionY;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.image_with_text);

imageView = (ImageView) findViewById(R.id.writeText_img);

EditText editText = (EditText) findViewById(R.id.writeText_et);

tvInImage = (TextView) findViewById(R.id.writeText_image_tv);

containerView = findViewById(R.id.writeText_img_rl);

imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

imagePositionX = imageView.getX();

imagePositionY = imageView.getY();

imageWidth = imageView.getWidth();

imageHeight = imageView.getHeight();

//设置文本大小

tvInImage.setMaxWidth((int) imageWidth);

}

});

imageView.setImageBitmap(getScaledBitmap(R.mipmap.test_img));

//输入框

editText.addTextChangedListener(new TextWatcher() {

@Override

public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {

if (s.toString().equals()) {

tvInImage.setVisibility(View.INVISIBLE);

} else {

tvInImage.setVisibility(View.VISIBLE);

tvInImage.setText(s);

}

}

@Override

public void afterTextChanged(Editable s) {

}

});

final GestureDetector gestureDetector = new GestureDetector(this, new SimpleGestureListenerImpl());

//移动

tvInImage.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

gestureDetector.onTouchEvent(event);

return true;

}

});

}

//确认,生成图片

public void confirm(View view) {

Bitmap bm = loadBitmapFromView(containerView);

String filePath = Environment.getExternalStorageDirectory() + File.separator + image_with_text.jpg;

try {

bm.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));

Toast.makeText(this, 图片已保存至:SD卡根目录/image_with_text.jpg, Toast.LENGTH_LONG).show();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

//以图片形式获取View显示的内容(类似于截图)

public static Bitmap loadBitmapFromView(View view) {

Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);

view.draw(canvas);

return bitmap;

}

private int count = 0;

//tvInImage的x方向和y方向移动量

private float mDx, mDy;

//移动

private class SimpleGestureListenerImpl extends GestureDetector.SimpleOnGestureListener {

@Override

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

//向右移动时,distanceX为负;向左移动时,distanceX为正

//向下移动时,distanceY为负;向上移动时,distanceY为正

count++;

mDx -= distanceX;

mDy -= distanceY;

//边界检查

mDx = calPosition(imagePositionX - tvInImage.getX(), imagePositionX + imageWidth - (tvInImage.getX() + tvInImage.getWidth()), mDx);

mDy = calPosition(imagePositionY - tvInImage.getY(), imagePositionY + imageHeight - (tvInImage.getY() + tvInImage.getHeight()), mDy);

//控制刷新频率

if (count % 5 == 0) {

tvInImage.setX(tvInImage.getX() + mDx);

tvInImage.setY(tvInImage.getY() + mDy);

}

return true;

}

}

//计算正确的显示位置(不能超出边界)

private float calPosition(float min, float max, float current) {

if (current < min) {

return min;

}

if (current > max) {

return max;

}

return current;

}

//获取压缩后的bitmap

private Bitmap getScaledBitmap(int resId) {

BitmapFactory.Options opt = new BitmapFactory.Options();

opt.inJustDecodeBounds = true;

BitmapFactory.decodeResource(getResources(), resId, opt);

opt.inSampleSize = Utility.calculateInSampleSize(opt, 600, 800);

opt.inJustDecodeBounds = false;

return BitmapFactory.decodeResource(getResources(), resId, opt);

}

}

一个工具类:

public class Utility {

//计算 inSampleSize 值,压缩图片

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

// Raw height and width of image

final int height = options.outHeight;

final int width = options.outWidth;

int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;

final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both

// height and width larger than the requested height and width.

while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {

inSampleSize *= 2;

}

}

return inSampleSize;

}

}

相关报道:

在进行数据查询的时候我们有真分页和假分页两种,所谓真分页就是按照根据pageIndex(当前页码)和pageSize(每页的记录条数)去数据库中查找响应的记录,而假分 更多

一.基本规则1.函数定义 在python中函数用关键字def声明,参数用逗号隔开,另外需要注意的是函数没有返回类型.Python函数不指定特定的返回类型,甚至不需要指定是否返回一个.但实际上,每一个python函数都会返回一 个.如果执行了return语句,那么它会返回 更多

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/419986.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Mac 下nginx 环境的配置

这个是在度娘那里学来的。 因为是使用brew所以先安装&#xff1a; 安装命令如下&#xff1a;curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C/usr/local --strip 1当brew安装成功后&#xff0c;就可以随意安装自己想要的软件了&#xff0c;例如w…

android抽屉屏幕右滑,android - Android - 使滑动抽屉从左向右滑动 - 堆栈内存溢出...

我使用下面的XML布局在我的应用程序中实现了“Sliding Drawer”:(我从androidpeople.com得到了这个例子)android:layout_width"fill_parent" android:layout_height"fill_parent"xmlns:android"http://schemas.android.com/apk/res/android"andr…

bzoj 3196/tyvj p1730 二逼平衡树

原题链接&#xff1a;http://www.tyvj.cn/p/1730 树套树。。。 如下&#xff1a; 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<algorithm> 5 #define lc root<<1 6 #define rc root<<1|1 7 #define INF…

观察者模式与Boost.Signals

1&#xff09; 观察者模式定义 略&#xff0c;各种设计模式的书上都有定义。 2&#xff09; 观察者模式一般实现 观察者模式一般实现&#xff0c;都是“被观察者”保存一个“观察者”的列表&#xff0c;循环这个列表来通知“观察者”。代码&#xff0c;其中使用了boost的智能…

Android获取最新发送短信的基本信息,没有之一

注册&#xff1a; getContentResolver().registerContentObserver( Uri.parse("content://sms"), true, new SmsObserver(this, new Handler())); 监听&#xff1a; //用于检测发出的短信 public class SmsObserver extends Conten…

联想android刷机教程,超详细的联想刷机教程~带你嘻刷刷

一、刷机是什么说到“刷机”&#xff0c;很多人可能会和“升级”混淆起来&#xff0c;其实升级和刷机并不是同一概念。通俗地讲&#xff0c;升级就是对手机内的软件或系统进行升级&#xff0c;比如很多厂商手机都支持的OTA空中在线升级。而刷机&#xff0c;则相当于就是重装系统…

我的github地址

我的github仓库地址 https://github.com/xutiantian/Test转载于:https://www.cnblogs.com/xuxiaomeng/p/4455850.html

多看 android6,多看阅读本地版

为您推荐&#xff1a;多看阅读《多看阅读本地版》是一款由多看科技倾情研发打造的海量优质完本小说免费在线阅读app软件&#xff0c;这款软件的功能非常的全面&#xff0c;操作性简单&#xff0c;上手起来非常的容易&#xff0c;在这款软件里&#xff0c;各位用户们将能够于此体…

UIProgressView-初识IOS

好几天没更新了&#xff0c;学的时候太紧&#xff0c;没时间复习了都。今天刚好有时间&#xff0c;多更几个。 今天复习的是UIProgressView,我们常见使用在修改某些属性的时候经常用到&#xff0c;比如透明度&#xff0c;今天我们介绍一个简单的使用例子 定义什么的&#xff0c…

android正则判断两个符号之间,Android字母、数字、字符任意两种组合正则验证

释放双眼&#xff0c;带上耳机&#xff0c;听听看~&#xff01;最近朋友有个用户名验证&#xff0c;要求字母、数字、字符任意两种组合即可&#xff0c;让我帮写个正则验证&#xff0c;现在正则验证如下&#xff1a;/*** 判断是否匹配正则** param regex 正则表达式* param inp…

android手机deviceowner,删除 androidDeviceOwnerWiFiConfiguration

删除 androidDeviceOwnerWiFiConfigurationDelete androidDeviceOwnerWiFiConfiguration2021/3/24本文内容命名空间&#xff1a;microsoft.graphNamespace: microsoft.graph重要提示&#xff1a; /beta 版本下的 Microsoft Graph API 可能会更改;不支持生产使用。Important: Mi…

浅谈0/1切换

前言:   做过GUI开发的同学, 都知晓双缓存机制. 其过程为先把所有的场景和实体对象画到一个备份canvas, 然后再把备份canvas的内容整个填充真正的画板canvas中. 如果不采用双缓存机制, 你的画面有可能会出现闪烁和抖动.   究其原因是整个绘制过程, 包含清屏, 绘制场景和各…

Action和Func区别

Action<>和Func<>其实都是委托的【代理】简写形式。 简单的委托写法&#xff1a; 1 //普通的委托2 public delegate void myDelegate(string str);3 4 //Delegate委托调。5 myDelegate dDelegate new myDelegate(SayHellow);6 dDelegate("Mr wang");7 8…

最好的android one手机,最高配置的Android One手机登场 LG推出G7 One与G7 F

原标题&#xff1a;最高配置的Android One手机登场 LG推出G7 One与G7 F集微网消息&#xff0c;Android One原本是谷歌与中国台湾的联发科共同开发的一个项目&#xff0c;旨在让手机制造商打造低成本的智能手机&#xff0c;这些手机主要是销售给新兴市场上的近10亿潜在用户。随着…

CAEmitterLayer 和 CAEmitterCell 粒子发射

CAEmitterLayer emitterCells&#xff1a;CAEmitterCell对象的数组&#xff0c;被用于把粒子投放到layer上 birthRate:可以通俗的理解为发射源的个数&#xff0c;默认1.0。当前每秒产生的真实粒子数为CAEmitterLayer的birthRate*子粒子的birthRate&#xff1b; lifetime emitte…

android 音量调节 seekbar,Android 使用SeekBar调节系统音量

以下是一个使用SeekBar来调节系统音量的实例&#xff1a;1、XML&#xff1a;android:id"id/sound"android:layout_width"150px"android:layout_height"10px"android:max"100"//设置拖动条最大值android:progress"10"//设置拖…

vs代码模板制作

VS2008代码模板制作 一&#xff0c;类模板制作&#xff1a; 路径&#xff1a;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\2052\Class.zip 操作&#xff1a;打开Class.cs文件&#xff0c;编辑内容如下&#xff1a; // <…

html语言文本框的符号,HTML中的文本框textarea标签

用来创建一个可以输入多行的文本框&#xff0c;此标志对用于标志对之间。具有以下属性&#xff1a;(1)onchange指定控件改变时要调用的函数(2)onfocus当控件接受焦点时要执行的函数(3)onblur当控件失去焦点时要执行的函数(4)onselect当控件内容被选中时要执行的函数(5)name这文…

pads导出坐标文件html,【教程】PADS如何导出SMT贴片机用的坐标文件

找到一个好办法&#xff0c;用wps的Excel软件的话&#xff0c;将脚本进行如下修改即可。修改前&#xff1a;Sub ExportToExcel (txt As String)FillClipboardDim xl As ObjectOn Error Resume NextSet xl GetObject(,"Excel.Application")On Error GoTo ExcelError …