zoukankan      html  css  js  c++  java
  • android的动画实例

    package src.com;

    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();
    }
    }

    }

  • 相关阅读:
    年度回忆录(2012.102013.01)
    Java中的Annotation(1)三个基本Annotation
    Java7中的文件和目录管理Path类
    Struts1和Struts2核心控制器的执行原理
    java中的IO基础3
    动态代理(2)动态代理和AOP
    java中的IO基础
    《嫌疑犯x的献身》看完了。。。
    像NHibernate致敬ado.net entity framework的范型DAO和open session in view实现
    我的MBTI职业性格测试
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2396043.html
Copyright © 2011-2022 走看看