그래픽과 애니메이션

2010. 4. 26. 22:09Scrapbook/개발 및 프로그래밍

반응형


onDraw()
Canvas 크기: 사이즈 정할수 없고 가지고 올 수는 있다
--> getHeight , getWidth()

물감 --> Paint:스타일 복합색, 렌더링 캡슐화 -> 그래픽이나,도형,글꼴

색상 : setColor() --> Paint 객체에 색상 설정 / 값을 정수값으로 받는다.

anti-aliasing : Paint 객체 생성시 인자값으로 처리
Paint aliasPaint = new Paint(paint.ANTI_ALIAS_FLAG);
setAntiAlias()메서드로 변경 가능

스타일
(Paint.Style.STROKE);

canvas.drawCircle(80,30,22,linePaint);

package com.android.mycircle;

import android.app.Activity;
import android.os.Bundle;
import android.content.*;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.*;

public class MyCircleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new ViewWithRedDot(this));
    }
   
 private static class ViewWithRedDot extends View{
     public ViewWithRedDot(Context context){
      super(context);
     }
     
     @Override
     protected void onDraw(Canvas canvas){
      canvas.drawColor(Color.BLACK);
      
      Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      circlePaint.setColor(Color.RED);
      canvas.drawCircle(canvas.getWidth()/2,canvas.getHeight()/2,
        canvas.getWidth()/3, circlePaint);
     }
     
    }
}



gradient 여러색을 매끄럽게
android.graphics.Shader
두가지 초과의 색 요구시 --> 배열로 전달
반사 반복 설정 가능
Gradent[R,B,비율값... --> Shader를 이용해서...적용
Paint 객체의 그래디언트 setShader()..

package com.android.mycircle;

import android.app.Activity;
import android.os.Bundle;
import android.content.*;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.LinearGradient;
import android.graphics.RadialGradient;
import android.graphics.SweepGradient;
import android.view.*;

public class MyCircleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new ViewWithGradient(this));
    }
   
 private static class ViewWithGradient extends View{
     public ViewWithGradient(Context context){
      super(context);
     }
     
     @Override
     protected void onDraw(Canvas canvas){
      canvas.drawColor(Color.WHITE);
      
      Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      
      LinearGradient linGrad = new LinearGradient(0,0,25,25,
        Color.RED, Color.BLACK, Shader.TileMode.MIRROR);
      circlePaint.setShader(linGrad);
      canvas.drawCircle(100, 100, 100, circlePaint);
      
      RadialGradient radGrad = new RadialGradient(250, 175,50,
        Color.GREEN, Color.BLACK, Shader.TileMode.MIRROR);
      circlePaint.setShader(radGrad);
      canvas.drawCircle(250, 175, 50, circlePaint);
      
      SweepGradient sweepGrad = new SweepGradient(canvas.getWidth()-175,
        canvas.getHeight()-175,
        new int[] {
         Color.RED, Color.YELLOW, Color.GREEN,
         Color.BLUE, Color.MAGENTA},null);
      circlePaint.setShader(sweepGrad);
      canvas.drawCircle(canvas.getWidth()-175,
        canvas.getHeight()-175, 100, circlePaint);
      
     }
     
    }
 
}



graphics -> bitmap, vector ==> 적정한 분배가 필요하다.

* 텍스트
글꼴 사용시 --> 실행 시점에서 AssetManager로 적재
산스세리프, 고정폭,세리프 서체 제공
--> 글꼴
mpaint.setTypeface(mtype);

*비트맵
Bitmap pic = BitmapFactory.decodeResource(getResource(), R.drawable.bluejay); //이진값 저장
canvas.drawBitmap(pic,0,0, null);

 -이미지 확대
 createScaledBitmap()
Bitmap jayPicSmall = Bitmap.createScaleBitmap(jayPic, 50, 75, false); //기본 100,100 -1일 경우 뒤집힌다. 그러므로  반사도 가능하다.

Matrix --< 하나의 그림으로 여러개의 그림을 표현.
 하나의 matrix 객체로 여러개의 변환을 적용 --> [변환 행렬]을 사용.
Matrix maxTopLeft = new Matrix();
maxTopLeft.preRotate(30); //왼쪽 상단에 30도 기울려야 표현

Matrix maxTopRight = new Matrix();
maxTopRight .preRotate(-30);
maxTopRight .preScale(-1,1);

---> 사용후 메모리에서 해지 중요. recycle();

# 직접그리기
ShapeDrawable, Shape
1) XML 자원으로 정의해서 도형 표시물
 /res/drawable

green_rect.xml --> XML로 그림 그릴려면 무조건 shape
<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://....."
  android:shape="rectangle">
 <solid android:color="#0f0"/>
</shape>
--> 위젯이 필요. main.xml ..@+id/ImageView1
..Java -->
ImageView iView = (ImageView) findViewById(R.Id.ImageView1);
iView.setImageResource(R.drawalble.green_rect);

2) XML 없이 직접 그릴 때
View --> onDraw(Canvas) -->Paint -->drawCircle()
iView2.SetImageDraw(..)

둥근모서리  RoundRectShape
--> float 값 8개 필요. (x,y)

AcrcShape --> 3시가 0도

경로도형:
선의 첫점 moveTo(x,y), 선의 끝점 linetTo(x,y)
직사각형 moveTo 1개 lineTo 4개


반응형