Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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 Android Canvas.drawText(…)在所有屏幕中都同样大_Java_Android_Android Canvas_Text Size - Fatal编程技术网

Java Android Canvas.drawText(…)在所有屏幕中都同样大

Java Android Canvas.drawText(…)在所有屏幕中都同样大,java,android,android-canvas,text-size,Java,Android,Android Canvas,Text Size,在我的项目中,我用canvas.drawText(…)绘制了文本,并用不同的设备对其进行了测试 然而,文本的大小因屏幕而异 我尝试将它与getResources().getDisplayMetrics().density相乘,但它们的大小仍然略有不同 有没有办法让文本在不同的设备上具有相同的大小?嗨,你可以看看这篇文章,我认为它对你有帮助。 @凌驾 已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh){ super.onSizeChanged(w,h,oldw,oldh) 你好,

在我的项目中,我用canvas.drawText(…)绘制了文本,并用不同的设备对其进行了测试

然而,文本的大小因屏幕而异

我尝试将它与
getResources().getDisplayMetrics().density相乘,但它们的大小仍然略有不同


有没有办法让文本在不同的设备上具有相同的大小?

嗨,你可以看看这篇文章,我认为它对你有帮助。 @凌驾 已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh){ super.onSizeChanged(w,h,oldw,oldh)


你好,你可以看看这篇文章,我认为它对你有帮助。 @凌驾 已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh){ super.onSizeChanged(w,h,oldw,oldh)

这是什么

getResources().getDisplayMetrics().density

它的作用是使它在相同的屏幕大小和不同的密度上看起来相似。感谢你的举动

有两种方法可以解决你的问题。 1.尝试使用大型、大型和普通布局。在相应的布局上相应地调整它们,使其看起来均匀

  • 获取画布的屏幕占有率(画布的高度/宽度和右/左/上/下百分比,以便在不同屏幕上均匀调整它们),并将其作为布局参数应用
  • 这是什么

    getResources().getDisplayMetrics().density

    它的作用是使它在相同的屏幕大小和不同的密度上看起来相似。感谢你的举动

    有两种方法可以解决你的问题。 1.尝试使用大型、大型和普通布局。在相应的布局上相应地调整它们,使其看起来均匀

  • 获取画布的屏幕占有率(画布的高度/宽度和右/左/上/下百分比,以便在不同屏幕上均匀调整它们),并将其作为布局参数应用

  • Paint类采用以像素为单位的文本大小。如果要通过它直接绘制文本,则需要在调用Paint.setTextSize(float size)之前处理单位转换。始终使用sp或dp单位,您可能希望为小屏幕、普通屏幕、大屏幕和XLAGE屏幕至少包含单独的dimens.xml资源。

    Paint类采用以像素为单位的文本大小。如果要直接通过它绘制文本,则需要在调用Paint.setTextSize(float size)之前处理单位转换。始终使用sp或dp单位,您可能希望为小屏幕、普通屏幕、大屏幕和xlarge屏幕至少包含单独的dimens.xml资源。

    调用时,文本大小首先由传入的
    Paint
    对象确定,该对象可通过设置。文本大小由
    画布根据画布密度自动缩放y、 可以使用找到

    在画布上绘制的绘制对象上设置文本大小时,使用单位值
    dp
    sp
    并让画布为您处理缩放。

    当然,您可能需要根据屏幕大小指定不同的值:

    调用时,文本大小首先由传入的
    Paint
    对象确定,该对象可以通过设置。文本大小根据画布密度自动缩放
    Canvas
    ,画布密度可以使用找到

    在画布上绘制的绘制对象上设置文本大小时,使用单位值
    dp
    sp
    并让画布为您处理缩放。


    当然,您可能需要根据屏幕大小指定不同的值:

    您是如何定义文本大小的?Android内置的功能可以根据屏幕大小缩放文本,除非您指定不这样做。我使用mPaint.setTextSize(mTextSize)使用“in”表示大小单位您如何定义文本大小?Android内置了功能,可以根据屏幕大小缩放文本,除非您指定不这样做。我使用mPaint.setTextSize(mTextSize)表示大小单位,使用“in”表示大小单位
        // save view size
        mViewWidth = w;
        mViewHeight = h;
    
        // first determine font point size
        adjustTextSize();
        // then determine width scaling
        // this is done in two steps in case the
        // point size change affects the width boundary
        adjustTextScale();
    }
    
    
    void adjustTextSize() {
        mTextPaint.setTextSize(100);
        mTextPaint.setTextScaleX(1.0f);
        Rect bounds = new Rect();
        // ask the paint for the bounding rect if it were to draw this
        // text
        mTextPaint.getTextBounds(mText, 0, mText.length(), bounds);
    
        // get the height that would have been produced
        int h = bounds.bottom - bounds.top;
    
        // make the text text up 70% of the height
        float target = (float)mViewHeight*.7f;
    
        // figure out what textSize setting would create that height
        // of text
        float size  = ((target/h)*100f);
    
        // and set it into the paint
        mTextPaint.setTextSize(size);
    }
    
    
    void adjustTextScale() {
        // do calculation with scale of 1.0 (no scale)
        mTextPaint.setTextScaleX(1.0f);
        Rect bounds = new Rect();
        // ask the paint for the bounding rect if it were to draw this
        // text.
        mTextPaint.getTextBounds(mText, 0, mText.length(), bounds);
    
        // determine the width
        int w = bounds.right - bounds.left;
    
        // calculate the baseline to use so that the
        // entire text is visible including the descenders
        int text_h = bounds.bottom-bounds.top;
        mTextBaseline=bounds.bottom+((mViewHeight-text_h)/2);
    
        // determine how much to scale the width to fit the view
        float xscale = ((float) (mViewWidth-getPaddingLeft()-getPaddingRight())) / w;
    
        // set the scale for the text paint
        mTextPaint.setTextScaleX(xscale);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        // let the ImageButton paint background as normal
        super.onDraw(canvas);
    
        // draw the text
        // position is centered on width
        // and the baseline is calculated to be positioned from the
        // view bottom
        canvas.drawText(mText, mViewWidth/2, mViewHeight-mTextBaseline, mTextPaint);
    }