2012-12-21 14 views
7

Próbuję utworzyć prostokąt, ale to co się dzieje, kiedy przenieść z rozpoczęciem współrzędne pod koniec współrzędneRysowanie prostokąta za pomocą gestu przesuwania

is what happens

, rzeczywiście chcę pokazać postęp kiedy użytkownik porusza się od jednego punktu do drugiego. To jest to, co chciałbym mieć. I would like to have.

Code: -

public boolean onTouch(View v, MotionEvent event) { 

    int action = event.getAction(); 
    switch (action) { 
    case MotionEvent.ACTION_DOWN: 
     downx = event.getX(); 
     downy = event.getY(); 

    //v.invalidate(); 
     break; 
    case MotionEvent.ACTION_MOVE: 
     upx = event.getX(); 
     upy = event.getY(); 

     canvas.drawRect(downx, downy, upx, upy, paint); 

     } 
     choosenImageView.invalidate(); 
     break; 
    case MotionEvent.ACTION_UP: 
     upx = event.getX(); 
     upy = event.getY(); 
      canvas.drawRect(downx, downy, upx, upy, paint); 
      } 
     } 

     // v.invalidate(); 
     break; 
    } 
    return true; 
} 

Edit Co chciałbym zrobić, to pokazać postęp to znaczy, gdy użytkownik przesuwa palcem, kształt powinien być sporządzony.

Sugestie/Przykłady/linki, co zostanie docenione.

+0

Ustaw Paint.Style na WYPEŁNIĆ – BLOB

+0

Nie chcę, aby prostokąt został całkowicie wypełniony. –

+0

wypełni wewnętrzną część prostokąta i uniemożliwi rysowanie wielu prostokątów ... tutaj jest [link] (http://developer.android.com/reference/android/graphics/Paint.Style.html) .. Nie pamiętam, który z nich ... albo FILL, albo FILL_AND_STROKE – BLOB

Odpowiedz

2
case MotionEvent.ACTION_MOVE: 
    upx = event.getX(); 
    upy = event.getY(); 

    canvas.drawRect(downx, downy, upx, upy, paint); 

    } 
    choosenImageView.invalidate(); 
break; 

Ten kod jest przyczyną. Ponieważ tworzysz tu wiele prostokątów. Zamiast tego w onDraw(Canvas canvas) należy użyć metody drawRect i unieważnić wszystkie zdarzenia. Mam nadzieję że to pomoże.

9

Bezpośrednia aktualizacja płótna w wydarzeniu onTouch() bez usuwania go. To nie jest rzekomy sposób rysowania czegoś w Widoku (zakładając, że tego właśnie chcesz).

Zamiast tego należy zapisać początkowe i bieżące współrzędne prostokąta i wykorzystać je w zdarzeniu onDraw(), aby narysować właściwy prostokąt (po tym, jak program Adnroid wyczyści dla ciebie unieważnioną część płótna). Android wyda to wydarzenie, czy płótno musi być odświeżana, więc trzeba powiedzieć, że jest to konieczne w przypadku onTouch(), stosując metodę invalidate():

class myView extends View { // or some other View-based class 
    boolean drawRectangle = false; 
    PointF beginCoordinate; 
    PointF endCoordinate; 

    public boolean onTouch(View v, MotionEvent event) { 
     switch(event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      drawRectangle = true; // Start drawing the rectangle 
      beginCoordinate.x = event.getX(); 
      beginCoordinate.y = event.getY(); 
      endCoordinate.x = event.getX(); 
      endCoordinate.y = event.getY(); 
      invalidate(); // Tell View that the canvas needs to be redrawn 
      break; 
     case MotionEvent.ACTION_MOVE: 
      endCoordinate.x = event.getX(); 
      endCoordinate.y = event.getY(); 
      invalidate(); // Tell View that the canvas needs to be redrawn 
      break; 
     case MotionEvent.ACTION_UP: 
      // Do something with the beginCoordinate and endCoordinate, like creating the 'final' object 
      drawRectangle = false; // Stop drawing the rectangle 
      invalidate(); // Tell View that the canvas needs to be redrawn 
      break; 
     } 
    } 

    protected void onDraw(Canvas canvas) { 
     if(drawRectangle) { 
     // Note: I assume you have the paint object defined in your class 
     canvas.drawRect(beginCoordinate.x, beginCoordinate.y, endCoordinate.x, endCoordinate.y, paint); 
     } 
    } 
} 
6

Czy oczyścić płótno między dwoma prostokąta? Dodaj funkcję jak ten:

