Views_Custom

old/API_Demo 2010. 4. 19. 12:16

[custom_view_1.xml]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
   
    <com.example.android.apis.view.LabelView
            android:background="@drawable/red"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            app:text="Red"/>
   
    <com.example.android.apis.view.LabelView
            android:background="@drawable/blue"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            app:text="Blue" app:textSize="20dp"/>
   
    <com.example.android.apis.view.LabelView
            android:background="@drawable/green"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            app:text="Green" app:textColor="#ffffffff" />

</LinearLayout>



[LabelView.java]

package com.example.android.apis.view;

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

import com.example.android.apis.R;

public class LabelView extends View {
    private Paint mTextPaint;
    private String mText;
    private int mAscent;


public LabelView(Context context) {
        super(context);
        initLabelView();
    }

public LabelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLabelView();

         // xml의 attrs를 LabelView 이름으로 가져온다.        
       TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.LabelView);

       // text를 가져와서 s에 setText
       CharSequence s = a.getString(R.styleable.LabelView_text);
        if (s != null) {
            setText(s.toString());
        }

       // color 가져온다. (default값도 설정)
       setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));

        // textSize 가져온다.
        int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize, 0);
        if (textSize > 0) {
            setTextSize(textSize);
        }

         //가져온 typedarray 해제
         a.recycle();
    }

    private final void initLabelView() {
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);    // edge 부분을 부드럽게
        mTextPaint.setTextSize(16);
        mTextPaint.setColor(0xFF000000);
        setPadding(3, 3, 3, 3);
    }

public void setText(String text) {
        mText = text;
        requestLayout();    // 갱신       
        invalidate();          // draw 갱신
    }

public void setTextSize(int size) {
        mTextPaint.setTextSize(size);
        requestLayout();    //  갱신
        invalidate();          // draw 갱신
    }

public void setTextColor(int color) {
        mTextPaint.setColor(color);
        invalidate();   // draw 갱신
    }

    // store measure width & height
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }

private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
                    + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }

        return result;
    }


    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        mAscent = (int) mTextPaint.ascent();
        if (specMode == MeasureSpec.EXACTLY) {

            result = specSize;
        } else {

            result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop()
                    + getPaddingBottom();
            if (specMode == MeasureSpec.AT_MOST) {

                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    // text 그리기    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint);
    }
}

'old > API_Demo' 카테고리의 다른 글

Views_Expandable_Cursor [contact]  (0) 2010.04.19
Views_Expandable_Custom Adapter  (0) 2010.04.19
Views_Controls_LightTheme  (0) 2010.04.05
Views_Chronometer  (0) 2010.04.05
View_Buttons  (0) 2010.04.05
Posted by jazzlife
,