Text mit Stroke erstellen

  • Antworten:0
Dominic Warzok
  • Forum-Beiträge: 19

23.04.2012, 08:50:38 via Website

Hallo zusammen,

ich habe vor in meiner App Text mit einer Umrandung da zu stellen, leider gibt es jetzt in einem TextView keine setStroke Methode, bzw. habe ich noch keine gefunden.

Auf einer koreanischen Seite bin ich dann über etwas gestolpert, was mir hilft. Allerdings verstehe ich nicht was sie dort machen und es hat auch nicht ganz den gewünschten Effekt.

Vielleicht hat jemand von euch noch eine Idee oder weiß wie das geht. Ich kann ja nicht der einzige sein der ein Stroke für einen Text braucht.

Hier ist das was ich gefunden habe:

Überschriebene TextView Klasse

1public class OutlineTextView extends TextView
2{
3
4 private boolean stroke = false;
5 private float strokeWidth = 0.0f;
6 private int strokeColor;
7
8
9
10 public OutlineTextView(Context context, AttributeSet attrs, int defStyle)
11 {
12 super(context, attrs, defStyle);
13
14 initView(context, attrs);
15 }
16
17
18
19 public OutlineTextView(Context context, AttributeSet attrs)
20 {
21 super(context, attrs);
22
23 initView(context, attrs);
24 }
25
26
27
28 public OutlineTextView(Context context)
29 {
30 super(context);
31 }
32
33
34
35 private void initView(Context context, AttributeSet attrs)
36 {
37 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OutlineTextView);
38 stroke = a.getBoolean(R.styleable.OutlineTextView_textStroke, false);
39 strokeWidth = a.getFloat(R.styleable.OutlineTextView_textStrokeWidth, 0.0f);
40 strokeColor = a.getColor(R.styleable.OutlineTextView_textStrokeColor, 0xFFFFFFFF);
41 }
42
43
44
45 @Override
46 protected void onDraw(Canvas Canvas)
47 {
48
49 if (stroke)
50 {
51
52 ColorStateList states = getTextColors();
53 getPaint().setStyle(Style.STROKE);
54 getPaint().setStrokeWidth(strokeWidth);
55 setTextColor(strokeColor);
56 super.onDraw(Canvas);
57
58 getPaint().setStyle(Style.FILL);
59 setTextColor(states);
60 }
61
62 super.onDraw(Canvas);
63 }

Teil des Layouts
1<RelativeLayout
2 android:id="@+id/transparentRight"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:layout_alignParentBottom="true"
6 android:layout_alignParentRight="true"
7 android:background="@drawable/transparent_right" >
8
9 <FrameLayout
10 android:id="@+id/pfeil"
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:layout_above="@id/manufacturerText"
14 android:layout_centerHorizontal="true"
15 android:layout_marginTop="110dp"
16 android:background="@drawable/pfeil"
17 android:paddingLeft="10dp"
18 android:paddingRight="10dp" >
19
20 <my.app.views.OutlineTextView
21 android:id="@+id/headline"
22 android:layout_width="match_parent"
23 android:layout_height="match_parent"
24 android:gravity="center"
25 android:paddingLeft="10dp"
26 android:paddingRight="10dp"
27 android:text="@string/technicalDetails"
28 android:textSize="40sp"
29 xplace:textStroke="true"
30 xplace:textStrokeColor="#000000"
31 xplace:textStrokeWidth="10.0" />
32 </FrameLayout>
33 </RelativeLayout>

Edit: Und das hier kommt bei raus:


Vielen Dank schonmal im Vorraus

— geändert am 23.04.2012, 09:42:15

Antworten