0

我正在尝试执行捏合以放大带有路径的自定义视图。路径应相应缩放。

以下代码可以正常工作,直到放大级别达到应用程序崩溃并显示的临界点:

A/libc:致命信号 11 (SIGSEGV),代码 2,tid 25828 (RenderThread) 中的故障地址 0xb88799c0

此外,在粉碎之前它显示:

W/OpenGLRenderer:路径太大而无法渲染成纹理

public class TestView extends View implements ScaleGestureDetector.OnScaleGestureListener {
    private String DEBUG_TAG = "TestView";
    private ScaleGestureDetector mScaleDetector;
    private Path mPath;
    private Paint mPaint;
    private float mScaleFactor = 1.f;
    private float mScaleMax = 2f;
    private float mScaleMin = 0.5f;
    private Matrix mMatrix;
    private boolean mScaling = false;
    private PointF mScaleFocus = new PointF();

    public TestView(Context context) {
        super(context);
        setupDrawing();
    }

    public TestView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupDrawing();
    }

    public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setupDrawing();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TestView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setupDrawing();
    }

    private void setupDrawing(){
        mPath = new Path();

        mPaint = new Paint();
        mPaint.setColor(0xFF660000);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(10);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);

        mMatrix = new Matrix();

        mScaleDetector = new ScaleGestureDetector(getContext(), this);

        mPath = new Path();
        int width = 100;
        int start = 20;
        mPath.moveTo(start,start);
        mPath.lineTo(start + width, start);
        mPath.lineTo(start + width, start + width);
        mPath.lineTo(start,start+width);
        mPath.close();
    }

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

        mPath.transform(mMatrix);
        canvas.drawPath(mPath, mPaint);
        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mScaleDetector.onTouchEvent(event);

        if(mScaling) return true;

        if (event.getPointerCount() > 1){
            return true;
        }

        invalidate();
        return true;
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor = detector.getScaleFactor();
        mScaleFactor = Math.max(mScaleMin, Math.min(mScaleFactor, mScaleMax));
        mScaleFocus.set(detector.getFocusX(), detector.getFocusY());
        Log.d(DEBUG_TAG, "onScale: " + mScaleFactor);
        mMatrix.setScale(mScaleFactor, mScaleFactor, mScaleFocus.x, mScaleFocus.y);
        invalidate();
        return true;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        Log.d(DEBUG_TAG, "onScaleBegin: " + mScaleFactor);
        mScaling = true;
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {
        Log.d(DEBUG_TAG, "onScaleEnd: " + mScaleFactor);
        mScaling = false;
    }
}

我没有缩放路径,而是尝试缩放画布。这工作正常,但放大时路径线会失真,这是不可取的行为。

4

1 回答 1

0

该问题与硬件加速有关。

设置android:hardwareAccelerated="false"活动解决了这个问题。

于 2016-01-13T00:28:19.243 回答