捏缩放重绘自定义视图

时间:2014-10-05 19:10:59

标签: android android-custom-view pinchzoom

我开发了一个显示巴黎地铁地图的应用程序(http://www.businesstravel.fr/images/stories/plan-metro-paris.jpg)。 这些行由自定义视图表示。他们有一个点列表,我在这些点之间画一条路。 (此时车站尚未开发)。 一切都在一个延伸relativeLayout的类中。 所以,这个类包含一个行列表。

我想实现一个缩放缩放手势。我已经关注此链接:Pinch zoom for custom view,它运行正常:检测到我的缩放缩放手势很酷。 但我有一个问题。在缩放到缩放手势期间,调用onDraw方法,但它是自定义relativelayout的onDraw方法...如果我想调用行和工作站自定义视图的onDraw方法,该怎么办? 我试图称无效,但它不起作用......

你有什么想法吗?

告诉我,如果我的说明不清楚,你是否想要一些代码。

另外,作为一个法国人,抱歉我的语言错误。

感谢。

编辑:

这是我的代码。 自定义RelativeLayout: 公共类MapMetro扩展了RelativeLayout {

private ArrayList<Line> listLines;
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;

public MapMetro(Context context){
    super(context);
    this.listLines = new ArrayList<>();
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Let the ScaleGestureDetector inspect all events.
    mScaleDetector.onTouchEvent(ev);
    return true;
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();
    canvas.scale(mScaleFactor, mScaleFactor);

    /*
     * Here is where I try to redraw my Custom views
     */

    for(int i=0;i<this.listLines.size();i++){
        this.listLines.get(i).getLineView().invalidate();
    }


    canvas.restore();
}   

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
        invalidate();
        return true;
    }
}   
}

这是我的自定义视图:

公共类LineView扩展了View {

private static final int HEIGHT = 5;
private Paint paint;
private Path path;
private CornerPathEffect cpe;
private int color;
private ArrayList<PointF> listLinePoints;

public LineView(Context ct, ArrayList<PointF>listLinePoints, int color) {
    super(ct);
    this.listLinePoints=listLinePoints;
    this.color=color;

    this.paint=new Paint();
    this.path=new Path();
    this.cpe=new CornerPathEffect(5);

}

@Override
public void onDraw(Canvas canvas) {
    this.paint.setColor(this.color);
    this.paint.setStrokeWidth(HEIGHT);
    this.paint.setStyle(Paint.Style.STROKE);
    this.paint.setDither(true);
    this.paint.setStrokeJoin(Paint.Join.ROUND);
    this.paint.setStrokeCap(Paint.Cap.ROUND);
    this.paint.setPathEffect(this.cpe);
    this.paint.setAntiAlias(true);
    this.path.moveTo(this.listLinePoints.get(0).x, this.listLinePoints.get(0).y);

    for(int i=1;i<this.listLinePoints.size();i++){
        this.path.lineTo(this.listLinePoints.get(i).x, this.listLinePoints.get(i).y);
    }

    canvas.drawPath(path,paint);

}

public static int getLineHeight() {
    return HEIGHT;
}

}

感谢您的帮助。

EDIT2: 我发现了什么似乎是一个解决方案。 在我的自定义视图的构造函数(行的视图)中,我强制视图不绘制自己:

setWillNotDraw(true);

在我的自定义relativeLayout的onDraw中,我调用了func&#34; draw(Canvas)&#34;用我的自定义relativeLayout的画布...不仅仅是......这样做很恶心吗?

感谢。

0 个答案:

没有答案
相关问题