Android用StaticLayout實(shí)現(xiàn)文字轉(zhuǎn)化為圖片效果(類(lèi)似長(zhǎng)微博發(fā)送)
前言
StaticLayout是android中處理文字換行的一個(gè)工具類(lèi),StaticLayout已經(jīng)實(shí)現(xiàn)了文本繪制換行處理,下面是如何使用StaticLayout的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(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來(lái)實(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.需要分行的字符串從第幾的位置開(kāi)始
3.需要分行的字符串到哪里結(jié)束
4.畫(huà)筆對(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.從什么位置開(kāi)始省略
11.超過(guò)多少開(kāi)始省略
需要指出的是這個(gè)layout是默認(rèn)畫(huà)在Canvas的(0,0)點(diǎn)的,如果需要調(diào)整位置只能在draw之前移Canvas的起始坐標(biāo)
canvas.translate(x,y);
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
詳解Retrofit Interceptor(攔截器) 攔截請(qǐng)求并做相關(guān)處理
本篇文章主要介紹了詳解Retrofit Interceptor(攔截器) 攔截請(qǐng)求并做相關(guān)處理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04超實(shí)用的Android手勢(shì)鎖制作實(shí)例教程
這篇文章主要介紹了一個(gè)超實(shí)用的Android手勢(shì)鎖制作實(shí)例教程,普通的圓環(huán)形圖標(biāo)變換,在App和系統(tǒng)的鎖屏界面中都可以調(diào)用,需要的朋友可以參考下2016-04-04分享幾個(gè)Android開(kāi)發(fā)有用的程序代碼
本文主要是給大家分享了幾個(gè)常用而且很實(shí)用的程序代碼片段,都是個(gè)人項(xiàng)目中提取出來(lái)的,有需要的小伙伴可以直接拿走使用2015-02-02Android適配安卓6.0藍(lán)牙通訊實(shí)現(xiàn)過(guò)程
這篇文章主要為大家詳細(xì)介紹了Android適配安卓6.0藍(lán)牙通訊實(shí)現(xiàn)過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android判斷應(yīng)用程序退到后臺(tái)的方法(示例代碼)
判斷手機(jī)是否退到后臺(tái),這是我們?cè)贏ndroid開(kāi)發(fā)中實(shí)現(xiàn)一些功能時(shí),經(jīng)常會(huì)考慮的問(wèn)題,這篇文章主要介紹了android判斷應(yīng)用程序退到后臺(tái)的方法,需要的朋友可以參考下2023-03-03Android學(xué)習(xí)教程之2D繪圖基礎(chǔ)及繪制太極圖
這篇文章主要給大家介紹了Android中2D繪圖基礎(chǔ)的相關(guān)資料,文中介紹了繪圖的基礎(chǔ)內(nèi)容,以及通過(guò)Canvas和Paint實(shí)現(xiàn)繪制太極圖的詳細(xì)過(guò)程,對(duì)各位Android新手開(kāi)發(fā)者們具有一定的參考價(jià)值,需要的朋友下面來(lái)一起看看吧。2017-04-04Android ListView出現(xiàn)異常解決辦法
這篇文章主要介紹了Android ListView出現(xiàn)異常ListView:The content of the adapter has changed but ListView did not receive a notification解決辦法的相關(guān)資料,需要的朋友可以參考下2016-11-11Flutter 設(shè)置全局字體的實(shí)現(xiàn)
本文主要介紹了Flutter 設(shè)置全局字體的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Android下拉刷新ListView——RTPullListView(demo)
下拉刷新已經(jīng)形成一種默認(rèn)的用戶(hù)習(xí)慣,今天主要介紹下在Android上實(shí)現(xiàn)下拉刷新的Demo,感興趣的朋友可以參考下哈,希望可以幫助到你2013-04-04