Android General Question
I have a small program where i have 2 images, right now i am doing OnTouch events for each image when clicked the image on top is set to invisible. This is working fine.. but having this in the main Activity is getting messy..
I created a TestView, I would like to move the code I have in the main activity to the testView, but I do not know where to put the OnTouch events and how to do the same from there?, I dont know where to put the OnTouch events and also how to draw the ImageViews from my testView.. when I was in the Main activity they drew fine.. but now.. i dont know how to draw them from the "testView extends View" class... can you help me organize it?
public class TestView extends View{
public TestView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public TestView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public TestView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
public class GameActivity extends Activity {
/** Called when the activity is first created. */
public int ColorId;
public boolean isUserTurn = false;
public int globalIndex = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myviewlay);
final ImageView image1= (ImageView)findViewById(开发者_如何转开发R.id.ImageView03);
image1.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(200);
ColorId = 1;
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN){
image1.setVisibility(View.INVISIBLE);
}
else
{
image1.setVisibility(View.VISIBLE);
}
//return super.onTouchEvent(event);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
});
final ImageView image2= (ImageView)findViewById(R.id.ImageView02);
image2.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(200);
ColorId = 2;
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN){
image2.setVisibility(View.INVISIBLE);
}
else
{
image2.setVisibility(View.VISIBLE);
}
//return super.onTouchEvent(event);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
});
I just want to know, if i move it to the test view, how can i re organize it so that it works from there. I am very new to Android...
Thank you
For the onTouch events you just need to implement onTouchListener
on your TestView
. If both ImageViews are inside your TestView
you need some kind of ViewGroup to do the layout of your custom view. If not you just extend ImageView instead of extending View and put your custom TestView in a XML layout and call setContentView(<your_layout>)
in your activity.
If not clear enough, say something :P
Hope it helped, JQCorreia
精彩评论