开源控件TextDrawable

本文介绍如何使用TextDrawable类将文本转换为drawable,并将其应用于ImageView中,通过示例代码展示了创建不同形状和颜色的drawable的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单描述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值