SurfaceView、SurfaceHolder画半径变化的圆和点移动成圆形

    xiaoxiao2021-03-26  32

    package com.rejointech.mitnick.circlefrom10to100viewtest; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new DemoSurfaceView(this)); } public class DemoSurfaceView extends SurfaceView implements SurfaceHolder.Callback { LoopThread thread; public DemoSurfaceView(Context context) { super(context); init(); //初始化,设置生命周期回调方法 } private void init() { SurfaceHolder holder = getHolder(); holder.addCallback(this); //设置Surface生命周期回调 thread = new LoopThread(holder, getContext()); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { thread.isRunning = true; thread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { thread.isRunning = false; try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 执行绘制的绘制线程 * * @author Administrator */ class LoopThread extends Thread { SurfaceHolder surfaceHolder; Context context; boolean isRunning; float radius = 10f; Paint paint; public LoopThread(SurfaceHolder surfaceHolder, Context context) { this.surfaceHolder = surfaceHolder; this.context = context; isRunning = false; paint = new Paint(); paint.setColor(Color. YELLOW ); paint.setStyle(Paint.Style. STROKE ); } @Override public void run() { Canvas c = null; while (isRunning) { try { synchronized (surfaceHolder) { c = surfaceHolder.lockCanvas(null); doDraw(c); //通过它来控制帧数执行一次绘制后休息50ms Thread. sleep (50); } } catch (InterruptedException e) { e.printStackTrace(); } finally { surfaceHolder.unlockCanvasAndPost(c); } } } public void doDraw(Canvas c) { //这个很重要,清屏操作,清楚掉上次绘制的残留图像 c.drawColor(Color. BLACK ); c.translate(200, 200); c.drawCircle(0, 0, radius++, paint); if (radius > 100) { radius = 10f; } } } }} =============================================== 点移动成圆形 package com.rejointech.mitnick.circlefrom10to100viewtest; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new DemoSurfaceView(this)); } public class DemoSurfaceView extends SurfaceView implements SurfaceHolder.Callback { LoopThread thread; public DemoSurfaceView(Context context) { super(context); init(); //初始化,设置生命周期回调方法 } private void init() { SurfaceHolder holder = getHolder(); holder.addCallback(this); //设置Surface生命周期回调 thread = new LoopThread(holder, getContext()); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { thread.isRunning = true; thread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { thread.isRunning = false; try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 执行绘制的绘制线程 * * @author Administrator */ class LoopThread extends Thread { SurfaceHolder surfaceHolder; Context context; boolean isRunning; float radius = 100f; float xPoint=99f; float yPoint=0f; boolean xFlag=true; Paint paint; float b=0; public LoopThread(SurfaceHolder surfaceHolder, Context context) { this.surfaceHolder = surfaceHolder; this.context = context; isRunning = false; paint = new Paint(); paint.setColor(Color. YELLOW ); paint.setStyle(Paint.Style. STROKE ); paint.setStrokeWidth(10f); } @Override public void run() { Canvas c = null; while (isRunning) { if (xPoint==99f) { for (xPoint=100f;xPoint<301f;xPoint++){ try { synchronized (surfaceHolder) { c = surfaceHolder.lockCanvas(null); doDraw(c,false); //通过它来控制帧数执行一次绘制后休息50ms // Thread.sleep(50); } } finally { surfaceHolder.unlockCanvasAndPost(c); } } } if (xPoint == 301f) { for (xPoint=300f;xPoint>99.999f;xPoint--){ try { synchronized (surfaceHolder) { c = surfaceHolder.lockCanvas(null); doDraw(c,true); //通过它来控制帧数执行一次绘制后休息50ms // Thread.sleep(50); } } finally { surfaceHolder.unlockCanvasAndPost(c); } } } } } public void doDraw(Canvas c,boolean isReturn) { //这个很重要,清屏操作,清除掉上次绘制的残留图像 c.drawColor(Color. BLACK ); c.translate(200, 100); // c.drawCircle(0, 0, radius++, paint); if (isReturn) { yPoint= -(float) Math. abs (Math. sqrt (100f*100f-(xPoint-200f)*(xPoint-200f))); }else { yPoint= (float) Math. abs (Math. sqrt (100f*100f-(xPoint-200f)*(xPoint-200f))); } Log. d ("~gyh", "xPoint: "+xPoint+" yPoint: "+yPoint); c.drawPoint(xPoint,yPoint+200.0f,paint);//1\2偏移量 } } }}
    转载请注明原文地址: https://ju.6miu.com/read-664244.html

    最新回复(0)