为什么我的撤销按钮在android java中不起作用?

时间:2016-02-06 08:36:18

标签: java android canvas bitmap

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

  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;
    }
}
  }

1 个答案:

答案 0 :(得分:0)

试试这个确切的代码 -

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

编辑在onGlobalLayout()中添加了额外的一行,试试这个 -

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;
    }
}
  }

编辑2

def auto_init(local_name_space):
    """Set instance attributes from arguments.
    """
    self = local_name_space.pop('self')
    for name, value in local_name_space.items():
        setattr(self, name, value)