一、首先我们将我们所需要用到的连连看图片放到res下的drawable下
二、我们在相应的layout文件夹下的xml布局文件中写上连连看布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/animal_bg"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/button1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="15dp"
android:background="@drawable/three"
android:onClick="myClick"
tools:ignore="InvalidId" />
<ImageButton
android:id="@+id/button2"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="15dp"
android:onClick="myClick"
android:background="@drawable/four" />
<ImageButton
android:id="@+id/button3"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="15dp"
android:onClick="myClick"
android:background="@drawable/one" />
<ImageButton
android:id="@+id/button4"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="15dp"
android:onClick="myClick"
android:background="@drawable/five" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/button5"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="15dp"
android:onClick="myClick"
android:background="@drawable/one"/>
<ImageButton
android:id="@+id/button6"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="15dp"
android:onClick="myClick"
android:background="@drawable/two"/>
<ImageButton
android:id="@+id/button7"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="15dp"
android:onClick="myClick"
android:background="@drawable/box"/>
<ImageButton
android:id="@+id/button8"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="15dp"
android:onClick="myClick"
android:background="@drawable/four" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/button9"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="15dp"
android:onClick="myClick"
android:background="@drawable/five"/>
<ImageButton
android:id="@+id/button10"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="15dp"
android:onClick="myClick"
android:background="@drawable/box"/>
<ImageButton
android:id="@+id/button11"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="15dp"
android:onClick="myClick"
android:background="@drawable/three"/>
<ImageButton
android:id="@+id/button12"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_margin="15dp"
android:onClick="myClick"
android:background="@drawable/two"/>
</LinearLayout>
<TextView
android:id="@+id/guanka"
android:layout_width="350dp"
android:layout_height="80dp"
android:autoSizeTextType="uniform"
android:textColor="@color/pink"
android:layout_marginLeft="50dp"
android:layout_marginTop="30dp"
android:text="🐧第一关🐇"/>
</LinearLayout>
</RelativeLayout>
上面代码中的 android:onClick代表着点击事件,android:background="@drawable/two"代表着是哪张背景图片,是以按钮的形式来作为连连看的方块
三、接下来就是我们的按钮点击事件也就是xml文件对应的java类
public class MainActivity extends AppCompatActivity {
private int count = 5;
private int sorce=0;
List<ImageButton> buttonList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvOne = findViewById(R.id.guanka);
Typeface typeface = Typeface.createFromAsset(getAssets(), "05汉仪乐喵字体.ttf");
tvOne.setTypeface(typeface);
}
public void myClick(View view){
if (buttonList.size()==2){
buttonList.clear();
ImageButton imageButton = (ImageButton)findViewById(view.getId());
buttonList.add(imageButton);
}else if (buttonList.size()==1){
ImageButton imageButton2 = (ImageButton)findViewById(view.getId());
ImageButton imageButton1 = buttonList.get(0);
Drawable drawable1 = imageButton1.getBackground();
Drawable drawable2 = imageButton2.getBackground();
Bitmap bitmap1 = ((BitmapDrawable) drawable1).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable) drawable2).getBitmap();
if (bitmap1.sameAs(bitmap2)){
Drawable drawable = getResources().getDrawable(R.drawable.box);
imageButton1.setBackground(drawable);
imageButton2.setBackground(drawable);
if (++sorce>=count){
startActivity(new Intent(MainActivity.this,TwoLevel.class));
}
buttonList.clear();
}else {
buttonList.clear();
Toast.makeText(this, "图案不同", Toast.LENGTH_SHORT).show();
}
}else if (buttonList.size()==0){
ImageButton imageButton = (ImageButton)findViewById(view.getId());
buttonList.add(imageButton);
}
}
}
四、最后实现效果就是
图片就各位大哥用自己老师或者自己的渠道的我就不发了。