我是 Android 新手,我的应用程序有问题:我有一个带有 ImageView 和更多 EditTexts 和 Buttons 的布局。每次我在 EditText 中输入坐标并按下按钮时,我都试图在图像视图上画一个圆圈。
我找到了第一次让它工作的方法,但是如果我只是为第二个按钮复制相同的代码,它会绘制新的圆圈并删除第一个。而且我想同时拥有它们...
有人可以告诉我我做错了什么吗?非常感谢。
我的活动:
public class IndoorPositioningMainActivity extends Activity {
Button InitialPos = null;
Button P1 = null;
EditText InitialPosX = null;
EditText InitialPosY = null;
EditText InitialPosZ = null;
EditText P1X = null;
EditText P1Y = null;
EditText P1Z = null;
ImageView Image = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_indoor_positioning_main);
InitialPos = (Button)findViewById(R.id.ButtonInitialPosInput);
P1 = (Button)findViewById(R.id.ButtonP1);
InitialPosX = (EditText)findViewById(R.id.InitialPosInputX);
InitialPosY = (EditText)findViewById(R.id.InitialPosInputY);
InitialPosZ = (EditText)findViewById(R.id.InitialPosInputZ);
P1X = (EditText)findViewById(R.id.P1InputX);
P1Y = (EditText)findViewById(R.id.P1InputY);
P1Z = (EditText)findViewById(R.id.P1InputZ);
Image = (ImageView)findViewById(R.id.imageView1);
InitialPos.setOnClickListener(InitialPosListener);
P1.setOnClickListener(P1Listener);
InitialPosX.addTextChangedListener(textWatcher);
InitialPosY.addTextChangedListener(textWatcher);
InitialPosZ.addTextChangedListener(textWatcher);
P1X.addTextChangedListener(textWatcher);
P1Y.addTextChangedListener(textWatcher);
P1Z.addTextChangedListener(textWatcher);
new PositionAsync().execute();
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
private OnClickListener InitialPosListener = new OnClickListener() {
@Override
public void onClick(View v) {
String x = InitialPosX.getText().toString();
String z = InitialPosZ.getText().toString();
float x0 = Float.valueOf(x);
float z0 = Float.valueOf(z);
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.est,myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(x0, z0, 50, paint);
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
}
};
private OnClickListener P1Listener = new OnClickListener() {
@Override
public void onClick(View v) {
String x11 = P1X.getText().toString();
String z11 = P1Z.getText().toString();
float x1 = Float.valueOf(x11);
float z1 = Float.valueOf(z11);
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.est,myOptions);
Paint paint1 = new Paint();
paint1.setAntiAlias(true);
paint1.setColor(Color.BLUE);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(x1, z1, 25, paint1);
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
}
};
//XML READER
class PositionAsync extends AsyncTask<Void, Void, Void> {
XMLHelper helper;
@Override
protected Void doInBackground(Void... arg0) {
helper = new XMLHelper();
helper.get();
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
}
我的 xml 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10px" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="70"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="centerInside"
android:src="@drawable/est" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:layout_weight="30"
android:orientation="vertical"
android:padding="10px" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/InitialPosX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
<EditText
android:id="@+id/InitialPosInputX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/InitialPosY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y" />
<EditText
android:id="@+id/InitialPosInputY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/InitialPosZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Z" />
<EditText
android:id="@+id/InitialPosInputZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<Button
android:id="@+id/ButtonInitialPosInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/P1X"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
<EditText
android:id="@+id/P1InputX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/P1Y"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y" />
<EditText
android:id="@+id/P1InputY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:id="@+id/P1Z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Z" />
<EditText
android:id="@+id/P1InputZ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<Button
android:id="@+id/ButtonP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
</LinearLayout>