dimanche 3 mai 2015

Android Studio using variables from one class in another

My basic app idea is drawing on screen. I've got a spinner object to use to select the color of the "pen". I have them set up for a switch case to change "pen" color. The spinner is in my MainActivity class. I've got my "pen" code in a class called Brush_Color.

Here's the code I have for MainActivity thats related to the Spinner. Each case refers to a color in my arrays.xml. The commented out Paint paint = ... is what I was trying to do but had no luck.

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener{

//Paint paint = new Paint(Brush_Choices.this.paint, Brush_Choices.class);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.color_selector, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);
}


public void onItemSelected(AdapterView<?> parent, View v, int position, long id){
    switch(position) {
        case 0:
            //paint.setColor(Color.BLACK);
            break;
        case 1:
            //paint.setColor(Color.BLUE);
            break;

public void onNothingSelected(AdapterView<?> parent){

}

And then here is the code of my Brush_Color class. I'm trying to access the Paint object from here and use it in my MainActivity class. I've got no idea how to do this though.

Path path = new Path();
SparseArray<PointF> points = new SparseArray<>();
Paint paint = new Paint();

public void onDraw(Canvas canvas){
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(10);
    canvas.drawPath(path,paint);
}

Aucun commentaire:

Enregistrer un commentaire