水平滚动视图以自定义视图的孩子 [英] Horizontal Scroll View with Custom View child
问题描述
我想实现一个HorizontalScrollView母公司内部的自定义视图。定制视图反复绘制一条线到其画布基于从处理程序定时。我想设置在可见宽度自定义视图为1207px x 548px,但我希望该行能够超出了可见的延伸,我想允许用户则能够水平滚动看到更多的线路。之前我只是在我周围的自定义视图类有一个固定的宽度和高度,这显然没有让任何滚动的框架布局。现在,当我尝试使用下面的布局,一切都只是出来的黑色(在自定义视图背景为白色)。
I am trying to implement a custom view inside of a HorizontalScrollView parent. The custom view repeatedly draws a line to its Canvas based on timing from a Handler. I want to set the visible width of the Custom view to be 1207px x 548px, but I want the line to be able to extend beyond what is visible and I want to allow the user to then be able to horizontally scroll to see more of the line. Before I was just surrounding my Custom View class with a frame layout with a fixed width and height, which obviously didn't allow any scrolling. Now when I try using the layout below, everything just comes out black (the background on the Custom View is white).
我开发的华硕变压器与蜂窝。
I am developing on an ASUS Transformer with Honeycomb.
下面是我使用的布局的相关部分:
Here's the relevant part of the layout I'm using:
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="548dp" >
<maavapp.layout.CustomDraw
android:id="@+id/custom_draw"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</maavapp.layout.CustomDraw>
</HorizontalScrollView>
这里是万一code为CustomDraw类是必要的:
And here is the code for the CustomDraw class in case that's necessary:
public class CustomDraw extends View {
private static int mSelected;
private ArrayList<Coordinate> measure1;
private ArrayList<Coordinate> measure2;
private ArrayList<Coordinate> measure3;
private boolean north = false, east = true, south = true, west = false;
private DrawHandler dh = new DrawHandler();
private boolean draw = true;
private int width;
private int height;
private int m2;
public CustomDraw(Context context) {
super(context);
init();
}
public CustomDraw(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomDraw(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init() {
measure1 = new ArrayList<Coordinate>();
measure2 = new ArrayList<Coordinate>();
measure3 = new ArrayList<Coordinate>();
mSelected = Constants.MEASURE_1;
width = 0;
height = 0;
m2 = 0;
}
public void setMeasure(int measure) {
mSelected = measure;
}
public void toggleDraw() {
draw = !draw;
if(draw) {
updateLine();
}
}
public boolean isDrawing() {
return draw;
}
public void updateLine() {
// grab new coordinates for each measure
/*new_coord(measure1);
new_coord(measure2);
new_coord(measure3);*/
if(measure1.isEmpty() && measure2.isEmpty() && measure3.isEmpty()) {
measure1.add(new Coordinate(0, 0));
measure2.add(new Coordinate(0, 0));
measure3.add(new Coordinate(0, 0));
} else {
Coordinate last_coord = measure1.get(measure1.size() - 1);
measure2.add(new Coordinate(++m2, 25));
if(last_coord.x >= width) {
east = false;
west = true;
} else if(last_coord.x <= 0) {
east = true;
west = false;
}
if(last_coord.y >= height) {
south = false;
north = true;
} else if(last_coord.y <= 0) {
south = true;
north = false;
}
Log.d("MAAV", "last_coord.x + 3: " + (last_coord.x + 3));
Log.d("MAAV", "last_coord.y + 3: " + (last_coord.y + 3));
if(south && east) {
measure1.add(new Coordinate(last_coord.x + 3, last_coord.y + 3));
} else if(south && west) {
measure1.add(new Coordinate(last_coord.x - 3, last_coord.y + 3));
} else if(north && east) {
measure1.add(new Coordinate(last_coord.x + 3, last_coord.y - 3));
} else if(north && west) {
measure1.add(new Coordinate(last_coord.x - 3, last_coord.y - 3));
}
}
if(draw) {
dh.sleep(10);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
this.width = w;
this.height = h;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
public void onDraw(Canvas c) {
super.onDraw(c);
Paint p = new Paint();
p.setStyle(Paint.Style.FILL);
p.setColor(Color.WHITE);
c.drawPaint(p);
p.setColor(Color.BLACK);
switch(mSelected) {
case Constants.MEASURE_1:
for(int i = 0; i < measure1.size(); i++) {
Coordinate coord = measure1.get(i);
Log.d("MAAV", "drawing coord.x, coord.y: " + (coord.x) + ", " + (coord.y));
c.drawRect(coord.x, coord.y, coord.x + 3, coord.y + 3, p);
}
break;
case Constants.MEASURE_2:
for(int i = 0; i < measure2.size(); i++) {
Coordinate coord = measure2.get(i);
c.drawRect(coord.x, coord.y, coord.x + 3, coord.y + 3, p);
}
break;
case Constants.MEASURE_3:
for(int i = 0; i < measure2.size(); i++) {
Coordinate coord = measure2.get(i);
c.drawRect(coord.x, coord.y, coord.x + 3, coord.y + 3, p);
}
break;
}
}
class DrawHandler extends Handler {
@Override
public void handleMessage(Message msg) {
CustomDraw.this.updateLine();
CustomDraw.this.invalidate();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
}
}
感谢您的帮助!
推荐答案
我想你也必须设置视:机器人:fillViewport =真正的
I think you also have to set the viewport: android:fillViewport="true"
像:
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
这篇关于水平滚动视图以自定义视图的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!