Android-文字转图片+镂空/斜体/粗体/自定义字体+背景色、

一、引入自定义字体文件(不需要的可忽略该步骤)

1、在项目工程的res文件夹下新建font文件夹,并将我们的字体文件引入工程。

2、代码使用

        Typeface typeface = mContext.getResources().getFont(R.font.alimama_shuheiti_bold);
        myTextView.setTypeface(typeface);

二、代码调用

1、新建工具类TextToBitmap

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;

public class TextToBitmap {

    /**
     * @param mContext     Context上下文
     * @param textStr      输入文本内容
     * @param textColorStr 文本颜色
     * @param textSize     文本大小
     * @param isSetBgColor 是否设置图片背景颜色,false则默认透明色
     * @param bgColorStr   如果isSetBgColor为true,则该参数必须传值
     * @param isBold       是否设置字体加粗
     * @param isIncline    是否设置字体倾斜
     * @param isHollowOut  是否设置镂空效果
     * @return
     */
    public static Bitmap textChangeToBitmap(Context mContext,
                                            String textStr,
                                            String textColorStr,
                                            float textSize,
                                            boolean isSetBgColor,
                                            String bgColorStr,
                                            boolean isBold,
                                            boolean isIncline,
                                            boolean isHollowOut) {

        // 1.0、创建一个文本字体处理对象
        TextPaint textPaint = new TextPaint();
        // 设置自定义字体样式(使整段代码可删除,则字体会默认字体)
        Typeface typeface = mContext.getResources().getFont(R.font.alimama_shuheiti_bold);
        textPaint.setTypeface(typeface);
        // 设置字体颜色
        // textPaint.setColor(Color.BLACK);
        // textPaint.setColor(Color.parseColor("#FF0000"));
        textPaint.setColor(Color.parseColor(textColorStr));
        // 设置字体尺寸
        textPaint.setTextSize(textSize);
        // 判断是否设置字体倾斜
        if (isIncline) {
            // 设置水印字体倾斜
            textPaint.setTextSkewX(-0.5f);
        }
        // 判断是否设置水印字体加粗
        if (isBold) {
            // 设置水印字体加粗
            textPaint.setFakeBoldText(true);
        }
        // 判断是否设置字体镂空
        if (isHollowOut) {
            // 设置字体镂空
            textPaint.setStyle(Paint.Style.STROKE);
            // 设置笔画线条宽度
            textPaint.setStrokeWidth(10);
        }

        //  2.0、创建一个文本尺寸管理对象
        StaticLayout staticLayout = new StaticLayout(textStr,
                textPaint,
                (int) textPaint.measureText(textStr),
                Layout.Alignment.ALIGN_NORMAL,
                1.3f,
                0.0f,
                true);

        // 3.0创建一个空Bitmap并根据传入文本计算尺寸大小
        int width = staticLayout.getWidth() + 120;
        int height = staticLayout.getHeight() + 20;
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        // 4.0、创建绘制对象
        Canvas canvas = new Canvas(bitmap);
        // 4.1、设置整个图片背景Bitmap颜色(不设置则图片默认为透明色)
        if (isSetBgColor) {
            if (!TextUtils.isEmpty(bgColorStr)) {
                Paint bgPaint = new Paint();
                // bgPaint.setColor(Color.WHITE);
                // bgPaint.setColor(Color.parseColor("#FFFFFF"));
                bgPaint.setColor(Color.parseColor(bgColorStr));
                canvas.drawRect(0, 0, width, height, bgPaint);
            }
        }
        // 4.2、进行文字绘制
        canvas.translate(10, 10);
        staticLayout.draw(canvas);

        return bitmap;
    }
}

2、在Activity中进行调用

        findViewById(R.id.testImgBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 字体颜色为红色,背景白色。
                final Bitmap bitmap = TextToBitmap.textChangeToBitmap(mContext,
                        "text_TEXT_1234_张三李四",
                        "#FF0000",
                        80,
                        true,
                        "#FFFFFF",
                        false,
                        true,
                        false);
                ImageView imgView = findViewById(R.id.testImg);
                imgView.setImageBitmap(bitmap);
            }
        });

3、在xml的布局文件中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/kHintColor"
    android:orientation="vertical">
   
     <ImageView
        android:id="@+id/testImg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    
    <TextView
        android:id="@+id/testImgBtn"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:background="@color/orange"
        android:gravity="center"
        android:text="测试按钮"
        android:textColor="@color/white" />

</LinearLayout>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容