android 画图------类似跑马灯的效果

这是第二个实例画图里面,看的不是太懂,只是从中可以学会控件的继承与使用,当然也可以通过这个实例改造成跑马灯的效果,文字的跑马灯效果在这里不写了。
public class AnimateDrawables extends Activity {
private static Drawable dr;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }
   
    private static class SampleView extends View {
        private AnimateDrawable mDrawable;

        public SampleView(Context context) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);

            dr = context.getResources().getDrawable(R.drawable.beach);
//最主要的还是这句了,这个和下面的canvas相关  mDrawable.draw(canvas);

           dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
// 就是一个动态效果没啥说的
            Animation an = new TranslateAnimation(0, 100, 0, 200);
            an.setDuration(2000);
            an.setRepeatCount(-1);
//这句也算比较重要的
            an.initialize(2, 2, 30, 30);
           
            mDrawable = new AnimateDrawable(dr, an);
            an.startNow();
        
        }
      
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(Color.WHITE);
//这句比较重要了
            mDrawable.draw(canvas);
         //  dr.draw(canvas);
            invalidate();
        }
       
    }
}

在public class AnimateDrawable extends ProxyDrawable中
@Override
    public void draw(Canvas canvas) {
        Drawable dr = getProxy();
        if (dr != null) {
            int sc = canvas.save();
            Log.i("aa",sc+"aa");
            Animation anim = mAnimation;
            if (anim != null) {
                anim.getTransformation(
                                    AnimationUtils.currentAnimationTimeMillis(),
                                    mTransformation);
                canvas.concat(mTransformation.getMatrix());
            }
            dr.draw(canvas);
            canvas.restoreToCount(sc);
        }
       
    }
比较重要了 只要实现图片的效果。

你可能感兴趣的:(android)