重新绘制自定义视图一段时间后,应用程序冻结或软锁定

时间:2019-04-22 18:39:49

标签: android android-custom-view

我写了一个自定义视图来绘制图形,并强制每20毫秒使用一次无效的图形进行重绘。有时,应用程序有时会停止对输入的响应,这非常不稳定。图形将继续绘制,并且在按下按钮时将切换为按下状态,但是它们不会触发onclick方法。此时,如果我退出该应用程序,除非将其杀死,否则将无法再次打开它。

我猜想这是由于重绘所致,因为将重绘计时器减少到每100ms可以使该问题消失(至少从我的测试中消失),但使其更快(10ms)则使其更加一致。

我似乎无法在网上找到有关该产品的任何信息,并且该应用程序也没有崩溃,因此我在Android Studio中没有出现任何错误

开奖代码

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(data.size() == 0) {
            DrawNoData(canvas);
            return;
        }
        int width = getWidth();
        int height = getHeight();
        int elWidth = (width - offset * 2) / data.size();
        int outline = 2;
        Iterator<Point> iterator = data.iterator();
        int i = 0;
        DecimalFormat df = new DecimalFormat("#0.#");
        textPaint.setTextAlign(Paint.Align.CENTER);
        if(data.size()>20) {
            textPaint.setTextSize(20);
        } else {
            textPaint.setTextSize(40);
        }
        while(iterator.hasNext()){
            Point next = iterator.next();
            int penX = offset + i * elWidth + elWidth/2;
            int penY = lerp(offset, height - offset, maxY == 0? 1 : 1 - next.y / maxY);
            canvas.drawRect(penX - elWidth/3, penY, penX + elWidth/3, height - offset, outlinePaint);
            canvas.drawRect(penX - elWidth/3 + outline, penY + outline, penX + elWidth/3 - outline, height - offset - outline, paint);

            //Draw text
            Rect bounds = new Rect();
            String text = df.format(next.x);
            textPaint.getTextBounds(text, 0, text.length(), bounds);
            int yPos = height - offset + bounds.height() * 2;
            canvas.drawText(text, penX, yPos, textPaint);
            i++;
        }
        DrawAxis(canvas);
        lerpData();
    }

    private void DrawNoData(Canvas canvas) {
        DrawAxis(canvas);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(50);
        int xPos = (getWidth() / 2);
        int yPos = (int) ((getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ;
        canvas.drawText("Not enough data", xPos, yPos, textPaint);
    }

    private void DrawAxis(Canvas canvas) {

        DecimalFormat df = new DecimalFormat("#0.#");
        int nbLines = (int)(maxY / div);
        textPaint.setTextAlign(Paint.Align.RIGHT);
        if(nbLines>20) {
            textPaint.setTextSize(20);
        } else {
            textPaint.setTextSize(40);
        }
        for(int i = 0; i < nbLines; i++){
            Rect bounds = new Rect();
            String text = df.format(maxY - i * div);
            textPaint.getTextBounds(text, 0, text.length(), bounds);
            int xPos = offset - 20;
            int yPos = offset + (getHeight() - 2*offset) * i/nbLines - 5 ; // - bounds.height()
            canvas.drawText(text, xPos, yPos, textPaint);
            canvas.drawLine(0, offset + (getHeight() - 2*offset) * i/nbLines , getWidth(), offset + (getHeight() - 2*offset) * i/nbLines, gridPaint);
        }
        //canvas.drawLine(0, 0, getWidth(), 0, gridPaint);
        //canvas.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1, gridPaint);

        //Draw axis
        canvas.drawLine(offset, offset, offset, getHeight() - offset, axisPaint);
        canvas.drawLine(offset, getHeight() - offset, getWidth() - offset, getHeight() - offset, axisPaint);

        //Draw Y axis arrows
        Path path = new Path();
        path.moveTo(offset, offset);
        path.lineTo(offset + 10, offset);
        path.lineTo(offset, offset - 20);;
        path.lineTo(offset - 10, offset);
        path.lineTo(offset, offset);
        path.close();
        canvas.drawPath(path, axisPaint);

        //Draw X axis arrows
        path = new Path();
        path.moveTo(getWidth() - offset, getHeight() - offset);
        path.lineTo(getWidth() - offset, getHeight() - offset + 10);
        path.lineTo(getWidth() - offset + 20, getHeight() - offset);;
        path.lineTo(getWidth() - offset, getHeight() - offset - 10);
        path.lineTo(getWidth() - offset, getHeight() - offset);
        path.close();
        canvas.drawPath(path, axisPaint);
    }

初始化代码

private void init(@Nullable AttributeSet attrs) {
        data = new ArrayList<>();
        target = new ArrayList<>();
        paint = new Paint();
        paint.setColor(Color.BLUE);
        outlinePaint = new Paint();
        outlinePaint.setColor(Color.BLACK);
        gridPaint = new Paint();
        gridPaint.setColor(Color.LTGRAY);
        axisPaint = new Paint();
        axisPaint.setColor(Color.DKGRAY);
        textPaint = new Paint();
        textPaint.setColor(Color.GRAY);
        tick = false;
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask(){
            @Override
            public void run(){
                invalidate();
            }
        },0,10);
    }

Here is a video of it happening

0 个答案:

没有答案