Android用StaticLayout實(shí)現(xiàn)文字轉(zhuǎn)化為圖片效果(類似長(zhǎng)微博發(fā)送)
前言
StaticLayout是android中處理文字換行的一個(gè)工具類,StaticLayout已經(jīng)實(shí)現(xiàn)了文本繪制換行處理,下面是如何使用StaticLayout的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
效果圖如下:

實(shí)例代碼
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,"輸入內(nèi)容不能為空",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的效果是一樣的,其實(shí)TextView也是調(diào)用StaticLayout來實(shí)現(xiàn)換行的。
StaticLayout的構(gòu)造函數(shù)有三個(gè):
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參數(shù)解釋
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.需要分行的字符串到哪里結(jié)束
4.畫筆對(duì)象
5.layout的寬度,字符串超出寬度時(shí)自動(dòng)換行。
6.layout的對(duì)其方式,有ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE 三種。
7.相對(duì)行間距,相對(duì)字體大小,1.5f表示行間距為1.5倍的字體高度。
8.在基礎(chǔ)行距上添加多少
實(shí)際行間距等于這兩者的和。
9.參數(shù)未知
10.從什么位置開始省略
11.超過多少開始省略
需要指出的是這個(gè)layout是默認(rèn)畫在Canvas的(0,0)點(diǎn)的,如果需要調(diào)整位置只能在draw之前移Canvas的起始坐標(biāo)
canvas.translate(x,y);
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android canvas drawBitmap方法詳解及實(shí)例
這篇文章主要介紹了 Android canvas drawBitmap方法詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android SlidingDrawer 抽屜效果的實(shí)現(xiàn)
本篇文章小編為大家介紹,Android SlidingDrawer 抽屜效果的實(shí)現(xiàn)。需要的朋友參考下2013-04-04
Android數(shù)據(jù)類型之間相互轉(zhuǎn)換系統(tǒng)介紹
一些初學(xué)Android的朋友可能會(huì)遇到JAVA的數(shù)據(jù)類型之間轉(zhuǎn)換的苦惱;本文將為有這類需求的朋友解決此類問題2012-11-11
Flutter控件之實(shí)現(xiàn)Widget基類的封裝
在實(shí)際的開發(fā)中,Widget的基類還是很有必要存在的,不然就會(huì)存在很多的冗余嵌套代碼,本文為大家介紹了Flutter中基類是如何封裝的,需要的可以收藏一下2023-05-05
Android 6.0權(quán)限申請(qǐng)?jiān)斀饧皺?quán)限資料整理
這篇文章主要介紹了Android 6.0權(quán)限申請(qǐng)?jiān)斀饧皺?quán)限資料整理的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android實(shí)現(xiàn)多線程斷點(diǎn)下載
大家好,本篇文章主要講的是Android實(shí)現(xiàn)多線程斷點(diǎn)下載,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
android實(shí)現(xiàn)定時(shí)拍照并發(fā)送微博功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)定時(shí)拍照并發(fā)送微博功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06

