2015-06-19 47 views
0

Intent of text with stroke 在這裏您可以看到我的問題是什麼,我在Android中創建了一個自定義TextView,以便爲筆劃添加筆劃。但到目前爲止,我有2個獨立的文本,而不是一個與中風......Android中的筆畫問題Android

這裏是我的代碼:

XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/game_end_share_relative_main" 
    android:layout_width="@dimen/share_width" 
    android:layout_height="@dimen/share_height" 
    android:background="#000000" > 

      <com.sharing.StrokeTextView 
       android:id="@+id/user_share_points" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="3" 
       android:textColor="@color/primary" 
       android:layout_marginRight="16dp" 
       style="@style/SecondaryFontFamily" 
       android:textSize="70dp" /> 

而定製的TextView:

public class StrokeTextView extends TextView { 
private int mStrokeColor; 
private int mStrokeWidth; 
private TextPaint mStrokePaint; 

public StrokeTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(attrs); 
} 

public StrokeTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public StrokeTextView(Context context) { 
    super(context); 
} 

public void setStrokeColor(int color) { 
    mStrokeColor = color; 
} 

public void setStrokeWidth(int width) { 
    mStrokeWidth = width; 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    if (mStrokePaint == null) { 
     mStrokePaint = new TextPaint(); 
    } 

    mStrokePaint.setTextSize(getTextSize()); 
    mStrokePaint.setTypeface(getTypeface()); 
    mStrokePaint.setFlags(getPaintFlags()); 

    mStrokePaint.setStyle(Paint.Style.STROKE); 
    mStrokePaint.setColor(mStrokeColor); 
    mStrokePaint.setStrokeJoin(Paint.Join.ROUND); 
    mStrokePaint.setStrokeCap(Paint.Cap.ROUND); 
    mStrokePaint.setStrokeWidth(mStrokeWidth); 
    mStrokePaint.setShadowLayer(2.0f, 5.0f, 5.0f, Color.BLACK); 

    mStrokePaint.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/MikadoBlack.otf")); 
    String text = getText().toString(); 
    canvas.drawText(text, getWidth() - (mStrokePaint.measureText(text)/2), getBaseline(), mStrokePaint); 
    super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/MikadoBlack.otf")); 
    super.onDraw(canvas); 
} 

} 

感謝您的幫助提前:)

何塞

回答

1

這是因爲超類中的drawText繪製的位置與您的位置不同。嘗試使用View.setGravity(Gravity.CENTER)將內容重力設置爲「中心」,這可以解決您的問題。此外,如果您正在使用的視圖填充,那麼你需要係數它而計算的drawText方法

int hPadding = getPaddingLeft()+getPaddingRight(); 
int contentAreaWidth = getWidth() - hPadding; 
canvas.drawText(text, contentAreaWidth - (mStrokePaint.measureText(text)/2), getBaseline(), mStrokePaint); 

這將有助於對準在超類中描繪的常態文本的描邊文字的起源。