视图的无效也会重绘先前的纹理

时间:2018-02-03 14:19:18

标签: java android android-view android-custom-view custom-view

我想实现这样的目标。

enter image description here

但不是那样,我得到的是这样的东西。

enter image description here

我正在使用invalidate重绘我的自定义视图。但每次更改多边形的边时,它都会创建另一个视图。我哪里错了?

这是我的代码。

PolygonView.Java

public class PolygonView extends View {

    public float polygonRadius;
    public int polygonSides;
    public int polygonColor;

    private Paint paint;
    private Path path;
    public PolygonView(Context context) {
        super(context);
        init(null);
    }

    public PolygonView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public PolygonView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);

    }

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

    private void init(AttributeSet attrs)
    {
        polygonColor = ContextCompat.getColor(getContext(),R.color.polygonColor);
        polygonSides = 5;

        if (attrs!=null){
            TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs,R.styleable.PolygonView,0,0);
            polygonColor = typedArray.getColor(R.styleable.PolygonView_polygon_color,polygonColor);
            polygonRadius = typedArray.getDimension(R.styleable.PolygonView_polygon_radius,polygonRadius);
            polygonSides = typedArray.getInteger(R.styleable.PolygonView_polygon_sides,polygonSides);
        }

        paint = new Paint();
        paint.setColor(polygonColor);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);

        path = new Path();
    }

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

        double angle = 2.0*Math.PI/polygonSides;
        int cx = getWidth()/2;
        int cy = getHeight()/2;
        path.moveTo(
                (float) (cx +polygonRadius*Math.cos(0.0)),
                (float) (cy +polygonRadius*Math.sin(0.0))
        );
        for(int i=1;i<=polygonSides;i++){
            path.lineTo(
                    (float) (cx + polygonRadius*Math.cos(angle*i)),
                    (float) (cy + polygonRadius*Math.sin(angle*i))
            );
        }

        path.close();

        canvas.drawPath(path,paint);

    }
}

Fragment2.Java

public class Fragment2 extends Fragment {


public Fragment2() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v =  inflater.inflate(R.layout.fragment_polygon, container, false);

    final PolygonView polygonView = v.findViewById(R.id.polygonView);
    SeekBar seekBarRadius = v.findViewById(R.id.seekBarRadius);
    SeekBar seekBarSides = v.findViewById(R.id.seekBarSides);

    seekBarSides.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            int p = progress/10;
            if(p<1){
                polygonView.polygonSides = 1;
            }else if (p>10){
                polygonView.polygonSides = 10;
            }
            else {
                polygonView.polygonSides = p;
            }

            polygonView.invalidate();

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });


    return v;
}

}

1 个答案:

答案 0 :(得分:2)

您总是在改变您的路径但从未重置过它。在onDraw()方法resetpath,然后才对其应用新操作:


    public void onDraw() {
        path.reset();
        ...
    }