2016-09-11 6 views
8

구조체와 같은 패밀리 트리를 Android에서 만들려고합니다. 저는 캔버스를 사용하여 직사각형을 그리며 가족 구성원 이름과 라인을 연결합니다.캔버스에 그리지 않은 여러 개의 사각형

나는 link

의 도움 DrawView.java으로 다음과 같은 방법으로 사각형과 선 그리기하고

나는 아래와 같은 방향 수평으로있는 LinearLayout에 여러 개의보기를 추가하려고 지금
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.util.Log; 
import android.view.View; 

public class DrawView extends View { 
    Paint paint = new Paint(); 
    float mx, my, mdensity; 
    Paint mBGPaint, mTXTPaint,mLINEPaint,mBRDPaint; 
    String text; 
    public DrawView(Context context, float x, float y, float density, String text) { 
     super(context); 
     paint.setColor(Color.RED); 
     paint.setStrokeWidth(8); 
     paint.setStyle(Paint.Style.STROKE); 

     mx = x; 
     my = y; 
     mdensity = density; 
     this.text = text; 
    } 
    @Override 
    public void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     init(); 

     mLINEPaint.setStrokeWidth(8); 

     //draw rect border 
     canvas.drawRect(100, 100, 200, 200, mBRDPaint); 
//  //draw text 
     canvas.drawText(text, 150, 150, mTXTPaint); 
//  //draw line 

     float x = mx+150; 

     canvas.drawLine(x, 10, x, 100, mLINEPaint); 

    } 
    public void init() { 

     //rectangle background 
     mBGPaint = new Paint(); 
     mBGPaint.setColor(Color.parseColor("#8")); 

     //your text 
     mTXTPaint = new Paint(); 
     mTXTPaint.setColor(Color.parseColor("#123456")); 

     //your line 
     mLINEPaint = new Paint(); 
     mLINEPaint.setColor(0xFFFF00FF); 

     //rectangle border 
     mBRDPaint = new Paint(); 
     mBRDPaint.setStyle(Paint.Style.STROKE); 
     mBRDPaint.setStrokeWidth(10); 
     mBRDPaint.setColor(Color.parseColor("#8")); 
    } 
} 

:

float density = getApplicationContext().getResources().getDisplayMetrics().density; 
    DrawView drawView; 

    float x = 100, y = 200; 
    int count1 = 1; 
    int id; 
    LinearLayout layout2 = new LinearLayout(this); 

    layout2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
    layout2.setOrientation(LinearLayout.HORIZONTAL); 

    main_layout.addView(layout2); 

    DrawView drawView1; 
    CircleView circleView; 
    for (Map.Entry<String, ArrayList<String>> entry : map.entrySet()) { 

     String key = entry.getKey(); 
     if (count1 < 2) { 
      x = dirButton.getX(); 
      y = dirButton.getY(); 
     } 
     drawView1 = new DrawView(this, x, y, density, key); 
     drawView1.setId(butId++); 
     drawView1.setLayoutParams(params); 
     layout2.addView(drawView1); 

     count1++; 
     x = x + 100; 
    } 

하지만 이렇게하면 하나의보기 만 캔버스에 추가되고 다른보기는 표시되지 않습니다. 캔버스에서 안드로이드로 작업 한 경험이 없습니다. 누군가이 문제로 나를 안내 할 수 있다면 기쁠 것입니다.

+1

캔버스 뷰 안에해야하고 당신은 캔버스에 모든 것을 그리는 onDraw() 메소드를 통해. 캔버스에 여러 개의 뷰가 필요하지 않습니다. 캔버스를 업데이트해야 할 때마다 onDraw()를 트리거하는 invalidate() 호출을 실행할 수 있습니다. –

+0

두 직사각형이 같은 xy에 그려져 있다고 생각합니다. 다른 사각형보다 크고 다른 직사각형이 있는지 확인하기 위해 직사각형에 다른 색상을 사용하십시오. – surya

+0

'init)'onDraw'에서 – FunkTheMonk

답변

0

프로젝트 작업을 시도했지만 답안지를 편집하기에 너무 광범위합니다. 나는이 모습을 제시해야합니다

  1. Multiple rect.
  2. Rectangle with view
  3. 위의 코드 줄에서
0
if (count1 < 2) { 
     x = dirButton.getX(); 
     y = dirButton.getY(); 
    } 

, 당신은 라인이 실행될 때의 조건을 설정합니다.

if 문을 사용하십시오. 이 코드를 만든다

 int count1 = 1; //Count was initialized to 1 

먼저 호출이 줄 따라서 2 카운트 값을 증가

count1++; 

을 블록이 다시 실행되지 않을 경우 ...의 if 문을 입력

오버레이로 연결되는 y 값은 절대로 변경되지 않습니다. 당신이 놓칠 수 있습니다 무엇

-1

내가 이런 짓을했는지 어떻게 확인하시기 바랍니다 도움이 Y의 일반 increament

y+=something; 

희망이다, 당신은 그것이 어떻게 작동하는지, here 형태의 MyApp에서 확인하실 수 있습니다

