带有渐变*和*笔划的Android TextView

时间:2014-06-05 21:59:03

标签: android textview styles gradient stroke

我现在正在寻找一段时间,我测试了很多东西,但没有任何作用。 我有一个TextView,它应该有一个渐变填充颜色一个笔划(不是Textview的背景,而是文本本身)。

我开始编写GradientTextView类。目前我有一个带渐变填充颜色和阴影的文本(阴影只是一个测试,也许我以后需要一个。此刻中风很重要)。但是,当我尝试添加笔划时,仅显示笔划或渐变填充颜色。 我尝试了很多东西,例如解决方案 here

import com.qualcomm.QCARSamples.ImageTargets.R;
import com.qualcomm.QCARSamples.ImageTargets.R.color;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.widget.TextView;

public class GradientTextView extends TextView
{
    public GradientTextView( Context context )
    {
        super( context, null, -1 );
    }
    public GradientTextView( Context context, 
        AttributeSet attrs )
    {
        super( context, attrs, -1 );
    }
    public GradientTextView( Context context, 
        AttributeSet attrs, int defStyle )
    {
        super( context, attrs, defStyle );
    }

int start_gradient = getResources().getColor(R.color.textview_start_gradient);
int end_gradient = getResources().getColor(R.color.textview_end_gradient);
Paint gradientpaint, strokepaint;

@Override
protected void onDraw(Canvas canvas) {
    // draw the shadow
    getPaint().setShadowLayer(10, 1, 1, 0xbf000000); 
    getPaint().setShader(null);
    super.onDraw(canvas);

    // draw the gradient filled text
    getPaint().clearShadowLayer();
    getPaint().setShader(new LinearGradient(0, 0, 0, getHeight(), 
        start_gradient, end_gradient, TileMode.CLAMP ) );
        super.onDraw(canvas);
        // **Attempts here**
    }
}

(尝试插入"尝试在此处"评论)

首次尝试:

super.onDraw(canvas);   
Paint one = new Paint();
one.setStyle(Style.STROKE);
one.setTextSize(20);
one.setStrokeWidth(5);
setTextColor(Color.BLACK); 
canvas.drawText(VIEW_LOG_TAG, 0, 0, one);

Paint two = new Paint();
two.setStyle(Style.FILL);
two.setTextSize(20);
two.setStrokeWidth(0);
setTextColor(Color.BLUE);
two.setShader(new LinearGradient(0, 0, 0, getHeight(), 
    start_gradient, end_gradient, TileMode.CLAMP ) );
canvas.drawText(VIEW_LOG_TAG, 0, 0, two);

第二次尝试:

Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
getPaint().setShader(new LinearGradient(0, 0, 0, getHeight(), 
    start_gradient, end_gradient, TileMode.CLAMP ) );
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeWidth(32);

super.onDraw(canvas);
Paint mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCenterPaint.setStyle(Paint.Style.STROKE);
mCenterPaint.setColor(Color.GREEN);
mCenterPaint.setStrokeWidth(5);
super.onDraw(canvas);

第三次尝试:

gradientpaint = this.getPaint();
gradientpaint.setShader(new LinearGradient(0, 0, 0, getHeight(), 
    start_gradient, end_gradient, TileMode.CLAMP ) );
super.onDraw(canvas);
strokepaint = new Paint(gradientpaint);
strokepaint.setStyle(Paint.Style.STROKE);
strokepaint.setStrokeWidth(30);
strokepaint.setARGB(255, 0, 0, 0);

super.onDraw(canvas);

textview_start_gradienttextview_and_gradient只是渐变的两种颜色。

每个都缺少笔画或填充(完全透明)。

我该怎么做?

不,我试过这个:

新尝试:

// draw the shadow
getPaint().setShadowLayer(10, 6, 6, 0xbf000000); 
getPaint().setShader(null);
super.onDraw(canvas);

// draw the stroke
getPaint().clearShadowLayer();
getPaint().setColor(Color.BLACK);
getPaint().setStyle(Style.STROKE);
getPaint().setStrokeWidth(5);
super.onDraw(canvas);
// draw the gradient filled text
getPaint().setStyle(Style.FILL);
getPaint().setShader(new LinearGradient(0, 0, 0, getHeight(), start_gradient, end_gradient, TileMode.CLAMP ));
//getPaint().setStrokeWidth(32);
super.onDraw(canvas);

有趣的是:阴影,填充渐变和笔触出现了! *但是笔划是白色的(不是黑色)。我认为颜色设置是错误的,因为它显示白色。有任何想法吗?

1 个答案:

答案 0 :(得分:2)

经过多次尝试,我解决了这个问题。现在我的TextView有一个黑色笔触,一个渐变填充颜色和一个阴影。

我仍然拥有相同的GradientTextView.java课程,但我对onDraw的解决方案是:

if(isInEditMode()){

}else{
    start_gradient = getResources().getColor(R.color.textview_start_gradient);
    end_gradient = getResources().getColor(R.color.textview_end_gradient);
}

// draw the shadow
getPaint().setShadowLayer(10, 6, 6, 0xbf000000); 
getPaint().setShader(null);
super.onDraw(canvas);

// draw the stroke
getPaint().clearShadowLayer();
getPaint().setStyle(Style.STROKE);
getPaint().setStrokeWidth(5);
getPaint().setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.BLACK, TileMode.CLAMP ));
super.onDraw(canvas);

// draw the gradient filled text
getPaint().setStyle(Style.FILL);
getPaint().setShader(new LinearGradient(0, 0, 0, getHeight(), start_gradient, end_gradient, TileMode.CLAMP ));
//getPaint().setStrokeWidth(32);
super.onDraw(canvas);

}

getPaint().setColor(Color.BLACK); 没有工作,所以我只是设置了一个带有黑色渐变的着色器(不知道这是否是一个很好的解决方案,但它有效)。

在我的xml中,我只是将类(包含)作为xmlns添加到我的top-layout

像这样的东西

xmlns:gradient="http://schemas.android.com/apk/lib/package-name"

而不是我写的TextView:

<package-name.GradientTextView
        android:id="@+id/textView_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|top"
        android:textSize="20sp"
        android:text="@string/main1" />

非常感谢所有给予的帮助!

希望这会帮助别人!