Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-ImageView上的多重绘制_Android_Android Layout_Imageview_Drawable - Fatal编程技术网

Android-ImageView上的多重绘制

Android-ImageView上的多重绘制,android,android-layout,imageview,drawable,Android,Android Layout,Imageview,Drawable,我是Android新手,我的应用程序有一个问题:我有一个带有图像视图和更多编辑文本和按钮的布局。每次在编辑文本中输入坐标并按下按钮时,我都试图在图像视图上画一个圆。 我找到了一种方法让它第一次工作,但如果我只是复制第二个按钮的相同代码,它会绘制新的圆圈并删除第一个。我想要两个… 有人能告诉我我做错了什么?非常感谢。 我的活动: public class IndoorPositioningMainActivity extends Activity { Button InitialPos = nu

我是Android新手,我的应用程序有一个问题:我有一个带有图像视图和更多编辑文本和按钮的布局。每次在编辑文本中输入坐标并按下按钮时,我都试图在图像视图上画一个圆。
我找到了一种方法让它第一次工作,但如果我只是复制第二个按钮的相同代码,它会绘制新的圆圈并删除第一个。我想要两个…
有人能告诉我我做错了什么?非常感谢。
我的活动:

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) {

    }
}

}
public类indoorPositionImactivity扩展活动{
按钮初始位置=空;
按钮P1=空;
EditText InitialPosX=null;
EditText InitialPosY=null;
EditText InitialPosZ=null;
编辑文本P1X=null;
编辑文本P1Y=null;
编辑文本P1Z=null;
ImageView图像=空;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动、室内、定位、主视图);
InitialPos=(按钮)findViewById(R.id.ButtonInInitialPosinInput);
P1=(按钮)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);
新建PositionAsync().execute();
}
私有TextWatcher TextWatcher=新TextWatcher(){
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
}                
@凌驾
public void beforeTextChanged(字符序列s、int start、int count、int after){
}
@凌驾
公共无效后文本更改(可编辑的s){
}
};
private OnClickListener InitialPosListener=新OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串x=InitialPosX.getText().toString();
字符串z=InitialPosZ.getText().toString();
float x0=float.valueOf(x);
浮点数z0=浮点数(z);
BitmapFactory.Options myOptions=新的BitmapFactory.Options();
myOptions.inDither=true;
myOptions.inScaled=false;
myOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//重要
myOptions.inpurgable=true;
位图Bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.est,myOptions);
油漆=新油漆();
paint.setAntiAlias(真);
油漆。设置颜色(颜色。红色);
位图工作位图=位图。创建位图(位图);
位图可变位图=工作位图.copy(Bitmap.Config.ARGB_8888,true);
画布画布=新画布(可变位图);
帆布画圈(x0,z0,50,油漆);
ImageView ImageView=(ImageView)findViewById(R.id.imageView1);
imageView.setAdjustViewBounds(true);
setImageBitmap(可变位图);
}
};
private OnClickListener P1Listener=new OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串x11=P1X.getText().toString();
字符串z11=P1Z.getText().toString();
float x1=float.valueOf(x11);
浮点数z1=浮点数(z11);
BitmapFactory.Options myOptions=新的BitmapFactory.Options();
myOptions.inDither=true;
myOptions.inScaled=false;
myOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//重要
myOptions.inpurgable=true;
位图Bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.est,myOptions);
油漆油漆1=新油漆();
paint1.setAntiAlias(真);
油漆1.setColor(颜色:蓝色);
位图工作位图=位图。创建位图(位图);
位图可变位图=工作位图.copy(Bitmap.Config.ARGB_8888,true);
画布画布=新画布(可变位图);
画布.画圈(x1,z1,25,1);
ImageView ImageView=(ImageView)findViewById(R.id.imageView1);
imageView.setAdjustViewBounds(true);
setImageBitmap(可变位图);
}
};
//XML读取器
类PositionAsync扩展了AsyncTask{
XMLHelper助手;
@凌驾
受保护的Void doInBackground(Void…arg0){
helper=新的XMLHelper();
helper.get();
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
}
}
}
我的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>


问题在于,在
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);
}
首先,我假设“@drawable/est”是将要绘制的基本图像。然后,每次单击按钮时,“est”图像都会重新打开,因此不会保存以前的圆圈

一种可能的解决方案是使用SurfaceView。网上有很多关于它的教程。 另一个解决方案是不要每次都加载“est”图像,而是尝试保存画布