2016-09-06 6 views
0

사용자 정의보기를 만들고 fragment.but 내부에 포함 시키십시오.지도 터치가 작동하지 않습니다. 왜곡 된보기는 warp_content를 수행 한 후 화면의 전체 해상도를 얻습니다. Google지도 위의 크기 조정 가능한 사각형을 그릴 때지도 터치가 작동하지 않습니다. 이 기본 활동 XML에서이 직사각형보기를 포함 할 곳을 지정합니다. 나에게 어떤 생각이 든주세요. "com.My-application.activities.rectangle"은이 클래스를 추가하는 패키지입니다. 실제로 나는 스크롤과 사각형 같은 시간 맵을 작업하고 싶습니다. 내 문제를 해결할 수있는 적절한 접근 방식을 권해주십시오.다른보기를 포함하면 Google지도가 작동하지 않습니다.

미리 감사드립니다.

이 내 XML 파일

 <Relative-layout 
     android:id="@+id/inner_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <fragment 
     android:id="@+id/map_fragment" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

      <com.Myapplication.activities.rectangle 
      android:id="@+id/drawView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true"/>     
    </RelativeLayout> 

이 난 사각형보기를 만들 내 수업입니다.

public class rectangle extends View { 
     Point[] points = new Point[4]; 
     /** 
     * point1 and point 3 are of same group and same as point 2 and point4 
     */ 
     int groupId = -1; 
     private ArrayList<ColorBall> colorballs = new ArrayList<ColorBall>(); 
     // array that holds the balls 
     private int balID = 0; 
     // variable to know what ball is being dragged 
     Paint paint; 
     Canvas canvas; 


     public rectangle(Context context) { 
      super(context); 
      paint = new Paint(); 
      setFocusable(true); // necessary for getting the touch events 
      canvas = new Canvas(); 

     } 


     public rectangle(Context context, AttributeSet attrs, int defStyle) { 
      super(context, attrs, defStyle); 
     } 

     public rectangle(Context context, AttributeSet attrs) { 
      super(context, attrs); 
      paint = new Paint(); 
      setFocusable(true); // necessary for getting the touch events 
      canvas = new Canvas(); 
     } 

     // the method that draws the balls 
     @Override 
     protected void onDraw(Canvas canvas) { 
      if(points[3]==null) //point4 null when user did not touch and move on screen. 
       return; 
      int left, top, right, bottom; 
      left = points[0].x; 
      top = points[0].y; 
      right = points[0].x; 
      bottom = points[0].y; 
      for (int i = 1; i < points.length; i++) { 
       left = left > points[i].x ? points[i].x:left; 
       top = top > points[i].y ? points[i].y:top; 
       right = right < points[i].x ? points[i].x:right; 
       bottom = bottom < points[i].y ? points[i].y:bottom; 
      } 
      paint.setAntiAlias(true); 
      paint.setDither(true); 
      paint.setStrokeJoin(Paint.Join.ROUND); 
      paint.setStrokeWidth(5); 

      //draw stroke 
      paint.setStyle(Paint.Style.STROKE); 
      paint.setColor(Color.parseColor("#AADB1255")); 
      paint.setStrokeWidth(2); 
      canvas.drawRect(
        left + colorballs.get(0).getWidthOfBall()/2, 
        top + colorballs.get(0).getWidthOfBall()/2, 
        right + colorballs.get(2).getWidthOfBall()/2, 
        bottom + colorballs.get(2).getWidthOfBall()/2, paint); 
      //fill the rectangle 
      paint.setStyle(Paint.Style.FILL); 
      paint.setColor(Color.parseColor("#25DB1255")); 
      paint.setStrokeWidth(0); 
      canvas.drawRect(
        left + colorballs.get(0).getWidthOfBall()/2, 
        top + colorballs.get(0).getWidthOfBall()/2, 
        right + colorballs.get(2).getWidthOfBall()/2, 
        bottom + colorballs.get(2).getWidthOfBall()/2, paint); 

      //draw the corners 
      BitmapDrawable bitmap = new BitmapDrawable(); 
      // draw the balls on the canvas 
      paint.setColor(Color.BLUE); 
      paint.setTextSize(18); 
      paint.setStrokeWidth(0); 
      for (int i =0; i < colorballs.size(); i ++) { 
       ColorBall ball = colorballs.get(i); 
       canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), 
         paint); 

