简单描述
TextDrawable的用处是把文字转为drawable,然后使用imgeView设置该drawable。
TextDrawable实际上是自定义Drawable.
效果
用法
eclipse为例,只需要在项目里加入自定义Drawable,即TextDrawable类,然后再Activity调用即可。
TextDrawable可以从下面地址的项目里找到
GitHub地址:https://github.com/amulyakhare/TextDrawable
使用
Activity
package com.example.learing;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 正方形
ImageView mImageOne = (ImageView) findViewById(R.id.img1);
TextDrawable d = TextDrawable.builder().buildRound("1", Color.RED);
mImageOne.setImageDrawable(d);
// 圆形
ImageView mImageTwo = (ImageView) findViewById(R.id.img2);
TextDrawable r = TextDrawable.builder().buildRect("J", Color.GREEN);
mImageTwo.setImageDrawable(r);
// 圆角矩形
ImageView mImageThree = (ImageView) findViewById(R.id.img3);
TextDrawable l = TextDrawable.builder().buildRoundRect("S6", Color.YELLOW, 30);
mImageThree.setImageDrawable(l);
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:background="#333" >
<ImageView
android:id="@+id/img1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp" />
<ImageView
android:id="@+id/img2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp" />
<ImageView
android:id="@+id/img3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp" />
</LinearLayout>
转载于:http://www.jcodecraeer.com/a/opensource/2014/1122/2044.html