2016-07-12 8 views
0

Ich implementiere einen Code, in dem ich versuche, einen Vordergrund und ein Hintergrundbild zu haben, ich möchte das Vordergrundbild löschen, und wenn ich einen Fehler gemacht hätte, wäre ich in der Lage, es rückgängig zu machen .Löschen von Leinwand-Bitmap, um Hintergrund sichtbar zu machen

für Hintergrund i eine Bildansicht und Vordergrund verwende ist Bitmap auf Leinwand

i bis hierher gearbeitet haben, den Vordergrund zu löschen (dh die Leinwand) aber Redo rückgängig machen nicht funktioniert

Code ist:

public class MainActivity extends AppCompatActivity { 

private Bitmap DrawBitmap; 
private Canvas mCanvas; 
private Path mPath; 
private Paint DrawBitmapPaint; 
RelativeLayout Rl; 
CustomView View; 

DrawView drawView; 
private Button undo, redo; 
private ArrayList<Path> paths = new ArrayList<Path>(); 
private ArrayList<Path> undonePaths = new ArrayList<Path>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    this.loadActivity(); 
} 

private Paint mPaint; 


public class CustomView extends View { 

    public CustomView(Context c) { 

     super(c); 

     create_image(); 


     setLayerType(android.view.View.LAYER_TYPE_SOFTWARE, mPaint); 

    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     // mCanvas.drawColor(Color.BLUE); 

     super.onSizeChanged(w, h, oldw, oldh); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

     for (Path p : paths){ 
      canvas.drawPath(p, mPaint); 
     } 
     setDrawingCacheEnabled(true); 
     canvas.drawBitmap(DrawBitmap, 0, 0, DrawBitmapPaint); 
     canvas.drawPath(mPath, mPaint); 
     canvas.drawRect(mY, 0, mY, 0, DrawBitmapPaint); 
    } 

    private float mX, mY; 
    private static final float TOUCH_TOLERANCE = 4; 

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

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

    private void touch_up() { 
     mPath.lineTo(mX, mY); 
     mCanvas.drawPath(mPath, mPaint); 
     mPath = new Path(); 
     paths.add(mPath); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     float x = event.getX(); 
     float y = event.getY(); 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       touch_start(x, y); 
       invalidate(); 
       break; 
      case MotionEvent.ACTION_MOVE: 
       touch_move(x, y); 
       invalidate(); 
       break; 
      case MotionEvent.ACTION_UP: 
       touch_up(); 
       // performClick(); 
       invalidate(); 
       break; 
     } 
     return true; 
    } 

    public void clear() { 
     create_image(); 

     // Added later.. 
     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setDither(true); 
     mPaint.setColor(Color.BLACK); 
     mPaint.setStyle(Paint.Style.STROKE); 
     // mCanvas.drawColor(Color.BLUE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(40); 
     this.invalidate(); 
    } 

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

     } 
     //toast the user 
    } 

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

     } 
     //toast the user 
    } 


} 

public void loadActivity() { 

    undo = (Button) findViewById(R.id.button1); 
    redo = (Button) findViewById(R.id.button2); 

    drawView = new DrawView(this); 

    View = new CustomView(this); 
    Rl = (RelativeLayout) findViewById(R.id.linearLayout2); 
    Rl.addView(View); 

    Bitmap bitmap = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.sample); 
    bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 


    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
    mPaint.setDither(true); 
    // mCanvas.drawColor(Color.BLUE); 
    mPaint.setStyle(Paint.Style.STROKE); 
    mPaint.setStrokeJoin(Paint.Join.ROUND); 
    mPaint.setStrokeCap(Paint.Cap.ROUND); 
    mPaint.setStrokeWidth(40); 
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
    mPaint.setStrokeWidth(40); 

    mPaint.setColor(Color.BLUE); 
    mPaint.setStrokeWidth(40); 



    undo.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      View.onClickUndo(); 
     } 
    }); 

    redo.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      View.onClickRedo(); 
     } 
    }); 

} 

public void create_image() { 

    DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    int screenWidth = displaymetrics.widthPixels; 
    int screenHeight = displaymetrics.heightPixels; 
    DrawBitmap = Bitmap.createBitmap(screenWidth, screenHeight, 
      Bitmap.Config.ARGB_4444); 

    mCanvas = new Canvas(DrawBitmap); 
    Bitmap bitmap = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.sample); 
    bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 
    mCanvas.drawBitmap(bitmap, 0, 0, null); 

    mPath = new Path(); 

    DrawBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    paths.add(mPath); 

} 
} 

und mein Layout ist:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.approduction.drawing.MainActivity"> 

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Undo" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Redo" /> 

</LinearLayout> 


<RelativeLayout 
    android:id="@+id/linearLayout2" 
    android:layout_below="@id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="@mipmap/ic_launcher"/> 
</RelativeLayout> 

Anregungen wertvolle Vielen Dank im Voraus ..

+0

Ich denke, es wird u [Android Leinwand Redo und Undo Operation] helfen (http://stackoverflow.com/questions/11114625/android-canvas-redo- und-undo-operation.) – Raghavendra

+0

Ich habe diesen Code implementiert, aber es zeichnet nicht das Bild, nur die Bildansicht ist sichtbar, kein Vordergrund ist da, ich möchte Dinge rückgängig machen, die im Vordergrund erledigt wurden. – Ashwani

Antwort

0

Das Problem ist, dass Sie 2 Leinwand wäre hier haben: canvas und mCanvas.
Ihr Undo/Redo funktioniert nur für canvas und nicht mCanvas. Das ist das Problem, das Sie haben. Sie müssen entweder mCanvas in onTouchUp() entfernen oder den Code dort ändern, um die Funktion zum Rückgängigmachen/Wiederherstellen zu implementieren.

Siehe meine Antwort hier für weitere Informationen: https://stackoverflow.com/a/38220061/4747587

Verwandte Themen