利用surfaceview和path绘制雨伞图形

本文介绍如何在Android中使用SurfaceView和Path类来绘制雨伞图形。通过结合线程技术,实现动态效果,但当前实现存在内存占用高的问题,需要进一步优化。示例代码和下载链接已提供。
摘要由CSDN通过智能技术生成

跟之前的一样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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值