2017-01-16 1 views
0

J'ai un drawable d'une jauge et j'essaie de tracer une ligne dessus. (Position fixe, pas de valeur fixe)DrawArc sur l'image, le problème de positionnement

Malheureusement, je n'ai aucune expérience avec le dessin. (Et je pense qu'il est difficile à comprendre)

le moment, il ressemble à ceci: https://i.stack.imgur.com/5tkXEl.png

Comme vous pouvez le voir, son droit de ne pas positionné et je suis tout à fait peur de regarder sur d'autres appareils.

code (classe étend Voir):

private void init(Context context) { 
    bounds = new Rect(); 
    gaugeBackground = ContextCompat.getDrawable(context, R.drawable.gauge); 
    arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    arcPaint.setColor(Color.RED); 
    arcPaint.setStyle(Paint.Style.STROKE); 
    arcPaint.setStrokeWidth(7f); 
} 

protected void onDraw(Canvas canvas) { 
    canvas.getClipBounds(bounds); 
    gaugeBackground.setBounds(bounds); 
    gaugeBackground.draw(canvas); 

    float padding = 5; 
    float size = getWidth() < getHeight() ? getWidth() : getHeight(); 
    float width = size - (2 * padding); 
    float height = size - (2 * padding); 
    float radius = (width < height ? width /2 : height /2); 

    float rectLeft = (width /2) - radius + padding; 
    float rectTop = (getHeight() /2) - radius + padding; 
    float rectRight = (getWidth() /2) - radius + padding + width; 
    float rectBottom = (getHeight() /2) - radius + padding + height; 

    RectF mRect = new RectF(rectLeft, rectTop, rectRight, rectBottom); 
    canvas.drawArc(mRect, 119f, 300f, false, arcPaint); 
} 

Quelqu'un peut-il fournir des informations utiles? J'espère avoir enfin tous les trucs de RectF/Drawing ..

Répondre

0

Eh bien, j'ai eu plusieurs problèmes avec mon implémentation. Problème principal: L'image de fond était «déformée», donc la jauge n'était pas vraiment ronde.

En outre: Le code était en quelque sorte foiré et trop compliqué.

Je laisse la question ici - peut-être que cela aidera quelqu'un dans le futur.

protected void onDraw(Canvas canvas) { 
    canvas.getClipBounds(bounds);+ 
    //0.86 == Aspect Ratio of the Background 
    gaugeBackground.setBounds(0, 0, bounds.right, (int) Math.round(bounds.bottom * 0.86)); 
    gaugeBackground.draw(canvas); 

    float padding = 3; 
    float size = getWidth() < getHeight() ? getWidth() : getHeight(); 
    float width = size - (2 * padding); 
    float height = size - (2 * padding); 

    RectF mRect = new RectF(padding, padding, width, height); 
    canvas.drawArc(mRect, 134f, 271f, false, arcPaint); 
}