Android 自定义 View 代码在 Jelly Bean 4.2 中停止正常工作

标签 android android-custom-view android-4.2-jelly-bean

下面的代码是一个自定义 View - 它绘制一个圆,根据比例添加凹口并添加比例文本。这源自 Mind The Robot 关于创建老式温度计的优秀教程。 http://mindtherobot.com/blog/272/android-custom-ui-making-a-vintage-thermometer/

此代码在运行至 Jelly Bean 4.1.2 的设备上运行良好,但在 4.2 上中断。在 4.2 上,数字不再围绕圆圈绘制,但似乎遍布整个屏幕。代码在 Nexus 7 上运行良好,直到它获得 4.2 更新,所以它不可能是设备问题。我已经在运行 4.1.2 的 Nexus S 和运行 4.2 的 Nexus 4 上对其进行了测试,它在 Nexus S 上运行良好,但在 Nexus 4 上运行良好。

不幸的是,作为一个新用户,我无法发布屏幕截图,我将尝试描述它:表盘前半部分的数字显示正确,其余数字分散在屏幕上。

我查看了 4.2 更改日志,但看不到任何会导致这种情况发生的内容。我在网上寻找过类似的问题,但这些似乎都与硬件加速有关 - 我尝试了在 list 中设置硬件加速的各种组合,但没有任何影响。

如果您能就可能导致这种情况发生的原因提供任何意见,我将不胜感激。

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

public class AneroidView extends View {


    // drawing tools
    private RectF rimRect;

    private RectF faceRect;

    private Paint scalePaint;
    private RectF scaleRect;

    private Paint backgroundPaint; 
    // end drawing tools

    private Bitmap background; // holds the cached static part

    private int totalNotches = 130;
    private int incrementPerLargeNotch = 10;
    private int incrementPerSmallNotch = 1;
    private float degreesPerNotch = 360.0f / totalNotches;  

    private int scaleCenterValue = 1000; // the one in the top center (12 o'clock)
    private int scaleMinValue = 935;
    private int scaleMaxValue = 1065;


    public AneroidView(Context context) {
        super(context);
        init(context, null);
    }

    public AneroidView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public AneroidView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        rimRect = new RectF(0.1f, 0.1f, 0.9f, 0.9f);

        float rimSize = 0.02f;
        faceRect = new RectF();
        faceRect.set(rimRect.left + rimSize, rimRect.top + rimSize, 
                 rimRect.right - rimSize, rimRect.bottom - rimSize);        


        scalePaint = new Paint();
        scalePaint.setStyle(Paint.Style.STROKE);
        scalePaint.setColor(Color.rgb(49, 79, 79));
        scalePaint.setStrokeWidth(0.005f);
        scalePaint.setAntiAlias(true);

        scalePaint.setTextSize(0.045f);
        scalePaint.setTypeface(Typeface.SANS_SERIF);
        scalePaint.setTextScaleX(0.8f);
        scalePaint.setTextAlign(Paint.Align.CENTER);        

        // The scale rectangular is located .10 from the outer rim.
        float scalePosition = 0.10f;

        scaleRect = new RectF();
        scaleRect.set(faceRect.left + scalePosition, faceRect.top + scalePosition,
                      faceRect.right - scalePosition, faceRect.bottom - scalePosition);

            }

    private void drawScale(Canvas canvas) {
        // Draw a large notch every large increment, and a small
        // notch every small increment.

        canvas.drawOval(scaleRect, scalePaint);

        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        for (int i = 0; i < totalNotches; ++i) {
            float y1 = scaleRect.top;
            float y2 = y1 - 0.015f;
            float y3 = y1 - 0.025f;

            int value = notchToValue(i);

            if (i % (incrementPerLargeNotch/incrementPerSmallNotch) == 0) {
                if (value >= scaleMinValue && value <= scaleMaxValue) {
                    // draw a nick
                    canvas.drawLine(0.5f, y1, 0.5f, y3, scalePaint);

                    String valueString = Integer.toString(value);
                    // Draw the text 0.15 away from y3 which is the long nick.
                    canvas.drawText(valueString, 0.5f, y3 - 0.015f, scalePaint);
                }
            }
            else{
                if (value >= scaleMinValue && value <= scaleMaxValue) {
                    // draw a nick
                    canvas.drawLine(0.5f, y1, 0.5f, y2, scalePaint);
                }
            }

            canvas.rotate(degreesPerNotch, 0.5f, 0.5f);
        }
        canvas.restore();       
    }

    private int notchToValue(int value) {
        int rawValue = ((value < totalNotches / 2) ? value : (value - totalNotches)) * incrementPerSmallNotch;
        int shiftedValue = rawValue + scaleCenterValue;
        return shiftedValue;
    }


    private void drawBackground(Canvas canvas) {
        if (background != null)         
            canvas.drawBitmap(background, 0, 0, backgroundPaint);
    }

    @Override
    protected void onDraw(Canvas canvas) {      

        drawBackground(canvas);

        float scale = (float) getWidth();       
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.scale(scale, scale); 

        canvas.restore();

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        regenerateBackground();
    }

    private void regenerateBackground() {
        // free the old bitmap
        if (background != null) {
            background.recycle();
        }

        background = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas backgroundCanvas = new Canvas(background);
        float scale = (float) getWidth();       
        backgroundCanvas.scale(scale, scale);

        drawScale(backgroundCanvas);
    }
}

最佳答案

添加scalePaint.setLinearText(true);

效果会更好,但文本间距可能看起来很糟糕。

查看以下主题:

Android 4.2 on Nexus 7: canvas.drawText() not working correctly

Android 4.2.1 wrong character kerning (spacing)

关于Android 自定义 View 代码在 Jelly Bean 4.2 中停止正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14405132/

相关文章:

java - HTTPS 请求到达服务器 android 两次

Android 圆形菜单点赞抓笔记

android - 如何强制Android重新索引手机上的所有照片?

java - 无法读取 Jelly Bean 上的套接字 InputStream

java - 如何以编程方式使图像适合 ImageButton

Android:实现一个VoIP程序

android - 在每次触摸事件时旋转图像

android - 如何自定义 ListView 以使行项目的第一和第二位置具有不同的布局?

android - 无法从代码中找到已声明的 XML 样式

android - Android Jelly Bean 服务中 updateViewLayout 的奇怪行为