안드로이드(Android) Canvas를 이용해서 이미지를 원하는 위치에 그리기 :: 안드로이드 설치 및 개발[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

안드로이드 설치 및 개발
[1]
등록일:2018-10-15 17:23:39 (0%)
작성자:
제목:안드로이드(Android) Canvas를 이용해서 이미지를 원하는 위치에 그리기

 

안드로이드(Android) Canvas를 이용해서 이미지를 원하는 위치에 그리기

 

안드로이드(Android)에서 Canvas를 이용해 이미지를 원하는 위치와 원하는 크기로 축소, 확대해서 출력하는 방법입니다.

위 그림은 아래의 예제를 실행해서 캡처한 화면입니다.

이번 예제는 XML은 사용하지 않고, java 문만 사용해서 작성한 예입니다.

테스트 과정이라서 일부는 주석처리해 두었습니다.

 

좀 더 상세하게 설명하면 안드로이드에서 Canvas에 그림을 그리는데, 비트맵(Bitmap)을 이용해서 그림을 불러와 화면에 그리는 방식입니다.

 

 

이미지는 C:\Android\workspace\AnimationCanvasTest\res\drawable-hdpi 에 pacman01, pacman02, pacman03 파일을 미리 저장해 두었으며, 3개의 파일 모두 png 파일입니다.

 

 

** AnimationCanvasTest.java 파일 소스 **

 

package com.example.animationcanvastest;

import android.support.v7.app.ActionBarActivity;
import android.os.*;
import android.view.*;
import android.content.Context;
import android.content.res.Resources;
import android.app.*;
import android.graphics.*;
import android.util.*;
import android.widget.*;


public class AnimationCanvasTest extends Activity {

 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  //setContentView(R.layout.activity_animation_canvas_test);
  setContentView(new MyView(this));
 }

 public class MyView extends View {
  private Bitmap image1, image2, image3;
  
  public MyView(Context context) {
   super(context);
   setBackgroundColor(Color.LTGRAY);
   
   Resources r = context.getResources();
   image1 = BitmapFactory.decodeResource(r, R.drawable.pacman01);
   image2 = BitmapFactory.decodeResource(r, R.drawable.pacman02);
   image3 = BitmapFactory.decodeResource(r, R.drawable.pacman03);
  }
  
  @Override
  protected void onDraw(Canvas canvas) {
   canvas.drawBitmap(image1, 0, 0, null);
   
   int w = image2.getWidth();
   int h = image2.getHeight();
   //Rect src = new Rect(0, 0, w, h);
   Rect dst = new Rect(400, 800, 400 + w / 2, 800 + h / 2);
   canvas.drawBitmap(image2, null, dst, null);
   
   w = image3.getWidth();
   h = image3.getHeight();
   //src = new Rect(0, 0, w, h);
   dst = new Rect(400, 1200, 400 + w / 2, 1200 + h / 2);
   canvas.drawBitmap(image3, null, dst, null);
   
   super.onDraw(canvas);
  }
 }
/* 
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.animation_canvas_test, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
*/
}

 

 

import 문 중 일부는 현재 사용하지 않지만 넣어둔 것도 있습니다.

전체 클래스 명은 AnimationCanvasTest 이며, onCreate 문을 통해 MyView 클래스를 열어서 화면에 그리는 방식입니다.

MyView 에는 비트맵(Bitmap)으로 image1, image2, image3를 선언했고,

배경색은 라이트 그레이색으로 해서 3개 이미지의 배경이 투과되어 보이도록 했습니다.

 

Resources r = context.getResources();
image1 = BitmapFactory.decodeResource(r, R.drawable.pacman01);
image2 = BitmapFactory.decodeResource(r, R.drawable.pacman02);
image3 = BitmapFactory.decodeResource(r, R.drawable.pacman03);

 

위의 문장을 통해 drawable-hdpi 폴더에 있는 pacman01-03 파일의 포인터를 참조합니다.

 

canvas.drawBitmap(image1, 0, 0, null);

위 문장은 image1을 불러와 0, 0 위치에 그리는 방식입니다.

 

int w = image2.getWidth();
int h = image2.getHeight();
//Rect src = new Rect(0, 0, w, h);
Rect dst = new Rect(400, 800, 400 + w / 2, 800 + h / 2);
canvas.drawBitmap(image2, null, dst, null);

 

위 문장은 image2를 불러와서 이미지의 폭과 넓이를 구하고, 크기 및 위치를 Rect 형태로 저장해서 canvas.drawBitmap 문을 이용해 화면에 뿌려주는 방식입니다.

canvas.drawBitmap(image2, null, dst, null); 문장을 canvas.drawBitmap(image2, src, dst, null); 로 변경하게 되면 src 이미지를 지우고 dst 이미지를 그린다는 의미입니다.

 

super.onDraw(canvas);

3개의 이미지를 설정 후 onDraw 문장을 이용해서 화면에 뿌려줍니다.



출처: http://blueegg.tistory.com/8 [블루에그]
[본문링크] 안드로이드(Android) Canvas를 이용해서 이미지를 원하는 위치에 그리기
[1]
코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=34869
작성자
비밀번호

 

SSISOCommunity

[이전]

Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.