Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.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 java中不起作用吗?_Java_Android_Canvas_Bitmap - Fatal编程技术网

为什么赢了';我的撤销按钮在android java中不起作用吗?

为什么赢了';我的撤销按钮在android java中不起作用吗?,java,android,canvas,bitmap,Java,Android,Canvas,Bitmap,我有一个用户可以在我的android应用程序中绘制的图像。我决定添加一个撤销按钮,这样用户就可以取消错误。我这样做是为了当用户在屏幕上绘制时,它会将位图保存到一个数组中。然后,当按下“撤消”按钮时,它会将图像更改为数组中的最后一个位图。但是,这只是将图像设置为当前图像的任何内容(即,它根本不会更改它) 公共类编辑扩展AppCompatActivity实现View.OnClickListener{ 浮动MoveX,MoveY,DownY,DownX,UpY=0,UpX; List undos=ne

我有一个用户可以在我的android应用程序中绘制的图像。我决定添加一个撤销按钮,这样用户就可以取消错误。我这样做是为了当用户在屏幕上绘制时,它会将位图保存到一个数组中。然后,当按下“撤消”按钮时,它会将图像更改为数组中的最后一个位图。但是,这只是将图像设置为当前图像的任何内容(即,它根本不会更改它)

公共类编辑扩展AppCompatActivity实现View.OnClickListener{
浮动MoveX,MoveY,DownY,DownX,UpY=0,UpX;
List undos=new ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
最终ImageButton btn2=(ImageButton)findViewById(R.id.imageButton2);
btn2.setOnClickListener(此);
最终按钮ButtonCoose=(按钮)findViewById(R.id.ButtonCoose);
ButtonCoose.setOnClickListener(此);
最终RelativeLayout视图x=(RelativeLayout)findViewById(R.id.RLXV);
ViewTreeObserver vto=ViewX.getViewTreeObserver();
vto.addOnGlobalLayoutListener(新的OnGlobalLayoutListener(){
@凌驾
公共图书馆{
getViewTreeObserver().removeOnGlobalLayoutListener(此);
Intent intent7=getIntent();
字符串bitmapXY=(字符串)intent7.getExtras().get(“BitmapImage”);
GraphRequest请求=GraphRequest.newGraphPathRequest(
AccessToken.getCurrentAccessToken(),
“/”+bitmapXY,
新建GraphRequest.Callback(){
@凌驾
未完成公共空白(图形响应){
HttpURLConnection=null;
试一试{
int ViewWidth=ViewX.getMeasuredWidth();
JSONObject photos=response.getJSONObject();
JSONArray linkY=photos.optJSONArray(“图像”);
最终JSONObject linkO=linkY.optJSONObject(0);
最终字符串link2=linkO.optString(“源”);
URL LINKF=新URL(link2);
connection=(HttpURLConnection)LINKF.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream输入=连接。getInputStream();
位图位图=BitmapFactory.decodeStream(输入);
float HH=(float)bitmap.getHeight();
float WW=(float)bitmap.getWidth();
float ViewWidthX=(float)ViewWidth;
浮动高度=(高/低)*视图宽度x;
位图bitmapF=位图.createScaledBitmap(位图、ViewWidth、Math.round(高度)、false);
最终ImageView image1=(ImageView)findViewById(R.id.image1);
image1.setImageBitmap(位图);
撤消.add(bitmapF);
image1.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
Display d=getWindowManager().getDefaultDisplay();
float dw=d.getWidth();
float dh=d.getHeight();
float x=event.getX();
float y=event.getY();
浮点数r=2;
位图BTX=((BitmapDrawable)image1.getDrawable()).getBitmap();
画布=新画布(BTX);
drawBitmap(BTX,0,0,null);
油漆油漆1=新油漆();
int bg1=buttonchoose.getDrawingCacheBackgroundColor();
ColorDrawable buttonColor=(ColorDrawable)ButtonCoose.getBackground();
int bg2=buttonColor.getColor();
油漆1.setColor(bg2);
paint1.setShadowLayer(5,2,2,bg2);
油漆1.设置行程宽度(20);
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
DownY=event.getY();
DownX=event.getX();
打破
case MotionEvent.ACTION\u移动:
MoveY=event.getY();
MoveX=event.getX();
帆布。抽绳(向下,向下,移动,移动,绘画1);
图1.invalidate();
DownX=MoveX;
DownY=MoveY;
打破
case MotionEvent.ACTION\u UP:
UpY=event.getY();
UpX=event.getX();
帆布.抽绳(向下,向下,向上,向上,向上,向上,向上1);
图1.invalidate();
位图BTXYZ=((BitmapDrawable)image1.getDrawable()).getBitmap();
打破
}
返回true;
}
});
}捕获(IOE异常){
e、 printStackTrace();
}
}
});
Bundle参数=新Bundle();
putString(“字段”、“图像”);
请求。设置参数(参数);
request.executeAsync();
}
});
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.按钮选择:
最终颜色选择器cp=新颜色选择器(edit.this,0,0,0);
/*显示颜色选择器对话框*/
cp.show();
/*单击对话框的侦听器,
  public class edit extends AppCompatActivity implements View.OnClickListener {
float MoveX,MoveY,DownY,DownX,UpY=0,UpX ;
List<Bitmap> undos = new ArrayList<Bitmap>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_edit);
   final  ImageButton btn2 = (ImageButton) findViewById(R.id.imageButton2);
    btn2.setOnClickListener(this);
    final Button buttonchoose = (Button) findViewById(R.id.buttonchoose);
    buttonchoose.setOnClickListener(this);
    final RelativeLayout ViewX = (RelativeLayout) findViewById(R.id.RLXV);
    ViewTreeObserver vto = ViewX.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            ViewX.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            Intent intent7 = getIntent();
            String bitmapXY = (String) intent7.getExtras().get("BitmapImage");
            GraphRequest request = GraphRequest.newGraphPathRequest(
                    AccessToken.getCurrentAccessToken(),
                    "/" + bitmapXY,
                    new GraphRequest.Callback() {
                        @Override
                        public void onCompleted(GraphResponse response) {
                            HttpURLConnection connection = null;
                            try {
                                int ViewWidth = ViewX.getMeasuredWidth();
                                JSONObject photos = response.getJSONObject();
                                JSONArray linkY = photos.optJSONArray("images");
                                final JSONObject linkO = linkY.optJSONObject(0);
                                final String link2 = linkO.optString("source");
                                URL LINKF = new URL(link2);
                                connection = (HttpURLConnection) LINKF.openConnection();
                                connection.setDoInput(true);
                                connection.connect();
                                InputStream input = connection.getInputStream();
                                Bitmap bitmap = BitmapFactory.decodeStream(input);
                                float HH = (float) bitmap.getHeight();
                                float WW = (float) bitmap.getWidth();
                                float ViewWidthX = (float) ViewWidth;
                                float height = (HH / WW) * ViewWidthX;

                                Bitmap bitmapF = Bitmap.createScaledBitmap(bitmap, ViewWidth, Math.round(height), false);
                               final  ImageView image1 = (ImageView) findViewById(R.id.image1);
                                image1.setImageBitmap(bitmapF);
    undos.add(bitmapF);
                                image1.setOnTouchListener(new View.OnTouchListener() {
                                    @Override
                                    public boolean onTouch(View v, MotionEvent event) {
 Display d = getWindowManager().getDefaultDisplay();

                                        float dw = d.getWidth();
                                        float dh = d.getHeight();
                                        float x = event.getX();
                                         float y = event.getY();
      float r = 2;

                                        Bitmap BTX = ((BitmapDrawable)image1.getDrawable()).getBitmap();
                                        Canvas canvas = new Canvas(BTX);

                                       canvas.drawBitmap(BTX,0,0,null);
                                        Paint paint1 = new Paint();
                                      int bg1 =  buttonchoose.getDrawingCacheBackgroundColor();
                                        ColorDrawable buttonColor = (ColorDrawable) buttonchoose.getBackground();
                                        int bg2 =buttonColor.getColor();
                                        paint1.setColor(bg2);
                                        paint1.setShadowLayer(5, 2, 2, bg2);
                                        paint1.setStrokeWidth(20);


        switch(event.getAction()){

case MotionEvent.ACTION_DOWN:
 DownY = event.getY();
  DownX = event.getX();
    break;
case MotionEvent.ACTION_MOVE:
    MoveY = event.getY();
MoveX = event.getX();
    canvas.drawLine(DownX, DownY, MoveX, MoveY, paint1);
    image1.invalidate();
    DownX = MoveX;
    DownY=MoveY;

    break;
case MotionEvent.ACTION_UP:
    UpY = event.getY();
    UpX = event.getX();
    canvas.drawLine(DownX, DownY, UpX, UpY, paint1);
    image1.invalidate();
    Bitmap BTXYZ = ((BitmapDrawable)image1.getDrawable()).getBitmap();

    break;

 }

                                        return true;
                                    }
                                });
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "images");
            request.setParameters(parameters);
            request.executeAsync();






        }
    });

}






@Override
public void onClick(View v) {
  switch(v.getId()) {

        case R.id.buttonchoose:
        final ColorPicker cp = new ColorPicker(edit.this, 0, 0, 0);
     /* Show color picker dialog */
        cp.show();

