import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
public class ProxyDrawable extends Drawable {
private Drawable mProxy;//画图对象
public ProxyDrawable(Drawable target){//构造函数
mProxy = target;
}
public Drawable getProxy(){//获取新对象
return mProxy;
}
public void setProxy(Drawable proxy){//设置画图对象
if( proxy != this ){
mProxy = proxy;
}
}
@Override
public void draw(Canvas canvas) {//画图
if( mProxy != null ){
mProxy.draw(canvas);
}
}
@Override
public void setAlpha(int alpha) {//设置透明度
if( mProxy != null ){
mProxy.setAlpha(alpha);
}
}
@Override
public void setColorFilter(ColorFilter cf) {//设置颜色过滤
if( mProxy != null ){
mProxy.setColorFilter(cf);
}
}
@Override
public int getOpacity() {//获取透明度
//return 0;
return mProxy != null ? mProxy.getOpacity() : PixelFormat.TRANSPARENT;
}
@Override
public int getIntrinsicWidth() {//返回固有的宽度
//return super.getIntrinsicWidth();
return mProxy != null ? mProxy.getIntrinsicWidth() : -1;
}
@Override
public int getIntrinsicHeight() {//返回固有的高度
//return super.getIntrinsicHeight();
return mProxy != null ? mProxy.getIntrinsicHeight() : -1;
}
@Override
public void setDither(boolean dither) {//设置抖动
//super.setDither(dither);
if( mProxy != null ){
mProxy.setDither(dither);
}
}
@Override
public void setFilterBitmap(boolean filter) {
//super.setFilterBitmap(filter);
if( mProxy != null ){
mProxy.setDither(filter);
}
}
}
package src.com;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Transformation;
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) {
//super.draw(canvas);
Drawable dr = getProxy();
if( dr != null ){
int sc = canvas.save();
Animation anim = mAnimation;
if( anim != null ){
anim.getTransformation(
AnimationUtils.currentAnimationTimeMillis(),
mTransformation);
canvas.concat(mTransformation.getMatrix());
}
dr.draw(canvas);
canvas.restoreToCount(sc);
}
}
}
package src.com;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
public class AnimationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(new AnimationView(this));
}
private static class AnimationView extends View{
private AnimateDrawable mDrawable;
public AnimationView(Context context){
super(context);
Drawable pic = context.getResources().getDrawable(R.drawable.mountain);
pic.setBounds(0, 0, pic.getIntrinsicWidth(), pic.getIntrinsicHeight());
Animation move = new TranslateAnimation(0, 150, 0, 300);
move.setDuration(5000);
move.setRepeatCount(-1);
move.initialize(0, 0, 0, 0);
mDrawable = new AnimateDrawable(pic, move);
move.startNow();
}
@Override
protected void onDraw(Canvas canvas) {
//super.onDraw(canvas);
canvas.drawColor(Color.GREEN);
mDrawable.draw(canvas);
invalidate();
}
}
}