繁体   English   中英

使用canvas android拉伸图像

[英]stretch image using canvas android

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

这是下面的代码

      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" >

任何人都可以帮助我如何拉伸图像并使它从底部和顶部完全充满,并将文本居中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM