如何在android中绘制动画视图?

时间:2013-05-02 04:13:57

标签: android view android-animation

我想用动画来绘制这个图像similar的视图。首先,我想要绘制圆圈,然后是眼睛,然后是嘴巴(像视图那样的拱形),动画流畅。我们可以实现这样的动画吗? 。在这里,我遇到了定位视角和弧形口的问题。

1 个答案:

答案 0 :(得分:1)

请试试这个

<强>&GT; AnimView.java

   import android.app.Activity;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.RectF;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.SurfaceHolder.Callback;

    public class AnimView  extends  SurfaceView implements Runnable,
    Callback  {

        Paint paint = new Paint();
        Thread thread = null;
        SurfaceHolder surfaceHolder;
        boolean running=false;
        long time=0;

        int pos=0;

        public AnimView(Context context) {
            super(context);
            paint.setColor(Color.GREEN);
            surfaceHolder = getHolder();
        }


        public AnimView(Activity context) {
            // TODO Auto-generated constructor stub
            super(context);
            paint.setColor(Color.GREEN);
            surfaceHolder = getHolder();
        }


        public void onResumeMySurfaceView() {

            thread = new Thread(AnimView.this);
            thread.start();
            running=true;


        }

        public void onPauseMySurfaceView() {
            boolean retry = true;
            running = false;
            while (retry) {
                try {
                    thread.join();
                    retry = false;
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                //  e.printStackTrace();
                }
            }
        }

    //  @Override
    //    public void onDraw(Canvas canvas) {
    //            canvas.drawLine(0, 0, 20, 20, paint);
    //            canvas.drawLine(20, 0, 0, 20, paint);
    //            Log.i("onDraw","Draw");
    //         
    //    }

        @Override
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void surfaceCreated(SurfaceHolder arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void run() {
            // TODO Auto-generated method stub

            while(running)
            {
                if(time==0)
                {
                    time=System.currentTimeMillis();
                    pos=0;
                }
                if((System.currentTimeMillis()-time) >3000)
                {
                    pos=1;
                }

                if((System.currentTimeMillis()-time) >6000)
                {
                    pos=2;
                }

                if((System.currentTimeMillis()-time) >9000)
                {
                    pos=3;
                }

                if((System.currentTimeMillis()-time) >12000)
                {
                    pos=0;
                    time=System.currentTimeMillis();

                }




                Canvas canvas = surfaceHolder.lockCanvas();
                if(canvas!=null)
                {
                    paint.setColor(Color.WHITE);
                canvas.drawRect(0, 0, this.getWidth(),this.getHeight(), paint);

                if(pos>0)
                {
                paint.setColor(Color.GREEN);

                canvas.drawCircle(200, 200, 50, paint);

                paint.setColor(Color.WHITE);

                canvas.drawCircle(200, 200, 48, paint);
                }


                if(pos>1)
                {
                paint.setColor(Color.GREEN);

                canvas.drawCircle(175, 185, 5, paint);

                canvas.drawCircle(225, 185, 5, paint);
                }



                if(pos>2)
                {
                paint.setStrokeWidth(5);
                // opacity
                //p.setAlpha(0x80); //

                RectF rectF = new RectF(160, 160, 240, 240);


                canvas.drawArc (rectF, 45, 135, true, paint);


                 rectF = new RectF(159, 159, 238, 238);

                 paint.setColor(Color.WHITE);

                canvas.drawArc (rectF, 35, 145, true, paint);
                }




                surfaceHolder.unlockCanvasAndPost(canvas);
                }

                 Log.i("onDraw","Draw");
            }

        }

    }

<强>&GT; MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


public class MainActivity extends Activity {

    AnimView av;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        av=new AnimView(this);
        setContentView(av);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        av.onPauseMySurfaceView();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        av.onResumeMySurfaceView();
    }

}