资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android开发中如何实现一个图片中叠加文字功能

本篇文章为大家展示了Android开发中如何实现一个图片中叠加文字功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

阳曲网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联公司自2013年起到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

效果图:

Android开发中如何实现一个图片中叠加文字功能

功能:

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;
  }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>

  
    
    
  
  
  

上述内容就是Android开发中如何实现一个图片中叠加文字功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


分享文章:Android开发中如何实现一个图片中叠加文字功能
本文来源:http://cdkjz.cn/article/jccdso.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220