312
No
class MainActivity
package com.example.choi.ex07_graphic;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v){
Intent intent=null;
switch(v.getId()){
case R.id.button1:
intent=new Intent(this, ShapeActivity.class);
break;
}
startActivity(intent);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.choi.ex07_graphic.MainActivity">
<Button
android:text="도형 그리기"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button1"
android:onClick="onClick" />
</RelativeLayout>
class ShapeActivity
package com.example.choi.ex07_graphic;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class ShapeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//xml 이 아닌 코드를 사용한 뷰(커스텀뷰)
setContentView(new MyView(this));
}
//내부 클래스
class MyView extends View {
//Alt + Insert , 생성자 추가
public MyView(Context context) {
super(context);
}
//화면출력
@Override
protected void onDraw(Canvas canvas) {
//canvas :도화지 역할(바탕화면)
canvas.drawColor(Color.LTGRAY);//바탕화면 색상
//Paint :붓 역할
Paint p=new Paint();
p.setColor(Color.GREEN); //색상 설정
//캔버스에 원 그리기 (x, y, width, paint)
canvas.drawCircle(100, 100, 50, p);
//사각형 그리기 (x1, y1, x2, y2, 페이트)
canvas.drawRect(200, 200, 300, 300, p);
}
}
}
댓글 ( 4)
댓글 남기기