안드로이드 개발 질문/답변
(글 수 45,052)
//--------------------------------------------------------------------------------------------------------------------------------
// trainer_dialog.xml 파일
//--------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
(패키지 경로랑 클래스 이름은 정확해요...)
<exam.PersonalTrainingSessionCheck.MyView
android:id="@+id/myview"
(둘 다 fill_parent로 해도 안돼요...)
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
//----------------------------------------------------------------------------------------------------------------
// .java 파일. 사용자의 터치를 입력받아서 화면에 그리는 View를 파생한 MyView클래스를 구현하고 있음
// 메인 액티비티(퍼스널트레이닝세션체크)에서 Button1_1을 눌러서 대화창을 열도록 구현되어 있음
// MyView는 대화창에 올리지 않고 독립적으로 실행시키면 화면에 꽉 차고 그려지는 것도 잘 그려지는데,
// 레이아웃에 올리면 대화창을 띄우는 버튼을 클릭하는 순간 에러가 나네요.
//-----------------------------------------------------------------------------------------------------------------
public class PersonalTrainingSessionCheckActivity extends Activity
{
private MyView vw;
ArrayList<Vertex> arVertex;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
vw = new MyView(this);
setContentView(R.layout.main);
arVertex = new ArrayList<Vertex>();
// 대화창 구현부분...
Button btn = (Button)findViewById(R.id.Button1_1);
btn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
final LinearLayout linear = (LinearLayout)
View.inflate(PersonalTrainingSessionCheckActivity.this, R.layout.trainer_dialog, null);
new AlertDialog.Builder(PersonalTrainingSessionCheckActivity.this)
.setTitle("트레이너분의 사인을 해주세요.")
.setIcon(R.drawable.icon)
.setView(linear)
.setPositiveButton("확인", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
TextView text = (TextView)findViewById(R.id.myview);
}
})
.setNegativeButton("취소", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton){
}
})
.show();
}
});
// 대화상자의 버튼 클릭 리스너... 아직 구현 안됨
Button.OnClickListener mClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
} // End Of onCreate()
// 정점 하나에 대한 정보를 가지는 클래스
public class Vertex{
Vertex(float ax, float ay, boolean ad){
x = ax;
y = ay;
Draw = ad;
}
float x;
float y;
boolean Draw;
}
protected class MyView extends View{
Paint mPaint;
public MyView(Context context){
super(context);
// Paint 객체 미리 초기화
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(3);
mPaint.setAntiAlias(true);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
// onDraw() 오버라이딩
public void onDraw(Canvas canvas){
canvas.drawColor(0xffe0e0e0);
// 정점을 순회하면서 선분으로 잇는다.
for(int i = 0; i < arVertex.size(); i++){
if(arVertex.get(i).Draw){
canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y,
arVertex.get(i).x, arVertex.get(i).y, mPaint);
} // End of if statement
} // End Of for loop
} // End Of onDraw()
// 크기가 안맞아서 문제가 되는 건 아닐까 해서 크기도 200x200으로 뷰의 크기를 충분히 작게도 만들어봤는데
// 역시 안됐습니다.
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(200, 200);
}
// 터치 이동시마다 정점들을 추가한다.
public boolean onTouchEvent(MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_DOWN){
arVertex.add(new Vertex(event.getX(), event.getY(), false));
return true;
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
arVertex.add(new Vertex(event.getX(), event.getY(), true));
invalidate();
return true;
}
return false;
} // End Of onTouchEvent()
} // End Of class MyView declaration
}
참고로 사용자 터치를 입력받아서 그림을 그리는 건
안드로이드 프로그래밍 정복의 FreeLine 예제를 그대로 따왔구요.
책도 찾아보고 검색도 엄청 해봤는데,
자신이 직접 만든 View를 상속받은 View 타입의 클래스를 레이아웃에 올리는 방법은
제대로 된 정보를 찾을 수가 없네요.
혹시 다른 방법이 있다면 그 방법을 알려주셔도 되고...
아니라면 어떻게 해야 문제가 해결될 수 있는지 알려주시면 감사합니다.
참고했던 사이트는
이 정도였는데 결과적으로는 도움이 안됐네요...
제발 좀 도와주세요. ㅠㅠ



