Android/Simple Custom View

Android Simple Custom View Demo

Soul-Learner 2010. 8. 3. 18:46

Google에서 제공한 커스텀뷰 예제를 참고하여 간단한 커스텀뷰와 커스텀속성을 테스트한 예제

res/values/attrs.xml (커스텀속성 선언)

<?xml version="1.0" encoding="utf-8"?>

<resources>   
<!-- These are the attributes that we want to retrieve from the theme        
in app/PreferencesFromCode.java -->   
<declare-styleable name="TogglePrefAttrs">       
 <attr name="android:preferenceLayoutChild" />   
</declare-styleable>   
<!-- These are the attributes that we want to retrieve from the theme        
in view/Gallery1.java -->   
<declare-styleable name="Gallery1">       
 <attr name="android:galleryItemBackground" />   
</declare-styleable>    
<declare-styleable name="MyView">       
 <attr name="text" format="string" />       
 <attr name="textColor" format="color" />       
</declare-styleable></resources>



res/layout/main.xml (커스텀속성의 설정)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res/com.dearpeople.android.test.comp"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 
  <com.dearpeople.android.test.comp.MyView
    android:id="@+id/myview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    app:text="테스트 문자열"
    app:textColor="#00ff00"
/>

</LinearLayout>



MyView.java (커스텀속성의 추출)

package com.dearpeople.android.test.comp;
import com.dearpeople.android.test.comp.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View{

 String text;
 int color;
 
  public MyView(Context context) {
   super(context);
  }
 
  /* xml 레이아웃파일에서 이 뷰를 사용하려면 이 생성자가 반드시 필요함*/
  public MyView(Context context, AttributeSet attrs) {
   super(context, attrs);

   /* 레이아웃파일로부터 커스텀속성을 추출해온다 */
   TypedArray a =context.obtainStyledAttributes(attrs, R.styleable.MyView);
   text = a.getString(R.styleable.MyView_text);
   color = a.getColor(R.styleable.MyView_textColor, Color.BLACK);
  }
 
  @Override
  protected void onDraw(Canvas canvas) {

   Paint paint = new Paint();
   paint.setColor(color);
   canvas.drawText(text, 100, 100, paint);
   this.invalidate();
  }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // TODO Auto-generated method stub
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  setMeasuredDimension(200, 200);
 }
 
}


Activity 클래스

package com.dearpeople.android.test.comp;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;

public class CustomUITest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}