Draw bitmap in specified XY positions in the Canvas

不想你离开。 提交于 2020-12-13 06:33:39

问题


I am trying to overlay a image in a specific position in the video so that i can combine both.But could not place the image to exact position selected.its moving somewhere else the final output. I am using this library to combine image and video https://github.com/MasayukiSuda/Mp4Composer-android (out of box Any other library suggestions)In this library i can set the image in 4 different positions:

 public enum Position {
    LEFT_TOP,
    LEFT_BOTTOM,
    RIGHT_TOP,
    RIGHT_BOTTOM
}
mp4Composer = new Mp4Composer(inputVideoPath, videoPath)
.filter(new WatermarkFilter(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher_sample),WatermarkFilter.Position.LEFT_TOP))

In the Processing this happens:

       case LEFT_TOP:
                canvas.drawBitmap(bitmap,0,0,null);
                break;
       case LEFT_BOTTOM:
                canvas.drawBitmap(bitmap, 0, canvas.getHeight() - bitmap.getHeight(), null);
                break;

What i did i slightly modified my code to set anywhere in the canvas by getting the X Y position of the bitmap using MotionEvent

   protected boolean onTouchDown(@NonNull MotionEvent event) {
currentMode = ActionMode.DRAG;

downX = event.getX();
downY = event.getY();

midPoint = calculateMidPoint();
oldDistance = calculateDistance(midPoint.x, midPoint.y, downX, downY);
oldRotation = calculateRotation(midPoint.x, midPoint.y, downX, downY);

currentIcon = findCurrentIconTouched();
if (currentIcon != null) {
  currentMode = ActionMode.ICON;
  currentIcon.onActionDown(this, event);
} else {
  handlingSticker = findHandlingSticker();
}

if (handlingSticker != null) {
  downMatrix.set(handlingSticker.getMatrix());
  if (bringToFrontCurrentSticker) {
    stickers.remove(handlingSticker);
    stickers.add(handlingSticker);
  }
  if (onStickerOperationListener != null){
    onStickerOperationListener.onStickerTouchedDown(handlingSticker);
  }
}

if (currentIcon == null && handlingSticker == null) {
  return false;
}
invalidate();
return true;
}

public float[] getMappedBoundPoints() {
float[] dst = new float[8];
getMappedPoints(dst, getBoundPoints());
return dst;
}

By this Method i get my X Y points

float X = sticker.getMappedBoundPoints()[0];
float Y = sticker.getMappedBoundPoints()[1];
Log.d(TAG, "SavedVideo: " + "X" + X + "?/" + "Y" + Y);

And Finally Passing my X Y to the Mp4Composer method:

  .filter(new WatermarkFilter(bitmap,X,Y))

on the other side

   canvas.drawBitmap(bitmap,X,Y,null);

Can anyone help out or point out where the mistake is ! Thanks

来源:https://stackoverflow.com/questions/61998318/draw-bitmap-in-specified-xy-positions-in-the-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!