Android Custom View适用于KitKat,但Lollipop存在问题

时间:2016-06-07 13:16:31

标签: android android-canvas android-custom-view

我正在使用画布绘图创建自定义视图,使用KitKat版本可以,但使用Lollipop版本时,缩放比例编号。 我找不到解决方案,所以任何想法如何解决它!!!

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 phone

1 个答案:

答案 0 :(得分:0)

我运行了你的代码,它在我的设备上运行正常,但有一些错误修复。在其他一些设备上查看。