Program/Android Java

Stroke TextView 만들기

너구리V 2012. 6. 20. 16:11

TextView 에 Stroke 효과 를 주기 위해 TextView 를 상속한 Custom TextView 를 만들었다.

캡처한 이미지


1. stroke 효과 주는 방법

참고: http://stackoverflow.com/questions/1723846/how-do-you-draw-text-with-a-border-on-a-mapview-in-android

설명:
먼저 위 사이트에서 Paint 에 stroke 값을 주는 방법을 알수 있었다.
다음 TextView 의 Paint 를 얻는 방법을 알아냈다.
 - TextView 에는 getPaint() 함수가 있다.
이제 TextView 의 onDraw 함수를 override 하고 Paint 에 stroke 스타일을 적용하여 한번 그림을 그리고 Paint 에 style 을 돌려놓고 그림을 다시 그리면 stroke 효과를 줄 수 있다.

예제:
  1.       
  2. @Override  
  3. protected void onDraw(Canvas canvas) {  
  4.   
  5.     ColorStateList states = getTextColors(); // text color 값 저장  
  6.   
  7.     // stroke 그리기  
  8.     getPaint().setStyle(Style.STROKE);  
  9.     getPaint().setStrokeWidth(2.0f);  
  10.     setTextColor(strokeColor);  
  11.     super.onDraw(canvas);  
  12.   
  13.     // stroke 위에 그리기  
  14.     getPaint().setStyle(Style.FILL);  
  15.     setTextColor(states);  
  16.     super.onDraw(canvas);  
  17. }  

2. Stroke TextView 사용

참고:
custom attribute: http://blog.pocketjourney.com/2008/05/02/android-tutorial-42-passing-custom-variables-via-xml-resource-files/


순서:
1) TextView 를 상속한 CustomTextView 라는 storke 효과를 주는 클래스를 만들었다.
2) xml 에서 stroke 속성값을 줄 수 있게 res/values/attrs.xml 파일에 속성값을 추가해 줬다.
3) layout xml 파일에서 CustomtextView 를 사용했다.

코드:
1) CustomTextView.java
  1. package com.tj.test;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.ColorStateList;  
  5. import android.content.res.TypedArray;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Paint.Style;  
  8. import android.util.AttributeSet;  
  9. import android.widget.TextView;  
  10.   
  11. public class CustomTextView extends TextView {  
  12.       
  13.     private boolean stroke = false;  
  14.     private float strokeWidth = 0.0f;  
  15.     private int strokeColor;  
  16.   
  17.     public CustomTextView(Context context, AttributeSet attrs, int defStyle) {  
  18.         super(context, attrs, defStyle);  
  19.           
  20.         initView(context, attrs);  
  21.     }  
  22.   
  23.     public CustomTextView(Context context, AttributeSet attrs) {  
  24.         super(context, attrs);  
  25.           
  26.         initView(context, attrs);  
  27.     }  
  28.   
  29.     public CustomTextView(Context context) {  
  30.         super(context);  
  31.     }  
  32.       
  33.     private void initView(Context context, AttributeSet attrs) {  
  34.         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);  
  35.         stroke = a.getBoolean(R.styleable.CustomTextView_textStroke, false);  
  36.         strokeWidth = a.getFloat(R.styleable.CustomTextView_textStrokeWidth, 0.0f);  
  37.         strokeColor = a.getColor(R.styleable.CustomTextView_textStrokeColor, 0xffffffff);         
  38.     }  
  39.   
  40.     @Override  
  41.     protected void onDraw(Canvas canvas) {  
  42.           
  43.         if (stroke) {  
  44.             ColorStateList states = getTextColors();  
  45.             getPaint().setStyle(Style.STROKE);  
  46.             getPaint().setStrokeWidth(strokeWidth);  
  47.             setTextColor(strokeColor);    
  48.             super.onDraw(canvas);  
  49.               
  50.             getPaint().setStyle(Style.FILL);  
  51.             setTextColor(states);  
  52.         }  
  53.           
  54.         super.onDraw(canvas);  
  55.     }  
  56. }  

2) res/values/attrs.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="CustomTextView">  
  4.         <attr format="boolean" name="textStroke"/>  
  5.         <attr format="float" name="textStrokeWidth"/>  
  6.         <attr format="color" name="textStrokeColor"/>  
  7.     </declare-styleable>   
  8. </resources>  

3) layout 에 적용
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tj="http://schemas.android.com/apk/res/com.tj.test"  
  5.     android:orientation="vertical"  
  6.     android:layout_width="fill_parent"  
  7.     android:layout_height="fill_parent"  
  8.     >  
  9. <com.tj.test.CustomTextView  
  10.     android:id="@+id/CustomTextView"  
  11.     android:layout_width="fill_parent"  
  12.     android:layout_height="wrap_content"  
  13.     android:text="Hello World"  
  14.     android:textColor="#ffffffff"  
  15.     android:singleLine="true"  
  16.     tj:textStroke="true"  
  17.     tj:textStrokeWidth="7.0"  
  18.     tj:textStrokeColor="#ffff0000"  
  19.     >  
  20.     </com.tj.test.CustomTextView>  
  21. </LinearLayout>  

반응형

'Program > Android Java' 카테고리의 다른 글

여가가지 Intent 활용  (0) 2012.06.21
ScrollView 위에 잔상 남는 현상  (0) 2012.06.20
물에 반사된듯한 이미지효과  (0) 2012.06.20
안드로이드 NFC 기본 기능 (NFC Basic)  (0) 2012.06.18
안드로이드 소켓  (0) 2012.06.14