2017-06-03 1 views
0

S'il vous plaît j'ai besoin de l'aide concernant la méthode Annuler, j'ai essayé de l'implémenter comme le code ci-dessous mais il ne supprime pas la dernière couleur, Veuillez m'aider à le résoudre.Supprimer le dernier bitmap de forme de couleur Annuler

en attente de votre réponse aimable.

Merci à l'avance.


MainActivity

public class MainActivity extends AppCompatActivity implements 
View.OnTouchListener { 
Button red, blue, yellow, undo; 
Paint paint; 
private RelativeLayout drawingLayout; 
private MyView myView; 
private ArrayList<Path> paths = new ArrayList<Path>(); 
private ArrayList<Path> undonePaths = new ArrayList<Path>(); 

/** 
* Called when the activity is first created. 
*/ 
    /* 
    * 
    * private ImageView imageView; private Canvas cv; private Bitmap mask, 
    * original, colored; private int r,g,b; private int sG, sR, sB; 
    */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    myView = new MyView(this); 
    drawingLayout = (RelativeLayout) findViewById(R.id.relative_layout); 
    drawingLayout.addView(myView); 

    red = (Button) findViewById(R.id.btn_red); 
    blue = (Button) findViewById(R.id.btn_blue); 
    yellow = (Button) findViewById(R.id.btn_yellow); 
    undo = (Button) findViewById(R.id.undo); 

    red.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      paint.setColor(Color.RED); 
     } 
    }); 

    yellow.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      paint.setColor(Color.YELLOW); 
     } 
    }); 
    blue.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      paint.setColor(Color.BLUE); 
     } 
    }); 
    undo.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      myView.onClickUndo(); 
     } 
    }); 
} 

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


// flood fill 
public class MyView extends View { 

    final Point p1 = new Point(); 
    Bitmap mBitmap; 
    ProgressDialog pd; 
    Canvas canvas; 
    private Path path; 

    // Bitmap mutableBitmap ; 
    public MyView(Context context) { 
     super(context); 

     paint = new Paint(); 
     paint.setAntiAlias(true); 
     pd = new ProgressDialog(context); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeWidth(5f); 
     mBitmap = BitmapFactory.decodeResource(getResources(), 
       R.drawable.gp1_1).copy(Bitmap.Config.ARGB_8888, true); 

     this.path = new Path(); 


    } 

    public void onClickUndo() { 
     if (paths.size() > 0) { 
      undonePaths.add(paths.remove(paths.size() - 1)); 
      invalidate(); 
     } else { 
      Toast.makeText(getContext(), getString(R.string.nomore), Toast.LENGTH_SHORT).show(); 
     } 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     this.canvas = canvas; 
     paint.setColor(Color.GREEN); 
     // int width = drawingLayout.getWidth(); 
    // int height = drawingLayout.getHeight(); 
    // float centerX = (width - mBitmap.getWidth()) * 0.5f; 
    //float centerY = (height - mBitmap.getHeight()) * 0.5f; 
     canvas.drawBitmap(mBitmap, 0, 0, paint); 
     /////////////////////////////// 
     for (Path p : paths) { 
      canvas.drawPath(p, paint); 
      ////////////////////////// 
     } 

    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 
     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 

       p1.x = (int) x; 
       p1.y = (int) y; 
       final int sourceColor = mBitmap.getPixel((int) x, (int) y); 
       final int targetColor = paint.getColor(); 
       new TheTask(mBitmap, p1, sourceColor, targetColor).execute(); 
       paths.add(path); 

       invalidate(); 
     } 
     return true; 
    } 

    public void clear() { 
     path.reset(); 
     invalidate(); 
    } 

    public int getCurrentPaintColor() { 
     return paint.getColor(); 
    } 

    class TheTask extends AsyncTask<Void, Integer, Void> { 

     Bitmap bmp; 
     Point pt; 
     int replacementColor, targetColor; 

     public TheTask(Bitmap bm, Point p, int sc, int tc) { 
      this.bmp = bm; 
      this.pt = p; 
      this.replacementColor = tc; 
      this.targetColor = sc; 
      pd.setMessage(getString(R.string.wait)); 
      pd.show(); 
     } 

     @Override 
     protected void onPreExecute() { 
      pd.show(); 

     } 

     @Override 
     protected void onProgressUpdate(Integer... values) { 

     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      FloodFill f = new FloodFill(); 
      f.floodFill(bmp, pt, targetColor, replacementColor); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      pd.dismiss(); 
      invalidate(); 
     } 
    } 
} 

public class FloodFill { 
    public void floodFill(Bitmap image, Point node, int targetColor, 
          int replacementColor) { 
     int width = image.getWidth(); 
     int height = image.getHeight(); 
     int target = targetColor; 
     int replacement = replacementColor; 
     if (target != replacement) { 
      Queue<Point> queue = new LinkedList<Point>(); 
      do { 

       int x = node.x; 
       int y = node.y; 
       while (x > 0 && image.getPixel(x - 1, y) == target) { 
        x--; 

       } 
       boolean spanUp = false; 
       boolean spanDown = false; 
       while (x < width && image.getPixel(x, y) == target) { 
        image.setPixel(x, y, replacement); 
        if (!spanUp && y > 0 
          && image.getPixel(x, y - 1) == target) { 
         queue.add(new Point(x, y - 1)); 
         spanUp = true; 
        } else if (spanUp && y > 0 
          && image.getPixel(x, y - 1) != target) { 
         spanUp = false; 
        } 
        if (!spanDown && y < height - 1 
          && image.getPixel(x, y + 1) == target) { 
         queue.add(new Point(x, y + 1)); 
         spanDown = true; 
        } else if (spanDown && y < height - 1 
          && image.getPixel(x, y + 1) != target) { 
         spanDown = false; 
        } 
        x++; 
       } 
      } while ((node = queue.poll()) != null); 
     } 
    } 
} 
    } 
+0

"Ne fonctionne pas" comme dans ...? – Zoe

+0

@LunarWatcher Pourriez-vous m'expliquer d 'avantage? – Leenah

+0

C'est ce que je vous demande. Vous écrivez: 'la méthode d'annulation ne fonctionne pas' - qu'est-ce qui ne fonctionne pas? Est-ce un accident? Exception au moins? Ou ne fait-il simplement rien? – Zoe

Répondre

0

Semble qu'il est impossible de retourner la couleur précédente. Il est nécessaire d'avoir un tableau pour chaque point pour stocker la couleur utilisée dans chaque point.

0

Je suppose que vous voulez dire la méthode undo ne fonctionne pas. La raison possible est que le dessin précédent sur la toile n'est pas supprimé. Essayez d'effacer la toile avant de dessiner dans la méthode onDraw().

@Override protected void onDraw(Canvas canvas) { 
this.canvas = canvas; 
canvas. drawColor(0,PorterDuff.Mode.CLEAR); //This clears the canvas. 
paint.setColor(Color.GREEN); 
//rest of the code 
} 
+0

Merci d'avoir essayé de m'aider, mais il montre plein écran noir. – Leenah