如何在android中裁剪imageview的矩形图像

时间:2014-06-13 06:17:20

标签: android bitmap crop

我正在imageview上绘制矩形形状,我得到那个矩形形状的所有坐标,这样我就需要在imageview上拖动到矩形形状的任何图像。我正在尝试将imageview转换为位图,然后我捕获该矩形部分并在弹出窗口显示,但它不起作用请帮帮我

代码:

  public class MyActivity extends Activity {

    private static final String TAG = "MyActivity";
     PopupWindow ppwindow;
     Button btnClosePopup;
     ImageView imagpopup;
     ImageView dummyimage;
     int imagewidth,imageheight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final DragRectView view = (DragRectView) findViewById(R.id.dragRect);
                 dummyimage=(ImageView) findViewById(R.id.image);
        if (null != view) {
            view.setOnUpCallback(new DragRectView.OnUpCallback() {
                @Override
                public void onRectFinished(final Rect rect) {
                    Toast.makeText(getApplicationContext(), "Rect is (" + rect.left + ", " + rect.top + ", " + rect.right + ", " + rect.bottom + ")",
                            Toast.LENGTH_LONG).show();

                    ViewTreeObserver vto = dummyimage.getViewTreeObserver();
                    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                        public boolean onPreDraw() {
                            dummyimage.getViewTreeObserver().removeOnPreDrawListener(this);
                           imageheight = dummyimage.getMeasuredHeight();
                           imagewidth = dummyimage.getMeasuredWidth();

                            return true;
                        }
                    });



                    Bitmap bitmap1 = Bitmap.createScaledBitmap(
                            ((BitmapDrawable) dummyimage.getDrawable())
                                    .getBitmap(),imagewidth,imageheight, false);

                    System.out.println(rect.height() + "===    "
                            + bitmap1.getHeight() + "  ====    " + rect.width()
                            + "  =====  " + bitmap1.getWidth());
                    if (rect.height() <= bitmap1.getHeight()
                            && rect.width() <= bitmap1.getWidth()) {
                        Bitmap bitmap = Bitmap.createBitmap(bitmap1,
                                view.getLeft(), view.getTop(), view.getWidth(),
                                view.getHeight());
                      showpopUp(bitmap);


                    }}
            });
        }
    }

     private void showpopUp(Bitmap bitmap) {

         LayoutInflater inflater = (LayoutInflater) MyActivity.this
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                 View layout = inflater.inflate(R.layout.screen_popup,
                 (ViewGroup) findViewById(R.id.popup_element));

                 ppwindow = new PopupWindow(layout, 100, 140, true);
                 ppwindow.showAtLocation(layout, Gravity.AXIS_Y_SHIFT, 0, 0);
                 btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
                 imagpopup=(ImageView) layout.findViewById(R.id.imageView_poup);
                 imagpopup.setImageBitmap(bitmap);
                 btnClosePopup.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        ppwindow.dismiss();


                    }
                });
    }
}

0 个答案:

没有答案
相关问题