[英]Handle ImageView touch event
我正在android studio 2.3.1中創建一個游戲,第一個屏幕是“開始”屏幕。 當用戶觸摸開始屏幕時,他們應該進入游戲場景。 圖像被視為背景,但另存為Imageview。 我一直在尋找幫助,但沒有得到幫助,我不知道從哪里開始。 這就是我所需要的一切。
activity_start:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.app.StartActivity"
>
<ImageView
android:id="@+id/start"
android:layout_width="600dp"
android:layout_height="380dp"
android:layout_marginBottom="-10dp"
android:layout_marginRight="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.444"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="@drawable/start" />
</android.support.constraint.ConstraintLayout>
在您的StartActivity onCreate方法中
findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// start your game scene
}
});
最好將image設置為布局的background屬性,而不是將imageView作為屏幕的背景。 如果您將imageView保留為背景,則可能會導致不同屏幕尺寸的設備出現問題。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="+@id/start"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.app.StartActivity"
android:background="@drawable/start"
>
.....
</android.support.constraint.ConstraintLayout>
在您的Java代碼中
public void onCreate(Bundle savedInstanceState)
{
....
ConstraintLayout startScreen = (ConstraintLayout)findViewByID(R.id.start)
startScreen.setOnClickListener(this);
}
public void onClick(View v) {
startGame();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.