- Code: Select all
public class crosshairs extends Activity {
final int BUFFER = 1;
final int SPAN = 30;
final int CENTER = 15;
final int BASE = 46;
Bitmap bmpLine = Bitmap.createBitmap(467, 467, Bitmap.Config.ARGB_8888);
Canvas cvsLine = new Canvas();
Paint pntLine = new Paint();
ImageView imgGrid;
Spinner spinRowStart, spinColStart, spinRowEnd, spinColEnd;
OnItemSelectedListener spinListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> a, View v, int i, long l) {
int rStart = spinRowStart.getSelectedItemPosition();
int cStart = spinColStart.getSelectedItemPosition();
int rEnd = spinRowEnd.getSelectedItemPosition();
int cEnd = spinColEnd.getSelectedItemPosition();
float xStart = BASE + (rStart * (BUFFER + SPAN));
float yStart = BASE + (cStart * (BUFFER + SPAN));
float xEnd = BASE + (rEnd * (BUFFER + SPAN));
float yEnd = BASE + (cEnd * (BUFFER + SPAN));
cvsLine.drawLine(xStart, yStart, xEnd, yEnd, pntLine);
imgGrid.draw(cvsLine);
imgGrid.invalidate();
}
@Override
public void onNothingSelected(AdapterView<?> a) {
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgGrid = (ImageView)findViewById(R.id.imgGrid);
cvsLine.setBitmap(bmpLine);
imgGrid.setImageBitmap(bmpLine);
pntLine.setColor(Color.RED);
pntLine.setStrokeWidth(4);
spinRowStart = (Spinner)findViewById(R.id.spinRowStart);
spinRowEnd = (Spinner)findViewById(R.id.spinRowEnd);
spinColStart = (Spinner)findViewById(R.id.spinColStart);
spinColEnd = (Spinner)findViewById(R.id.spinColEnd);
spinRowStart.setOnItemSelectedListener(spinListener);
spinRowEnd.setOnItemSelectedListener(spinListener);
spinColStart.setOnItemSelectedListener(spinListener);
spinColEnd.setOnItemSelectedListener(spinListener);
}
}
First, the line only seems to be drawn if I hardcode starting and stopping points, but the line only shows up after I tilt my phone to change the orientation. If I use the values of the variables like in the code above then the line never shows up.
I want the line to be drawn and show up anytime the selection of one of the spinners changes. Any idea how I would get that done?

