Question

i want to create an app like joining the dots

for that i am using imagebuttons on main_layout file

on its onclick event i want to draw a line between two buttons

e.g on button2 click ,line from button1 to button2 should be drawn on button3 click ,line from button2 to button3

this is simple,agreed but to draw lines i am using canvas and that is causing me difficulties to handle onclick events

i am calculating their coordinates in onResume() method of my mainActivity.

what to do next?any alternative for canvas?

 public class MainActivity extends Activity implements OnClickListener {
public Button b1,b2,b3;
public int co_orodinates[];
public int b1x,b1y,b2x,b2y,b3x,b3y;
public Bitmap bitmap;
public Canvas canvas;
@Override
protected void onResume()
{
    super.onResume();
    b1.getLocationOnScreen(co_orodinates);
    Log.d("b1 location "," "+co_orodinates[0]+" "+co_orodinates[1]);
    b1x=co_orodinates[0];b1y=co_orodinates[1];
    co_orodinates[0]=0;co_orodinates[1]=0;

    b2.getLocationOnScreen(co_orodinates);
    Log.d("b2 location "," "+co_orodinates[0]+" "+co_orodinates[1]);
    b2x=co_orodinates[0];b2y=co_orodinates[1];
    co_orodinates[0]=0;co_orodinates[1]=0;

    b3.getLocationOnScreen(co_orodinates);
    Log.d("b3 location "," "+co_orodinates[0]+" "+co_orodinates[1]);
    b2x=co_orodinates[0];b2y=co_orodinates[1];
    co_orodinates[0]=0;co_orodinates[1]=0;

}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    co_orodinates=new int[2];
    b1=(Button) findViewById(R.id.imageButton1);
    b2=(Button) findViewById(R.id.imageButton2);
    b3=(Button) findViewById(R.id.imageButton3);

    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    b3.setOnClickListener(this);

    bitmap = Bitmap.createBitmap(50, 100, Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);
    Drawable drawable = new BitmapDrawable(bitmap);
    Paint paint = new Paint();

}
@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
    switch(view.getId())
    {
    case R.id.imageButton2:

//a line should be drawn from button1 to button2

            break;
    case R.id.imageButton3:

//a line should be drawn from button2 to button3

        break;
    }   
}

}

Était-ce utile?

La solution

maybe you can try the answer provided here. I think it's the same concept what you're trying to develop. If it works, give an upvote to the original person who answered this!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top