溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android開(kāi)發(fā)如何繪制淘寶收益圖折線效果

發(fā)布時(shí)間:2021-05-19 11:54:47 來(lái)源:億速云 閱讀:180 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

這篇文章主要介紹Android開(kāi)發(fā)如何繪制淘寶收益圖折線效果,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

Android開(kāi)發(fā)實(shí)現(xiàn)繪制淘寶收益圖折線效果。分享給大家供大家參考,具體如下:

實(shí)現(xiàn)的效果我一會(huì)貼上,我先說(shuō)下原理,我們知道要實(shí)現(xiàn)在canvas上畫(huà)線,不就是要搞一個(gè)paint嘛,然后首先肯定要設(shè)置下paint的屬性,那么畫(huà)文字呢,不就是Textpaint嗎,對(duì),就是這么簡(jiǎn)單,接下來(lái)怎么畫(huà),折線圖主要分為X軸和y軸,x軸表示日期,y表示收益,好,說(shuō)道這里,大家應(yīng)該知道怎么去做了,下面直接貼代碼

這個(gè)方法是,畫(huà)x,y坐標(biāo)系的,以及上面的日期和收益了

private void drawCoordinate(Canvas canvas) {
  //坐標(biāo)系畫(huà)筆
  Paint coordinatePaint = new Paint();
  coordinatePaint.setAntiAlias(true);
  coordinatePaint.setStrokeWidth(1);
  coordinatePaint.setColor(getResources().getColor(R.color.c5));
  //坐標(biāo)系文字畫(huà)筆
  TextPaint coordinateTextPaint = new TextPaint();
  coordinateTextPaint.setAntiAlias(true);
  coordinateTextPaint.setTextSize(scaleTextSize);
  coordinateTextPaint.setAntiAlias(true);
  coordinateTextPaint.setColor(scaleTextColor);
  coordinateTextPaint.setTextAlign(Align.CENTER);
  //水平的刻度線
  float verticalScaleStep = getVerticalScaleStep();
  coordinateTextPaint.setTextAlign(Align.RIGHT);
  float textHeight = getTextHeight(coordinateTextPaint, "8");
  for (int i = 0; i < maxVerticalScaleValue; i++) {
    float y = getHeight() - bottomPadding - (verticalScaleStep * i);
    canvas.drawLine(leftPadding, y, getWidth() - rightPadding, y, coordinatePaint);
    canvas.drawText(i + "", leftPadding - 13, y + textHeight / 2, coordinateTextPaint);
  }
  //垂直的刻度線
  float horizontalScaleStep = getHorizontalScaleStep();
  for (int i = 0; i < line.getSize(); i++) {
    float x = leftPadding + (horizontalScaleStep * i);
    if (i == 0) {
      canvas.drawLine(x, topPadding, x, getHeight() - bottomPadding, coordinatePaint);
    }
    coordinateTextPaint.setColor(mTouchIndex == i ? verticalLineColor : scaleTextColor);
    coordinateTextPaint.setTextAlign(i == 0 ? Align.LEFT : Align.CENTER);
    canvas.drawText(line.getPoint(i).getX(), x, getHeight() - bottomPadding + textHeight + 10, coordinateTextPaint);
  }
}

但是產(chǎn)品有個(gè)需求啊,就是點(diǎn)擊當(dāng)前日期可以查看我的收益,并且在交匯點(diǎn)上展示出來(lái)

private void drawCurve(Canvas canvas) {
  Paint curvePaint = new Paint();//曲線畫(huà)筆
  curvePaint.setColor(curveColor);
  curvePaint.setAntiAlias(true);
  curvePaint.setStrokeWidth(curveStrokeWidth);
  float horizontalScaleStep = getHorizontalScaleStep();
  float lastXPixels = 0, newYPixels = 0;
  float lastYPixels = 0, newXPixels = 0;
  float useHeight = getHeight() - bottomPadding - topPadding;
  for (int i = 0; i < line.getSize(); i++) {
    float yPercent = line.getPoint(i).getY() / maxVerticalScaleValue;
    if (i == 0) {
      lastXPixels = leftPadding + i * horizontalScaleStep;
      lastYPixels = getHeight() - bottomPadding - useHeight * yPercent;
    } else {
      newXPixels = leftPadding + i * horizontalScaleStep;
      newYPixels = getHeight() - bottomPadding - useHeight * yPercent;
      canvas.drawLine(lastXPixels, lastYPixels, newXPixels, newYPixels, curvePaint);
      lastXPixels = newXPixels;
      lastYPixels = newYPixels;
    }
    line.getPoint(i).fLineX = lastXPixels;
    line.getPoint(i).fLineY = lastYPixels;
  }
}

點(diǎn)擊交匯點(diǎn),有文字提示說(shuō)明,

