본문 바로가기

Android

[13일차]그래픽 실습.

package com.chapark.samplegraphics;

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

//점과 선을 이용해서 그리기를 한다.
// 점을 0,0 에서 300,0으로 선을 그린다.
//10을 증가 시키고 10을 감소시켜서 또 선을 그린다.
// 반복 한다. x가 10씩 계속 증가 되고, y가 10씩 계속 감소된다.
// 그래서 점을 나타내기 위해서 x,y가 필요하다. 근데 그리기에서는 float라고 나타낸다.
// 왜냐면 변환이라는걸 할 수 있는데 이걸 하면 scale을 두배로 했을 경우 손실되는 양이 많다.
// 실제로 다 적용하고 그림을 그리기 할때 int로 변환해서 찍는거다.
// 각 점의 x,y는 float로 한다는 것이다. 점 하나는 floatArray로 나타낼수도 있다. 점은 float2개, 선은 4개
// 30+1*2*2 개의 float개수가 필요하다. 연산을 다하고 나서 그리기를 나중에 하는거다.
// 일단 점을 다 계산해서 Array에 넣고 그림을 그릴거다.

public class CustomView extends View {
public CustomView(Context context) {
super(context, null);// 이게 왜 ?
}


Paint mPaint;

public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);

mPaint = new Paint();// 생성을 생성자에서 하네 ?
initPoint();

}

float[] points;
private static final float START = 0, END = 300, INTERVAL=10;

private void initPoint() {
int count =(int)((END-START)/INTERVAL)+1;//간격에 +1해야지 카운트가 된다.
points = new float[count*2*2];
for (int i =0; i<count;i++){
points[i*4+0]=START;
points[i*4+1]=START+i*INTERVAL;
points[i*4+2]=END-i*INTERVAL;
points[i*4+3]=START;

}

}

@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawColor(Color.LTGRAY);
mPaint.setColor(Color.RED);
mPaint.setAntiAlias(true); // 계산 량이 많기 때문에 반드시 false로 꺼줘야 한다.
canvas.drawLines(points,mPaint);
mPaint.setAntiAlias(false);
mPaint.setColor(Color.BLUE);
mPaint.setStrokeWidth(5);
canvas.drawPoints(points,mPaint);// Paints로 그려야한다.
};


}




'Android' 카테고리의 다른 글

[13일차]View의 상태 저장  (0) 2016.08.03
[13일차] 커스텀 뷰 그래픽  (0) 2016.08.03
[13일차] 그래픽  (0) 2016.08.03
[12일차]Typeface  (0) 2016.08.03
[12일차] 환경에 따라 달라지는 뷰 ORIENTATION  (0) 2016.08.02