본문 바로가기

Android

캔버스(canvas)

MainActivity.java


package com.example.viewex01;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.view.Menu;

import android.view.View;

 

public class MainActivity extends Activity {

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           //setContentView(R.layout.activity_main);

          

           MyView2 view = new MyView2(this);

           setContentView(view);

     }

    

     protected class MyView1 extends View {

 

           // Context

           // 가상 내용물 (화면 안에 그린 내용)

           public MyView1(Context context) {

                super(context);

                // TODO Auto-generated constructor stub

           }

 

           @Override

           protected void onDraw(Canvas canvas) {

                // TODO Auto-generated method stub

                //super.onDraw(canvas);

               

                Paint paint = new Paint();

                paint.setColor(Color.BLUE);

               

                // drawRecrt(왼쪽상단x, 왼쪽상단y,오른쪽하단x, 오른쪽하단y, paint)

               

                canvas.drawRect(100, 100, 200, 200, paint);

           }

          

          

           }

          

          

 

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}

 

 

Myview2.java

 

package com.example.viewex01;

 

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.view.View;

 

public class MyView2 extends View {

 

     public MyView2(Context context) {

           super(context);

           // TODO Auto-generated constructor stub

     }

 

     @Override

    

           protected void onDraw(Canvas canvas) {

                // TODO Auto-generated method stub

                //super.onDraw(canvas);

               

                canvas.drawColor(Color.LTGRAY);

                Paint pnt = new Paint();

                //검정색

                canvas.drawPoint(10, 10, pnt);

                //파란색

                pnt.setColor(Color.BLUE);

                canvas.drawLine(20, 10, 200, 50, pnt);

                //빨간색

                pnt.setColor(Color.RED);

                canvas.drawCircle(100, 90, 50, pnt);

                //반투명한 파란색 사각형

                pnt.setColor(0x800000ff);

                canvas.drawRect(10, 100, 200, 170, pnt);

                //검정색 문자열

                pnt.setColor(Color.BLACK);

                canvas.drawText("Canvas Text Out", 10, 200, pnt);

               

     }

    

}




 

'Android' 카테고리의 다른 글

캔버스 -AntiAlias  (0) 2013.03.29
캔버스 - 복잡한 도형 그리기  (0) 2013.03.29
이미지 뷰(ImageView) - 이미지 버튼(ImageButton)  (0) 2013.03.28
이미지 뷰(imageView) - 확대 모드  (0) 2013.03.28
버튼 - 토글(toggle)  (0) 2013.03.28