2. AnimationDrawable
//Drawble객체를 파생하는 이유는 특정하게 그릴수 있는 객체를 하나 만들어서 .Draw(Canvers)를
//콜하면 Canvers 위에 Drawable 객체를 그려준다.
//객체를 한번 생성한후에 그객체를 그냥 Draw함수만 부르면 밑에 드로우 로직이 어떤지는 알 필요 없이
//그려지게 하기 위한 역할을 만들 수 있다.
public class ProxyDrawable extends Drawable {
..기타 함수들을 override를 한다.
//그려질지 말지를 셋팅을 한다.
@Override
public Drawable mutate() {
if (mProxy != null && !mMutated && super.mutate() == this) {
mProxy.mutate();
mMutated = true;
}
return this;
}
}
public class AnimateDrawable extends ProxyDrawable {
private Animation mAnimation;
private Transformation mTransformation = new Transformation();
public AnimateDrawable(Drawable target) {
super(target);
}
public AnimateDrawable(Drawable target, Animation animation) {
super(target);
mAnimation = animation;
}
public Animation getAnimation() {
return mAnimation;
}
public void setAnimation(Animation anim) {
mAnimation = anim;
}
public boolean hasStarted() {
return mAnimation != null && mAnimation.hasStarted();
}
public boolean hasEnded() {
return mAnimation == null || mAnimation.hasEnded();
}
@Override
public void draw(Canvas canvas) {
Drawable dr = getProxy();
if (dr != null) {
//롤백할 캔버스 객체를 저장한다.
int sc = canvas.save();
Animation anim = mAnimation;
if (anim != null) {
//Animation객체를
anim.getTransformation(
AnimationUtils.currentAnimationTimeMillis(),
mTransformation);
//캔퍼스를 이동한다.
canvas.concat(mTransformation.getMatrix());
}
//dr객체를 캔버스에 그려준다.
dr.draw(canvas);
//어디가지 기억한 캔버스로 돌아간다.
canvas.restoreToCount(sc);
}
}
}
private static class SampleView extends View {
private AnimateDrawable mDrawable;
public SampleView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
Drawable dr = context.getResources().getDrawable(R.drawable.beach);
dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
Animation an = new TranslateAnimation(0, 100, 0, 200);
an.setDuration(2000);
an.setRepeatCount(-1);
an.initialize(10, 10, 10, 10);
mDrawable = new AnimateDrawable(dr, an);
an.startNow();
}
@Override protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
mDrawable.draw(canvas);
invalidate();
}
}
//Drawble객체를 파생하는 이유는 특정하게 그릴수 있는 객체를 하나 만들어서 .Draw(Canvers)를
//콜하면 Canvers 위에 Drawable 객체를 그려준다.
//객체를 한번 생성한후에 그객체를 그냥 Draw함수만 부르면 밑에 드로우 로직이 어떤지는 알 필요 없이
//그려지게 하기 위한 역할을 만들 수 있다.
public class ProxyDrawable extends Drawable {
..기타 함수들을 override를 한다.
//그려질지 말지를 셋팅을 한다.
@Override
public Drawable mutate() {
if (mProxy != null && !mMutated && super.mutate() == this) {
mProxy.mutate();
mMutated = true;
}
return this;
}
}
public class AnimateDrawable extends ProxyDrawable {
private Animation mAnimation;
private Transformation mTransformation = new Transformation();
public AnimateDrawable(Drawable target) {
super(target);
}
public AnimateDrawable(Drawable target, Animation animation) {
super(target);
mAnimation = animation;
}
public Animation getAnimation() {
return mAnimation;
}
public void setAnimation(Animation anim) {
mAnimation = anim;
}
public boolean hasStarted() {
return mAnimation != null && mAnimation.hasStarted();
}
public boolean hasEnded() {
return mAnimation == null || mAnimation.hasEnded();
}
@Override
public void draw(Canvas canvas) {
Drawable dr = getProxy();
if (dr != null) {
//롤백할 캔버스 객체를 저장한다.
int sc = canvas.save();
Animation anim = mAnimation;
if (anim != null) {
//Animation객체를
anim.getTransformation(
AnimationUtils.currentAnimationTimeMillis(),
mTransformation);
//캔퍼스를 이동한다.
canvas.concat(mTransformation.getMatrix());
}
//dr객체를 캔버스에 그려준다.
dr.draw(canvas);
//어디가지 기억한 캔버스로 돌아간다.
canvas.restoreToCount(sc);
}
}
}
private static class SampleView extends View {
private AnimateDrawable mDrawable;
public SampleView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
Drawable dr = context.getResources().getDrawable(R.drawable.beach);
dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
Animation an = new TranslateAnimation(0, 100, 0, 200);
an.setDuration(2000);
an.setRepeatCount(-1);
an.initialize(10, 10, 10, 10);
mDrawable = new AnimateDrawable(dr, an);
an.startNow();
}
@Override protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
mDrawable.draw(canvas);
invalidate();
}
}
댓글