我已经为我的应用程序创建了条形图,但我的条形图不可滚动,因此我已将我的班轮布局添加到水平滚动视图中。
这是我的xml文件是...
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/horizaontalforscroll">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearforscroll" >
</LinearLayout>
在这里,我调用 MyGraph 类,它扩展了 View 类..
LinearLayout linear=(LinearLayout) findViewById(R.id.linearforscroll);
MyGraph graph = new MyGraph(this);
linear.addView(graph);
HorizontalScrollView hr = (HorizontalScrollView)findViewById(R.id.horizaontalforscroll);
hr.removeAllViewsInLayout();
hr.addView(linear);
最后添加我的 MyGraph 类..
public class MyGraph extends View{
private static final int SHAPE_WIDTH = 10;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public MyGraph(Context context) {
// TODO Auto-generated constructor stub
super(context);
}
@Override
protected void onDraw(Canvas canvas){
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setStyle(Style.FILL);
int legendSize = 0;
if(legendSize == 0){
legendSize = height / 5;
}
int[] margine = new int[]{40,30,20,200,250};
int startX = x + margine[0];
int startY = y;
int stopX = x + margine[0];
int stopY = 0;
Log.d("JWP", "W & H:"+width+":"+height);
if(width < height){
stopY = y + height - margine[4];
}
else{
stopY = y + height - margine[3];
}
int length = values.length;
paint.setColor(Color.WHITE);
canvas.drawLine(startX, startY, stopX, stopY, paint);
canvas.drawLine(stopX, stopY, x + width, stopY, paint);
int rHeight = stopY;
for(int i = 0;i < length; i++){
double percentage = (values[i]*rHeight)/maxValue;
Log.d("JWP", "Values:"+values[i]);
Log.d("JWP", "PERCENTAGE:"+percentage);
paint.setColor(COLORS[(i) % COLORS.length]);
int modifiedStratY = rHeight - (int) percentage + 20;
if(modifiedStratY > rHeight){
modifiedStratY = rHeight - (int) percentage;
}
canvas.drawRect(startX, modifiedStratY, startX+30, rHeight, paint);
paint.setTextSize(15);
canvas.drawText(String.valueOf(values[i]), startX, modifiedStratY, paint);
canvas.save();
canvas.rotate(35, startX, rHeight + 7);
paint.setColor(Color.WHITE);
paint.setTextSize(13);
canvas.drawText(name[i], startX, rHeight + 7, paint);
canvas.restore();
startX += 70;
}
String xTitle = "Directory Name";
int xTitleLength = xTitle.length()/2;
int xTitleStart = (x + width/2) - xTitleLength/2;
paint.setTextSize(15);
canvas.drawText(xTitle, xTitleStart,y + rHeight + 65, paint);
String yTitle = "Amount of Space in MB";
int yTitleLength = yTitle.length();
int yTitleStart = (y + rHeight/2) + yTitleLength*2;
canvas.save();
canvas.rotate(-90, x + 10 , yTitleStart);
canvas.drawText(yTitle, x + 10, yTitleStart, paint);
canvas.restore();
paint.setTextSize(12);
canvas.drawText(String.valueOf(maxValue), x + 12, y + 20, paint);
canvas.drawText("0.0", x + 11, rHeight, paint);
float yLabelValue = (float) maxValue / 4;
int yLabelPoints = rHeight / 4;
canvas.drawText(String.valueOf(yLabelValue*3), x + 12, (rHeight - yLabelPoints*3 + 20), paint);
for(int j = 1; j < 3; j++){
canvas.drawText(String.valueOf(yLabelValue), x + 12, (rHeight - yLabelPoints + 20), paint);
yLabelPoints += yLabelPoints;
yLabelValue += yLabelValue;
}
int left = x + 15;
int right = x + width - 5;
String[] title = new String[length];
for(int i = 0; i < length; i++){
title[i] = name[i];
}
drawLegends(canvas,title,left,right,y,width,height,legendSize,paint);
}
}
我的查询是,当我运行代码时,屏幕上什么也没有显示,如果我在没有水平滚动视图的情况下运行,结果显示清楚..请帮我解决这个问题..
谢谢你。