일단 저는 간단한 그림판 파일을 만들고싶고
이클립스 쓰던때 글을 보고 만들었어고 문제는 그리기를 누르면 튕겨요
일단
앱네임은 MyApplication 에요 패키지 이름은 org.androidtown.myapplication 에요.
우선
activity_main.xml 파일
<LinearLayout 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:orientation="vertical"
tools:context="org.androidtown.myapplication.MainActivity"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="50dip"
android:gravity="center"
android:textSize="45dip"
android:text="그 림 판"/>
<Button
android:id="@+id/draw_btn"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:layout_marginTop="110dip"
android:layout_gravity="center"
android:textSize="30dip"
android:text="그리기"
/> // 그리는 레이아웃으로 갈 버튼
<Button
android:id="@+id/end_btn"
android:layout_width="300dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="30dip"
android:text="끝내기"
/> //끝낼 버튼
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button draw_btn; // 그리로갈 버튼
Button end_btn; // 끝낼 버튼
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
draw_btn = (Button) findViewById(R.id.draw_btn); //자바 변수에 레이아웃 버튼 연결
end_btn = (Button) findViewById(R.id.end_btn); //자바 변수에 레이아웃 버튼 연결
draw_btn.setOnClickListener(this);
end_btn.setOnClickListener(this);
}
public void onClick(View v) {
Intent i;
switch(v.getId()) {
case R.id.draw_btn:
i = new Intent(this, org.androidtown.myapplication.DrawActivity.class);
finish();
startActivity(i);
break;
case R.id.end_btn:
finish();
break;
} // 클릭했을경우 draw_btn 이면 새로운 DrawActivity를 열고 end_btn일시 앱을 종료
}
}
draw.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"
tools:context="org.androidtown.myapplication.DrawActivity"
>
<view
class="org.androidtown.myapplication.Paper"
android:id="@+id/paper"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="@+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
/> //뒤로가기 버튼
</RelativeLayout>
DrawActivity.java
package org.androidtown.myapplication;import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class DrawActivity extends Activity {
Button back_btn; // 뒤로가기 버튼 변수정의
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.draw);
back_btn = (Button) findViewById(R.id.back_btn); // 자바 변수에 xml파일 벡버튼 연결
back_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(DrawActivity.this, org.androidtown.myapplication.MainActivity.class);
finish();
startActivity(i); // 벡버튼을 누르면 현제 페이지를 끝내고 새 액티비티 MainActivity 실행
}
});
}
}
class Paper extends View {
Paint paint = new Paint();
Path path = new Path();
float y ;
float x ;
public Paper(Context context) {
super(context);
}
public Paper(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onDraw(Canvas canvas) {
paint.setStrokeWidth(3);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(x, y);
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
path.lineTo(x, y);
break;
case MotionEvent.ACTION_UP:
break;
}
invalidate();
return true;
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.androidtown.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".DrawActivity" android:label="Draw" /> //AndroidManifest.xml 파일에 DrawActivity.java 등록
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
이렇게 했고 이게 구동되면
기능좀 추가하고 해보고싶은데
그리기 버튼을 누르면 튕기네요 ..
튕길때 문구는
My Application 앱을 중지하였습니다. 에요.
긴글 봐주셔서 감사하고 더 필요한거 있으시면 쓰거나 찍어드려요 .
좀 도와주세요 ㅠ
최대한 념글질문양식에 맞게 해봤으여..'
길어서 읽지는 않았습니다만, ㅄ이냐? 디버깅하면 다 나올텐데...
보통 저런 경우 십중팔구 참조에러다.
매니페스트에서 드로액티비티가 어플리케이션 하위에 가야지
ㄴㄴㄴ 진짜로 ㅄ이라 그런데 고걸 좀만 자세히좀..
ㅇㅇ//오 됫어요 감사해여 혹시 가능하시면 좀만 설명해주실수 있나요? 자율동아리에서 선생님이 잠시 얘기해주신거같은데 정확히 기억이안나네여..
로그캣을 보고 충돌날때 exception뜨는걸 검색해
ㄴ 옹 그게 뭔지도 몰랏는데 팁 감사함다 '