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

时间:2013-01-18 18:19:08

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

下面的代码是自定义视图 - 它绘制一个圆圈,根据比例添加凹槽并添加缩放文本。这是源于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更改日志,但我看不到会导致这种情况发生的任何事情。我在网上寻找类似的问题,但这些似乎都与硬件加速有关 - 我已尝试过各种组合,在清单中设置硬件加速但没有任何影响。

我真的很感激可能导致这种情况发生的任何意见。

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

3 个答案:

答案 0 :(得分:3)

添加scalePaint.setLinearText(true);

它会更好用,但文字间距看起来可能不好。

请参阅以下主题:

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

Android 4.2.1 wrong character kerning (spacing)

答案 1 :(得分:1)

我设法通过使用scalePaint.setLinearText(true)解决问题,以便在一个地方绘制文本字符,设置textSize&gt; 1.0f绕过字距调整问题,然后使用canvas.scale(float,float)将字体调整到正确的大小。它既丑陋又痛苦,但对我有用。

答案 2 :(得分:1)

这是kerning issue的另一种解决方法。 drawTextOnPath正常运作......

替换它:
//canvas.drawText("Smushed text.", 0.5f, 0.7F, myTextPaint);

用这个:

private Path strightPath; <br>
strightPath = new Path(); <br>
strightPath.moveTo(0.1f, 0.5f);<br>
strightPath.lineTo(0.9f, 0.5f); <br>
canvas.drawTextOnPath("This text is not smushed together.", strightPath, 0.0f, 0.2f, myTextPaint);