私は Android が初めてで、アプリに問題があります。ImageView とその他の EditTexts と Buttons を含むレイアウトがあります。EditTextに座標を入力してボタンを押すたびに、Image Viewに円を描こうとしています。
初めて機能させる方法を見つけましたが、2番目のボタンに同じコードをコピーするだけで、新しい円が描画され、最初の円が消去されます。そして、私はそれらの両方を持ちたい.
誰かが私が間違っていると教えてもらえますか? どうもありがとう。
私の活動:
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>