달력

5

« 2024/5 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
728x90
반응형
안드로이드 - animation Drawable , 움직이는 그림 만들기

2011/06/16 20:09

복사 http://blog.naver.com/ijoos/60132314317

#. 소스 설명

#. BitmapDrawable과 Bitmap

BitmapDrawable과 Bitmap에 대해서 알아봅시다.(blog.vizpei.kr/105116344 자료를 참고하였습니다)

- 리소스로부터 Bitmap을 얻어내는 방법1

Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);

1. Bitmap을 만들고

2. Canvas로 연결한 뒤에

3. Drawable 메소드를 통해서 Bitmap과 연결된 Canvas에 Drawable의 내용을 그림

- 리소스로부터 Bitmap을 얻어내는 방법2

BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(R.drawable.icon);
Bitmap bitmap = drawable.getBitmap()

#. BitmapDrawable Bitmap의 특징

1. Bitmap을 얻어 올 때는 final이다.

2. Immutable이다.

3. recycle() 메소드를 호출해서는 안됨

#. 예제 소스 코드

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class AnimationTestActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button btn1;
Button btn2;
Button btn3;
ImageView image1;
AnimationDrawable animationDrawable;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
image1 = (ImageView) findViewById(R.id.image1);

btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);

BitmapDrawable frame1 = (BitmapDrawable) getResources().getDrawable(
R.drawable.plane1);
BitmapDrawable frame2 = (BitmapDrawable) getResources().getDrawable(
R.drawable.plane2);
BitmapDrawable frame3 = (BitmapDrawable) getResources().getDrawable(
R.drawable.plane3);
BitmapDrawable frame4 = (BitmapDrawable) getResources().getDrawable(
R.drawable.plane4);
BitmapDrawable frame5 = (BitmapDrawable) getResources().getDrawable(
R.drawable.plane5);
BitmapDrawable frame6 = (BitmapDrawable) getResources().getDrawable(
R.drawable.plane6);



animationDrawable = new AnimationDrawable(); // frame1들을 넣는다. 시간과 함께
// 넣는다.
animationDrawable.addFrame(frame1, 500);
animationDrawable.addFrame(frame2, 500);
animationDrawable.addFrame(frame3, 500);
animationDrawable.addFrame(frame4, 500);
animationDrawable.addFrame(frame5, 500);
animationDrawable.addFrame(frame6, 500);

image1.setBackgroundDrawable(animationDrawable);
animationDrawable.setOneShot(false); // 반복실행을 하겠다. true일 시에는 한번만 수행하겠다.
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {

case R.id.btn1:
animationDrawable.start();
break;
case R.id.btn2:
animationDrawable.stop();
break;
case R.id.btn3:
finish();
break;
}
}
}

#. animationDrawable

객체의 인스턴스를 직접 new로 생성해서 하는 방법도 있지만 이미 프로젝트 내에서 사용할 이미지 리소스를 가지고 있는 경우라면 xml로 drawable 파일을 생성해 병행하여 작업하는 것이 편리하다.

<animation-list>

<item android:drawable="@drawable/파일이름" android:duration="미리초단위" />

<item />

<item />

</animation-list> 이와 같은 방식으로 추가하면 된다.

android:oneshot="boolean값" 이 속성은 애니메이션이 한번만 실행될지 아니면 계쏙해서 반복될지를 결정한다. false가 반복한다.

#. 애니메이션 시작

mInAnimation.setBackgroundResource(R.drawable.id값);

AnimationList에 아이템으로 포함되어 있는 drawable 리소스를 imageView의 백그라운드로 지정한다. 최초에 보여지는 이미지는 첫번째 아이템이다.

AnimationDrawable frameAnimation = (AnimationDrawable)

mIvAnimation.getBackgraoud();

위에서 백그라운드로 지정된 애니메이션 리소스를 AnimationDrawable로 캐스팅하여 인스턴스를 만든다.

frameAnimation.start(); .stop();

애니메이션을 시작하고 멈춘다.

728x90
반응형
:
Posted by mapagilove