/* On Click listener for the dialog, when the user select the color */
        Button okColor = (Button) cp.findViewById(R.id.okColorButton);
        okColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            /* You can get single channel (value 0-255) */
                int selectedColorR = cp.getRed();
                int selectedColorG = cp.getGreen();
                int selectedColorB = cp.getBlue();

            /* Or the android RGB Color (see the android Color class reference) */
                int selectedColorRGB = cp.getColor();
                Button buttonchoose = (Button) findViewById(R.id.buttonchoose);
                buttonchoose.setText("");
                buttonchoose.setBackgroundColor(selectedColorRGB);
                cp.dismiss();
            }
        });
        break;
      case R.id.imageButton2:

         int newImage = undos.size();
          Log.e("Lenght of Array",""+newImage);
          if(newImage >=1) {
              Bitmap newImage2 = undos.get(newImage-2);
              ImageView IMGXV = (ImageView) findViewById(R.id.image1);
              IMGXV.setImageBitmap(newImage2);

              undos.remove(newImage-1);
          }
          break;
    }
}
  }
case R.id.imageButton2:

      if(undos.size() > 0) {
          Bitmap newImage2 = undos.remove(undos.size() - 1);
          ImageView IMGXV = (ImageView) findViewById(R.id.image1);
          IMGXV.setImageBitmap(newImage2);
      }
      break;
