我制作了一个包含条形图的视图,现在由随机数生成器提供燃料并每秒刷新一次。我们的想法是,在单击其中一个条形后,它将变为折线图。 这是视图的代码:
public class BarGraph extends View implements OnClickListener{
private Paint paint;
private String[] horlabels;
private String[] verlabels;
private String title;
private int[] values;
Context co;
private RectF[] rects;
public BarGraph(Context context, String title, String[] hor, String[] var) {
super(context);
// TODO Auto-generated constructor stub
if (title == null)
title = "";
else
this.title = title;
if (hor == null)
this.horlabels = new String[0];
else
this.horlabels = hor;
if (var == null)
this.verlabels = new String[0];
else
this.verlabels = var;
paint = new Paint();
co = context;
}
public void updateVals(int[] vals){
values = vals;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
float border = 20;
float horstart = border;
float height = getHeight();
float width = getWidth() - 1;
int max = 100;
int min = 0;
float diff = max - min;
float graphheight = height - (2 * border);
float graphwidth = width - (2 * border);
rects = new RectF[horlabels.length];
this.setClickable(true);
paint.setTextAlign(Align.LEFT);
int vers = verlabels.length - 1;
for (int i = 0; i < verlabels.length; i++) {
paint.setColor(Color.DKGRAY);
float y = ((graphheight / vers) * i) + border;
canvas.drawLine(horstart, y, width - border, y, paint);
paint.setColor(Color.WHITE);
canvas.drawText(verlabels[verlabels.length - 1 - i], 0, y, paint);
}
int hors = horlabels.length;
for (int i = 0; i < horlabels.length + 1; i++) {
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);
paint.setColor(Color.WHITE);
}
for (int i = 0; i < horlabels.length; i++)
{
float x = ((graphwidth / hors) * i) + horstart;
canvas.drawText(horlabels[i], x + (graphwidth / (2*hors)), height - 4, paint);
}
paint.setTextAlign(Align.CENTER);
canvas.drawText(title , (graphwidth / 2) + horstart, border - 4, paint);
if (max != min) {
paint.setColor(Color.LTGRAY);
float datalength = values.length;
float colwidth = (width - (2 * border)) / datalength;
for (int i = 0; i < values.length; i++) {
float val = values[i] - min;
float rat = val / diff;
float h = graphheight * rat;
RectF r = new RectF((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1));
rects[i] = r;
canvas.drawRect(r, paint);
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int[] coords = new int[2];
v.getLocationInWindow(coords);
Toast t = Toast.makeText(co, "KLIKNIETO", Toast.LENGTH_LONG);
t.show();
for(RectF r : rects){
if(r.contains(coords[1], coords[2]))
{
try {
this.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return;
}
这是从这项活动开始的:
public class GraphTestActivity extends Activity{
/** Called when the activity is first created. */
public List<DataSet> sets;
String[] verlabels;
int[] vals;
BarGraph graph;
Thread t1;
String[] hortable;
// private Updater task;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sets = new LinkedList<DataSet>();
verlabels = new String[] {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"};
DataSet set1 = new DataSet("set1");
DataSet set2 = new DataSet("set2");
DataSet set3 = new DataSet("set3");
DataSet set4 = new DataSet("set4");
DataSet set5 = new DataSet("set5");
sets.add(set1);
sets.add(set2);
sets.add(set3);
sets.add(set4);
sets.add(set5);
hortable = new String[sets.size()];
vals = new int[sets.size()];
for(int i = 0; i < sets.size(); i++)
{
hortable[i] = sets.get(i).getName();
}
graph = new BarGraph(this, "Diagram slupkowy", hortable, verlabels);
for(DataSet d : sets)
{
d.nextGen();
}
graph.updateVals(vals);
setContentView(graph);
new Updater().execute();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
new BarRefresh().execute();
}
class Updater extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
// doSomething();
while(true){
for(DataSet d : sets)
{
d.nextGen();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(isCancelled()){
break;
}
}
return null;
}
}
class BarRefresh extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
// doSomething();
while(true){
for(int i = 0; i < sets.size(); i++)
{
vals[i] = sets.get(i).getLast();
}
graph.updateVals(vals);
graph.postInvalidate();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(isCancelled()){
break;
}
}
return null;
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
try {
this.finish();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
问题是,它对触摸没有反应。它可能与我在BarRefresh任务中设置刷新的方式有关吗?
答案 0 :(得分:0)
如果你想要点击的BarGraph View Class,你应该让它有一个OnClickListener,而不是BEING一个OnClickListener。
这是你做的事情
1删除实现OnClickListener()
2在你的活动中做点什么
bargraph.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//do whatever
}});
或者您可以将该代码块放在构造函数(this.setOnClickListener())中,以便所有BarGraph类都可以获得它。