0

我是 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>

4

2 回答 2

0

问题是你只在你OnClickListener()的 .

相反,您应该将坐标保存到列表中,并在单击侦听器中绘制列表中的所有圆圈。

像这样:

String x11 = P1X.getText().toString();
String z11 = P1Z.getText().toString();

float x1 = Float.valueOf(x11);
float z1 = Float.valueOf(z11);

points.add(new Point(x1, z1));

接着:

for (Point point : points) {
    canvas.drawCircle(point.x, point.y, 25, paint1);
}
于 2013-09-05T00:29:29.720 回答
0

首先,我认为“@drawable/est”是要绘制的基本图像。然后每次单击按钮时,都会重新打开“est”图像,因此不会保存以前的圆圈。

一种可能的解决方案是使用 SurfaceView。互联网上有很多关于它的教程。其他解决方案不是每次都加载“est”图像,而是尝试保存画布并一个接一个地绘制圆圈。

希望能帮助到你

于 2013-09-05T00:55:10.773 回答