Bitmap bitmapF = Bitmap.createScaledBitmap(bitmap, ViewWidth, Math.round(height), false);
final  ImageView image1 = (ImageView) findViewById(R.id.image1);
image1.setImageBitmap(bitmapF);
//The extra line before adding bitmap to arraylist.
Bitmap bitmapFcopy = bitmapFcopy.copy(bitmapFcopy.getConfig(), true);
undos.add(bitmapFcopy);
public class edit extends AppCompatActivity implements View.OnClickListener {

//Declare the imageview here and access this everywhere.
ImageView image1;
float MoveX,MoveY,DownY,DownX,UpY=0,UpX ;
List<Bitmap> undos = new ArrayList<Bitmap>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_edit);

   //Get the reference here
   image1 = (ImageView) findViewById(R.id.image1);
   final  ImageButton btn2 = (ImageButton) findViewById(R.id.imageButton2);
    btn2.setOnClickListener(this);
    final Button buttonchoose = (Button) findViewById(R.id.buttonchoose);
    buttonchoose.setOnClickListener(this);
    final RelativeLayout ViewX = (RelativeLayout) findViewById(R.id.RLXV);
    ViewTreeObserver vto = ViewX.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            ViewX.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            Intent intent7 = getIntent();
            String bitmapXY = (String) intent7.getExtras().get("BitmapImage");
            GraphRequest request = GraphRequest.newGraphPathRequest(
                    AccessToken.getCurrentAccessToken(),
                    "/" + bitmapXY,
                    new GraphRequest.Callback() {
                        @Override
                        public void onCompleted(GraphResponse response) {
                            HttpURLConnection connection = null;
                            try {
                                int ViewWidth = ViewX.getMeasuredWidth();
                                JSONObject photos = response.getJSONObject();
                                JSONArray linkY = photos.optJSONArray("images");
                                final JSONObject linkO = linkY.optJSONObject(0);
                                final String link2 = linkO.optString("source");
                                URL LINKF = new URL(link2);
                                connection = (HttpURLConnection) LINKF.openConnection();
                                connection.setDoInput(true);
                                connection.connect();
                                InputStream input = connection.getInputStream();
                                Bitmap bitmap = BitmapFactory.decodeStream(input);
                                float HH = (float) bitmap.getHeight();
                                float WW = (float) bitmap.getWidth();
                                float ViewWidthX = (float) ViewWidth;
                                float height = (HH / WW) * ViewWidthX;

                                Bitmap bitmapF = Bitmap.createScaledBitmap(bitmap, ViewWidth, Math.round(height), false);

                                //Remove from here and declare this in onCreate() and don't make it final                                   
                                //final ImageView image1 = (ImageView) findViewById(R.id.image1);
                                image1.setImageBitmap(bitmapF);
    undos.add(bitmapF);
                                image1.setOnTouchListener(new View.OnTouchListener() {
                                    @Override
                                    public boolean onTouch(View v, MotionEvent event) {
 Display d = getWindowManager().getDefaultDisplay();

                                        float dw = d.getWidth();
                                        float dh = d.getHeight();
                                        float x = event.getX();
                                         float y = event.getY();
      float r = 2;

                                        Bitmap BTX = ((BitmapDrawable)image1.getDrawable()).getBitmap();
                                        Canvas canvas = new Canvas(BTX);

                                       canvas.drawBitmap(BTX,0,0,null);
                                        Paint paint1 = new Paint();
                                      int bg1 =  buttonchoose.getDrawingCacheBackgroundColor();
                                        ColorDrawable buttonColor = (ColorDrawable) buttonchoose.getBackground();
                                        int bg2 =buttonColor.getColor();
                                        paint1.setColor(bg2);
                                        paint1.setShadowLayer(5, 2, 2, bg2);
                                        paint1.setStrokeWidth(20);


        switch(event.getAction()){

case MotionEvent.ACTION_DOWN:
 DownY = event.getY();
  DownX = event.getX();
    break;
case MotionEvent.ACTION_MOVE:
    MoveY = event.getY();
MoveX = event.getX();
    canvas.drawLine(DownX, DownY, MoveX, MoveY, paint1);
    image1.invalidate();
    DownX = MoveX;
    DownY=MoveY;

    break;
case MotionEvent.ACTION_UP:
    UpY = event.getY();
    UpX = event.getX();
    canvas.drawLine(DownX, DownY, UpX, UpY, paint1);
    image1.invalidate();
    Bitmap BTXYZ = ((BitmapDrawable)image1.getDrawable()).getBitmap();

    break;

 }

                                        return true;
                                    }
                                });
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "images");
            request.setParameters(parameters);
            request.executeAsync();






        }
    });

}






