绘制自定义线条Android

时间:2013-10-19 16:35:00

标签: android bitmap android-canvas

我正在为android开发思维导图工具。直到现在,对象被绘制在屏幕ontouch事件上。我想创建一个主对象并从该对象中绘制这种类型的线。这个例子是, 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);
}
}

0 个答案:

没有答案