Showing posts with label Paint. Show all posts
Showing posts with label Paint. Show all posts

Paint Undo And Redo Example


DrawActivity 

public class DrawActivity extends Activity {

     private Canvas  mCanvas;
     private Path    mPath;
     private Paint       mPaint;
     private ArrayList<Path> paths = new ArrayList<Path>();
     private ArrayList<Path> undonePaths = new ArrayList<Path>();
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final DrawView drawView = new DrawView(this);
        setContentView(R.layout.activity_main);
     
        FrameLayout frm_layout=(FrameLayout) findViewById(R.id.main_frame);
        frm_layout.addView(drawView);
     
        Button btn_undo=(Button) findViewById(R.id.undo);
        btn_undo.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {            
                drawView.onClickUndo();
            }
        });

        Button btn_redo=(Button) findViewById(R.id.redo);
        btn_redo.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {        
                drawView.onClickRedo();
            }
        });
     
        /*Button color = (Button) findViewById (R.id.color);
        color.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

int _color = R.color.red;
new PickerDialog(v.getContext(),new OnColorChangedListener()  {

         public void colorChanged(int color) {
         mPaint.setColor(color);
          }
      }, mPaint.getColor(), _color).show();

}
});  */    
    }
 
    /// color changed function, getting value from ColorPickerDialog ///

    public void colorChanged(int color) {
    mPaint.setColor(color);
    }
 
    public class DrawView extends View implements OnTouchListener  {
     
     
        public DrawView(Context context)   {    
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);    
            this.setOnTouchListener(this);
         
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(Color.GREEN);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(5);
            mCanvas = new Canvas();
            mPath = new Path();
            paths.add(mPath);      
        }            
     
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        }

        @Override
        protected void onDraw(Canvas canvas) {          
        for (Path p : paths){
        canvas.drawPath(p, mPaint);
        }
        }

        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);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw          
        mPath = new Path();
        paths.add(mPath);
        }

        public void onClickUndo () {
        if (paths.size()>0)  {
        undonePaths.add(paths.remove(paths.size()-1));
        invalidate();
        } else  {
        Log.i("undo", "Undo elsecondition");
        }          
        }

        public void onClickRedo (){
        if (undonePaths.size()>0)  {
        paths.add(undonePaths.remove(undonePaths.size()-1));
        invalidate();
        }
        else  {
        Log.i("undo", "Redo elsecondition");
        }          
        }

        public boolean onTouch(View arg0, 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();
        invalidate();
        break;
        }
        return true;
        }
    }
}

XML


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FF4500"
        android:orientation="horizontal" >
     
        <Button
            android:id="@+id/undo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Undo" />

        <Button
            android:id="@+id/redo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Redo" />    

      <!--   <Button
            android:id="@+id/color"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Color" /> -->
    </LinearLayout>

    <FrameLayout
        android:id="@+id/main_frame"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/linearlayout" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/cat" />
    </FrameLayout>

</RelativeLayout>

Draw paint in free hand and clear paint button click

1. ACTIVITY

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;


public class MainActivity extends Activity {

private RelativeLayout relativelayout;

// Instance variables
private Paint mPaint, mBitmapPaint;
private MyView mView;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Button ClearPaint;

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

relativelayout = (RelativeLayout) findViewById(R.id.item);
ClearPaint = (Button) findViewById(R.id.ClearPaint);
ClearPaint.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
mBitmap.eraseColor(Color.TRANSPARENT);
mPath.reset();
mView.invalidate();
}
});

DisplayMetrics metrics = getBaseContext().getResources().getDisplayMetrics();
int w = metrics.widthPixels;
int h = metrics.heightPixels;

System.out.println(" width " + w);
System.out.println(" height " + h);

mView = new MyView(this, w, h);
mView.setDrawingCacheEnabled(true);

mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(5);

relativelayout.addView(mView);
}

// //////////******************* Pinting view *******************///////////////////

public class MyView extends View {
// int bh = originalBitmap.getHeight();
// int bw = originalBitmap.getWidth();

public MyView(Context c, int w, int h) {
super(c);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
// Bitmap mBitmap =
// Bitmap.createScaledBitmap(originalBitmap,200,200,true);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mBitmapPaint
.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// mBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
// mCanvas = new Canvas(mBitmap);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}

// //////************touching evants for painting**************///////
private float mX, mY;
private static final float TOUCH_TOLERANCE = 5;

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);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}

@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();
invalidate();
break;
}
return true;
} // end of touch events for image
}// end MyView
}

2. LAYOUT

<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" >

    <RelativeLayout
        android:id="@+id/item"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:src="@drawable/dog" />

        </RelativeLayout>        

         <Button
             android:id="@+id/ClearPaint"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_alignParentBottom="true"
             android:layout_alignParentLeft="true"
             android:layout_weight="1"
             android:text="ClearPaint" />
</RelativeLayout>

3. OUTPUT

Draw paint by free hand

 Click Clearpaint button Paint is Clear

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...