void clearCanvas() 
    { 
    canvas.drawRect(0,0, width, height, paint); 

    } 
2

Jeśli rozumiem skorygować chcesz, aby użytkownik, aby narysować prostokąt na ekranie i może następnie wyciągnąć go na wyświetlaczu.

Myślę, że można po prostu pomalować punkt, w którym użytkownik naciśnie klawisz, po prostu dać zwrotnej do użytkownika, a kiedy zakończy rysowanie zastąpić przez prostokąt

lub narysować linie prostokąta, więc użytkownik zaczyna rysować i masz linię od początku do palca, kiedy użytkownik zmienia kierunek, rozpoczynasz nową linię od miejsca, w którym użytkownik zmienił kierunek i palec, a kiedy użytkownik wróci, aby rozpocząć, masz prostokąt narysowany jako 4 linie

3

Przepraszam Szanowny Panie, podaję całą aktywność, którą używam i rysuję na płótnie.

public class CanvasExample extends Activity 
    { 
     /** Called when the activity is first created. */ 

     RelativeLayout relMainOperationLayout; 
     RelativeLayout relTabHeader; 
     //RelativeLayout relMidalLayout; 
     RelativeLayout relBelowLayout; 
     Context myContext; 
     DrawCanvas drawCanvas; 
     static boolean loadFlage=true; 
     BibleHelper bibleHelper; 

     Bitmap mainBitmap; 




     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      myContext=CanvasExample.this; 
      bibleHelper=new BibleHelper(CanvasExample.this,myContext); 
      bibleHelper.setFullScreen(); 

      LayoutInflater layoutInflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      int layoutId = myContext.getResources().getIdentifier("main","layout",getPackageName()); 

      relMainOperationLayout = (RelativeLayout) layoutInflater.inflate(layoutId,null); 

      relTabHeader=(RelativeLayout) relMainOperationLayout.findViewById(R.id.relHeadLayout); 

      //relMidalLayout=(RelativeLayout) relMainOperationLayout.findViewById(R.id.relmidalLayout); 

      relBelowLayout=(RelativeLayout) relMainOperationLayout.findViewById(R.id.relBelowLayout); 



      mainBitmap=getIconDrawable(R.drawable.splash); 


      drawCanvas=new DrawCanvas(CanvasExample.this,myContext); 

      //drawCanvas.setBackgroundColor(Color.YELLOW); 

      //drawCanvas.setBackgroundDrawable(CanvasExample.this.getResources().getDrawable(R.drawable.splash)); 

      drawCanvas.setBackgroundDrawable(new BitmapDrawable(mainBitmap)); 



      RelativeLayout.LayoutParams drawParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 

      drawParams.addRule(RelativeLayout.BELOW, relTabHeader.getId()); 

      //relMidalLayout.addView(drawCanvas,drawParams); 

      relMainOperationLayout.addView(drawCanvas,drawParams); 


//   mainImageView=new ImageView(CanvasExample.this); 
//   
//   RelativeLayout.LayoutParams mainParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,200); 
//   relMainOperationLayout.addView(mainImageView,mainParams); 
//   mainImageView.setBackgroundDrawable(CanvasExample.this.getResources().getDrawable(R.drawable.ic_launcher)); 





      setContentView(relMainOperationLayout); 



     } 




     class DrawCanvas extends View 
     { 

      Context drawContext; 
      Activity drawActivity; 
      ImageView image; 
      Paint mPaint; 
      int left=0,right=0,top=0,bottom=0; 
      Canvas passCanvas; 

      //Bitmap bitmapOrg; 
      // bitmapOrg; 

      public DrawCanvas(Activity activity,Context context) 
      { 
       super(activity); 

       this.drawActivity=activity; 

       this.drawContext=context; 

       //bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.splash); 

        mPaint = new Paint(); 

        mPaint.setDither(true); 

        mPaint.setColor(Color.RED); 

        mPaint.setStyle(Paint.Style.STROKE); 

        mPaint.setStrokeJoin(Paint.Join.ROUND); 

        mPaint.setStrokeCap(Paint.Cap.ROUND); 

        mPaint.setStrokeWidth(3); 

        mPaint.setFilterBitmap(true); 

       this.setOnTouchListener(new View.OnTouchListener() { 

        //@Override 
        public boolean onTouch(View v, MotionEvent event) 
        { 

         switch (event.getActionMasked()) 
         { 
         case MotionEvent.ACTION_DOWN: 

          invalidate(); 

          left=(int) event.getX(); 
          right=left+1; 
          top=(int) event.getY(); 
          bottom=top+1;    
          mPaint.setColor(Color.BLACK); 
          onDraw(passCanvas=new Canvas()); 
         break; 

         case MotionEvent.ACTION_MOVE:     
          invalidate(); 
          int tempX=(int) event.getX(); 

          //System.err.println("Get X->"+event.getX()); 
          if(tempX>right) 
          { 
           right=right+1; 
          } 
          else 
          { 
           right=right-1; 

          } 
          //System.err.println("Get Y->"+event.getY()); 
          int tempY=(int) event.getY(); 

          if(tempY>bottom) 
          { 
           bottom=bottom+1;     

          }else 
          { 
           bottom=bottom-1;     

          } 

          mPaint.setColor(Color.GREEN); 
          onDraw(passCanvas=new Canvas()); 
         break; 

         case MotionEvent.ACTION_UP: 
          invalidate(); 
          mPaint.setColor(Color.RED); 
          onDraw(passCanvas=new Canvas()); 
          System.err.println("After Touch Up"); 
          CanvasExample.loadFlage=false; 
          onDraw(passCanvas=new Canvas()); 


          /* 
          bibleHelper.showErrorLog("Start X -->"+left); 

          bibleHelper.showErrorLog("Real X -->"+event.getX()); 

          bibleHelper.showErrorLog("End X-->"+right); 

          bibleHelper.showErrorLog("Start Y-->"+top); 

          bibleHelper.showErrorLog("Real Y-->"+top); 


          bibleHelper.showErrorLog("End Y-->"+bottom); 
          */ 

          Bitmap croppedBmp = Bitmap.createBitmap(mainBitmap,left,top,right,bottom); 


          final Dialog dialog = new Dialog(CanvasExample.this); 

          dialog.setContentView(R.layout.custom_dialog); 
          dialog.setTitle("Title..."); 
          dialog.setCancelable(true); 

          ImageView image = (ImageView) dialog.findViewById(R.id.main); 
          image.setImageBitmap(croppedBmp); 
          Button btnClose=(Button) dialog.findViewById(R.id.btnClose); 

          btnClose.setOnClickListener(new View.OnClickListener() 
          { 

           public void onClick(View v) 
           { 
            dialog.dismiss(); 
           } 
          }); 

          dialog.show(); 

         break; 

         default: 

          break; 
         } 
         return true; 
        } 
       });  

      } 

      @Override 
      protected void onDraw(Canvas canvas) 
      { 

       canvas.drawRect(left, top, right, bottom, mPaint); 
      } 

     } 
     private Bitmap getIconDrawable(int imageId) 
     { 
      //Drawable rtnDrawable = null; 
      Bitmap imageBitmap=null; 
      try 
      { 
       int getImageWH[]; 
       imageBitmap = BitmapFactory.decodeResource(getResources(),imageId); 
       int IW = imageBitmap.getWidth(); 
       int IH = imageBitmap.getHeight(); 

       bibleHelper.showErrorLog("Icon Width->" + IW); 
       bibleHelper.showErrorLog("Icon Height->" + IH); 

       WindowManager winManager = (WindowManager) CanvasExample.this.getSystemService(Context.WINDOW_SERVICE); 
       int screenwidth = winManager.getDefaultDisplay().getWidth(); 
       int screenheight = winManager.getDefaultDisplay().getHeight(); 

       getImageWH = bibleHelper.getObjectWidthHeight(screenwidth,screenheight, IW, IH); 

       bibleHelper.showErrorLog("Get Icon Width->" + getImageWH[0]); 
       bibleHelper.showErrorLog("Get Icon Height->" + getImageWH[1]); 

       imageBitmap = Bitmap.createScaledBitmap(imageBitmap, getImageWH[0],getImageWH[1], false); 

       bibleHelper.showErrorLog("New Width-->"+imageBitmap.getWidth()); 
       bibleHelper.showErrorLog("New Height-->"+imageBitmap.getHeight()); 

       //rtnDrawable = (Drawable) new BitmapDrawable(imageBitmap); 
      } catch (Exception ex) { 
       bibleHelper.showErrorLog("Convert Icon Exception-->"+ ex.toString()); 
      } 
      //return rtnDrawable; 
      return imageBitmap; 
     } 


} 

W tym BibleHelper jest klasa Helper używać czarownicy do wyświetlania niektórych wiadomości i dziennika.

Powiązane problemy