跟之前的一样surface类
package com.umbrella;
import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class UmbrellaView extends SurfaceView implements SurfaceHolder.Callback {
private MyThread thread;
public UmbrellaView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public UmbrellaView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public UmbrellaView(Context context) {
super(context);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread = new MyThread(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
thread.setWidth(width);
thread.setHeight(height);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
thread.isRunning =false;
}
}
下面是绘制线程
package com.umbrella;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.SystemClock;
import android.view.SurfaceHolder;
public class MyThread extends Thread {
private SurfaceHolder holder;
public boolean isRunning;
private int width;
private int height;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public MyThread(SurfaceHolder holder){
this.holder = holder;
isRunning = true;
}
int count = 0;
private int radio;
@Override
public void run() {
int helfWidth = getWidth()/2;
int helfHeight = getHeight()/2;
Paint paint = new Paint();
radio = (helfWidth-100);
while(isRunning){
Canvas canvas = holder.lockCanvas();
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
paint.setColor(Color.RED);
paint.setStyle(Style.STROKE);
// paint.setStrokeWidth(19);
canvas.drawCircle(helfWidth, helfHeight, 2*radio, paint);
paint.setStyle(Style.FILL);
// paint.setColor(Color.DKGRAY);
// canvas.drawCircle(helfWidth, helfHeight, 200, paint);
drawUmbrella(canvas,paint,helfWidth,helfHeight);
SystemClock.sleep(10);
count++;
//TODO 绘制八边形
holder.unlockCanvasAndPost(canvas);
}
}
private void drawUmbrella(Canvas canvas,Paint paint,int centerX,int centerY){
paint.setAntiAlias(true);
for(int i=0;i<8;i++){
Path path = new Path();
path.moveTo(centerX, centerY);
int x1 = (int) (centerX+radio*Math.sin(Math.toRadians(count+i*45)));
int y1 = (int) (centerY+radio*Math.cos(Math.toRadians(count+i*45)));
int x2 = (int) (centerX+radio*Math.sin(Math.toRadians(count+i*45+45)));
int y2 = (int) (centerY+radio*Math.cos(Math.toRadians(count+i*45+45)));
path.lineTo(x1, y1);
path.lineTo(x2, y2);
path.close();
if(i%2==0){
paint.setColor(Color.BLUE+setColor(count));
}else{
paint.setColor(Color.GREEN+setColor(count));
}
canvas.drawPath(path, paint);
}
paint.setColor(Color.WHITE);
for(int i=0;i<8;i++){
Path path1 = new Path();
// path.addArc(new RectF(centerX-2*radio,centerY-2*radio,2*radio+centerX,centerY+2*radio),i*45,45);
// int x1 = (int) (centerX+radio*Math.sin(Math.toRadians(count+i*45)));
// int y1 = (int) (centerY+radio*Math.cos(Math.toRadians(count+i*45)));
// int x2 = (int) (centerX+radio*Math.sin(Math.toRadians(count+i*45+45)));
// int y2 = (int) (centerY+radio*Math.cos(Math.toRadians(count+i*45+45)));
// path1.addArc(new RectF(100,centerY-radio,2*radio+100,centerY+radio),i*45,45);
//
// Matrix matrix = new Matrix();
// matrix.setRotate(10, (x1), (y1));
matrix.setRotate(180,x1,y1);
matrix.postRotate(180);
// path1.transform(matrix);
// canvas.drawPath(path1, paint);
int ppx = (int) (centerX+2*radio*(Math.cos(Math.toRadians(22.5)))*Math.sin(Math.toRadians(count+i*45+22.5)));
int ppy = (int) (centerY+2*radio*(Math.cos(Math.toRadians(22.5)))*Math.cos(Math.toRadians(count+i*45+22.5)));
canvas.drawCircle(ppx, ppy, radio, paint);
}
}
public int setColor(int color) {
int red = (color & 0xff0000) >> 16;
int green = (color & 0x00ff00) >> 8;
int blue = (color & 0x0000ff);
return Color.rgb(red, green, blue);
}
}
这样就可以了,不过有待优化,比较占用内存
下载地址:
http://download.csdn.net/detail/jackice01/7669397