android:合并文本和图像时更改了文本大小

android: text size changed while combining text and image

我有 textView (mTextOnImage) 和 imageView (mImageView)。
我使用函数 combineImages 组合它们,但是当我组合时,文本大小发生了变化。

//generate bitmap of textView by using getDrawingCache()
Bitmap bmp = Bitmap.createBitmap(mTextOnImage.getDrawingCache());

//getting image as bitmap from image view ( to use as background to combine )
BitmapDrawable drawable = (BitmapDrawable) mImageView.getDrawable();
Bitmap bitmapBackground = drawable.getBitmap();

//combining two bitmaps
Bitmap combined = combineImages(bitmapBackground, bmp);

这是combineImages函数

 public Bitmap combineImages(Bitmap background, Bitmap foreground) {


        Bitmap cs;
        cs = Bitmap.createBitmap(background.getWidth(), background.getHeight(), Bitmap.Config.ARGB_8888);

        //creating canvas by background image's width and height
        Canvas comboImage = new Canvas(cs);
        background = Bitmap.createScaledBitmap(background, background.getWidth(), background.getHeight(), true);

        //Drawing background to canvas
        comboImage.drawBitmap(background, 0, 0, null);

        //Drawing foreground (text) to canvas            
        comboImage.drawBitmap(foreground, mTextOnImage.getLeft(),mTextOnImage.getTop(), null);

        return cs;
    }

位图合并成功,但文字大小发生变化。
这就是我设置文字大小

的方式
mTextOnImage.setTextSize(getResources().getDimensionPixelSize(R.dimen.myFontSize));

在字符串资源中,

<resources>
    <dimen name="myFontSize">40sp</dimen>
</resources>

我从设备图库中获取背景图像,因此分辨率(图像尺寸)可能不同。
有没有我错过的计算?

另外,textView(mTextOnImage)是可拖动的,所以我也想在结合这两者时正确设置位置。

看看您的布局 XML 和几张图片会很有帮助。缺少这些,我建议您检查以确保您的图像在显示时没有以某种方式调整大小。

更新: 在查看较长的解决方案之前,请尝试更改设置文本大小的方式。默认值为 "sp" 而您使用的是 "px".

mTextOnImage.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.myFontSize))

如果这不起作用,请尝试以下操作:

我采用了您的代码并进行了一些更改以尝试重现该问题。在布局中,我显示了一个文本视图(高度和宽度 = wrap_content)和一个未调整大小的图像。在这两个视图下方,我显示了组合视图。我将组合视图的文本放置在顶部,背景为白色,以便进行快速比较。这是结果:

这两个 "Hello World!" 在我看来是一样的。这让我相信您的组合图像视图正在拉伸或缩小,并且在此过程中,您的文本正在改变大小,因为它只是图像的一部分。

这是生成上述图像的代码。该图像只是一个 "png" 图形。

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private TextView mTextOnImage;
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextOnImage = findViewById(R.id.textOnImage);
        mImageView = findViewById(R.id.imageView);
        mTextOnImage.post(new Runnable() {
            @Override
            public void run() {
                //generate bitmap of textView by using getDrawingCache()
                mTextOnImage.buildDrawingCache();
                Bitmap bmp = Bitmap.createBitmap(mTextOnImage.getDrawingCache());

//getting image as bitmap from image view ( to use as background to combine )
                mImageView.buildDrawingCache();
                BitmapDrawable drawable = (BitmapDrawable) mImageView.getDrawable();
                Bitmap bitmapBackground = drawable.getBitmap();

//                Bitmap bitmapBackground = mImageView.getDrawingCache();

//combining two bitmaps
                Bitmap combined = combineImages(bitmapBackground, bmp);
                ((ImageView) findViewById(R.id.imageCombined)).setImageBitmap(combined);
            }
        });
    }

    public Bitmap combineImages(Bitmap background, Bitmap foreground) {


        Bitmap cs;
        cs = Bitmap.createBitmap(background.getWidth(), background.getHeight(), Bitmap.Config.ARGB_8888);

        //creating canvas by background image's width and height
        Canvas comboImage = new Canvas(cs);
        background = Bitmap.createScaledBitmap(background, background.getWidth(), background.getHeight(), true);

        //Drawing background to canvas
        comboImage.drawBitmap(background, 0, 0, null);

        //Drawing foreground (text) to canvas
//        comboImage.drawBitmap(foreground, mTextOnImage.getLeft(),mTextOnImage.getTop(), null);
        comboImage.drawBitmap(foreground, (mImageView.getWidth() - foreground.getWidth()) / 2,
                              0, null);

        return cs;
    }
}   

activity_main.xml

<android.support.constraint.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        app:layout_constraintBottom_toTopOf="@+id/textOnImage"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"
        app:srcCompat="@drawable/sky" />

    <TextView
        android:id="@+id/textOnImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:text="Hello World!"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="40sp"
        app:layout_constraintBottom_toTopOf="@+id/imageCombined"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <ImageView
        android:id="@+id/imageCombined"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textOnImage" />

</android.support.constraint.ConstraintLayout>