// 직사각형을 그 으려면 어떻게해야합니까?

이것은 SDV입니다.클래스

다음
public static boolean isDrawing = false; 

public static float mStartX; 
public static float mStartY; 

public static float mx; 
public static float my; 

public static void Shape14(float x, float y, float radius) { 
    Path path = new Path(); 
    y -= 2 * radius; 
    radius *= 2; 
    path.moveTo(x + radius, y + radius); 
    path.lineTo(x - radius, y + radius); 
    path.lineTo(x, y); 
    path.lineTo(x + radius, y + radius); 
    float div = (2 * radius)/5; 
    float top = y + radius; 
    RectF rect1 = new RectF(x + radius/4, y, x + radius/1.9f, y 
      + radius); 
    RectF rect2 = new RectF(x + div/2, top, x + div/2 + div, top + div 
      * 2); 
    RectF rect3 = new RectF(x - div/2 - div, top, x - div/2, top + div 
      * 2); 
    RectF rect4 = new RectF(x - div/2, top, x + div/2, top + div); 

    HashMap<String, Object> hm = new HashMap<String, Object>(); 
    hm.put("type", shape14); 
    hm.put("paint", new Paint(DrawingView.mColorPaint)); 
    hm.put("path", path); 

    hm.put("rect1", rect1); 
    hm.put("rect2", rect2); 
    hm.put("rect3", rect3); 
    hm.put("rect4", rect4); 
    al.add(hm); 
    Gmap.mDrawingView.invalidate(); 
} 

우리의 관점은,

public class DrawingView extends View { 

public static Paint mPaint; 
public static int mCurrentShape; 
Point p1, p2, p3, p4; 

public static Paint mDotedPaint; 
public static Paint mColorPaint; 

GoogleMap googleMap; 
SeekBar sbWidth; 
public static float sx, sy; 

public DrawingView(Context context, GoogleMap googleMap, SeekBar sbWidth) { 
    super(context); 
    this.googleMap = googleMap; 
    this.sbWidth = sbWidth; 

    mPaint = new Paint(Paint.DITHER_FLAG); 
    mPaint.setAntiAlias(true); 
    mPaint.setDither(true); 
    mPaint.setColor(SDV.colorChoosen); 
    mPaint.setStyle(Paint.Style.STROKE); 
    mPaint.setStrokeJoin(Paint.Join.ROUND); 
    mPaint.setStrokeCap(Paint.Cap.ROUND); 
    mPaint.setStrokeWidth(SDV.width); 

    mDotedPaint = new Paint(Paint.DITHER_FLAG); 
    mDotedPaint.setAntiAlias(true); 
    mDotedPaint.setDither(true); 
    mDotedPaint.setColor(SDV.colorChoosen); 

    mDotedPaint.setStyle(Paint.Style.STROKE); 
    mDotedPaint.setStrokeJoin(Paint.Join.ROUND); 
    mDotedPaint.setStrokeCap(Paint.Cap.ROUND); 
    mDotedPaint.setStrokeWidth(SDV.width); 

    mColorPaint = new Paint(Paint.DITHER_FLAG); 
    mColorPaint.setAntiAlias(true); 
    mColorPaint.setDither(true); 
    mColorPaint.setFilterBitmap(true); 
    mColorPaint.setStyle(Paint.Style.FILL); 
    mColorPaint.setStrokeWidth(SDV.width); 
    mColorPaint.setColor(SDV.colorChoosen); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    sx = super.getWidth() * 0.5f; 
    sy = super.getHeight() * 0.5f; 

    if (SDV.isDrawing) { 
     new OnGoingDrawings().HandleAllOnGoingDrawings(mCurrentShape, 
       canvas); 
    } else { 
     new ShapeDrawer().DrawEverything(canvas, googleMap, sbWidth); 
    } 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    SDV.mx = event.getX(); 
    SDV.my = event.getY(); 
    switch (mCurrentShape) { 
    case SDV.shape14: 
     TouchEvents.Shape14(event); 
     break; 

    return true; 
} 

} 여기

터치 리스너는이

public static void Shape14(MotionEvent event) { 
    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
     SDV.isDrawing = true; 
     SDV.mStartX = SDV.mx; 
     SDV.mStartY = SDV.my; 
     Gmap.mDrawingView.invalidate(); 
     break; 
    case MotionEvent.ACTION_MOVE: 
     Gmap.mDrawingView.invalidate(); 
     break; 
    case MotionEvent.ACTION_UP: 
     SDV.isDrawing = false; 
     float x = SDV.mStartX, 
     y = SDV.mStartY; 
     float DifX = Math.abs(SDV.mx - SDV.mStartX); 
     float DifY = Math.abs(SDV.my - SDV.mStartY); 
     float radius; 
     if (DifY > DifX) 
      radius = Math.abs(SDV.my - SDV.mStartY); 
     else 
      radius = Math.abs(SDV.mx - SDV.mStartX); 
     SDV.Shape14(x, y, radius); 
     break; 
    } 
}