亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android用StaticLayout實(shí)現(xiàn)文字轉(zhuǎn)化為圖片效果(類(lèi)似長(zhǎng)微博發(fā)送)

 更新時(shí)間:2017年08月28日 11:41:55   作者:一生中所愛(ài)  
這篇文章主要給大家介紹了關(guān)于Android利用StaticLayout實(shí)現(xiàn)文字轉(zhuǎn)化為圖片效果,實(shí)現(xiàn)的效果類(lèi)似我們常見(jiàn)的長(zhǎng)微博效果,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。

前言

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)文章

最新評(píng)論