当前位置: 首页 > 编程笔记 >

Android用StaticLayout实现文字转化为图片效果(类似长微博发送)

南门鸿雪
2023-03-14
本文向大家介绍Android用StaticLayout实现文字转化为图片效果(类似长微博发送),包括了Android用StaticLayout实现文字转化为图片效果(类似长微博发送)的使用技巧和注意事项,需要的朋友参考一下

前言

StaticLayout是android中处理文字换行的一个工具类,StaticLayout已经实现了文本绘制换行处理,下面是如何使用StaticLayout的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

效果图如下:

实例代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private EditText textView;
private ImageView imageView;
private Button btn;
private String content;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = (EditText) findViewById(R.id.input_text);
imageView = (ImageView) findViewById(R.id.input_image);
imageView.setVisibility(View.INVISIBLE);
btn = (Button) findViewById(R.id.btn_close);
btn.setOnClickListener(this);
//

}

public static Bitmap textAsBitmap(String text, float textSize) {

TextPaint textPaint = new TextPaint();

// textPaint.setARGB(0x31, 0x31, 0x31, 0);
textPaint.setColor(Color.BLACK);
textPaint.setAntiAlias(true);
textPaint.setTextSize(textSize);

StaticLayout layout = new StaticLayout(text, textPaint, 450,
Layout.Alignment.ALIGN_NORMAL, 1.3f, 0.0f, true);
Bitmap bitmap = Bitmap.createBitmap(layout.getWidth() + 20,
layout.getHeight() + 20, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.translate(10, 10);
// canvas.drawColor(Color.GRAY);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);//绘制透明色
layout.draw(canvas);
Log.d("textAsBitmap",
String.format("1:%d %d", layout.getWidth(), layout.getHeight()));
return bitmap;
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_close:
content = textView.getText().toString().trim();
if (content != null && content != "") {
Bitmap bitmap = textAsBitmap(content, 28);
imageView.setVisibility(View.VISIBLE);
imageView.setBackgroundResource(R.mipmap.liaotian);
imageView.setImageBitmap(bitmap);
}else{
Toast.makeText(MainActivity.this,"输入内容不能为空",Toast.LENGTH_SHORT);
}
}
}
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.admin.enjoytalk.MainActivity">

<TextView

android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

<!--<android.support.v7.widget.RecyclerView-->
<!--android:layout_centerInParent="true"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--/>-->
<EditText
android:id="@+id/input_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_close"
android:layout_width="match_parent"
android:text="输入完成"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/input_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

这跟TextView的效果是一样的,其实TextView也是调用StaticLayout来实现换行的。

StaticLayout的构造函数有三个:

public StaticLayout(CharSequence source,
   TextPaint paint,
   int width,
   Layout.Alignment align,
   float spacingmult,
   float spacingadd,
   boolean includepad)
   
public StaticLayout(CharSequence source,
   int bufstart,
   int bufend,
   TextPaint paint,
   int outerwidth,
   Layout.Alignment align,
   float spacingmult,
   float spacingadd,
   boolean includepad)
   
public StaticLayout(CharSequence source,
   int bufstart,
   int bufend,
   TextPaint paint,
   int outerwidth,
   Layout.Alignment align,
   float spacingmult,
   float spacingadd,
   boolean includepad,
   TextUtils.TruncateAt ellipsize,
   int ellipsizedWidth)

android StaticLayout参数解释

StaticLayout(CharSequence source, int bufstart, int bufend,
  TextPaint paint, int outerwidth,
  Alignment align,
  float spacingmult, float spacingadd,
  boolean includepad,
  TextUtils.TruncateAt ellipsize, int ellipsizedWidth)

1.需要分行的字符串

2.需要分行的字符串从第几的位置开始

3.需要分行的字符串到哪里结束

4.画笔对象

5.layout的宽度,字符串超出宽度时自动换行。

6.layout的对其方式,有ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE 三种。

7.相对行间距,相对字体大小,1.5f表示行间距为1.5倍的字体高度。

8.在基础行距上添加多少

实际行间距等于这两者的和。

9.参数未知

10.从什么位置开始省略

11.超过多少开始省略

需要指出的是这个layout是默认画在Canvas的(0,0)点的,如果需要调整位置只能在draw之前移Canvas的起始坐标
canvas.translate(x,y);

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对小牛知识库的支持。

 类似资料:
  • 本文向大家介绍js控住DOM实现发布微博效果,包括了js控住DOM实现发布微博效果的使用技巧和注意事项,需要的朋友参考一下 这段代码的效果具体是输入标题和内容,点击发布把消息发布出去,并使最新的消息始终在内容的最上面,代码为: 这段代码主要运用了一些DOM节点操作的知识,纯属学习之余练手作品,大家可以参考参考。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍Android仿微信微博多图展示效果,包括了Android仿微信微博多图展示效果的使用技巧和注意事项,需要的朋友参考一下 1.简介 这是一个用于实现像微信朋友圈和微博的类似的九宫格图片展示控件,通过自定义viewgroup实现,使用方便。 多图根据屏幕适配,单张图片时需要自己指定图片的宽高; 2.使用方法 引用: compile 'com.w4lle.library:NineLayo

  • 本文向大家介绍avalonjs实现仿微博的图片拖动特效,包括了avalonjs实现仿微博的图片拖动特效的使用技巧和注意事项,需要的朋友参考一下 效果: HTML: JS: 以上所述就是本文的全部内容了,希望大家能够喜欢。

  • 本文向大家介绍Android实现图片滚动效果,包括了Android实现图片滚动效果的使用技巧和注意事项,需要的朋友参考一下 Android开发图片滚动效果,供大家参考,具体内容如下 效果图: 设置适配来设置图片位置大小 main添加图片资源 布局 drawable放置图片资源 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍Android实现图片文字轮播特效,包括了Android实现图片文字轮播特效的使用技巧和注意事项,需要的朋友参考一下 本文实例讲解了Android实现图片文字轮播特效的详细代码,分享给大家供大家参考,具体内容如下 图片轮播是类似知乎日报上的一个轮播效果,如下图。 好了直接进入正题,首先是出示一下效果: MainActivity: activity_main:  以上就是关于Andro

  • 本文向大家介绍java实现新浪微博Oauth接口发送图片和文字的方法,包括了java实现新浪微博Oauth接口发送图片和文字的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了java实现新浪微博Oauth接口发送图片和文字的方法。分享给大家供大家参考。具体如下: 基于网上很多人利用新浪api开发新浪微博客户端的时候遇到无法发图片的问题,很多人卡在了这一布。现将代码呈上,希望能帮到一些朋