0

ねえ、保存メニューからfingerpaintAPIでユーザーが作成した画像を保存したいです。
これがアクティビティクラス`です

    public int mImageCount = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button res = (Button) findViewById(R.id.reset);
        res.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method
                TouchEventView touchView = (TouchEventView) findViewById(R.id.mydrawview1);
                touchView.clear();

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_name, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.save:
            save();
            return true;
        case R.id.open:
            open();
            return true;
        default:
            return super.onOptionsItemSelected(item);

        }

    }

    public void save() {
        TouchEventView touchview = (TouchEventView) findViewById(R.id.mydrawview1);

        File myDir = new File("/sdcard/saved_images");
        if (!myDir.exists())
            myDir.mkdirs();
        mImageCount++;
        String fname = "image-" + mImageCount + ".jpg";
        File file = new File(myDir, fname);
        if (file.exists())
            file.delete();
        touchview.saveAsJpg(file);
        Toast.makeText(this, "Image has been saved", 7000).show();
    }

    public void open() {
        TextView tv = new TextView(this);
        tv.setText("Hello, Android");
        setContentView(tv);

    }

}

`これがViewクラスです

       public class TouchEventView extends View {
    private static Bitmap mBitmap;

    private Paint paint = new Paint();
    private Path path = new Path();
    private Canvas mCanvas;
    private Path mPath;
    private Paint mBitmapPaint;

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

        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(5f);
        mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);

    }

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

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

        canvas.drawPath(path, paint);
    }

    public void clear() {
        path = new Path();
        invalidate();
    }

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

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);

            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(eventX, eventY);
            break;
        case MotionEvent.ACTION_UP:
            // nothing to do
            break;
        default:
            return false;
        }

        // Schedules a repaint.
        invalidate();
        return true;
    }

    public void saveAsJpg(File f) {
        String fname = f.getAbsolutePath();
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(f);
            // Take the bitmap of the view and write it out as a jpeg.
            mBitmap.compress(CompressFormat.JPEG, 95, fos);

        } catch (Throwable ex) {
            ex.printStackTrace();

        }
    }
}

`コードを編集しました。空白の画面しか保存されません。フィンガーペイント画像が保存されないのはなぜですか?Plzzは私を助けます

4

1 に答える 1

0

を初期化することはありませんtouchview。次の行を挿入します。

TouchEventView touchview = (TouchEventView) findViewById(R.id.mydrawview1);

public void save()、2行目の宣言を削除します。

于 2012-07-09T19:33:14.860 に答える