画布改变路径

时间:2016-08-28 17:18:35

标签: android android-canvas android-spinner

我的布局中有一个微调器。我想在选择其他项目时更改绘制为路径的形状。目前我的代码是将第二条路径添加到第一条路径,而不仅仅是替换它。这是我的代码:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    private MyView myView;
    private boolean firstTimeThru = true;
    private LinearLayout root;
    private Spinner mySpinner;
    private Paint myPaint;
    private Path path = new Path();

    float[] finalXCoord, finalYCoord;
    float[] xCoord1 = {0,30, 50};
    float[] yCoord1 = {0, 100, 40};

    float[] xCoord2 = {52,300, 100, 60};
    float[] yCoord2 = {100, 200, 40, 80};


    @Override


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        finalXCoord = xCoord1;
        finalYCoord = yCoord1;
        // Hide the Title Bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Hide the Status Bar
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        root = new LinearLayout(this);
        root.setOrientation(LinearLayout.VERTICAL);

        // Create the spinner, add contents, add events, add to Layout
        mySpinner = new Spinner(this);

        List<String> list = new ArrayList<String>();
        list.add("1st Path");
        list.add("2nd Path");

        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
        mySpinner.setAdapter(spinnerArrayAdapter);

        myPaint = new Paint();
        myPaint.setColor(Color.GREEN);

        mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                System.out.println(mySpinner.getSelectedItemId());
                if (mySpinner.getSelectedItemId()==0) {
                    myPaint.setColor(Color.GREEN);
                    finalXCoord = xCoord1;
                    finalYCoord = yCoord1;

                }
                if (mySpinner.getSelectedItemId()==1) {
                    myPaint.setColor(Color.RED);
                    finalXCoord = xCoord2;
                    finalYCoord = yCoord2;
                }
                if (!firstTimeThru) {
                    System.out.println("Not First Time");
                    //demoview.changeToRed();
                }
                System.out.println(myPaint.getColor());
            }
            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }
        });

        root.addView(mySpinner);

        // Add view with canvas
        myView = new MyView(this);
        root.addView(myView);

        setContentView(root);

    }

    public class MyView extends View {

        public Canvas mCanvas;

        public MyView(Context context) {
            super(context);
        }

        public void drawPath(float[] xCoord, float[] yCoord) {
            path.moveTo(xCoord[0], yCoord[0]);
            for(int i=1; i<xCoord.length; i++) {
                path.lineTo(xCoord[i], yCoord[i]);
            }
            path.close();
            mCanvas.drawPath(path, myPaint);
            invalidate();
        }

        protected void onDraw(Canvas canvas) {
            mCanvas = canvas;

            //myPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            myPaint.setStrokeWidth(10);
            myPaint.setPathEffect(null);
            //paint.setColor(Color.BLACK);
            myPaint.setStyle(Paint.Style.STROKE);

            drawPath(finalXCoord, finalYCoord);
            //invalidate();

        }
    }
}

1 个答案:

答案 0 :(得分:3)

您可能需要重置路径:

public void drawPath(float[] xCoord, float[] yCoord) {

    path.reset();

    path.moveTo(xCoord[0], yCoord[0]);
    for(int i=1; i<xCoord.length; i++) {
        path.lineTo(xCoord[i], yCoord[i]);
    }
    path.close();
    mCanvas.drawPath(path, myPaint);
    invalidate();
}