问题
How can I create gauge same as this? any library or maven exist for this gauge type? I use canvas for circle gauge ,but I can't create this one
RectF oval = getOval(canvas, 1);
RectF oval2 = getOval(canvas, 1.1f);
Paint paint = new Paint();
paint.setColor(Color.DKGRAY);
canvas.drawArc(oval2, 180, 90, true, paint);
canvas.drawArc(oval, 180, 90, true, backgroundPaint);
RectF innerOval = getOval(canvas, 0.9f);
canvas.drawArc(innerOval, 180, 90, true, backgroundInnerPaint);
<GaugeView
android:id="@+id/gauge"
android:layout_width="@dimen/panel_gauge_height"
android:layout_height="@dimen/panel_gauge_width"
app:divisions="8"
app:scaleEndValue="8"
app:scaleStartAngle="50"
app:scaleStartValue="0"
app:showNeedle="true"
app:subdivisions="2" />
回答1:
You could just google to find a bunch of GagueViews. I think you already did that.
This seems simple. So you could write your own custom
View
. You can usedrawArc
to draw the 3 different color arc. You can usedrawLine
to draw the measuring points (just remember to setPaint.setStrokeCap(Cap.Round)
). As for the needle, you could usedrawPath
. With some effort and the right coordinates, you should be able to write a nifty GaugeView yourself.If you find writing your own view difficult, you can refer some
GaugeView
in theGitHub
. You will get a good starting point.
UPDATE: I wrote a simple GaugeView
based on the image in you question. The calculations are in Pixels, you might want to multiply them with DisplayMetrics.density
, so that they are in independent pixels. Also, you might want to expose most of the values here via the xml, so you can control them in the layout. This might be a good starting point.
public class GaugeView extends View {
private Paint arcPaint;
public GaugeView(Context context) {
super(context);
initialize();
}
public GaugeView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public GaugeView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize();
}
private void initialize() {
arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
arcPaint.setStyle(Paint.Style.STROKE);
arcPaint.setStrokeWidth(15f);
}
@Override
protected void onDraw(Canvas canvas) {
int width = canvas.getWidth();
int height = canvas.getHeight();
int arcCenterX = width - 10;
int arcCenterY = height - 10;
final RectF arcBounds = new RectF(arcCenterX - 100, arcCenterY - 100, arcCenterX + 100, arcCenterY + 100);
// Draw the arc
canvas.drawArc(arcBounds, 180f, 20f, false, arcPaint);
arcPaint.setColor(Color.DKGRAY);
canvas.drawArc(arcBounds, 200f, 40f, false, arcPaint);
arcPaint.setColor(Color.GRAY);
canvas.drawArc(arcBounds, 2400f, 30f, false, arcPaint);
// Draw the pointers
final int totalNoOfPointers = 20;
final int pointerMaxHeight = 25;
final int pointerMinHeight = 15;
int startX = arcCenterX - 120;
int startY = arcCenterY;
arcPaint.setStrokeWidth(5f);
arcPaint.setStrokeCap(Paint.Cap.ROUND);
int pointerHeight;
for (int i = 0; i <= totalNoOfPointers; i++) {
if(i%5 == 0){
pointerHeight = pointerMaxHeight;
}else{
pointerHeight = pointerMinHeight;
}
canvas.drawLine(startX, startY, startX - pointerHeight, startY, arcPaint);
canvas.rotate(90f/totalNoOfPointers, arcCenterX, arcCenterY);
}
}
}
The final rendered image looks like this:
来源:https://stackoverflow.com/questions/33591441/draw-quarter-gauge-with-canvas-in-android