마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

그림판 크기를 사용자가 수시로 변경가능 여부

0 추천
package com.example.test;

import java.io.OutputStream;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  PaintBoard pb = new PaintBoard(this);
  setContentView(pb);
 }

 public class PaintBoard extends View {

  /**
   * Canvas instance
   */
  Canvas mCanvas;
  
  /**
   * Bitmap for double buffering
   */
  Bitmap mBitmap;
  
  /**
   * Paint instance
   */
  final Paint mPaint;
  
  /**
   * X coordinate
   */
  int lastX;
  
  /**
   * Y coordinate
   */
  int lastY;

  /**
   * Initialize paint object and coordinates
   *
   * @param c
   */
  public PaintBoard(Context context) {
   super(context);
   
   // create a new paint object
   this.mPaint = new Paint();
   this.mPaint.setColor(Color.BLACK);
   
   this.lastX = -1;
   this.lastY = -1;
   
   Log.i("PaintBoard", "initialized.");
  }

  /**
   * onSizeChanged
   */
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
   Bitmap img = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
   Canvas canvas = new Canvas();
   canvas.setBitmap(img);
   canvas.drawColor(Color.WHITE);
   
   mBitmap = img;
   mCanvas = canvas;
   
  }

  /**
   * Draw the bitmap
   */
  protected void onDraw(Canvas canvas) {
   if (mBitmap != null) {
    canvas.drawBitmap(mBitmap, 0, 0, null);
   }
  }

  /**
   * Handles touch event, UP, DOWN and MOVE
   */
  public boolean onTouchEvent(MotionEvent event) {
   int action = event.getAction();

   int X = (int) event.getX();
   int Y = (int) event.getY();

   switch (action) {
    case MotionEvent.ACTION_UP:
     // reset coordinates
     lastX = -1;
     lastY = -1;
     
     break;
  
    case MotionEvent.ACTION_DOWN:
     // draw line with the coordinates
     if (lastX != -1) {
      if (X != lastX || Y != lastY) {
       mCanvas.drawLine(lastX, lastY, X, Y, mPaint);
      }
     }
     
     // set the last coordinates
     lastX = X;
     lastY = Y;
     
     break;
  
    case MotionEvent.ACTION_MOVE:
     // draw line with the coordinates
     if (lastX != -1) {
      mCanvas.drawLine(lastX, lastY, X, Y, mPaint);
     }
  
     lastX = X;
     lastY = Y;
     
     break;
   }

   // repaint the screen
   invalidate();

   return true;
  }

  /**
   * Save this contents into a Jpeg image
   *
   * @param outstream
   * @return
   */
  public boolean Save(OutputStream outstream) {
   try {
    mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
    invalidate();
    
    return true;
   } catch (Exception e) {
    return false;
   }
  }
 }
}

 

 

 

안녕하세요.

캔바스에 간신히 그림을 그릴 수 있는 코드를 만들었습니다. (엄밀히는 에러없이 복붙..)

현재는 화면의 크기가 고정이 되어 있는데요. 이 화면의 크기를 세로 또는 가로로 계속 확장하려고 합니다.

수식 같은 긴 식을 넣을 때 불편해서요.

캔파스의확장(?)이 가능한지요.
익명사용자 님이 2016년 1월 24일 질문
2016년 1월 24일 수정

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...