@Override
public void onClick(View v) {
  switch(v.getId()) {

        case R.id.buttonchoose:
        final ColorPicker cp = new ColorPicker(edit.this, 0, 0, 0);
     /* Show color picker dialog */
        cp.show();

/* On Click listener for the dialog, when the user select the color */
        Button okColor = (Button) cp.findViewById(R.id.okColorButton);
        okColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            /* You can get single channel (value 0-255) */
                int selectedColorR = cp.getRed();
                int selectedColorG = cp.getGreen();
                int selectedColorB = cp.getBlue();

            /* Or the android RGB Color (see the android Color class reference) */
                int selectedColorRGB = cp.getColor();
                Button buttonchoose = (Button) findViewById(R.id.buttonchoose);
                buttonchoose.setText("");
                buttonchoose.setBackgroundColor(selectedColorRGB);
                cp.dismiss();
            }
        });
        break;
      case R.id.imageButton2:

         int newImage = undos.size();
          Log.e("Lenght of Array",""+newImage);
          if(newImage >=1) {
              Bitmap newImage2 = undos.get(newImage-2);
              //Don't take reference again here
              //ImageView IMGXV = (ImageView) findViewById(R.id.image1);
              image1.setImageBitmap(newImage2);

              undos.remove(newImage-1);
          }
          break;
    }
}
  }