Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何找到相对文本大小_Java_Android_Algorithm_Math_Android Canvas - Fatal编程技术网

Java 如何找到相对文本大小

Java 如何找到相对文本大小,java,android,algorithm,math,android-canvas,Java,Android,Algorithm,Math,Android Canvas,我试图在不同大小的图像上绘制文本。图像显示在ImageView中,然后在画布上绘制。虽然画布保留了图像的原始高度和宽度,但图像在ImageView中的显示确实要小得多,因此为画布选择适当的文本大小非常繁琐,因为ImageView会错误地感知文本在图像上的显示方式 为了解决这个问题,我决定使用交叉乘法为画布上的文本找到一个合适的文本大小,它应该像在ImageView上一样出现在画布上。以下是我的尝试: textSize = topTextView.getTextSize(); imageViewA

我试图在不同大小的图像上绘制文本。图像显示在ImageView中,然后在画布上绘制。虽然画布保留了图像的原始高度和宽度,但图像在ImageView中的显示确实要小得多,因此为画布选择适当的文本大小非常繁琐,因为ImageView会错误地感知文本在图像上的显示方式

为了解决这个问题,我决定使用交叉乘法为画布上的文本找到一个合适的文本大小,它应该像在ImageView上一样出现在画布上。以下是我的尝试:

textSize = topTextView.getTextSize();
imageViewArea = ((img.getWidth()) * (img.getHeight()));
canvasArea = ((canvas.getWidth()) * (canvas.getHeight()));
x = (((textSize)/(imageViewArea)) * (canvasArea));
第一行获取ImageView上显示的文本的大小。第2行和第3行计算ImageView和画布的面积。第三行基本上将所有内容放在一起,理想情况下输出一个浮点值,即画布上绘制的文本大小

然而,它并没有接近它应该是什么样子。在小图像上绘制文本时,文本无法理解。在中等大小的图像上绘制时,它又薄又丑。基本上,它与预览中的完全不同

我的假设是:交叉乘法不是可行的方法

有什么建议吗

public Bitmap createMeme(ImageView img){
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable());
        Bitmap bitmap = bitmapDrawable.getBitmap();
        Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

        String topText = topTextView.getText().toString();
        String bottomText = bottomTextView.getText().toString();

        topText = topText.toUpperCase();
        bottomText = bottomText.toUpperCase();

        Canvas canvas = new Canvas(mutableBitmap);

        TextPaint topFillPaint = new TextPaint();
        TextPaint bottomFillPaint = new TextPaint();

        TextPaint topStrokePaint = new TextPaint();
        TextPaint bottomStrokePaint = new TextPaint();

        Typeface typeface = getResources().getFont(R.font.impact);

        textSize = topTextView.getTextSize();
        imageViewArea = ((img.getWidth()) * (img.getHeight()));
        canvasArea = ((canvas.getWidth()) * (canvas.getHeight()));
        val = textSize * sqrt(canvasArea / imageViewArea);
        x = (float)val;

        topFillPaint.setColor(Color.WHITE);
        topFillPaint.setTextSize(x);
        topFillPaint.setTypeface(typeface);

        topStrokePaint.setStyle(Paint.Style.STROKE);
        topStrokePaint.setStrokeWidth(4);
        topStrokePaint.setTextSize(x);
        topStrokePaint.setColor(Color.BLACK);
        topStrokePaint.setTypeface(typeface);

        bottomFillPaint.setColor(Color.WHITE);
        bottomFillPaint.setTextSize(x);
        bottomFillPaint.setTypeface(typeface);

        bottomStrokePaint.setStyle(Paint.Style.STROKE);
        bottomStrokePaint.setStrokeWidth(4);
        bottomStrokePaint.setColor(Color.BLACK);
        bottomStrokePaint.setTextSize(x);
        bottomStrokePaint.setTypeface(typeface);

        StaticLayout topFillLayout = new StaticLayout(topText, topFillPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                0.8f, 0.0f, false);
        StaticLayout topStrokeLayout = new StaticLayout(topText, topStrokePaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                0.8f, 0.0f, false);
        StaticLayout bottomFillLayout = new StaticLayout(bottomText, bottomFillPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                0.8f, 0.0f, false);
        StaticLayout bottomStrokeLayout = new StaticLayout(bottomText, bottomStrokePaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                0.8f, 0.0f, false);


        topFillLayout.draw(canvas);

        topStrokeLayout.draw(canvas);

        canvas.translate(0, canvas.getHeight() - 50);
        bottomFillLayout.draw(canvas);

        bottomStrokeLayout.draw(canvas);

        return mutableBitmap;
    }
输出

所以我上传了4张不同尺寸的图片,看看每个图片的文本大小。以下是我的发现:

274 x 184 topTextSize:0,bottomTextSize:0 3704 x 2469顶部文本大小:237.58底部文本大小:237.58 640 x 480 topTextSize:0,bottomTextSize:0 2560 x 1920顶部文本大小:168底部文本大小:168 对于2和4,位图上仅显示顶部文本

例如,以下是尺寸为274 x 184的图像的值:


您计算的比率是面积比率,而不是长度比率。如果你沿着两边缩放2的图像,你将得到4或1/4的面积比,这取决于你考虑的方向。字体大小应为长度,因此应使用长度比

如果您知道缩放是均匀的,则高度与宽度的缩放相等,只需根据宽度或高度而不是面积计算比率。如果不是,则需要一种文本呈现方法,可以将字体向一个方向拉伸。如果要使用某种保持面积比的平均字体大小,只需使用x=textSize*sqrtcanvasArea/imageViewArea


非主题注释:代码段中有很多不必要的括号。就我个人而言,我认为这使得阅读代码更加困难。因此,我建议删除不必要的括号,除非它们是有意用来表达某种意义的。

评论不用于扩展讨论;这段对话已经结束。
textSize = 168
imageViewArea = 1728000
canvasArea = 50416
x = 0