最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

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

最新評(píng)論

政和县| 淮阳县| 教育| 滨海县| 宜黄县| 西充县| 泸州市| 临湘市| 石家庄市| 滨海县| 德江县| 中山市| 喀喇沁旗| 通榆县| 淮安市| 威宁| 大田县| 旬邑县| 塔河县| 六盘水市| 鄢陵县| 昌吉市| 四子王旗| 巧家县| 汝州市| 富宁县| 恩平市| 乐昌市| 昌乐县| 神农架林区| 永寿县| 香港 | 荥经县| 商城县| 黄冈市| 团风县| 泰兴市| 宜兰市| 阳新县| 曲沃县| 宜春市|