Java 在Android中在画布上的圆圈外绘制位图(箭头)

Java 在Android中在画布上的圆圈外绘制位图(箭头),java,android,canvas,bitmap,android-canvas,Java,Android,Canvas,Bitmap,Android Canvas,我刚刚开始在Android中创建一些自定义视图,但在圆外绘制位图(箭头)时遇到了问题 这是我的密码: Canvas osCanvas = new Canvas(windowFrame); // Create a canvas to draw onto the new image RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight()); Paint paint = new Paint(Paint.ANTI_

我刚刚开始在Android中创建一些自定义视图,但在圆外绘制位图(箭头)时遇到了问题

这是我的密码:

Canvas osCanvas = new Canvas(windowFrame); // Create a   canvas to draw onto the new image
    RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight());
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // Anti alias allows for smooth corners
    paint.setColor(Color.parseColor("#FAFAFA")); // This is the color of your activity background
    osCanvas.drawRect(outerRectangle, paint);
    final Paint stroke = new Paint();

    //paint.setColor(Color.TRANSPARENT); // An obvious color to help debugging
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); // A out B http://en.wikipedia.org/wiki/File:Alpha_compositing.svg
    float centerX = getWidth() / 2;
    float centerY = getHeight() / 2;
    double rad = Math.min(getWidth(), getHeight()) / 2.5 - menuInnerPadding;
    float radius = (float) rad;

    stroke.setColor(Color.parseColor("#999999"));
    stroke.setStyle(Paint.Style.STROKE);
    stroke.setAntiAlias(true);
    stroke.setStrokeWidth(5f);
    osCanvas.drawCircle(centerX ,
            centerY , radius - stroke.getStrokeWidth() +5f, stroke);

    for(int i=0;i<elements.size();i++){
        double angle =0;
        if(i==0){
            angle = startAngle;
        }else{
            angle = startAngle+(i * ((2 * Math.PI) / elements.size()));
        }
        elements.get(i).x = (int) (centerX + Math.cos(angle)*(radius));
        elements.get(i).y = (int) (centerY + Math.sin(angle)*(radius));
        float ang = (float) Math.cos(angle)*(radius);
        Path path = new Path();
        path.addCircle(elements.get(i).x,elements.get(i).y, radius, Path.Direction.CW);
        if(BLINKER_ID == i){
            if(blinkerPain != null){
                osCanvas.drawBitmap(elements.get(i).bitmap,elements.get(i).x,elements.get(i).y,blinkerPain);
                blinkerPain = null;
            }
        }else{

            // here i am drawing the red arrows (bitmap images) But it's not fitting outside the circle.
            osCanvas.drawBitmap(elements.get(i).bitmap,elements.get(i).x  ,elements.get(i).y ,textPaint);
        }


    }
Canvas osCanvas=新画布(窗框);//创建画布以绘制到新图像上
RectF outerRectangle=新的RectF(0,0,getWidth(),getHeight());
油漆油漆=新油漆(油漆.防油漆别名标志);//防锯齿允许平滑的拐角
paint.setColor(Color.parseColor(#FAFAFA”);//这是活动背景的颜色
osCanvas.drawRect(外直角、油漆);
最终油漆冲程=新油漆();
//paint.setColor(Color.TRANSPARENT);//有助于调试的明显颜色
paint.setXfermode(新的PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));//A出Bhttp://en.wikipedia.org/wiki/File:Alpha_compositing.svg
浮动中心X=getWidth()/2;
浮动中心=getHeight()/2;
double rad=Math.min(getWidth(),getHeight())/2.5-menuinnerpladding;
浮动半径=(浮动)rad;
stroke.setColor(Color.parseColor(#999999”);
笔划.设置笔划(画图.笔划.笔划);
stroke.setAntiAlias(真);
行程设置行程宽度(5f);
osCanvas.drawCircle(centerX,
中心,半径-笔划。getStrokeWidth()+5f,笔划);
对于(inti=0;i我编写了新代码。
你应该检查这个代码

@Override
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(Color.BLUE);
    paint.setStrokeWidth( 2 );
    paint.setStyle(Paint.Style.STROKE );
    canvas.drawColor(Color.WHITE);

    float x = 400;
    float y = 400;
    float r = 200;

    canvas.drawCircle( x , y , r , paint );

    Bitmap icon = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_launcher);

    int length = 8;

    float radian = (float)(Math.PI * 2f) / length;

    for( int i = 0; i<length; i++ ) {
        drawBitmap( canvas , icon , x , y , r, radian * i);
    }
}

private void drawBitmap( Canvas canvas, Bitmap bitmap, float pivotX, float pivotY, float radius, float radian ) {
    Matrix m = new Matrix();
    m.postTranslate( -bitmap.getWidth()/2 , -bitmap.getHeight()/2 );
    m.postRotate( (float)(radian * 180 / Math.PI) + 90 );

    float drawHeight = radius + (bitmap.getHeight()/2);

    m.postTranslate( ((float)Math.cos( radian ) * drawHeight) + pivotX
            , ((float)Math.sin( radian ) * drawHeight) + pivotY );

    canvas.drawBitmap( bitmap , m , null );
}
@覆盖
受保护的void onDraw(画布){
油漆=新油漆();
油漆。设置颜色(颜色。蓝色);
油漆。设置行程宽度(2);
绘制.setStyle(绘制.Style.STROKE);
画布。drawColor(颜色。白色);
浮点数x=400;
浮动y=400;
浮点数r=200;
画布。画圈(x、y、r、油漆);
位图图标=BitmapFactory.decodeResource(getContext().getResources(),R.mipmap.ic_启动器);
整数长度=8;
浮动弧度=(浮动)(Math.PI*2f)/长度;
对于(inti=0;i我编写了新代码。
你应该检查这个代码

@Override
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(Color.BLUE);
    paint.setStrokeWidth( 2 );
    paint.setStyle(Paint.Style.STROKE );
    canvas.drawColor(Color.WHITE);

    float x = 400;
    float y = 400;
    float r = 200;

    canvas.drawCircle( x , y , r , paint );

    Bitmap icon = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_launcher);

    int length = 8;

    float radian = (float)(Math.PI * 2f) / length;

    for( int i = 0; i<length; i++ ) {
        drawBitmap( canvas , icon , x , y , r, radian * i);
    }
}

private void drawBitmap( Canvas canvas, Bitmap bitmap, float pivotX, float pivotY, float radius, float radian ) {
    Matrix m = new Matrix();
    m.postTranslate( -bitmap.getWidth()/2 , -bitmap.getHeight()/2 );
    m.postRotate( (float)(radian * 180 / Math.PI) + 90 );

    float drawHeight = radius + (bitmap.getHeight()/2);

    m.postTranslate( ((float)Math.cos( radian ) * drawHeight) + pivotX
            , ((float)Math.sin( radian ) * drawHeight) + pivotY );

    canvas.drawBitmap( bitmap , m , null );
}
@覆盖
受保护的void onDraw(画布){
油漆=新油漆();
油漆。设置颜色(颜色。蓝色);
油漆。设置行程宽度(2);
绘制.setStyle(绘制.Style.STROKE);
画布。drawColor(颜色。白色);
浮点数x=400;
浮动y=400;
浮点数r=200;
画布。画圈(x、y、r、油漆);
位图图标=BitmapFactory.decodeResource(getContext().getResources(),R.mipmap.ic_启动器);
整数长度=8;
浮动弧度=(浮动)(Math.PI*2f)/长度;
对于(int i=0;i