Draw Lines perodically

我与影子孤独终老i 提交于 2019-11-30 23:25:24

Another way, a bit more generic:

public class LinesActivity extends Activity {
    DemoView demoview;
    int mCount = 0;
    int mListSize = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Paint paint = new Paint();
        paint.setColor(Color.RED);

        List<Coords> coordList = new ArrayList<Coords>();
        // Load up the coordinates
        coordList.add(new Coords(60, 60, 120, 60));
        coordList.add(new Coords(60, 60, 60, 120));
        coordList.add(new Coords(60, 120, 120, 120));
        coordList.add(new Coords(120, 120, 120, 180));
        coordList.add(new Coords(120, 180, 60, 180));
        mListSize = coordList.size();
        demoview = new DemoView(this, paint, coordList);
        setContentView(demoview);
        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            public void run() {
                mCount++;
                demoview.postInvalidate();
                Log.d("LINES", "Timer triggered");
                if (mCount >= mListSize) {
                    Log.d("LINES", "All done, cancelling timer");
                    cancel();
                }
            }
        }, 5000, 5000);

    }

    private class Coords {
        // little class to hold the coordinates
        float mSx; float mSy; float mEx; float mEy;

        public Coords(float sx, float sy, float ex, float ey) {
            mSx = sx;   mSy = sy; // start/end x/y
            mEx = ex;   mEy = ey;
        }
    }

    private class DemoView extends View {

        Paint mPaint;
        List<Coords> mcList;

        public DemoView(Context context, Paint paint, List<Coords> cList) {
            super(context);
            mPaint = paint;
            mcList = cList;
        }
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            int count = 0;
            Log.d("LINES", "mcount" + mCount);
            for (Coords c : mcList) { // draw all the lines
                if (count >= mCount)
                    break; // up to the number in mCount
                canvas.drawLine(c.mSx, c.mSy, c.mEx, c.mEy, mPaint);
                count++;
            }

        }
    }
}

Ammu

try this code

final Thread thread = new Thread(new Runnable() 
                           {
                                @Override
                                public void run() 
                                {
                                    synchronized (this)
                                    {

                                        try {
                                            wait(500);
                                            Main.this.runOnUiThread(new Runnable() {@Override public void run()
                                            {
                                                mCanvas.drawLine(60, 60, 120,60, touchPaint);
                                                v.invalidate();
                                            }});

                                            wait(500);
                                            Main.this.runOnUiThread(new Runnable() {@Override public void run()
                                            {
                                                mCanvas.drawLine(60, 60, 60, 120, touchPaint);
                                                v.invalidate();
                                            }});
                                            wait(500);
                                            Main.this.runOnUiThread(new Runnable() {@Override public void run()
                                            {
                                                mCanvas.drawLine(60, 120, 120, 120, touchPaint);
                                                v.invalidate();
                                            }});
                                            wait(500);
                                            Main.this.runOnUiThread(new Runnable() {@Override public void run()
                                            {
                                                mCanvas.drawLine(120, 120, 120, 180, touchPaint);
                                                v.invalidate();
                                            }});
                                            wait(500);
                                            Main.this.runOnUiThread(new Runnable() {@Override public void run()
                                            {
                                                mCanvas.drawLine(120, 180, 60, 180, touchPaint);
                                                v.invalidate();
                                            }});
                                        } catch (Exception e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }

                                    }

                                }
                            });
                           thread.start();

where mCanvas is your canvas, v is your View, touchpaint is your Paint & Main.this is your activity

seems u need to use the runnable and in the runnable u have to invalidate the canvas view , ypur code seems like that .

handler.post(runnable);
int i = 0;
Runnable runnable = new Runnable() {
        @Override
        public void run() {

                             i++;
     switch(i){
     // do whatever yo want to do ..
        } 
                                layoutObj.removeView(your canvas View); 
                                layoutObj.addView(your canvas View);

                handler.removeCallbacks(runnable);
                handler.postDelayed(runnable, 5000L);
        }
    };
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!