一般的想法是允许用户点击字段并查找项目的应用程序。项目定义来自excel文件。物品将以随机位置散布在整个场地上。当用户点击屏幕时,它将创建漏洞。按下库存按钮时,找到的项目将显示在列表中。我在使用DrawSurface脚本时遇到问题。
xml:
<android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/mField" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dp">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.5"
android:text="View Inventory"
android:id="@+id/btnInventory"/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.5"
android:text="Credits"
android:id="@+id/btnCredits"/>
</LinearLayout>
</LinearLayout>
MainActivity:
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.app.AlertDialog;
import android.content.DialogInterface;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivityimplementsView.OnClickListener {
ArrayList<Item> inv = DrawSurface.getInventory();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnInventory).setOnClickListener(this);
findViewById(R.id.btnCredits).setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btnCredits) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Made by");
dialog.setMessage("DJon Archer" + "\nMGMS | CSE\n08/10/2017");
dialog.setPositiveButton(" OK ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
dialog.show();
}
else if (view.getId() == R.id.btnInventory)
{
loadItems();
}
}
private ArrayList<Item> loadItems()
{
InputStream input = getResources().openRawResource(R.raw.items);
BufferedReader reader = null;
ArrayList<Item> items = new ArrayList<Item>();
String line;
try
{
reader = new BufferedReader(new InputStreamReader(input));
while ((line = reader.readLine()) !=null)
{
items.add(new Item(line));
}
}
catch (Exception e)
{
Log.e("MainActivity", "Reading list of Items failed!", e);
}
finally
{
try {
if (reader != null) reader.close();
}
catch (Exception e)
{
Log.e("MainActivity", "Error closing file reader.", e);
}
}
return items;
}
}
档案:
public class Item {
public String name;
public int x;
public int y;
//constructor
public Item(String n) {
name = n;
}
}
DrawSurface:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;uu
import android.view.SurfaceView;
import android.view.View;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
abstract class DrawSurface extends SurfaceView implements SurfaceHolder.Callback, View.OnTouchListener
{
SurfaceHolder holder;
Bitmap mBMPField;
Bitmap mBMPHole;
private static ArrayList<Item> mItems;
public DrawSurface(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Getting the holder
holder = getHolder();
holder.addCallback(this);
setOnTouchListener(this);
findViewById(R.id.mField);
}
int radius = Math.max(mBMPHole.getHeight(),mBMPHole.getWidth()) / 2;
public static ArrayList<Item> getInventory() {
//called from MainActivity.java
return mItems;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()== MotionEvent.ACTION_DOWN)
{
for (int i=mItems.size()-1; i>0; i--)
mItems.get(i);
dx = mItems.x - center.x;
dy = mItems.y - center.y;
if((dx)*(dx) + (dy)*(dy) < radius*radius)
{
mItems.remove(i);
}
}
setWillNotDraw(false);
}
//Surface holder allows to control and monitor the surface
@Override
public void surfaceCreated(SurfaceHolder holder)
{
Canvas c = holder.lockCanvas();
if (c!=null) mField.Dim.set(0,0, c.getWidth(),c.getHeight());
holder.unlockCanvasAndPost(c);
invalidate();
for (int i=0; i<mItems.size(); i++)
{
mItems.get(i).x = (int)(Math.random() * (float)mFieldDim.width());
mItems.get(i).y = (int)(Math.random() * (float)mFieldDim.height());
}
}
@Override
public void onDraw(Canvas canvas)
{
drawBitmap();
}
void drawBitmap()
{
mBMPField = BitmapFactory.decodeResource(getResources(), R.drawable.field);
mBMPHole = BitmapFactory.decodeResource(getResources(), R.drawable.hole);
}
}