11

我需要一些帮助来理解在 Android 中滚动到画布上的项目的基础知识。假设我想创建一个时间线,其中 0 处的时间是可视化的顶部,并且随着时间的增加,时间线继续呈现在前一点之下。如果我想在 Android 上渲染它,我知道我可以通过覆盖 onDraw() 在画布上简单地创建一堆项目。但是,让我们假设可视化大于屏幕允许的大小。

例如,在下面的第一张图片中,大黑框包含我渲染它时的整个画布。我创建了一条垂直上下的蓝线以及几个黄色、绿色和蓝色的矩形。红色框表示正在呈现可视化的 Android 屏幕。当它最初打开时,所有项目都被绘制,但只有红色框中包含的项目显示在屏幕上。

Before_Scroll

现在,如果用户向下滚动,最初出现在红色框下方的项目在视图中,而超出红色框范围的项目不再可见,如第二张图片所示。

After_Scroll

我相信我需要使用可滚动的,但我很迷茫如何做到这一点。我已阅读此页面http://developer.android.com/training/custom-views/custom-drawing.html 解释如何创建自己的客户图像和此页面http://developer.android.com/training /custom-views/making-interactive.html解释了如何使 UI 具有交互性,但我认为我遗漏了一些东西。

说明此问题的示例代码(这是基本的,假设有逻辑指示框/线的位置等)如下:

package com.example.scrolltest;

import com.example.scrolltest.Draw;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;

public class MainActivity extends Activity {
    Draw draw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    draw = new Draw(this);
    draw.setBackgroundColor(Color.WHITE);
    setContentView(draw);
    }
}

package com.example.scrolltest;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;


public class Draw extends View {
    Paint paint = new Paint();

    public Draw(Context context) {
        super(context);            
    }

    @Override
    public void onDraw(Canvas canvas) {

        paint.setColor(Color.GREEN);
        canvas.drawRect(30, 30, 90, 200, paint);
        paint.setColor(Color.BLUE);

        canvas.drawLine(100, 20, 100, 1900, paint);

        paint.setColor(Color.GREEN);
        canvas.drawRect(200, 2000, 400, 3000, paint);
        }
}

但我无法弄清楚的是,我如何使用可滚动来向下滚动到屏幕外的矩形。我也不确定我是否正确地开始了这个或者应该使用drawables来代替......

4

2 回答 2

21

简单方法(如果所需高度不是很大)。

使用 ScrollView 并在其中添加您的 Draw 视图。在 onMeasure 中计算该视图所需的高度。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

draw = new Draw(this);
draw.setBackgroundColor(Color.WHITE);
ScrollView scrollView = new ScrollView(this);
scrollView.addView(draw);
setContentView(scrollView);
}

public class Draw extends View {
        Paint paint = new Paint();

        public Draw(Context context) {
            super(context);            
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // Compute the height required to render the view
            // Assume Width will always be MATCH_PARENT.
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = 3000 + 50; // Since 3000 is bottom of last Rect to be drawn added and 50 for padding.
            setMeasuredDimension(width, height);
        }

        @Override
        public void onDraw(Canvas canvas) {

            paint.setColor(Color.GREEN);
            canvas.drawRect(30, 30, 90, 200, paint);
            paint.setColor(Color.BLUE);

            canvas.drawLine(100, 20, 100, 1900, paint);

            paint.setColor(Color.GREEN);
            canvas.drawRect(200, 2000, 400, 3000, paint);
            }
    }
于 2013-04-12T06:05:50.823 回答
2

另一种方法是使用偏移 X/Y 值。

您如何处理偏移值取决于您,尽管我更喜欢使用我调用的类Camera。访问应该是静态的。

public void render(Canvas c){
    c.drawRect(100 - offsetX, 100 - offsetY, 120 - offsetX, 120 - offsetY, Paints.BLUE);

}

并平移画布:

float x, y;
@Override
public boolean onTouchEvent(MotionEvent ev) {
    if(ev.getAction() == MotionEvent.ACTION_DOWN){
        x = ev.getX();
        y = ev.getY();
    }else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        float currX = ev.getX();
        float currY = ev.getY();

        float newOffsetX = (x   - currX),
                newOffsetY = (y - currY);
        //The next two if-statements are to add sensitivity to the scrolling.
        //You can drop these if you don't plan on having touch events
        //with pressing. Pressing works without this as well, but the canvas may move slightly 
        //I use DP to handle different screen sizes but it can be replaced with pixels. c is a context-instance
        if (newOffsetY < Maths.convertDpToPixel(2, c) && newOffsetY > -Maths.convertDpToPixel(2, c))
            newOffsetY = 0;

        if (newOffsetX < Maths.convertDpToPixel(2, c) && newOffsetX > -Maths.convertDpToPixel(2, c))
            newOffsetX = 0;
        offsetX += newOffsetX;
        offsetY += newOffsetY;
        x = ev.getX();
        y = ev.getY();
    }

    return true;

}

还有一个示例相机类:

public class Camera{

    public static float offsetX, offsetY;
    //The constructor. ix and iy is the initial offset. Useful if you are creating a game and need to change the initial offset to center around a starting position.
    //Most of the time it will be enough to set the values to 0
    public Camera(float ix, float iy){
        this.offsetX = ix;
        this.offsetY = iy;
    }

}
于 2017-02-04T12:06:56.707 回答