1

您好,我有两张图片,我正在使用画布合并这些图片

这是下面的代码

      Intent intent = getIntent();

       //bm is the image get from camera
        bm = BitmapFactory.decodeFile(intent.getStringExtra("getdata"));

       //this is simple white text image
        bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.txt_made_hawk_nelson);
        imgSeletedPhoto = (ImageView) findViewById(R.id.imgSelectedPhoto);

        int width = bm.getWidth();
        int height = bm.getHeight();

        int maxWidth = (width > bm1.getWidth() ? width : bm1.getWidth());
        int maxHeight = (height > bm1.getHeight() ? height : bm1.getHeight());


        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) maxWidth) / width;
        float scaleHeight = ((float) maxHeight) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);          
    bmOverlay = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);

    Canvas canvas = new Canvas(bmOverlay);
    //canvas.drawBitmap(bm, 0, 0, null);
    canvas.drawBitmap(bm1, 0, 50, null);
    imgSeletedPhoto.setImageBitmap(bmOverlay);
    } catch (Exception e) {
        e.printStackTrace();
    }

我想缩放图像,图像应该如下所示

在此处输入图像描述

但不是通过上面的代码,它看起来像

在此处输入图像描述

这个文件的 XML 代码是

         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="10" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="7" >   // this is covering 70% of screen

    <ImageView
        android:id="@+id/imgSelectedPhoto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="3"
    android:background="#baca9d"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

任何人都可以帮助我如何拉伸图像并使其从底部和顶部充满并将文本放在中心

4

1 回答 1

5
int targetWidth  = 70% of screen width;
int targetHeight = 70% of screen height;

int saclex = (int)((float)targetWidth / (float)bmp1.getWidth());
int sacley = (int)((float)targetHeight / (float)bmp1.getHeight());

将比例值应用于 bmp1 并创建缩放位图

saclex = (int)((float)targetWidth / (float)bmp.getWidth());
sacley = (int)((float)targetHeight / (float)bmp.getHeight());

应用比例值 bmp 并创建缩放位图。希望这会有所帮助。否则您可以使用此方法创建它。

Bitmap bitmap = Bitmap.createScaledBitmap(mTargetImage, bWidth,
                bHeight, false);
于 2013-04-03T10:31:43.047 回答