private void drawTipRect(Canvas canvas) {
  if (mTouchIndex == -1) return;
  LinePoint point = line.getPoint(mTouchIndex);
  float x = point.fLineX;
  float y = point.fLineY;
  // 描繪豎線
  Paint paint = new TextPaint();
  PathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
  paint.setPathEffect(effects);
  paint.setAntiAlias(true);
  paint.setStrokeWidth(verticalLineStrokeWidth);
  paint.setColor(verticalLineColor);
  canvas.drawLine(x, topPadding, x, getHeight() - bottomPadding, paint);
  //描繪交匯圓點(diǎn)
  paint.setPathEffect(null);
  paint.setStyle(Paint.Style.FILL_AND_STROKE);
  paint.setColor(Color.WHITE);
  canvas.drawCircle(x, y, circleRadius, paint);
  paint.setStyle(Paint.Style.STROKE);
  paint.setColor(circleColor);
  paint.setStrokeWidth(circleStrokeWidth);
  canvas.drawCircle(x, y, circleRadius, paint);
  float midY = (topPadding + getHeight() - bottomPadding) / 2;
  float midX = (leftPadding + getWidth() - rightPadding) / 2;
  //描繪圓角矩形
  TextPaint textPaint = new TextPaint();
  textPaint.setTextSize(tipTextSize);
  textPaint.setTextAlign(Align.CENTER);
  textPaint.setColor(tipTextColor);
  textPaint.setAntiAlias(true);
  String label = tipPrefix + point.getY();
  float textWidth = textPaint.measureText(label) + 15;
  float textHeight = getTextHeight(textPaint, label) + 8;
  float hMargin = 10;//水平間距
  float vMargin = 8;//垂直間距
  float w = textWidth + hMargin * 2;//寬
  float h = textHeight + vMargin * 2;//高
  RectF rect = new RectF();
  if (x > midX) {
    rect.right = x - hMargin;
    rect.left = x - w;
  } else {
    rect.left = x + hMargin;
    rect.right = x + w;
  }
  if (y > midY) {
    rect.top = y - h;
    rect.bottom = y - vMargin;
  } else {
    rect.bottom = y + h;
    rect.top = y + vMargin;
  }
  Paint roundRectPaint = new Paint();
  roundRectPaint.setColor(tipRectColor);
  roundRectPaint.setStyle(Paint.Style.FILL);
  roundRectPaint.setAntiAlias(true);
  canvas.drawRoundRect(rect, 3, 3, roundRectPaint);
  // 描繪圓角矩形中間的文字
  float roundTextX = (rect.left + rect.right) / 2;
  float roundTextY = (rect.top + rect.bottom + getTextHeight(textPaint, label)) / 2;
  canvas.drawText(label, roundTextX, roundTextY, textPaint);
}

好了核心的代碼就這么多了,由于我們把它當(dāng)做的是控件再用,那么我們?cè)诔跏蓟臅r(shí)候,肯定會(huì)引入一些自定義的樣式表,

private void initViews(AttributeSet attrs, int defStyle) {
  TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.LineGraph, defStyle, 0);
  scaleTextSize = typedArray.getDimension(R.styleable.LineGraph_scale_text_size, 20);
  scaleTextColor = typedArray.getColor(R.styleable.LineGraph_scale_text_color, getResources().getColor(R.color.c5));
  tipRectColor = typedArray.getColor(R.styleable.LineGraph_tip_rect_color, getResources().getColor(R.color.c8));
  tipTextSize = typedArray.getDimension(R.styleable.LineGraph_tip_text_size, 22);
  tipTextColor = typedArray.getColor(R.styleable.LineGraph_tip_text_color, getResources().getColor(R.color.c12));
  curveStrokeWidth = typedArray.getDimension(R.styleable.LineGraph_curve_stroke_width, 4);
  curveColor = typedArray.getColor(R.styleable.LineGraph_curve_color, getResources().getColor(R.color.c8));
  verticalLineStrokeWidth = typedArray.getDimension(R.styleable.LineGraph_vertical_line_stroke_width, 2);
  verticalLineColor = typedArray.getColor(R.styleable.LineGraph_vertical_line_color, getResources().getColor(R.color.c8));
  circleStrokeWidth = typedArray.getDimensionPixelSize(R.styleable.LineGraph_circle_stroke_width, 3);
  circleColor = typedArray.getColor(R.styleable.LineGraph_circle_color, getResources().getColor(R.color.c8));
  circleRadius = typedArray.getDimensionPixelSize(R.styleable.LineGraph_circle_radius, 7);
  typedArray.recycle();
  bottomPadding = dip2px(getContext(), 20);
  topPadding = dip2px(getContext(), 10);
  leftPadding = dip2px(getContext(), 20);
  rightPadding = dip2px(getContext(), 10);
}

樣式表文件我就不多說(shuō)了,行如下面的格式,

<declare-styleable name="LineGraph">
  <attr name="scale_text_size" format="dimension" />
  <attr name="scale_text_color" format="color" />
  <attr name="tip_text_size" format="dimension" />
  <attr name="tip_text_color" format="color" />
  <attr name="tip_rect_color" format="color" />
  <attr name="curve_stroke_width" format="dimension" />
  <attr name="curve_color" format="color" />
  <attr name="vertical_line_stroke_width" format="dimension" />
  <attr name="vertical_line_color" format="color" />
  <attr name="circle_stroke_width" format="dimension" />
  <attr name="circle_color" format="color" />
  <attr name="circle_radius" format="dimension" />
</declare-styleable>

最后貼上個(gè)效果圖:

Android開(kāi)發(fā)如何繪制淘寶收益圖折線效果

Android是什么

Android是一種基于Linux內(nèi)核的自由及開(kāi)放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開(kāi)放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開(kāi)發(fā)。

以上是“Android開(kāi)發(fā)如何繪制淘寶收益圖折線效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI