如何在图像两点之间画线在android系统? [英] How to draw line between two points on image in android?

查看:257
本文介绍了如何在图像两点之间画线在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有这样的形象,我想画线路两个点之间。

hi all, I have this image and I wanna to draw line "the path" between the two point.

什么,我尝试:

是绘制路径,使用的主要点,这是保存在数据库中。 X,Y为点和绘制多点之间线路,直至到达终点。像在图像数2

is draw the path , using main point which is save in database. "x,y for the point" and draw line between multi-points until arrive the end point. like in image number 2

什么,我想的是,如果我把图像以正方形网格例如15 * 15,并给予每平方米X,Y,标签,其中标签可以是0,1这意味着墙壁或任何东西0不能走扔1 u能。
和动态创建洗澡。

what i'm thinking about is if I divide the image to square grid for example 15*15 ,and give each square x,y,tag where tag can be 0,1 which mean wall or any thing 0 can't walk throw it 1 u can. and create the bath dynamically.

这是所有我对现在:)如果有电气特性的方式,我不知道它的想法。 plz帮助我:)

that are all the idea that i have for now :) if there is anyother way i don't know it. plz help me :)

更新

- 用户可以放大/缩小。
  - 什么我想要做的是一样的东西

-The user can zoom in/out. -what i'm trying to do is something like this

推荐答案

您可以直接修改位图,或者你可以把位图上的顶部 SurfaceView 和借鉴在 SurfaceView 。所有你会在这种情况下做的就是绘制线条中的的onDraw 方法 SurfaceView

You could modify the Bitmap directly or you could put a SurfaceView on top of the bitmap and draw on the SurfaceView. All you would have to do in this scenario is draw your lines in the onDraw method of the SurfaceView.

我画了很多线在我的海龟绘图应用程序...这里是我如何延长 SurfaceView

I draw a lot of lines in my Turtle Draw app... here is how I extend SurfaceView

public class DrawView extends SurfaceView {

    Paint paint = new Paint();

    List < float[] > lines = new ArrayList < float[] > ();
    List < Integer > colors = new ArrayList < Integer > ();
    int curColor = Color.WHITE;
    int bgColor = Color.BLACK;

    Bitmap mBitmap;

    ImageView turtle;

    float curX, curY, curTurn = 0f;

    Matrix transform = new Matrix();

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mBitmap = getBitmapFromDrawable(context);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(DpiUtils.getPxFromDpi(getContext(), 2));
        setScrollContainer(true);
        // clear();
    }

    public DrawView(Context context) {
        super(context);

    }

    public void addLine(float...l) {
        synchronized(lines) {
            lines.add(l);
            colors.add(curColor);
        }
    }

    public List < float[] > getLines() {
        return lines;
    }

    public List < Integer > getColors() {
        return colors;
    }

    @Override
    public void onDraw(final Canvas canvas) {

        synchronized(lines) {
            super.onDraw(canvas);
            int i = 0;
            for (float[] l: lines) {
                paint.setAntiAlias(true);
                paint.setColor(colors.get(i++));
                canvas.drawLines(l, paint);
                curX = l[2];
                curY = l[3];
            }

            transform.setTranslate(curX - 13, curY - 13);
            transform.preRotate(360 - curTurn, 13, 13);

            paint.setColor(Color.BLACK);
            canvas.drawBitmap(mBitmap, transform, paint);
        }
    }

    public void setTurn(float turn) {
        this.curTurn = turn;
    }

    public void clear() {
        lines.clear();
        colors.clear();
        DisplayMetrics metrics = DpiUtils.getDisplayMetrics(getContext());

        curX = metrics.widthPixels / 2f;
        curY = (metrics.heightPixels / 2f) - DpiUtils.getPxFromDpi(getContext(), 50);
        curTurn = 0;

        scrollTo(0, 0);
    }

    public static Bitmap getBitmapFromAsset(Context context, String strName) {
        AssetManager assetManager = context.getAssets();

        InputStream istr;
        Bitmap bitmap = null;
        try {
            istr = assetManager.open(strName);
            bitmap = BitmapFactory.decodeStream(istr);
        } catch (IOException e) {
            return null;
        }

        return bitmap;
    }

    public static Bitmap getBitmapFromDrawable(Context context) {
        Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.turtle_26);
        return icon;
    }

    public void setDrawColor(int color) {
        paint.setColor(color);
        curColor = color;
    }

    public int getDrawColor() {
        return curColor;
    }

    int x, y = 0;
    int scrollByX, scrollByY = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int action = (event.getAction() & MotionEvent.ACTION_MASK);

        if (action == MotionEvent.ACTION_DOWN) {
            x = (int) event.getX() + scrollByX;
            y = (int) event.getY() + scrollByY;
        } else if (action == MotionEvent.ACTION_MOVE) {
            scrollByX = x - (int) event.getX();
            scrollByY = y - (int) event.getY();
            scrollTo(scrollByX, scrollByY);
        }

        return true;
    }

    @Override
    public void scrollTo(int x, int y) {
        // TODO Auto-generated method stub
        super.scrollTo(x, y);
        scrollByX = x;
        scrollByY = y;
    }

    @Override
    public void setBackgroundColor(int color) {
        // TODO Auto-generated method stub
        super.setBackgroundColor(color);
        bgColor = color;
    }

    public int getBackgroundColor() {
        return bgColor;
    }

}

这篇关于如何在图像两点之间画线在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