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

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偏移量
}

}

}}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值