Tecnologia do Blogger.
RSS

[androidbrasil-dev] Problemas ao salvar canvas

Pessoal,

Estou utilizando esse exemplo[1] para desenhar e salvar um canvas.
Acontece que ao salvar a imagem, na galeria ela aparece com algumas falhas em determinados locais da imagem.
Alguém sabe o que pode ser?

[1]http://www.tutorialforandroid.com/2010/11/drawing-with-canvas-in-android-renewed.html

Code(Vou colocar duas classes que eu acredito que seja por ai)

package com.almondmendoza.drawings;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by IntelliJ IDEA.
 * User: almondmendoza
 * Date: 07/11/2010
 * Time: 2:15 AM
 * Link: http://www.tutorialforandroid.com/
 */
public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback {
    private Boolean _run;
    protected DrawThread thread;
    private Bitmap mBitmap;

    private CommandManager commandManager;

    public DrawingSurface(Context context, AttributeSet attrs) {
        super(context, attrs);

        getHolder().addCallback(this);


        commandManager = new CommandManager();
        thread = new DrawThread(getHolder());
    }


    class DrawThread extends  Thread{
        private SurfaceHolder mSurfaceHolder;


        public DrawThread(SurfaceHolder surfaceHolder){
            mSurfaceHolder = surfaceHolder;

        }

        public void setRunning(boolean run) {
            _run = run;
        }


        @Override
        public void run() {
            Canvas canvas = null;
            while (_run){
                try{
                    canvas = mSurfaceHolder.lockCanvas(null);
                    if(mBitmap == null){
                        mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                    }
                    final Canvas c = new Canvas (mBitmap);

                    c.drawColor(0, PorterDuff.Mode.CLEAR);
                   

                    commandManager.executeAll(c);

                } finally {// patch para android 4.x
                    if (canvas!=null) {
                        canvas.drawBitmap (mBitmap, 0,  0,null);
                        mSurfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
            }

        }
    }


    public void addDrawingPath (DrawingPath drawingPath){
        commandManager.addCommand(drawingPath);
    }

    public boolean hasMoreRedo(){
        return commandManager.hasMoreRedo();
    }

    public void redo(){
        commandManager.redo();
    }

    public void undo(){
        commandManager.undo();
    }

    public boolean hasMoreUndo(){
        return commandManager.hasMoreRedo();
    }

    public Bitmap getBitmap(){
        return mBitmap;
    }


    public void surfaceChanged(SurfaceHolder holder, int format, int width,  int height) {
        // TODO Auto-generated method stub
        mBitmap =  Bitmap.createBitmap (width, height, Bitmap.Config.ARGB_8888);;
    }


    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        thread.setRunning(true);
        thread.start();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        boolean retry = true;
        thread.setRunning(false);
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }

}

 



package com.almondmendoza.drawings;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class ExportBitmapToFile extends AsyncTask<Intent, Void, Boolean> {
   
    private Context mContext;
    private Handler mHandler;
    private Bitmap nBitmap;
    private String uri;

    public static int SAVE_FAILURE = 0;
    public static int SAVE_SUCCESS = 1;

    private static File APP_FILE_PATH = new File(Constants.PATH_SIGNATURE);

    public ExportBitmapToFile(Context context, Handler handler, Bitmap bitmap) {
        mContext = context;
        nBitmap = bitmap;
        mHandler = handler;
    }

    @Override
    protected Boolean doInBackground(Intent... arg0) {
        try {
            if (!APP_FILE_PATH.exists()) {
                APP_FILE_PATH.mkdirs();
            }
            File f = new File(APP_FILE_PATH + File.separator
                    + generateSingatureName());

            if (!f.exists()) {
                f.createNewFile();
            }
            uri = f.getAbsolutePath();
            final FileOutputStream out = new FileOutputStream(f);
            nBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            Log.d(Constants.TAG, "Error saving signature:" + e.getMessage(), e);
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean bool) {
        super.onPostExecute(bool);
        if (bool) {
            Bundle bundle = new Bundle();
            bundle.putString("uri", uri);
            Message msg = new Message();
            msg.setData(bundle);
            msg.what = SAVE_SUCCESS;
            mHandler.sendMessage(msg);
        } else {
            mHandler.sendEmptyMessage(SAVE_FAILURE);
        }
    }

    private String generateSingatureName() {
        return String.valueOf(new Date().getTime()) + ".jpg";
    }

}




--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário