基本游戏,其中有2个轨道(自动滚动),其中有一个将在轨道上移动的对象。如果对象移出Track,那么Game over。 布局: -
<FrameLayout>
<ScrollView> <Track2/><Track2/></ScrollView>
<ImageView>(Object)
</FrameLayout>
注意:每个Track分辨率1026 * 3000
app在分辨率为1080 * 1080
的设备上运行轨道上的图像让我们说x = 540 y = 540
Scrollview自动滚动3px,
每次检查&#34;是否在轨道上对象?&#34;为此我尝试匹配轨道边框的颜色代码,如果颜色匹配轨道边界游戏结束,但无法得到正确的解决方案。
<ScrollView
android:id="@+id/vertical_scrollview_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:fadingEdge="none"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/vertical_outer_layout_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fm1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/track2" />
<FrameLayout
android:id="@+id/fm0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/track1" >
</FrameLayout>
</LinearLayout>
</ScrollView>
<ImageView
android:id="@+id/imageView"
android:layout_width="72dp"
android:layout_height="78dp"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:src="@drawable/red_hexagon" />
</FrameLayout>
public void moveScrollView(){
scrollPos = (int) (verticalScrollview.getScrollY() + 1.0);
if (scrollPos == 1) {
verticalScrollview.fullScroll(View.FOCUS_DOWN);
}
if (mRootLayout != null) {
isTouchInView(mRootLayout, event);
}
verticalScrollview.scrollBy(scrollPos, -2);
}
私人 boolean isTouchInView(视图视图,MotionEvent事件){
fmLayoutWidth = 1280;// mRootLayout.getWidth();
fmLayoutHeight = 2289;// mRootLayout.getHeight();
System.out.println("fmLayoutWidth---" + fmLayoutWidth
+ "--fmLayoutHeight--" + fmLayoutHeight);
Bitmap bitmap = loadBitmapFromView(mRootLayout);
System.out.println("isTouchInView -x---" + x + "--yyyyyy--" + y);
int pixel1 = bitmap.getPixel(0, 0);
int pixel2 = bitmap.getPixel(71, 77);
int pixel3 = bitmap.getPixel(71, 0);
int pixel4 = bitmap.getPixel(0, 77);
String hex1 = convetRGBtoHexaDecimal(pixel1);
String hex2 = convetRGBtoHexaDecimal(pixel2);
String hex3 = convetRGBtoHexaDecimal(pixel3);
String hex4 = convetRGBtoHexaDecimal(pixel4);
System.out.println("----h1-----" + hex1 + "----h2-----" + hex2
+ "----h3-----" + hex3 + "----h4-----" + hex4);
if (hex1.contains("#FFE400") || hex1.contains("#FFE50F")
|| hex2.contains("#FFE400") || hex2.contains("#FFE50F")
|| hex3.contains("#FFE400") || hex3.contains("#FFE50F")
|| hex4.contains("#FFE400") || hex4.contains("#FFE50F")) {
score++;
}
tvScore.setText("" + score);
return true;
}
public Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(fmLayoutWidth, fmLayoutHeight,
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
if (viewObject != null)
v.layout(mRootLayout.getLeft(), mRootLayout.getTop(),
mRootLayout.getRight(), mRootLayout.getBottom());
v.draw(c);
return b;
}
private String convetRGBtoHexaDecimal(int pixel) {
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
return String.format("#%02X%02X%02X", redValue, greenValue, blueValue);
}