티스토리 뷰
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();
}
}
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Linked List
- 지원
- 청년
- 혁오
- 김광석
- 연결 리스트
- 발생후
- 공연
- 청년정책
- 안드로이드
- 신청
- 공모전
- 뜻밖의 샤워
- 민경훈
- 버즈
- 태풍발생
- 지원금
- 김광석 코드
- 축제
- 서울특별시
- android
- 청년몽땅정보통
- 유니티
- 문화
- 고양이 깜놀
- 임창정
- 서울시
- 유머
- 아이유
- 김광석 기타 코드
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함