现在的位置: 首页 > 综合 > 正文

android 使用Canvas画箭头

2012年03月25日 ⁄ 综合 ⁄ 共 2509字 ⁄ 字号 评论关闭

画箭头这个东西太麻烦啦,开始想用把箭头画好,然后到指定点旋转的方法,但是,效果一直不好。想用数学的方法来画,但是发现计算很复杂啊。于是google,发现一个兄台使用了java当中的awt实现了画箭头(http://www.bangchui.org/simple/?t16755.html),于是就借过来,改了一下,结果真能用。成果不敢独占,在此拿来给大家分享:

public class MyCanvas extends View{
	
	private Canvas myCanvas;
	private Paint myPaint=new Paint();
	
	public MyCanvas(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public MyCanvas(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public MyCanvas(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		this.myCanvas=canvas;
		drawAL(0, 0, 100, 100);
	}
	
	/**
	 * 设置画笔默认样式
	 */
	public void setPaintDefaultStyle(){
		myPaint.setAntiAlias(true);
		myPaint.setColor(Color.RED);
		myPaint.setStyle(Paint.Style.STROKE);
		myPaint.setStrokeWidth(3);
	}
	
	
	/**
	 * 画圆
	 * @param x x坐标
	 * @param y	y坐标
	 * @param radius	圆的半径
	 */
	public void drawCircle(float x,float y,float radius){
		myCanvas.drawCircle(x, y, radius, myPaint);
		invalidate();
	}
	
	/**
	 * 画一条直线
	 * @param fromX 起点x坐标
	 * @param fromY	起点Y坐标
	 * @param toX	终点X坐标
	 * @param toY	终点Y坐标
	 */
	public void drawLine(float fromX,float fromY,float toX,float toY){
		Path linePath=new Path();
		linePath.moveTo(fromX, fromY);
		linePath.lineTo(toX, toY);
		linePath.close();
		myCanvas.drawPath(linePath, myPaint);
		invalidate();
	}
	
	
	/**
	 * 画箭头
	 * @param sx
	 * @param sy
	 * @param ex
	 * @param ey
	 */
	public void drawAL(int sx, int sy, int ex, int ey)
    {
        double H = 8; // 箭头高度   
        double L = 3.5; // 底边的一半   
        int x3 = 0;
        int y3 = 0;
        int x4 = 0;
        int y4 = 0;
        double awrad = Math.atan(L / H); // 箭头角度   
        double arraow_len = Math.sqrt(L * L + H * H); // 箭头的长度   
        double[] arrXY_1 = rotateVec(ex - sx, ey - sy, awrad, true, arraow_len);
        double[] arrXY_2 = rotateVec(ex - sx, ey - sy, -awrad, true, arraow_len);
        double x_3 = ex - arrXY_1[0]; // (x3,y3)是第一端点   
        double y_3 = ey - arrXY_1[1];
        double x_4 = ex - arrXY_2[0]; // (x4,y4)是第二端点   
        double y_4 = ey - arrXY_2[1];
        Double X3 = new Double(x_3);
        x3 = X3.intValue();
        Double Y3 = new Double(y_3);
        y3 = Y3.intValue();
        Double X4 = new Double(x_4);
        x4 = X4.intValue();
        Double Y4 = new Double(y_4);
        y4 = Y4.intValue();
        // 画线   
        myCanvas.drawLine(sx, sy, ex, ey,myPaint);
        Path triangle = new Path();
        triangle.moveTo(ex, ey);
        triangle.lineTo(x3, y3);  
        triangle.lineTo(x4, y4); 
        triangle.close();
        myCanvas.drawPath(triangle,myPaint);

    }
    // 计算   
    public double[] rotateVec(int px, int py, double ang, boolean isChLen, double newLen)
    {
        double mathstr[] = new double[2];
        // 矢量旋转函数,参数含义分别是x分量、y分量、旋转角、是否改变长度、新长度   
        double vx = px * Math.cos(ang) - py * Math.sin(ang);
        double vy = px * Math.sin(ang) + py * Math.cos(ang);
        if (isChLen) {
            double d = Math.sqrt(vx * vx + vy * vy);
            vx = vx / d * newLen;
            vy = vy / d * newLen;
            mathstr[0] = vx;
            mathstr[1] = vy;
        }
        return mathstr;
    }
	
	
}

显示效果如下图所示:

希望能够帮助到大家!

抱歉!评论已关闭.