본문 바로가기

ANDROID의 속삭임

[android][기초]CustomView

* 예제로 보는 것이 가장 좋을거 같다. 그래서 예제로 소스를 올린다.

* xml에 추가하여 사용하는 방법이 조금더 알아야 됨으로 해당 방법을 선택 했다.

* 터치로 드래그 하면은 해당 선을 그리는 그리기 View를 만들었다.





* /res/values/attr.xml을 추가.

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

<resources>

    <declare-styleable name="FreeLineView">

        <attr name="color" format="string" /> 

        <attr name="strokeWidth" format="integer" /> 

        <attr name="antiAlias" format="boolean" />

        <attr name="background" format="string" />

    </declare-styleable>

</resources>


* FreeLineInfo.java

package com.exam.freeline;


public class FreeLineInfo {


public float x;

public float y;

public boolean draw;

public FreeLineInfo(float x, float y, boolean draw){

this.x = x;

this.y = y;

this.draw = draw;

}

}


* FreeLineView.java

package com.exam.freeline;


import java.util.ArrayList;


import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;


public class FreeLineView extends View {

public ArrayList <FreeLineInfo> mArInfo = new ArrayList <FreeLineInfo>();

private Paint mPaint;

private int background;

public FreeLineView(Context context){

super(context);

mPaint = new Paint();

mPaint.setColor(Color.BLUE);

mPaint.setStrokeWidth(5);

mPaint.setAntiAlias(true);

background = Color.LTGRAY;

}


    public FreeLineView(Context c, AttributeSet attrs)

    {


        super(c, attrs);

        

        TypedArray typeArray = c.obtainStyledAttributes(attrs,  R.styleable.FreeLineView);

        mPaint = new Paint();

mPaint.setColor( Color.parseColor( typeArray.getString( R.styleable.FreeLineView_color ) ) );

mPaint.setStrokeWidth( typeArray.getInt( R.styleable.FreeLineView_strokeWidth, 1) );

mPaint.setAntiAlias( typeArray.getBoolean( R.styleable.FreeLineView_antiAlias, false) );

background = Color.parseColor( typeArray.getString( R.styleable.FreeLineView_background ) );

        

    }



    public FreeLineView(Context c, AttributeSet attrs, int defStyle)

    {


        super(c, attrs, defStyle);

        

        TypedArray typeArray = c.obtainStyledAttributes(attrs,  R.styleable.FreeLineView);

        mPaint = new Paint();

mPaint.setColor( Color.parseColor( typeArray.getString( R.styleable.FreeLineView_color ) ) );

mPaint.setStrokeWidth( typeArray.getInt( R.styleable.FreeLineView_strokeWidth, 1) );

mPaint.setAntiAlias( typeArray.getBoolean( R.styleable.FreeLineView_antiAlias, false) );

background = Color.parseColor( typeArray.getString( R.styleable.FreeLineView_background ) );

    }

public void onDraw(Canvas canvas){

canvas.drawColor(background);

for(int i=0; i < mArInfo.size(); i++){

if( mArInfo.get(i).draw){

canvas.drawLine(mArInfo.get(i-1).x, mArInfo.get(i-1).y, mArInfo.get(i).x, mArInfo.get(i).y, mPaint);

}

}

}

public boolean onTouchEvent(MotionEvent event){

if(event.getAction() == MotionEvent.ACTION_DOWN){

mArInfo.add(new FreeLineInfo(event.getX(), event.getY(), false));

return true;

}

if(event.getAction() == MotionEvent.ACTION_MOVE ){

mArInfo.add(new FreeLineInfo(event.getX(), event.getY(), true));

invalidate();

return true;

}

return false;

}

public Paint getPaint(){

return mPaint;

}

}



* activity_main.xml 에 등록.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >


    <com.exam.freeline.FreeLineView xmlns:app="http://schemas.android.com/apk/res/com.exam.freeline"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:scaleType="fitXY"

        app:color="#FF000000"

        app:background="#FFFFFFFF"

        app:strokeWidth="5"

        app:antiAlias="true"/>

    

    

</RelativeLayout>