안녕하세요
그림그리기 예제를 보다보면
거의 대부분이
setContent(new 클래스(this));
이렇게 해서 View 객체를 집어 넣었더라고요
근데 이렇게 말고
R.layout.main 이렇게 xml 파일을 넣어서
원하는 부분에만 그릴 수 있도록 어떻게 해야할까요??
예제 소스는 아래것을 썻어요
public class Test_ReDrawActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View {
ArrayList<Vertex> Picture;
Paint mPntBack;
Paint mPntPath;
Paint mPntFore;
Path[][] arPath;
public MyView(Context context) {
super(context);
Picture = new ArrayList<Vertex>();
mPntBack = new Paint();
// LinearGradient lshader = new LinearGradient(0, 0, 320, 0,
// Color.DKGRAY, Color.LTGRAY, TileMode.CLAMP);
//mPntBack.setShader(lshader);
mPntBack.setColor(Color.WHITE);
mPntPath = new Paint();
mPntPath.setStyle(Paint.Style.STROKE);
mPntPath.setStrokeWidth(2);
mPntPath.setColor(0x80ff0000);
mPntFore = new Paint();
mPntFore.setColor(Color.BLACK);
mPntFore.setStrokeWidth(3);
mPntFore.setAntiAlias(true);
arPath = new Path[5][5];
for (int x = 0; x < 5; x++) {
for (int y = 0; y < 5; y++) {
arPath[x][y] = new Path();
arPath[x][y].moveTo(x * 70, y * 70);
for (int dis = 32; dis > 1; dis--) {
switch (dis % 4) {
case 0:
arPath[x][y].rLineTo(dis * 2, 0);
break;
case 3:
arPath[x][y].rLineTo(0, dis * 2);
break;
case 2:
arPath[x][y].rLineTo(-dis * 2, 0);
break;
case 1:
arPath[x][y].rLineTo(0, -dis * 2);
break;
}
}
}
}
}
public void onDraw(Canvas canvas) {
canvas.drawRect(0, 0, getWidth(), getHeight(), mPntBack);
// for (int x = 0; x < 5; x++) {
// for (int y = 0; y < 5; y++) {
// canvas.drawPath(arPath[x][y], mPntPath);
// }
// }
for (int i = 0; i < Picture.size(); i++) {
if (Picture.get(i).Draw) {
canvas.drawLine(Picture.get(i - 1).x, Picture.get(i - 1).y,
Picture.get(i).x, Picture.get(i).y, mPntFore);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Picture.add(new Vertex(event.getX(), event.getY(), false));
return true;
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
Picture.add(new Vertex(event.getX(), event.getY(), true));
Rect rect = GetLineRect(Picture.size() - 1);
invalidate(rect);
return true;
}
return false;
}
Rect GetLineRect(int idx) {
Rect rect = new Rect();
Vertex prev = Picture.get(idx - 1);
Vertex now = Picture.get(idx);
rect.set((int)Math.min(now.x, prev.x)-2, (int)Math.min(now.y, prev.y)-2, (int)Math.max(now.x,prev.x)+3,(int)Math.max(now.y,prev.y)+3);
return rect;
}
}
public class Vertex {
Vertex(float ax, float ay, boolean ad) {
x = ax;
y = ay;
Draw = ad;
}
float x;
float y;
boolean Draw;
}
}
근데 xml파일에 패키지.MyView를 넣어도 안되더라고요
당연히 안되겠지만...ㅠ
팁좀 주세요