0

嘿,我想通过保存菜单将用户制作的图像保存在 fingerpaint API 中。
这里是活动课`

    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);

    }

}

` 这是视图类

       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();

        }
    }
}

` 我已经编辑了我的代码。它只保存了一个空白屏幕。为什么没有保存指纹图像?请帮帮我

4

1 回答 1

0

您永远不会初始化touchview. 插入这一行:

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

public void save()并删除第二行的声明。

于 2012-07-09T19:33:14.860 回答