UFO ET IT

Android : 비트 맵을 오버레이하고 비트 맵 위에 그리는 방법은 무엇입니까?

ufoet 2020. 12. 14. 20:25
반응형

Android : 비트 맵을 오버레이하고 비트 맵 위에 그리는 방법은 무엇입니까?


실제로 세 가지 질문이 있습니다.

  1. 비트 맵에 이미지를 그리거나 리소스로 비트 맵을 만든 다음 비트 맵 위에 그리는 것이 더 낫습니까? 성능면에서 어느 것이 더 낫습니까?
  2. 비트 맵 위에 투명한 것을 그리려면 어떻게해야합니까?
  3. 투명 비트 맵 하나를 다른 비트 맵 위에 오버레이하려면 어떻게해야합니까?

긴 목록에 대해 죄송하지만 학습을 위해 두 가지 접근 방식을 모두 살펴보고 싶습니다.


아직 아무도 대답하지 않았다 니 믿을 수 없습니다! SO에서 드문 발생!

1

이 질문은 제게 이해가되지 않습니다. 그러나 나는 그것을 찔러 줄 것입니다. 캔버스에 직접 그리기 (폴리곤, 음영, 텍스트 등)와 비트 맵을로드하고 그리기의 복잡성에 따라 캔버스에 블리 팅하는 것에 대해 묻는 경우. 도면이 더 복잡해지면 필요한 CPU 시간도 그에 따라 늘어납니다. 그러나 비트 맵을 캔버스에 블리 팅하는 것은 항상 비트 맵의 ​​크기에 비례하는 일정한 시간입니다.

2

"무언가"가 무엇인지 모른 채 어떻게 그 방법을 보여줄 수 있습니까? # 3에 대한 답에서 # 2를 알아낼 수 있어야합니다.

가정 :

  • bmp1이 bmp2보다 큽니다.
  • 둘 다 왼쪽 상단 모서리에서 오버레이되기를 원합니다.

        private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
            Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
            Canvas canvas = new Canvas(bmOverlay);
            canvas.drawBitmap(bmp1, new Matrix(), null);
            canvas.drawBitmap(bmp2, new Matrix(), null);
            return bmOverlay;
        }
    

다음과 같이 할 수 있습니다.

public void putOverlay(Bitmap bitmap, Bitmap overlay) {
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(overlay, 0, 0, paint);
} 

아이디어는 매우 간단합니다. 비트 맵을 캔버스와 연결하면 캔버스의 모든 메서드를 호출하여 비트 맵 위에 그릴 수 있습니다.

이것은 투명도가있는 비트 맵에서 작동합니다. 비트 맵에 알파 채널이있는 경우 투명도가 있습니다. Bitmap.Config보십시오 . ARGB_8888을 사용하고 싶을 것입니다.

중요 : 그리기를 수행 할 수있는 다양한 방법 Android 샘플을 참조하십시오. 그것은 당신을 많이 도울 것입니다.

성능 측면에서 (정확히 메모리 측면에서) 비트 맵은 기본 비트 맵을 단순히 래핑하기 때문에 사용하기에 가장 좋은 개체입니다. ImageView는 View의 하위 클래스이며 BitmapDrawable은 내부에 Bitmap을 포함하지만 다른 많은 항목도 포함합니다. 그러나 이것은 지나치게 단순화 된 것입니다. 정확한 답변을 위해 성능 별 시나리오를 제안 할 수 있습니다.


public static Bitmap overlayBitmapToCenter(Bitmap bitmap1, Bitmap bitmap2) {
    int bitmap1Width = bitmap1.getWidth();
    int bitmap1Height = bitmap1.getHeight();
    int bitmap2Width = bitmap2.getWidth();
    int bitmap2Height = bitmap2.getHeight();

    float marginLeft = (float) (bitmap1Width * 0.5 - bitmap2Width * 0.5);
    float marginTop = (float) (bitmap1Height * 0.5 - bitmap2Height * 0.5);

    Bitmap overlayBitmap = Bitmap.createBitmap(bitmap1Width, bitmap1Height, bitmap1.getConfig());
    Canvas canvas = new Canvas(overlayBitmap);
    canvas.drawBitmap(bitmap1, new Matrix(), null);
    canvas.drawBitmap(bitmap2, marginLeft, marginTop, null);
    return overlayBitmap;
}

If the purpose is to obtain a bitmap, this is very simple:

Canvas canvas = new Canvas();
canvas.setBitmap(image);
canvas.drawBitmap(image2, new Matrix(), null);

In the end, image will contain the overlap of image and image2.


I think this example will definitely help you overlay a transparent image on top of another image. This is made possible by drawing both the images on canvas and returning a bitmap image.

Read more or download demo here

private Bitmap createSingleImageFromMultipleImages(Bitmap firstImage, Bitmap secondImage){

        Bitmap result = Bitmap.createBitmap(firstImage.getWidth(), firstImage.getHeight(), firstImage.getConfig());
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(firstImage, 0f, 0f, null);
        canvas.drawBitmap(secondImage, 10, 10, null);
        return result;
    }

and call the above function on button click and pass the two images to our function as shown below

public void buttonMerge(View view) {

        Bitmap bigImage = BitmapFactory.decodeResource(getResources(), R.drawable.img1);
        Bitmap smallImage = BitmapFactory.decodeResource(getResources(), R.drawable.img2);
        Bitmap mergedImages = createSingleImageFromMultipleImages(bigImage, smallImage);

        img.setImageBitmap(mergedImages);
    }

For more than two images, you can follow this link, how to merge multiple images programmatically on android


public static Bitmap createSingleImageFromMultipleImages(Bitmap firstImage, Bitmap secondImage, ImageView secondImageView){

    Bitmap result = Bitmap.createBitmap(firstImage.getWidth(), firstImage.getHeight(), firstImage.getConfig());
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(firstImage, 0f, 0f, null);
    canvas.drawBitmap(secondImage, secondImageView.getX(), secondImageView.getY(), null);

    return result;
}

참고URL : https://stackoverflow.com/questions/1540272/android-how-to-overlay-a-bitmap-and-draw-over-a-bitmap

반응형