gpt4 book ai didi

java - 使用 Canvas android拉伸(stretch)图像

转载 作者:行者123 更新时间:2023-11-29 07:58:53 25 4
gpt4 key购买 nike

你好,我有两张图片,我正在使用 Canvas 合并这些图片

下面是代码

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

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

enter image description here

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

enter image description here

这个文件的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" >

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

最佳答案

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);

关于java - 使用 Canvas android拉伸(stretch)图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15784174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com