Naci Naci - 3 months ago 28
Android Question

Canvas change path

I have a spinner in my layout. I would like to change the shape drawn as a path when a different item is selected. Currently my code is adding my second path to the first one, instead of just replacing it. Here is my code:

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();

}
}
}

Answer

You probably need to reset your path:

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();
}