0

i'm creating a custom view using canvas drawings, the with KitKat version is ok but with Lollipop version the scale numbers are trimmed. I can't find a solution, So any idea how to fix it!!!

public final class MyView extends View {

private static final String TAG = MyView.class.getSimpleName();

private Handler handler;

private RectF rimRect;
private Paint rimPaint;
private Paint rimCirclePaint;

private Paint scalePaint;
private Paint scalePaint2;
private RectF scaleRect;

private Paint backgroundPaint;
// end drawing tools

private Bitmap background; // holds the cached static part

// scale configuration
private static final int totalNicks = 80;
private static final float degreesPerNick = 240.0f / totalNicks;
private static final int centerDegree = 40; // the one in the top center (12 o'clock)

public MyView(Context context) {
    super(context);
    this.startTime = System.currentTimeMillis();
    init();
}

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.startTime = System.currentTimeMillis();
    init();
}

public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.startTime = System.currentTimeMillis();
    init();
}

private void init() {
    handler = new Handler();
    initDrawingTools();
}

private String getTitle() {
    return "My custom gauge";
}


private void initDrawingTools() {
    rimRect = new RectF(0.1f, 0.1f, 0.9f, 0.9f);

    // the linear gradient is a bit skewed for realism
    rimPaint = new Paint();
    rimPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    rimPaint.setColor(Color.rgb(0xD1, 0xD5, 0xDE));

    rimCirclePaint = new Paint();
    rimCirclePaint.setAntiAlias(true);
    rimCirclePaint.setStyle(Paint.Style.STROKE);
    rimCirclePaint.setColor(Color.argb(0x4f, 0x19, 0x15, 0x16));
    rimCirclePaint.setStrokeWidth(0.005f);


    scalePaint = new Paint();
    scalePaint.setStyle(Paint.Style.STROKE);
    scalePaint.setColor(0x9fff3030);
    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);

    scalePaint2 = new Paint();
    scalePaint2.setStyle(Paint.Style.STROKE);
    scalePaint2.setColor(0x9fff3030);
    scalePaint2.setStrokeWidth(0.01f);
    scalePaint2.setAntiAlias(true);

    scaleRect = new RectF();
    scaleRect.set(rimRect.left , rimRect.top ,
            rimRect.right , rimRect.bottom );
    scalePaint.setLinearText(true);

   backgroundPaint = new Paint();
   backgroundPaint.setFilterBitmap(true);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Log.d(TAG, "Width spec: " + MeasureSpec.toString(widthMeasureSpec));
    Log.d(TAG, "Height spec: " + MeasureSpec.toString(heightMeasureSpec));

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    int chosenWidth = chooseDimension(widthMode, widthSize);
    int chosenHeight = chooseDimension(heightMode, heightSize);

    int chosenDimension = Math.min(chosenWidth, chosenHeight);

    setMeasuredDimension(chosenDimension, chosenDimension);
}

private int chooseDimension(int mode, int size) {
    if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) {
        return size;
    } else { // (mode == MeasureSpec.UNSPECIFIED)
        return getPreferredSize();
    }
}

// in case there is no size specified
private int getPreferredSize() {
    return 300;
}

private void drawRim(Canvas canvas) {

    canvas.drawOval(rimRect, rimPaint);
    canvas.drawOval(rimRect, rimCirclePaint);

}

private void drawScale(Canvas canvas) {
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    canvas.rotate(-120, 0.5f, 0.5f);
    for (int i = 0; i <= totalNicks; ++i) {
        if (i % 10 == 0) {
        float y1 = scaleRect.top;
        float y2 = y1 + 0.060f;
        float y3 = y1 - 0.020f;

        canvas.drawLine(0.5f, y1, 0.5f, y3, scalePaint2);
        canvas.drawText(Integer.toString(i), 0.50f, y2 - 0.015f, scalePaint);
        }
        else{
            float y1 = scaleRect.top-0.01f;
            float y3 = y1 - 0.010f;
            canvas.drawLine(0.5f, y1, 0.5f, y3, scalePaint);
        }
        canvas.rotate(degreesPerNick, 0.5f, 0.5f);
    }
    canvas.restore();
}

private void drawBackground(Canvas canvas) {
    if (background == null) {
        Log.w(TAG, "Background not created");
    } else {
        canvas.drawBitmap(background, 0, 0, backgroundPaint);
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    drawBackground(canvas);

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

}
public void setValue(float value){

    handPosition= value;
    invalidate();
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    Log.d(TAG, "Size changed to " + w + "x" + h);

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

    drawRim(backgroundCanvas);

    drawScale(backgroundCanvas);
}
}

Custom view in Emulator
Custom view in Emulator

Custom view in phone
 Custom view in phone

  • Most likely due to screen differences, maybe check a different Emulator, pretty sure it has nothing to do with using an emulator. – Doppie Jun 07 '16 at 13:26
  • Thanks for ur reply, do u have any idea how to make it work on different screens ? – majdeddine Jun 07 '16 at 13:33
  • It seems your text is getting trimmed due to size of your one view. Check it by changing your numbers to something like 23,34 i.e without 0 at end. – Vivek Mishra Jun 07 '16 at 14:01
  • i've tried it on lollipop it didn't work and on kitkat with different screen sizes it worked. So the problem is that it is working on KitKat but not lollipop. Any idea how to fix it !! – majdeddine Jun 08 '16 at 09:52
  • I've found a solution to my problem in [here](http://stackoverflow.com/questions/13974129/android-4-2-1-wrong-character-kerning-spacing) – majdeddine Jun 09 '16 at 14:37

1 Answers1

0

I ran your code and it is working fine on my Device with some error fixing. Check it on some other devices.