2016-12-17 3 views
0

J'essaie de dessiner avec plusieurs fichiers bitmaps de couleur avec l'opération annuler/rétablir dans une vue. Voici ma configuration pour l'undo/redo et la configuration couleur/bitmap,La vue ne répond pas après l'application d'une longue liste de chemins

private ArrayList<Path> paths = new ArrayList<Path>(); 
private ArrayList<Path> undonePaths = new ArrayList<Path>(); 

private Map<Path, Integer> colorMap = new HashMap<>(); 
private Map<Path, Bitmap> colorFillMap = new HashMap<Path, Bitmap>(); 

Peinture configurations:

 drawPath = new Path(); 
     drawPaint = new Paint(Paint.DITHER_FLAG); 
     drawPaint.setAntiAlias(true); 
     drawPaint.setFilterBitmap(true); 
     drawPaint.setDither(true); 
     drawPaint.setColor(paintColor); 
     drawPaint.setStrokeWidth(brushSize); 
     drawPaint.setStyle(Paint.Style.STROKE); 
     drawPaint.setStrokeJoin(Paint.Join.ROUND); 
     drawPaint.setStrokeCap(Paint.Cap.ROUND); 
     drawPaint.setAlpha(100); 

     setLayerType(View.LAYER_TYPE_SOFTWARE, drawPaint); 

Je stocker simplement la couleur/bitmap modifié dans la liste des chemins chaque fois qu'il ya un changement. Cela permet de récupérer exactement la même couleur/bitmap utilisée dans un état précédent.

private void touch_start(float x, float y) { 
     undonePaths.clear(); 
     drawPath.reset(); 
     drawPath.moveTo(x, y); 
     mX = x; 
     mY = y; 
    } 

    private void touch_move(float x, float y, float x2, float y2) { 
     float dx = Math.abs(x - mX); 
     float dy = Math.abs(y - mY); 
     if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
      drawPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
      mX = x; 
      mY = y; 
     } 
    } 

    private void touch_up() { 

     // commit the path to our offscreen 
     drawPath.lineTo(mX, mY); 
     drawCanvas.drawPath(drawPath, drawPaint); 

     // kill this so we don't double draw 
     paths.add(drawPath); 
     // the selectedColor is dynamically changeable 
     colorMap.put(drawPath, selectedColor); 
     drawPath = new Path(); 
     drawPath.reset(); 
    } 

    public void onClickUndo() { 
     if (paths.size() > 0) { 
      undonePaths.add(paths.remove(paths.size() - 1)); 
      invalidate(); 
     } 
    } 

    public void onClickRedo() { 
     if (undonePaths.size() > 0) { 
      paths.add(undonePaths.remove(undonePaths.size() - 1)); 
      invalidate(); 
     } 
    } 

et enfin à l'intérieur du onDraw() méthode je dessine chaque chemin sur la base des valeurs stockées,

protected void onDraw(Canvas canvas) { 
     canvas.save(); 

     try { 
      for (Path p : paths) { 

      if (colorFillMap.get(p) != null) { 
       // the colorFillMap is dynamically changeable 
       canvas.drawBitmap(colorFillMap.get(p), 0, 0, drawPaint); 
      } else { 
        if (colorMap.get(p) != null) { 
         drawPaint.setColor(colorMap.get(p)); 
        } 
         if (p != null) { 
          canvas.drawPath(p, drawPaint); 

          drawPaint.setColor(selectedColor); 

          canvas.drawPath(drawPath, drawPaint); 
         } 
       } 
      } 
     } 

     canvas.restore(); 
    } 

Cependant, après l'application d'un couple de chemins de la vue devient lent et la La réactivité tactile ne répond plus. Je me demande à cause du grand nombre de chemins dans la liste provoque cela. Y at-il de toute façon que je puisse accélérer le processus? Est-ce que je fais quelque chose de mal?

Répondre

0

Y at-il de toute façon je peux accélérer le processus?

Vous pouvez exécuter vos dessins sur une image bitmap, il vous suffit donc de dessiner votre image bitmap dans onDraw(). Par exemple:

private Canvas mCanvas; 
private Bitmap mCanvasBitmap; 
@Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     if (mCanvasBitmap == null) { 
      mCanvasBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888); 
      mCanvas = new Canvas(mCanvasBitmap); 
     } 

    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     if(mCanvasBitmap == null) return; 
     canvas.drawBitmap(mCanvasBitmap,0,0,null); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
    .... 

    mCanvas.draw(...); 
    invalidate(); 

    .... 

    } 

Cependant, vous devrez trouver une nouvelle méthode pour l'opération de rétablissement/annulation.