Alright

So now i have 2 classes in my application.
the one is the custom view extending view and has all touch events on key
down and on key up.
This view is being called in my main.xml through the package name.
The other one is my main class which has the code to call options menu.
One menu item is Save and if user clicks on it the bitmap image created by
the custom view is to be saved .
It is actually getting saved but it is showing just the background not the
changed bitmap image through the touch of the user.

The custom view code is exactly the same as the FingerPaint class file in
api demos

The only thing where i am getting stuck is now how to save the changed
bitmap image which is created by the users touch i can save the initial one

The code of main class file is :-
package org.testCircle;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class testCircle extends Activity {
    TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(new customView(this));
        setContentView(R.layout.main);
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, 1, 0, "save").setShortcut('3', 'c');
        return true;
    }
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 1:
            //new ColorPickerDialog(this, this, mPaint.getColor()).show();
            fingerPaint cv = new fingerPaint(this);
            Bitmap viewBitmap = Bitmap.createBitmap(cv.getWidth(),
cv.getHeight(),Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(viewBitmap);
            cv.draw(canvas);
            String url = Images.Media.insertImage(getContentResolver(),
viewBitmap, "title", null);
            Toast.makeText(testCircle.this, url, Toast.LENGTH_LONG).show();
            return true;
    }
        return super.onOptionsItemSelected(item);
}
}


and the Custom View is

package org.testCircle;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class fingerPaint extends View {
    Paint mPaint;

    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;

    private static Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;

    public fingerPaint(Context c) {
        super(c);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);
        mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

    public fingerPaint(Context c , AttributeSet attrs){
        super(c , attrs);
         mPaint = new Paint();
         mPaint.setAntiAlias(true);
         mPaint.setDither(true);
         mPaint.setColor(0xFFFF0000);
         mPaint.setStyle(Paint.Style.STROKE);
         mPaint.setStrokeJoin(Paint.Join.ROUND);
         mPaint.setStrokeCap(Paint.Cap.ROUND);
         mPaint.setStrokeWidth(12);
         mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
         mCanvas = new Canvas(mBitmap);
         mPath = new Path();
         mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

    public void onerase(){
        mCanvas=null;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFAAAAAA);

        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath(mPath, 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.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;
    }

}


On Tue, Feb 15, 2011 at 6:35 PM, Marcin Orlowski
<[email protected]>wrote:

> On 15 February 2011 13:10, Robin Talwar <[email protected]>
> wrote:
>
> > Saving this bitmap and
>
> String path = Environment.getExternalStorageDirectory().toString();
> OutputStream fOut = new FileOutputStream( new File(path, "filename.jpg) );
> bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
> fOut.flush();
> fOut.close();
>
> > Erase button to clear all the canvas so that user can again make
> signature
> > on clear screen.
>
>
> http://developer.android.com/reference/android/graphics/Canvas.html#drawRGB%28int,%20int,%20int%29
>
> but again: do RTFM first as there are other options you may want to
> utilse (i.e. you may want to save as PNG instead of lossy JPEG) or
> simply to know whene you will be trying to reinvent the wheel.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to