-1

我正在尝试在 Horizo​​ntalScrollView 父级内实现自定义视图。自定义视图根据来自 Handler 的时间重复绘制一条线到其 Canvas。现在自定义视图填充了屏幕的宽度(由 548dp 高度限制),我希望线条能够延伸到可见的范围之外。此外,我想让用户能够水平滚动以查看更多行。我尝试将 Horizo​​ntalScrollView 的宽度设置为 1500dp(比屏幕宽度大得多),然后尝试水平滚动,但它没有移动。如果视图随着线条越过屏幕的可见部分而滚动,这也是理想的。

我正在开发带有蜂窝的华硕变压器。

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

<HorizontalScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="1500dp"
    android:layout_height="548dp"
    android:fillViewport="true" >

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

谢谢你的帮助!

4

1 回答 1

0

对于任何好奇的人,我找到了适合我情况的答案。希望我在下面概述的内容也对您有所帮助。

为了让自定义视图滚动,我必须在自定义视图周围添加一个 LinearLayout 包装器。不是 100% 确定为什么。

此外,虽然我为我的自定义视图声明了一个固定大小(在下面的 xml 文件中),但我可以在我的代码中使用以下方法动态更新它:

CustomDraw.this.setLayoutParams(new LinearLayout.LayoutParams(width + {adjust size}, height));

其中“width”和“height”是自定义View的宽度和高度。同样,不知道为什么我必须说“new LinearLayout.LayoutParams(...);” 但这似乎可行。

这是我正在使用的xml:

<HorizontalScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroller"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="548dp"
        android:orientation="vertical" >

        <maavapp.layout.CustomDraw
            android:id="@+id/custom_draw"
            android:layout_width="4800dp"
            android:layout_height="fill_parent" >
        </maavapp.layout.CustomDraw>
    </LinearLayout>
</HorizontalScrollView>

这是我正在使用的自定义类代码的相关部分:

public class CustomDraw extends View {
// member variables

public void setHSV(HorizontalScrollView hsv) {
    this.hsv = hsv;
}

public void updateLine() {
    if(measure1.isEmpty()) {
        measure1.add(new Coordinate(0, 0));
    } else {
        Coordinate last_coord = measure1.get(measure1.size() - 1);

                    // calculate south, north, east, and west

        if(((last_coord.x + 3) % 1200 == 0) 
                || ((last_coord.x + 3) % 1200 == 1) 
                || ((last_coord.x + 3) % 1200 == 2)
                || ((last_coord.x + 3) % 1200 == 3) && (last_coord.x + 3 >= 1200)) {
            hsv.smoothScrollTo(last_coord.x - 50, 0);
        }
        if(last_coord.x >= (width - 100)) {
            this.setLayoutParams(new LinearLayout.LayoutParams(width + 4800, height));
            width += 4800;
        }

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

        Coordinate coord;
    for(int i = 0; i < measure1.size(); i++) {
        coord = measure1.get(i);
        c.drawRect(coord.x, coord.y, coord.x + 3, coord.y + 3, p);  
    }
}

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);
    }
}
}
于 2012-04-19T16:48:51.023 回答