保存的位图不会显示在其上绘制的线条

时间:2015-09-22 12:29:18

标签: android bitmap android-canvas

我尝试了以下链接 - draw-on-a-canvas-with-bitmap-as-backgroundsaving-bitmap-drawn-on-canvas

第一个为我工作并解决了这个问题,这使得我不仅可以绘制位图图像,还可以使用绘制的线条进行保存。

最近,我搞砸了布局,事情又恢复了原样。尽管经过了数天的努力,但没有解决方案,即它保存了位图图像,但是在图像上绘制的任何内容都没有出现在图像中。相同的代码,但仍然不起作用,我不知道为什么。

以下是我的代码:

class MyView extends View
   {
   public MyView(Context context)
   {
       super(context);
       paint = new Paint();
       paint.setDither(true);
       paint.setStrokeWidth(4);
       paint.setStyle(Paint.Style.STROKE);
       paint.setColor(Color.RED);

       cw = new ContextWrapper(context);
        canvas=new Canvas();
       path = new Path();
       paths = new ArrayList<>();

       opt = new BitmapFactory.Options();
       opt.inMutable = true;
   }

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

       opt = new BitmapFactory.Options();
       opt.inMutable = true;

       bmp = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.fabialeft, opt);
       bmp2 = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
       bmp2 = Bitmap.createBitmap(bmp);
       canvas = new Canvas(bmp2);

   }

   @Override
   protected void onDraw(Canvas canvas) {
       super.onDraw(canvas);
       canvas.drawBitmap(bmp2, 0, 0, paint);
       for(Path p:paths)
           canvas.drawPath(p, paint);
   }

   public void Save() {
       directory =  cw.getDir("final", Context.MODE_PRIVATE);
       file = new File(directory,"fabialeft");
       FileOutputStream fos = null;
       try {
               fos = new FileOutputStream(file);
               bmp2.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                   System.out.println("Saved to --" + file);
                   Toast.makeText(getContext(), file.toString(), Toast.LENGTH_SHORT).show();
               fos.close();
       }catch (FileNotFoundException e)
       {
           System.out.println(e);
           System.out.println("Saved" + file);
           Toast.makeText(getContext(), "FNF EXCEPTION", Toast.LENGTH_SHORT).show();
       }catch (IOException ioe)
       {
           System.out.println("ERROR" + file);Toast.makeText(getContext(), "IO EXCEPTION", Toast.LENGTH_SHORT).show();
       }

   }

   public void Draw() {
       Toast.makeText(getApplicationContext(), "DRAWING...", Toast.LENGTH_SHORT).show();
       path.moveTo(100, 100);
       path.lineTo(300, 300);
       paths.add(path);
       invalidate();
   }

 }

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:id="@+id/relayout1"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Draw"
    android:id="@+id/button2" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您似乎以某种方式混淆了c:\windows\system32\drivers\etc\hosts ajax.googleapis.com 127.0.0.1 bmp变量(您应该使用更好的名字......)。

在这里,您将bmp2定义为画布的位图

bmp2

但在canvas = new Canvas(bmp2); 中你有点试图将画布的位图画到自身上。

onDraw()

我猜你应该在这里使用canvas.drawBitmap(bmp2, 0, 0, paint); 而不是bmp