Android用StaticLayout實現(xiàn)文字轉(zhuǎn)化為圖片效果(類似長微博發(fā)送)
前言
StaticLayout是android中處理文字換行的一個工具類,StaticLayout已經(jīng)實現(xiàn)了文本繪制換行處理,下面是如何使用StaticLayout的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
效果圖如下:
實例代碼
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的效果是一樣的,其實TextView也是調(diào)用StaticLayout來實現(xiàn)換行的。
StaticLayout的構(gòu)造函數(shù)有三個:
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.畫筆對象
5.layout的寬度,字符串超出寬度時自動換行。
6.layout的對其方式,有ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE 三種。
7.相對行間距,相對字體大小,1.5f表示行間距為1.5倍的字體高度。
8.在基礎(chǔ)行距上添加多少
實際行間距等于這兩者的和。
9.參數(shù)未知
10.從什么位置開始省略
11.超過多少開始省略
需要指出的是這個layout是默認(rèn)畫在Canvas的(0,0)點的,如果需要調(diào)整位置只能在draw之前移Canvas的起始坐標(biāo)
canvas.translate(x,y);
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
詳解Retrofit Interceptor(攔截器) 攔截請求并做相關(guān)處理
本篇文章主要介紹了詳解Retrofit Interceptor(攔截器) 攔截請求并做相關(guān)處理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04Android適配安卓6.0藍(lán)牙通訊實現(xiàn)過程
這篇文章主要為大家詳細(xì)介紹了Android適配安卓6.0藍(lán)牙通訊實現(xiàn)過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09Android判斷應(yīng)用程序退到后臺的方法(示例代碼)
判斷手機(jī)是否退到后臺,這是我們在Android開發(fā)中實現(xiàn)一些功能時,經(jīng)常會考慮的問題,這篇文章主要介紹了android判斷應(yīng)用程序退到后臺的方法,需要的朋友可以參考下2023-03-03Android學(xué)習(xí)教程之2D繪圖基礎(chǔ)及繪制太極圖
這篇文章主要給大家介紹了Android中2D繪圖基礎(chǔ)的相關(guān)資料,文中介紹了繪圖的基礎(chǔ)內(nèi)容,以及通過Canvas和Paint實現(xiàn)繪制太極圖的詳細(xì)過程,對各位Android新手開發(fā)者們具有一定的參考價值,需要的朋友下面來一起看看吧。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-11Android下拉刷新ListView——RTPullListView(demo)
下拉刷新已經(jīng)形成一種默認(rèn)的用戶習(xí)慣,今天主要介紹下在Android上實現(xiàn)下拉刷新的Demo,感興趣的朋友可以參考下哈,希望可以幫助到你2013-04-04