java - 在Android中的Canvas上绘制圆外的位图(箭头)

标签 java android canvas bitmap android-canvas

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

这是我的代码:

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);
        }


    }

这是我的输出

Here is my output

我认为问题出在填充上。

最佳答案

我做了新写的代码。 您应该检查此代码。

@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 );
}

关于java - 在Android中的Canvas上绘制圆外的位图(箭头),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46573897/

相关文章:

java - 任务 ':app:dexDebug' 执行失败

java - 用Java存储个人数据的好方法?

java - elasticsearch:将 StreamOutput 转换为 String

java - 如何通过测试容器启动 Informix?

javascript - 如何在 HTML5 Canvas 上进行正确的边界检查?

javascript - EaselJS 阿尔法蒙版过滤器

javascript - 如何旋转 <canvas> 的一部分,而不是整个元素?

java - 在客户端-服务器环境中建模用户类的最佳实践

java - Android 版 Gson

java - 在将输入流解析为 XML 之前,如何替换输入流中的 HTML 转义字符?