我的基本應用程序想法是在屏幕上繪製。我有一個Spinner
對象用於選擇「筆」的顏色。我讓他們設置了一個開關盒來改變「筆」的顏色。 Spinner
在我的MainActivity
類中。我在一個名爲Brush_Color
的課程中有我的「筆」代碼。Java:使用另一個類中的變量
下面是我對MainActivity
的代碼,它與Spinner
有關。每種情況都指向我的arrays.xml中的一種顏色。註釋掉了Paint paint = ...
就是我想要做的,但沒有運氣。
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){
}
然後這裏是我的Brush_Color
類的代碼。我試圖從這裏訪問Paint
對象,並在我的MainActivity
類中使用它。我不知道如何做到這一點。
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);
}