Hi everyone,
I'm programming an app but i have a problem...
I want that when i click on the screen of my phone, my image goes where i clicked.
(I just want to change the "x" alignement of the image).
This is the code of the Java Activity :
public class gameActivity extends AppCompatActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final IShip ship = new StandardShip();
final ImageView iv_ship = (ImageView)findViewById(R.id.iv_ship);
final RelativeLayout rl_game = (RelativeLayout)findViewById(R.id.rl_game);
final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
iv_ship.setImageResource(R.mipmap.img_standardship);
iv_ship.setOnTouchListener(new View.OnTouchListener() {
@override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
ship.setX(0 - size.x / 2 + event.getX());
iv_ship.setX(ship.getX());
}
return true;
}
});
}
}
This is the code of the layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android
addingBottom="@dimen/activity_vertical_margin"
android
addingLeft="@dimen/activity_horizontal_margin"
android
addingRight="@dimen/activity_horizontal_margin"
android
addingTop="@dimen/activity_vertical_margin"
tools:context="aurelien.pizza.gameActivity"
android:id="@+id/rl_game">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/iv_ship" />
</TableRow>
</RelativeLayout>
I'm programming an app but i have a problem...
I want that when i click on the screen of my phone, my image goes where i clicked.
(I just want to change the "x" alignement of the image).
This is the code of the Java Activity :
public class gameActivity extends AppCompatActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final IShip ship = new StandardShip();
final ImageView iv_ship = (ImageView)findViewById(R.id.iv_ship);
final RelativeLayout rl_game = (RelativeLayout)findViewById(R.id.rl_game);
final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
iv_ship.setImageResource(R.mipmap.img_standardship);
iv_ship.setOnTouchListener(new View.OnTouchListener() {
@override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
ship.setX(0 - size.x / 2 + event.getX());
iv_ship.setX(ship.getX());
}
return true;
}
});
}
}
This is the code of the layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android

android

android

android

tools:context="aurelien.pizza.gameActivity"
android:id="@+id/rl_game">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/iv_ship" />
</TableRow>
</RelativeLayout>