android - 带有自定义 View 子项的水平 ScrollView

标签 android android-3.0-honeycomb horizontal-scrolling android-custom-view horizontalscrollview

我正在尝试在 Horizo​​ntalScrollView 父级内部实现自定义 View 。自定义 View 根据处理程序的计时重复在其 Canvas 上绘制一条线。我想将自定义 View 的可见宽度设置为 1207px x 548px,但我希望线条能够延伸到可见范围之外,并且我希望允许用户能够水平滚动以查看更多行。在我只是用具有固定宽度和高度的框架布局包围我的自定义 View 类之前,这显然不允许任何滚动。现在,当我尝试使用下面的布局时,所有内容都显示为黑色(自定义 View 上的背景为白色)。

我正在使用 Honeycomb 的 ASUS Transformer 上进行开发。

这是我正在使用的布局的相关部分:

<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>

下面是 CustomDraw 类的代码(如有必要):

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);
    }
}

}

感谢您的帮助!

最佳答案

我认为您还必须设置视口(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" >

关于android - 带有自定义 View 子项的水平 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10214244/

相关文章:

android - Google Play 服务中 UserRecoverableAuthException.getIntent() 中的 NullPointerException

android - 有没有一种方法可以像结果的开始 fragment 一样工作?

Android:将图像和文本发布到 Instagram

java - Android 应用程序的发布调试版本

Honeycomb 上的 Android 设置 LocalePicker

android-3.0-honeycomb - 非法参数异常 : No view found for id for fragment when fast switching ActionBar Tabs

ios - 垂直 UIScrollView 优于水平 UIScrollView

jquery - 具有动画滚动效果的居中水平网页布局

css - 在水平滚动页面上水平居中

android - 在 Android 中验证手机号码