gpt4 book ai didi

android - 如何在android中制作移动图形或图表?

转载 作者:行者123 更新时间:2023-12-05 00:17:45 27 4
gpt4 key购买 nike

我是 Android 新手,我需要知道如何使图形或图表(例如折线图)移动,即从右到左。

基本上我会根据手机的 RAM 内存使用情况来绘制图表。我需要绘制一个与 Windows 任务管理器中类似的图表。

请帮忙解决这个问题。

最佳答案

看看这个类。

简单又有用。

原作者是Arno den Hond,谷歌一下。

我重新设计了这个类。

import android.content.Context; 
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.view.View;

/**
* GraphView creates a scaled line or bar graph with x and y axis labels.
* @author Arno den Hond
* @redesign Reinhard & kimkmkm
*
*/
public class GraphView extends View {

public static boolean BAR = true;
public static boolean LINE = false;

private Paint paint;
private float[] values;
private String[] horlabels;
private String[] verlabels;
private String title;
private boolean type;

/**
* Generate Graph
* Variable Array for GraphView
* verlabel : Background Height Values
* horlabel : Background Width Values
* values : Max Values of Foreground Active Graph
*
* basic draw rule
* if Array is not null
* Draw Background width, height & active Graph all time
* or Array is null
* Draw Background width, height
* active Graph fixed 0
*
*/
public GraphView(Context context, float[] values, String title, String[] horlabels, String[] verlabels, boolean type) {
super(context);
if (values == null)
values = new float[0];
else
this.values = values;
if (title == null)
title = "";
else
this.title = title;
if (horlabels == null)
this.horlabels = new String[0];
else
this.horlabels = horlabels;
if (verlabels == null)
this.verlabels = new String[0];
else
this.verlabels = verlabels;
this.type = type;
paint = new Paint();
}
/**
* Graph for Background
* &
* Value for Background
*/
@Override
protected void onDraw(Canvas canvas) {
float border = 20;
float horstart = border * 2;
float height = getHeight();
float width = getWidth() - 1;
float max = 700;
float min = 0;
float diff = max - min;
float graphheight = height - (2 * border);
float graphwidth = width - (2 * border);

paint.setTextAlign(Align.LEFT);
/** vers : BackGround Height Values length*/
int vers = verlabels.length - 1;
for (int i = 0; i < verlabels.length; i++) {

paint.setColor(Color.LTGRAY);
// Width Line of background

float y = ((graphheight / vers) * i) + border;
// float y : ((getHeight / Height values length) * Height values length ) + 20

canvas.drawLine(horstart, y, width, y, paint);
// drawLine ( 40, y, getWidth()-1, y, paint)

paint.setColor(Color.WHITE);
// Left Height of background

canvas.drawText(verlabels[i], 0, y, paint);
}

/** hors : BackGround width Values length*/
int hors = horlabels.length - 1;
for (int i = 0; i < horlabels.length; i++) {
// Height Line of background
paint.setColor(Color.DKGRAY);
float x = ((graphwidth / hors) * i) + horstart;
canvas.drawLine(x, height - border, x, border, paint);
paint.setTextAlign(Align.CENTER);
if (i==horlabels.length-1)
paint.setTextAlign(Align.RIGHT);
if (i==0)
paint.setTextAlign(Align.LEFT);
// Value of Width
paint.setColor(Color.WHITE);
canvas.drawText(horlabels[i], x, height - 4, paint);
}

paint.setTextAlign(Align.CENTER);
canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);

/**
* Yellow Line Graph
* continue Repaint....
*
*/
if (max != min) {
paint.setColor(Color.YELLOW);
if (type == BAR) {
float datalength = values.length;
float colwidth = (width - (10 * border)) / datalength;
for (int i = 0; i < values.length; i++) {
float val = values[i] - min;

float rat = val / diff;
//diff : max - min

float h = graphheight * rat;
canvas.drawRect((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1), paint);
}
} else {
float datalength = values.length;
float colwidth = (width - (2 * border)) / datalength;
float halfcol = colwidth / 2;
float lasth = 0;
for (int i = 0; i < values.length; i++) {
float val = values[i] - min;
float rat = val / diff;
float h = graphheight * rat;
if (i > 0)
canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint);
lasth = h;
}
}
}
}

关于android - 如何在android中制作移动图形或图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10992806/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com