title: Stretched image in canvas when background is set in android
body: <p>Ok so i was finally able to get user's signature over an image .
  And through all i was able to learn from canvas and saving an image + user drawings , i knew that i had to set the canvas background like so :</p>
  <pre><code>mDrawLayout.setBackground(d); 
  </code></pre>
  <p>But my issue is : image gets really really stretched from the original one , here s the original image :</p>
  <p><a href="https://i.stack.imgur.com/2yWJ8.jpg" rel="nofollow noreferrer">original</a></p>
  <p>heres the out put on the canvas by setting its background :
  <a href="https://i.stack.imgur.com/Vd2iq.jpg" rel="nofollow noreferrer">output</a></p>
  <p>heres my mDrawLayout class :</p>
  <pre><code>public class DrawingView extends View {
  public int width;
  public  int height;
  static Path drawPath;
  private Paint drawPaint, canvasPaint;
  static int paintColor = 0xFFFF0000;
  private  float STROKE_WIDTH = 5f;
  private Canvas drawCanvas;
  private Bitmap canvasBitmap;
  private boolean erase=false;
  
  
  public DrawingView(Context context, AttributeSet attrs){
      super(context, attrs);
      setupDrawing();
      setErase(erase);
  }
  private void setupDrawing(){
      drawPath = new Path();
      drawPaint = new Paint();
      drawPaint.setColor(paintColor);
      drawPaint.setAntiAlias(true);
      drawPaint.setStrokeWidth(STROKE_WIDTH);
      drawPaint.setStyle(Paint.Style.STROKE);
      drawPaint.setStrokeJoin(Paint.Join.ROUND);
      drawPaint.setStrokeCap(Paint.Cap.ROUND);
      canvasPaint = new Paint(Paint.DITHER_FLAG);
  }
  public void clearDrawing()
  {
  
      setDrawingCacheEnabled(false);
  
  
      onSizeChanged(width, height, width, height);
      invalidate();
  
      setDrawingCacheEnabled(true);
  }
  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
      super.onSizeChanged(w, h, oldw, oldh);
      width = w;
      height = h;
      canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
      drawCanvas = new Canvas(canvasBitmap);
  }
  
  public void setErase(boolean isErase){
      erase=isErase;
      drawPaint = new Paint();
      if(erase) {
          setupDrawing();
          int srcColor= 0x00000000;
  
          PorterDuff.Mode mode = PorterDuff.Mode.CLEAR;
          PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(srcColor, mode);
  
          drawPaint.setColorFilter(porterDuffColorFilter);
  
          drawPaint.setColor(srcColor);
          drawPaint.setXfermode(new PorterDuffXfermode(mode));
  
      }
      else {
  
          setupDrawing();
  
      }
  }
  
  //************************************   draw view  *************************************************************
  
  @Override
  protected void onDraw(Canvas canvas) {
      canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
      canvas.drawPath(drawPath, drawPaint);
  }
  
  //***************************   respond to touch interaction   **************************************************
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
  
      canvasPaint.setColor(paintColor);
      float touchX = event.getX();
      float touchY = event.getY();
      //respond to down, move and up events
  
      switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
              drawPath.moveTo(touchX, touchY);
              break;
          case MotionEvent.ACTION_MOVE:
              drawCanvas.drawPath(drawPath, drawPaint);
              drawPath.lineTo(touchX, touchY);
              break;
          case MotionEvent.ACTION_UP:
              drawPath.lineTo(touchX, touchY);
              drawCanvas.drawPath(drawPath, drawPaint);
              drawPath.reset();
              break;
          default:
              return false;
      }
      //redraw
      invalidate();
      return true;
  }
  
  //***********************************   return current alpha   ***********************************************
  public int getPaintAlpha(){
      return Math.round((float)STROKE_WIDTH/255*100);
  }
  
  //**************************************  set alpha   ******************************************************
  public void setPaintAlpha(int newAlpha){
      STROKE_WIDTH=Math.round((float)newAlpha/100*255);
      drawPaint.setStrokeWidth(newAlpha);
  }
  </code></pre>
  <p>heres my canvas class :</p>
  <pre><code>public class SignatureActivity extends AppCompatActivity {
  SeekBar mThickness;
  private AnexoTemporario imagePath;
  private DrawingView mDrawLayout;
  Button erase, draw;
  private Paint drawPaint = new Paint();
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_signature);
      mThickness = findViewById(R.id.thickness);
      mDrawLayout = findViewById(R.id.viewDraw);
      erase = findViewById(R.id.erase);
      draw= findViewById(R.id.draw);
      imagePath = (AnexoTemporario) getIntent().getSerializableExtra(&quot;IMAGEURL&quot;);
      File file = new File(imagePath.getFilePath());
      Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
  
      Drawable d = new BitmapDrawable(getResources(), bitmap);
      mDrawLayout.setVisibility(View.VISIBLE);
      mDrawLayout.setDrawingCacheEnabled(true);
      mDrawLayout.setEnabled(true);
  
  
      mDrawLayout.setBackground(d);
      mDrawLayout.setRotation(90);
  
  
  
  
      mThickness.setMax(50);
      mThickness.setProgress(10);
      mDrawLayout.setPaintAlpha(mThickness.getProgress());
      int currLevel = mDrawLayout.getPaintAlpha();
      mThickness.setProgress(currLevel);
      mDrawLayout.invalidate();
      erase.setOnClickListener(v -&gt; {
          saveDrawing();
          drawPaint.setColor(Color.TRANSPARENT);
          mDrawLayout.setErase(true);
  
      });
  
      draw.setOnClickListener(v -&gt; mDrawLayout.clearDrawing());
  
      mThickness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  
          @Override
          public void onStopTrackingTouch(SeekBar seekBar) {
          }
  
          @Override
          public void onStartTrackingTouch(SeekBar seekBar) {
          }
  
          @Override
          public void onProgressChanged(SeekBar seekBar, int progress,
                                        boolean fromUser) {
              mDrawLayout.setPaintAlpha(mThickness.getProgress());
          }
      });
  }
  public void saveDrawing()
  {
      Bitmap whatTheUserDrewBitmap = mDrawLayout.getDrawingCache();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      whatTheUserDrewBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
  
      GetImageTask task = new GetImageTask(whatTheUserDrewBitmap, base64string -&gt; {
  
          try {
              WriteSDCard writeSDCard = new WriteSDCard(this);
              writeSDCard.writeToSDFile(&quot;assinatura.png&quot;, base64string.getBytes());
  
              Toast.makeText(this, &quot;Assinatura Salva!&quot;, Toast.LENGTH_LONG).show();
  
              Intent intent = new Intent();
              intent.putExtra(&quot;ASSINATURA&quot;, &quot;assinatura.png&quot;);
              setResult(Activity.RESULT_OK, intent);
              finish();
          } catch (Exception e) {
              //
              e.printStackTrace();
              Toast.makeText(this, &quot;Erro ao tentar salvar assinatura!&quot;, Toast.LENGTH_LONG).show();
          }
      });
      task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  
  }
  </code></pre>
  <p>xml file :</p>
  <pre><code>&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  android:layout_width=&quot;match_parent&quot;
  android:layout_height=&quot;match_parent&quot;
  tools:context=&quot;.canvas.SignatureActivity&quot; &gt;
  
  &lt;mobi.stos.gwmobile.canvas.DrawingView
      android:id=&quot;@+id/viewDraw&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;0dp&quot;
      android:scaleType=&quot;centerInside&quot;
      app:layout_constraintBottom_toTopOf=&quot;@+id/ll&quot;
      app:layout_constraintEnd_toEndOf=&quot;parent&quot;
      app:layout_constraintStart_toStartOf=&quot;parent&quot;
      app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  
  
  &lt;LinearLayout
      android:id=&quot;@+id/linearLayout2&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:orientation=&quot;horizontal&quot;
      android:weightSum=&quot;2&quot;
      app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
      app:layout_constraintEnd_toEndOf=&quot;parent&quot;
      app:layout_constraintStart_toStartOf=&quot;parent&quot;&gt;
  
  
      &lt;Button
          android:id=&quot;@+id/erase&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_below=&quot;@+id/thickness&quot;
          android:layout_weight=&quot;1&quot;
          android:text=&quot;SALVAR&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
  
      &lt;Button
          android:id=&quot;@+id/draw&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_below=&quot;@+id/erase&quot;
          android:layout_weight=&quot;1&quot;
          android:text=&quot;APAGAR&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot; /&gt;
  
  &lt;/LinearLayout&gt;
  
  &lt;LinearLayout
      android:id=&quot;@+id/ll&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:orientation=&quot;vertical&quot;
      app:layout_constraintBottom_toTopOf=&quot;@+id/linearLayout2&quot;
      app:layout_constraintEnd_toEndOf=&quot;parent&quot;
      app:layout_constraintStart_toStartOf=&quot;parent&quot;&gt;
  
      &lt;TextView
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginStart=&quot;8dp&quot;
          android:layout_marginBottom=&quot;10dp&quot;
          android:text=&quot;Tamanho da linha&quot; /&gt;
  
      &lt;SeekBar
          android:id=&quot;@+id/thickness&quot;
          android:layout_width=&quot;fill_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_below=&quot;@+id/viewDraw&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/viewDraw&quot; /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  <p>&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;</p>