Android:如何在大滚动画布上绘制简单图形

发布于 2024-11-17 06:20:36 字数 181 浏览 0 评论 0原文

在 Android 中,我需要能够在大型可滚动画布(即手机屏幕是更大区域中的视口)上绘制简单的图形(点、线、圆、文本)。我一直在寻找有关如何做到这一点的教程,但没有成功。

Android 市场上的“World Map FREE”是我需要实现的效果的一个很好的例子。

这肯定是一个常见问题,所以我很惊讶我找不到例子。

In Android, I need to be able to draw simple graphics (points, lines, circles, text) on a large scrollable canvas (i.e the phone's screen is a viewport in a much larger area). I have hunted for tutorials on how to do this without success.

"World Map FREE" on Android market is a good example of the sort of effect I need to achieve.

This must be a common problem, so I'm surprised I can't find examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯了 2024-11-24 06:20:36

我最近不得不实施类似的事情。基本上,我的自定义视图中的坐标会通过一些辅助函数移动绘制对象的位置:

public class BoardView extends View {

    //coordinates to shift the view by
    public float shiftX=0f;
    public float shiftY=0f;

    //used in the dragging code
    public float lastX=-1f;
    public float lastY=-1f;

    /*...*/

    //functions that take into account the shifted x and y values
    //pretty straightforward
    final public void drawLine(Canvas c,float x1,float y1,float x2,float y2,Paint p){
        c.drawLine(x1+shiftX, y1+shiftY, x2+shiftX, y2+shiftY, p);
    }

    final public void drawText(Canvas c,String s,int x,int y,Paint p){
        c.drawText(s, x+shiftX, y+shiftY, p);
    }

    /*etc*/

为了实际实现拖动,我编写了一个自定义 onTouchEvent 方法来实现拖动:

public boolean onTouchEvent(MotionEvent event){
        int eventaction=event.getAction();
        float x=event.getX();
        float y=event.getY();
        switch(eventaction){
        case MotionEvent.ACTION_DOWN:
            time=System.currentTimeMillis();//used in the ACTION_UP case
            break;
        case MotionEvent.ACTION_MOVE:
            if(lastX==-1){ lastX=x;lastY=y;}//initializing X, Y movement
            else{//moving by the delta
                if(Math.abs(x-lastX)>1 || Math.abs(y-lastY)>1){//this prevents jittery movement I experienced
                    shiftX+=(x-lastX);//moves the shifting variables
                    shiftY+=(y-lastY);//in the direction of the finger movement
                    lastX=x;//used to calculate the movement delta
                    lastY=y;
                    invalidate();//DON'T FORGET TO CALL THIS! this redraws the view
                }
            }
            break;
        case MotionEvent.ACTION_UP: //this segment is to see whether a press is a selection click(quick press)
        //or a drag(long press)
            lastX=-1;
            if(System.currentTimeMillis()-time)<100)
                try {
                    onClickEvent(x,y);//custom function to deal with selections
                } catch (Exception e) {
                    e.printStackTrace();
                }
            break;
        }
        return true;
    }

I've had to implement something fairly recently of this sort. Basically I had coordinates in my custom view that would shift where objects would be drawn, via some auxiliarry functions:

public class BoardView extends View {

    //coordinates to shift the view by
    public float shiftX=0f;
    public float shiftY=0f;

    //used in the dragging code
    public float lastX=-1f;
    public float lastY=-1f;

    /*...*/

    //functions that take into account the shifted x and y values
    //pretty straightforward
    final public void drawLine(Canvas c,float x1,float y1,float x2,float y2,Paint p){
        c.drawLine(x1+shiftX, y1+shiftY, x2+shiftX, y2+shiftY, p);
    }

    final public void drawText(Canvas c,String s,int x,int y,Paint p){
        c.drawText(s, x+shiftX, y+shiftY, p);
    }

    /*etc*/

To actually implement the dragging, I wrote a custom onTouchEvent method to implement dragging:

public boolean onTouchEvent(MotionEvent event){
        int eventaction=event.getAction();
        float x=event.getX();
        float y=event.getY();
        switch(eventaction){
        case MotionEvent.ACTION_DOWN:
            time=System.currentTimeMillis();//used in the ACTION_UP case
            break;
        case MotionEvent.ACTION_MOVE:
            if(lastX==-1){ lastX=x;lastY=y;}//initializing X, Y movement
            else{//moving by the delta
                if(Math.abs(x-lastX)>1 || Math.abs(y-lastY)>1){//this prevents jittery movement I experienced
                    shiftX+=(x-lastX);//moves the shifting variables
                    shiftY+=(y-lastY);//in the direction of the finger movement
                    lastX=x;//used to calculate the movement delta
                    lastY=y;
                    invalidate();//DON'T FORGET TO CALL THIS! this redraws the view
                }
            }
            break;
        case MotionEvent.ACTION_UP: //this segment is to see whether a press is a selection click(quick press)
        //or a drag(long press)
            lastX=-1;
            if(System.currentTimeMillis()-time)<100)
                try {
                    onClickEvent(x,y);//custom function to deal with selections
                } catch (Exception e) {
                    e.printStackTrace();
                }
            break;
        }
        return true;
    }
毁梦 2024-11-24 06:20:36

研究一下瓦片引擎,在谷歌上可以找到很多关于它们的信息。

Look into tile engines, there's a lot to find on Google about them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文