【问题标题】:Android how to reset canvas/bitmap to capture a new imageAndroid如何重置画布/位图以捕获新图像
【发布时间】:2012-02-20 11:37:42
【问题描述】:

我正在开发一个应用程序,当用户单击按钮时,我需要将表数据保存为图像。为此,我使用了以下代码:

//save is the button to click
this.save.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        AlertDialog.Builder alert=new AlertDialog.Builder(MyActivity.this);
        alert.setTitle("Save");
        alert.setMessage("Enter a file name");        
        final EditText input=new EditText(MyActivity.this);
        alert.setView(input);         
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                table.setDrawingCacheEnabled(true); 
                Bitmap b=table.getDrawingCache();
                Bitmap combo=Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas=new Canvas(combo);
                canvas.drawBitmap(b, 0f, 175f, null);               
                OutputStream outStream = null;
                String value=input.getText().toString();
                File directory =new File(extStorageDirectory+"/Files/");
                if(!directory.mkdir())
                    directory.mkdir();
                File file = new File(directory, value);
                try {
                    outStream = new FileOutputStream(file);
                    combo.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                    FileOutputStream fOut=openFileOutput("public.dat", Context.MODE_PRIVATE|Context.MODE_APPEND);
                    OutputStreamWriter osw=new OutputStreamWriter(fOut);
                                osw.write(value+"\n");                          
                    osw.flush();
                    osw.close();
                    fOut.flush();
                    fOut.close();
                    outStream.flush();
                    outStream.close();                                              
                } catch (FileNotFoundException e) {                     
                    e.printStackTrace();                    
                } catch (IOException e) {                       
                    e.printStackTrace();                    
                }
            }               
        });

此功能可以毫无问题地保存图像,但是当我更改表格并尝试保存另一个图像时,它会保存第一个图像。我想我需要重置画布或类似的东西,所以我在保存文件后尝试了以下操作:

  canvas.restore();

但它不起作用。但是当我在模拟器上重新启动应用程序时,我可以保存一个新图像。有人可以帮我解决这个问题。

【问题讨论】:

  • table是什么类型的对象?您无需指定,因为它是您解决方案的一部分
  • @IcedD​​ante 这是一个线性布局,其中包含 Tablerow 元素。

标签: android image canvas bitmap reset


【解决方案1】:

我找到了解决问题的方法。我只需要添加

table.destroyDrawingCache();

就在捕获异常之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    相关资源
    最近更新 更多