我尝试执行捏合以放大自定义视图,并在其上显示路径。路径应相应缩放。
以下代码正常工作,直到放大级别达到应用崩溃并显示的关键点:
A / libc:致命信号11(SIGSEGV),代码2,故障地址0xb88799c0 in tid 25828(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;
}
}
我尝试缩放画布,而不是缩放路径。这很好用,但是在放大时路径线会变形,这不是理想的行为。
答案 0 :(得分:0)
问题与硬件加速有关。
在活动中设置android:hardwareAccelerated="false"
解决了问题。