Drawable里面文本作为背景

时间:2016-11-15 15:49:10

标签: android graphics textview drawable android-drawable

我想实现这个目标:

enter image description here

我创建了一个TextDrawable类,并绘制了一个文本。但是我如何填充我的抽屉?

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;

public class TextDrawable extends Drawable {
    private static final int DEFAULT_COLOR = Color.WHITE;
    private static final int DEFAULT_TEXTSIZE = 45;
    private Paint mPaint;
    private CharSequence mText;
    private int mIntrinsicWidth;
    private int mIntrinsicHeight;

    public TextDrawable(Resources res, CharSequence text) {
        mText = text;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.RED);
        mPaint.setTextAlign(Align.CENTER);
        mPaint.setStrokeWidth(5);
        float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, DEFAULT_TEXTSIZE, res.getDisplayMetrics());
        mPaint.setTextSize(textSize);
        mIntrinsicWidth = (int) (mPaint.measureText(mText, 0, mText.length()) + .5);
        mIntrinsicHeight = mPaint.getFontMetricsInt(null);
    }

    @Override
    public void draw(Canvas canvas) {
        Rect bounds = getBounds();
        canvas.drawText(mText, 0, mText.length(), bounds.centerX() + 15, bounds.centerY() + 15, mPaint);
    }

    @Override
    public int getOpacity() {
        return mPaint.getAlpha();
    }

    @Override
    public int getIntrinsicWidth() {
        return mIntrinsicWidth;
    }

    @Override
    public int getIntrinsicHeight() {
        return mIntrinsicHeight;
    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter filter) {
        mPaint.setColorFilter(filter);
    }

}

可绘制渐变:

    <?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/night_blue"
tools:targetApi="lollipop">
<item android:id="@android:id/background">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >

        <gradient
            android:angle="0"
            android:endColor="@color/color_end"
            android:startColor="@color/color_start" />


    </shape>
</item>
</ripple>

如果需要,可以使用涟漪效果,但是,我需要能够像这张图像一样使用填充来绘制文本。

2 个答案:

答案 0 :(得分:1)

像这样使用 LinearGradient

Shader linearGradient = new LinearGradient(getWidth()/2f, 0f, getWidth()/2f, getHeight()
                , Color.RED, Color.GREEN, Shader.TileMode.CLAMP);
mPaint.setShader(linearGradient);

答案 1 :(得分:0)

像这样使用:

        mPaint.setShader(new LinearGradient(0, 0, 0, mIntrinsicHeight, res.getColor(R.color.color_start), res.getColor(R.color.color_end), Shader.TileMode.CLAMP));