在另一个具有相同宽度和高度的位图上绘制位图 [英] Draw Bitmap on another Bitmap with same width and height

查看:83
本文介绍了在另一个具有相同宽度和高度的位图上绘制位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在另一个Bitmap上覆盖一个Bitmap.可以说,我想在另一个bitmap上绘制一个Bitmap.

假设我有一个Bitmap为:

我想在此位图保持冷静并检查工作"上绘制另一个bitmap!这将是位图1

我要放置在此bitmap上的Bitmap就像是一个效果叠加层,如下所示:

这将是位图2


问题

我想将位图2 叠加到位图1 上,说位图2 应该位于位图1 上方./p>

位图2 应该获得位图1 的宽度和高度,以覆盖所有内容.

位图1 可以具有任意宽度和高度,但是,我希望位图2 应该具有位图1 的宽度和高度. >无论如何.

我的代码问题是裁剪 位图1 ,然后在其上覆盖位图2 .我的意思是代码正在获取位图2 的宽度和高度!


我做了什么

public Bitmap overlay123(Bitmap bmp1, Bitmap bmp2) {
            Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2.getHeight(), bmp1.getConfig());
            float left =(bmp2.getWidth() - (bmp1.getWidth()*((float)bmp2.getHeight()/(float)bmp1.getHeight())))/(float)2.0;
            float bmp1newW = bmp1.getWidth()*((float)bmp2.getHeight()/(float)bmp1.getHeight());
            Bitmap bmp1new = getResizedBitmap(bmp1, bmp2.getHeight(), (int)bmp1newW);
            Canvas canvas = new Canvas(bmOverlay);
            canvas.drawBitmap(bmp1new, left ,0 , null);
            canvas.drawBitmap(bmp2, new Matrix(), null);
            return bmOverlay;
        }

        public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
            int width = bm.getWidth();
            int height = bm.getHeight();
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
            return resizedBitmap;
}


上述代码的问题是它会裁剪或调整位图1 的宽度和高度!


尝试2

private Bitmap overlayer(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;
}


代码存在的问题是,它总是在较小的左上角显示位图2


我只需要知道如何将位图2 放置到位图1 上,使宽度和高度与位图1 相同./p>

我是位图代码的新手,所以我对此一无所知

谢谢你!

最终图像应如下所示:

解决方案

MainActivity 输出.

public class MainActivity extends Activity {
    private ImageView Img;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);
        Img = (ImageView) findViewById(R.id.imageView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.top);
                Bitmap bmt = BitmapFactory.decodeResource(getResources(), R.drawable.back);
                Img.setImageBitmap(overlayer(bm, bmt));

            }
        });
    }
    private Bitmap overlayer(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;
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

输出:

输出结果请参见此处.

there I want to overlay an Bitmap onto my another Bitmap. You can say, I want to draw a Bitmap on another bitmap.

Suppose that I have a Bitmap as:

And I want to draw another bitmap onto this bitmap "KEEP CALM AND CHECK YOUR WORK"! This will be Bitmap 1

The Bitmap I want to place on this bitmap is like an effect overlay as:

This will be Bitmap 2


The Problem

I want to overlay Bitmap 2 onto Bitmap 1 say Bitmap 2 should come above Bitmap 1.

The Bitmap 2 Should get the width and height of the Bitmap 1 to cover it all.

The Bitmap 1 will be of any width and height but, I want the Bitmap 2 should get the width and height of the Bitmap 1 in any case.

My code problem is that it crops the Bitmap 1 and overlay Bitmap 2 onto it then. I mean the code is getting the Bitmap 2 width and height!


What I have done

public Bitmap overlay123(Bitmap bmp1, Bitmap bmp2) {
            Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2.getHeight(), bmp1.getConfig());
            float left =(bmp2.getWidth() - (bmp1.getWidth()*((float)bmp2.getHeight()/(float)bmp1.getHeight())))/(float)2.0;
            float bmp1newW = bmp1.getWidth()*((float)bmp2.getHeight()/(float)bmp1.getHeight());
            Bitmap bmp1new = getResizedBitmap(bmp1, bmp2.getHeight(), (int)bmp1newW);
            Canvas canvas = new Canvas(bmOverlay);
            canvas.drawBitmap(bmp1new, left ,0 , null);
            canvas.drawBitmap(bmp2, new Matrix(), null);
            return bmOverlay;
        }

        public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
            int width = bm.getWidth();
            int height = bm.getHeight();
            float scaleWidth = ((float) newWidth) / width;
            float scaleHeight = ((float) newHeight) / height;
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
            return resizedBitmap;
}


Problem with the above code is that it crops or resizes bitmap 1 width and height!


Try 2

private Bitmap overlayer(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;
}


Problem with code is that It always show the Bitmap 2 in the top left corner with a smaller size!


I just need to know how can I place the Bitmap 2 onto Bitmap 1 getting width and height same as of Bitmap 1.

I am newbie to Bitmap Codes so I have no idea about it

Thanks in advace!

The final image should look like:

解决方案

MainActivity Output.

public class MainActivity extends Activity {
    private ImageView Img;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);
        Img = (ImageView) findViewById(R.id.imageView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.top);
                Bitmap bmt = BitmapFactory.decodeResource(getResources(), R.drawable.back);
                Img.setImageBitmap(overlayer(bm, bmt));

            }
        });
    }
    private Bitmap overlayer(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;
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Output:

Output result see here.

这篇关于在另一个具有相同宽度和高度的位图上绘制位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