0

我正在为安卓开发思维导图工具。到目前为止,对象是在屏幕上的触摸事件上绘制的。我想创建一个主要对象并从该对象中绘制这种类型的线。例如, http://www.biggerplate.com/mapImages/xl/e666ca33-abf7-4cc7-acc9-4aea9487feef.png

请帮忙。

这是绘制节点触摸事件的类

package com.example.mindmapping;
import java.util.LinkedList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;

public class CreateBitmap extends Activity {

Bitmap bitmap;
SurfaceHolder holder;
//LinkedList<newNode> nodes;
LinkedList<Node> nodes;
float x, y;
Node node;
Boolean collision;
SurfaceView surfaceView;
Context baseCtx;
//DrawMindMap dm;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    baseCtx = this.getBaseContext();
    setContentView(R.layout.drawmindmap);
    nodes = new LinkedList<Node>();
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rectangle_small);
    surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
    holder = surfaceView.getHolder();

    surfaceView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
      // TODO Auto-generated method stub

    switch(event.getAction()) {

       case MotionEvent.ACTION_DOWN:
         Log.d("TOUCH", "On touch start, total nodes: " + String.valueOf(nodes.size())); 
                    x = event.getX();
                    Log.d("XPOS", "touch x" + String.valueOf(x));
                    y = event.getY();
                    Log.d("YPOS", "touch y" + String.valueOf(y));

                    Canvas c = holder.lockCanvas();
                    //c.drawColor(Color.BLACK);
                    Node n = new Node(bitmap, c, x, y);

                    Paint paint = new Paint();
                    paint.setAntiAlias(true);
                    paint.setStrokeWidth(6f);
                    paint.setColor(Color.BLACK);
                    paint.setStyle(Paint.Style.STROKE);
                    paint.setStrokeJoin(Paint.Join.ROUND);

                    collision = false;
                    //nodes.add(n);
                    if (!nodes.isEmpty()){
                        for (Node no : nodes) {
                            collision = no.isHere(x, y);
                            if (collision){
                                break;
                            }
                        }
                    }
                    if(!collision){
                        nodes.add(n);
                    }
                    c.drawColor(Color.BLACK);
                    if (!nodes.isEmpty()){

                        for (Node no : nodes) {
                            no.Draw();
                            c.drawLine(x, y, event.getX(), event.getY(), paint);
                            Log.d("Node POS","Node X,Y: " + String.valueOf(no.xPosition) + " " + String.valueOf(no.yPosition));

                        }
                    }
                    holder.unlockCanvasAndPost(c);
                break;
            }
            return true;
        }
    });

}

在下面给出的类中用于处理和绘制节点

package com.example.mindmapping;

import android.graphics.Bitmap;
import android.graphics.Canvas;

public class Node {

public Bitmap img;
public Canvas c;
public float xPosition, yPosition, topLeft;
int width, height,gResId;
boolean isItOk;

public Node(Bitmap b,Canvas canv, float xP, float yP) {
    img = b;
    c = canv;
    width = b.getWidth();
    height = b.getHeight();
    xPosition = xP - (width/2);
    yPosition = yP - (height/2);
}

public void SetPos( float xP, float yP) {
    xPosition = xP - (width/2);
    yPosition = yP - (height/2);
}

public boolean isHere(float x, float y){
    x = (float) (x + width * 0.5);
    y = (float) (y + height * 0.5);
    if((x > xPosition - width * 0.5) && (x < xPosition + width * 0.5) && (y > yPosition - height * 0.5) && (y < yPosition + height * 0.5) ){
        return true;
    }
    return false;
}

//@SuppressWarnings("static-access")
public void Draw() {
    c.drawBitmap(img, xPosition, yPosition, null);
}
}
4

0 回答 0