显示图像的选定部分

时间:2017-11-20 10:58:25

标签: android android-canvas ontouchlistener

我正在实现滚动和裁剪功能。 附上了截图供参考。

我想要实现的目标是:

  1. 当用户将手指放在较大的图像上时,我需要缩放所选区域。

  2. 我需要放置一个矩形框,表示所选区域。

  3. 用户应该可以通过在editText中输入不同的值来更改此框的大小。

  4. 确认后我想裁剪并将所选区域发送到我的服务器。

  5. ****我面临的问题:****

    1. 我无法在选定区域放置此矩形框。

    2. 我需要帮助了解如何在EditText中的值更改时更改此矩形框的大小。

    3. 当我拖动较大的图像时,我只能缩放左边三分之一的图像。另外两个三分之一没有缩小。

    4. 这是我在Activity中的OnTouchListener

        mural.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View view, MotionEvent event) {
      
                  int topParam = mural.getPaddingTop();
                  int rightParam = mural.getPaddingRight();
                  int maxTopParam = topParam + mural.getMaxHeight();
                  int maxRightParam = rightParam + mural.getMaxWidth();
      
                  int x = 0, y = 0;
      
                  Matrix m = new Matrix();
                  if (event.getX() > topParam && event.getX() < maxTopParam) {
                      x = (int) event.getX();
      
                  }
      
                  if (event.getY() > maxRightParam && event.getY() < rightParam) {
                      y = (int) event.getY();
      
                  }
                  mBitmap = ((BitmapDrawable) mural.getDrawable()).getBitmap();
                  Bitmap croppedBitmap = Bitmap.createBitmap(mBitmap, x, y, 80, 80, m, true);
                  croped.setImageBitmap(croppedBitmap);
      
                  return true;
              }
          });
      

      这是我的XML文件

      <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          tools:context="com.outlineindia.zooming_drag_touch_crop_portion_image_examaple.MainActivity">
      
          <ImageView
              android:layout_gravity="left"
              android:layout_marginBottom="20dp"
              android:visibility="visible"
              android:id="@+id/cropedmg"
              android:layout_marginTop="20dp"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />
      
          <ImageView
      
              android:visibility="visible"
              android:id="@+id/mural"
              android:src="@drawable/muraltheme"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />
      
      </LinearLayout>
      

1 个答案:

答案 0 :(得分:-2)

关于问题

  

我需要帮助了解如何在EditText中的值更改时更改此rectangulat框的大小。

您需要为您的edittext实施addTextChangedListener。在此方法中,您将更新矩形尺寸。

代码参考:

editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(TextUtils.isEmpty(s)) {
                // call method
            }
        }
    });
相关问题