Home > Mobile >  Android studio in how to realize the two can drive each other independent of the picture
Android studio in how to realize the two can drive each other independent of the picture

Time:09-25

Newbie on the road now experimenting with the use of touch events, created two pictures of the custom view, use twice the onTouch method, the problem is two pictures after click on the screen at any position can be moved to click on the location, depending on the return value, will only have a picture when dragging moves with the mouse, tried online provide different methods will remain unresolved, first in the first version of the code posted a great god please advise
 public class GameActivity extends the Activity {
@ SuppressLint (" ClickableViewAccessibility ")
@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
The setContentView (R.l ayout. Activity_game);
FrameLayout frameLayout1=the findViewById (R.i d.m yView);//create the frame layout manager
Final RabbitView2 bear=new RabbitView2 (GameActivity. This);
Bear. SetOnTouchListener (new the OnTouchListener () {//add touch event listener
@ Override
Public Boolean the onTouch (View v, MotionEvent event) {
Bear. BitmapX=event. GetX ();//set the X coordinate
Bear. BitmapY=event. GetY ();
Bear. Invalidate ();
return true;
}
});
FrameLayout1. AddView (bear);//added to the layout manager

FrameLayout frameLayout2=the findViewById (R.i d.m yView);//create the frame layout manager
Final SheepView sheep=new SheepView (GameActivity. This);
Sheep. SetOnTouchListener (new the OnTouchListener () {//add touch event listener
@ Override
Public Boolean the onTouch (View v, MotionEvent event) {

The sheep. BitmapX=event. GetX ();//sets the initial X display
The sheep. BitmapY=event. GetY ();
The sheep. Invalidate ();//redraw component
return false;
}
});
FrameLayout2. AddView (sheep);//added to the layout manager
}
}

CodePudding user response:

Below one is true

CodePudding user response:

Had tried the return value, the following also returns true when the picture is completely

CodePudding user response:

That means you have a picture 2 size problem, completely hides behind the controls
  • Related