在ImageView上绘图太慢了

时间:2016-06-13 01:31:17

标签: android android-imageview android-canvas android-image

我想要实现的是简单的ImageEditor,我已经开始创建简单的工具CropTool,如下所示:

public class CropTool implements ITool {
    RectF top, right, bottom, left, crop;
    Paint fillPaint, cropPaint;

    public CropTool() {
        crop = new RectF();
        top = new RectF();
        right = new RectF();
        left = new RectF();
        bottom = new RectF();

        fillPaint = new Paint();
        fillPaint.setStyle(Paint.Style.FILL);
        fillPaint.setARGB(150, 0, 0, 0);

        cropPaint = new Paint();
        cropPaint.setStyle(Paint.Style.STROKE);
        cropPaint.setARGB(255, 255, 99, 71);
    }

    @Override
    public void TouchStart(ImageView context, float x, float y) {
        crop.set(x, y, crop.right, crop.bottom);
    }

    @Override
    public void TouchMove(ImageView context, float x, float y) {
        crop.set(crop.left, crop.top, x, y);
    }

    @Override
    public void TouchUp(ImageView context, float x, float y) {
        crop.set(crop.left, crop.top, x, y);
    }

    @Override
    public void OnDraw(ImageView context, Canvas canvas) {
        if (crop.isEmpty())
            canvas.drawRect(0, 0, context.getWidth(), context.getHeight(), fillPaint);
        else {
            top.set(0, 0, context.getWidth(), crop.top);
            bottom.set(0, crop.bottom, context.getWidth(), context.getBottom());
            left.set(0, crop.top, crop.left, crop.bottom);
            right.set(crop.right, crop.top, context.getRight(), crop.bottom);

            canvas.drawRect(top, fillPaint);
            canvas.drawRect(bottom, fillPaint);
            canvas.drawRect(left, fillPaint);
            canvas.drawRect(right, fillPaint);

            canvas.drawRect(crop, cropPaint);
        }
    }
}

和自定义ImageView

public class ImageEditView extends ImageView {
    private ITool currentTool;

    public ImageEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        currentTool = new CropTool();
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (currentTool != null) currentTool.TouchStart(this, event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                if (currentTool != null) currentTool.TouchMove(this, event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_UP:
                if (currentTool != null) currentTool.TouchUp(this, event.getX(), event.getY());
                break;
        }
        invalidate();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (currentTool != null) currentTool.OnDraw(this, canvas);
    }
}

问题是,绘图太慢了,显示裁剪矩形需要20秒,如何优化,或者在这里做错了,我的方法是正确的还是有更好的方法?< / p>

1 个答案:

答案 0 :(得分:0)

仿真器在与PC竞争资源方面非常缓慢(甚至在Xeon上)!在硬件设备上尝试 - 手机或平板电脑。应该工作得很好。