本文实例讲述了Android编程实现支持拖动改变位置的图片中叠加文字功能。分享给大家供大家参考,具体如下:

之所以做了这么一个Demo,是因为最近项目中有一个奇葩的需求:用户拍摄照片后,分享到微信的同时添加备注,想获取用户在微信的弹出框输入的内容,保存在自己的服务器上。而事实上,这个内容程序是无法获取的,因此采取了一个折衷方案,将文字直接写在图片上。
首先上Demo效果图:
功能:
1.用户自由输入内容,可手动换行,并且行满也会自动换行。
2.可拖动改变图片中文本位置(文字不会超出图片区域)。
3.点击“生成图片”按钮之后,生成一张带有文字的图片文件。
代码不多,直接全部贴上了:
Activity:
/**
* 将文字写在图片中(截图方式),支持拖动文字。<br/>
* 说明:很明显,截图方式会降低图片的质量。如果需要保持图片质量可以使用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;
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<RelativeLayout
android:id="@+id/writeText_img_rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<ImageView
android:id="@+id/writeText_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="360dp"
android:adjustViewBounds="true"
android:contentDescription="@null"/>
<TextView
android:id="@+id/writeText_image_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_centerInParent="true"
android:background="#79652a"
android:clickable="true"
android:padding="4dp"
android:textColor="@android:color/white"
android:textSize="15sp" />
</RelativeLayout>
<EditText
android:id="@+id/writeText_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="添加备注" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="confirm"
android:text="生成图片" />
</LinearLayout>
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
# Android
# 拖动
# 改变位置
# 图片
# 叠加
# 文字
# Android编程实现图片背景渐变切换与图层叠加效果
# Android实现图片叠加效果的两种方法
# android中TabHost的图标(48×48)和文字叠加解决方法
# Android实现图片叠加功能
# 写在
# 自己的
# 换行
# 进阶
# 是因为
# 也会
# 相关内容
# 不多
# 中有
# 感兴趣
# 给大家
# 弹出
# 可以使用
# 很明显
# 更多关于
# 类似于
# 解决方法
# 贴上
# 所述
相关文章:
网站制作与设计教程,如何制作一个企业网站,建设网站的基本步骤有哪些?
大型企业网站制作流程,做网站需要注册公司吗?
如何高效配置香港服务器实现快速建站?
电视网站制作tvbox接口,云海电视怎样自定义添加电视源?
如何制作网站标识牌,动态网站如何制作(教程)?
网站制作壁纸教程视频,电脑壁纸网站?
广州网站设计制作一条龙,广州巨网网络科技有限公司是干什么的?
建站之星收费标准详解:套餐费用及年费价格表一览
学校建站服务器如何选型才能满足性能需求?
广州美橙建站如何快速搭建多端合一网站?
建站之星IIS配置教程:代码生成技巧与站点搭建指南
常州自助建站工具推荐:低成本搭建与模板选择技巧
建站之星导航如何优化提升用户体验?
如何在云指建站中生成FTP站点?
小建面朝正北,A点实际方位是否存在偏差?
哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?
如何在Mac上搭建Golang开发环境_使用Homebrew安装和管理Go版本
建站之星CMS五站合一模板配置与SEO优化指南
如何在阿里云服务器自主搭建网站?
如何在IIS中配置站点IP、端口及主机头?
如何彻底删除建站之星生成的Banner?
移动端手机网站制作软件,掌上时代,移动端网站的谷歌SEO该如何做?
,有什么在线背英语单词效率比较高的网站?
标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?
如何选择适配移动端的WAP自助建站平台?
如何选择高效响应式自助建站源码系统?
西安专业网站制作公司有哪些,陕西省建行官方网站?
建站之星如何通过成品分离优化网站效率?
网站制作新手教程,新手建设一个网站需要注意些什么?
专业型网站制作公司有哪些,我设计专业的,谁给推荐几个设计师兼职类的网站?
建站主机服务器选购指南:轻量应用与VPS配置解析
上海网站制作开发公司,上海买房比较好的网站有哪些?
测试制作网站有哪些,测试性取向的权威测试或者网站?
rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted
小自动建站系统:AI智能生成+拖拽模板,多端适配一键搭建
c# F# 的 MailboxProcessor 和 C# 的 Actor 模型
沈阳制作网站公司排名,沈阳装饰协会官方网站?
html制作网站的步骤有哪些,iapp如何添加网页?
如何快速启动建站代理加盟业务?
广东企业建站网站优化与SEO营销核心策略指南
建站之星微信建站一键生成小程序+多端营销系统
成都网站制作公司哪家好,四川省职工服务网是做什么用?
如何用好域名打造高点击率的自主建站?
大连 网站制作,大连天途有线官网?
微信h5制作网站有哪些,免费微信H5页面制作工具?
SQL查询语句优化的实用方法总结
建站之星五站合一营销型网站搭建攻略,流量入口全覆盖优化指南
如何通过多用户协作模板快速搭建高效企业网站?
如何用花生壳三步快速搭建专属网站?
如何在阿里云通过域名搭建网站?
*请认真填写需求信息,我们会在24小时内与您取得联系。