0
我試圖執行縮放以放大具有路徑的自定義視圖。路徑應該相應地縮放。如何在縮放時正確縮放和Android路徑
以下代碼正常工作,直到放大水平達到一個臨界點,其中應用崩潰,並顯示:
A/libc的:致命信號11(SIGSEGV),碼2,在TID 25828故障地址0xb88799c0( RenderThread)
而且,粉碎之前它顯示:
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;
}
}
而不是縮放路徑我試圖縮放畫布。這工作得很好,但路徑線在放大時會變形,這不是理想的行爲。