       canvas.drawText("" + (i+1), ball.getX(), ball.getY(), paint); 
      } 
     } 

     // events when touching the screen 
     public boolean onTouchEvent(MotionEvent event) { 
      int eventaction = event.getAction(); 

      int X = (int) event.getX(); 
      int Y = (int) event.getY(); 

      switch (eventaction) { 

       case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on 
        // a ball 
        if (points[0] == null) { 

         //initialize rectangle. 

         points[0] = new Point(); 
         points[0].x = X; 
         points[0].y = Y; 

         points[1] = new Point(); 
         points[1].x = X; 
         points[1].y = Y + 30; 

         points[2] = new Point(); 
         points[2].x = X + 30; 
         points[2].y = Y + 30; 

         points[3] = new Point(); 
         points[3].x = X +30; 
         points[3].y = Y; 

         balID = 2; 
         groupId = 1; 
         // declare each ball with the ColorBall class 
         for (Point pt : points) { 
          colorballs.add(new ColorBall(getContext(), R.drawable.ic_slide, pt)); 

         } 



        } else { 
         //resize rectangle 
         balID = -1; 
         groupId = -1; 
         for (int i = colorballs.size()-1; i>=0; i--) { 
          ColorBall ball = colorballs.get(i); 
          // check if inside the bounds of the ball (circle) 
          // get the center for the ball 
          int centerX = ball.getX() + ball.getWidthOfBall(); 
          int centerY = ball.getY() + ball.getHeightOfBall(); 
          paint.setColor(Color.CYAN); 
          // calculate the radius from the touch to the center of the 
          // ball 
          double radCircle = Math 
            .sqrt((double) (((centerX - X) * (centerX - X)) + (centerY - Y) 
              * (centerY - Y))); 

          if (radCircle < ball.getWidthOfBall()) { 

           balID = ball.getID(); 
           if (balID == 1 || balID == 3) { 
            groupId = 2; 
           } else { 
            groupId = 1; 
           } 

           invalidate(); 
           break; 
          } 
          invalidate(); 
         } 
        } 
        break; 

       case MotionEvent.ACTION_MOVE: // touch drag with the ball 
        if (balID > -1) { 
         // move the balls the same as the finger 

         colorballs.get(balID).setX(X); 
         colorballs.get(balID).setY(Y); 

         paint.setColor(Color.CYAN); 
         if (groupId == 1) { 
          colorballs.get(1).setX(colorballs.get(0).getX()); 
          colorballs.get(1).setY(colorballs.get(2).getY()); 
          colorballs.get(3).setX(colorballs.get(2).getX()); 
          colorballs.get(3).setY(colorballs.get(0).getY()); 
         } else { 
          colorballs.get(0).setX(colorballs.get(1).getX()); 
          colorballs.get(0).setY(colorballs.get(3).getY()); 
          colorballs.get(2).setX(colorballs.get(3).getX()); 
          colorballs.get(2).setY(colorballs.get(1).getY()); 
         } 

         invalidate(); 
        } 

        break; 

       case MotionEvent.ACTION_UP: 
        // touch drop - just do things here after dropping 

        break; 
      } 
      // redraw the canvas 
      invalidate(); 
      return true; 

     } 


    } 

답변

1

내부 메서드 onTouch false를 반환합니다. 체계에, 그 접촉은 파견되지 않았다는 것을 말한다. 의사 코드 :

if (rectangleTouched) 
    return true; 
else return false; 

Android docs에서

부울 onTouchEvent (MotionEvent 이벤트)

결과 : 부울 참 이벤트가 처리 된 경우, 그렇지 않은 경우는 false.

+0

반환 후 false지도는 움직이지만 ractangle는 아닙니다. – Umesh

+0

@umesh 터치가 사각형 범위 내에 있으면 감지해야합니다. 참이면 true를 반환하고 그렇지 않으면 false를 반환합니다. –

관련 문제