在Android中,当我在画布上绘制一个点时,当我移动,缩放或旋转该位图时,该点不会停留在位图上

时间:2018-10-30 05:06:57

标签: android bitmap imageview android-canvas gesture

我的应用有两个按钮。一种用于从存储中选择图像并将其加载到ImageView的方法。我使用Picasso库来做。另一个按钮在“画布”上绘制一个点。我已经在Imageview上附加了旋转,缩放和移动手势检测器。

这里的问题是,在我绘制点之后,我希望在执行移动手势时该点可以移动。同样,执行缩放手势时,该点应放大或缩小。但重点一点都没有。

MainActivity.class

    import android.app.Dialog;
    import android.content.Intent;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.PointF;
    import android.graphics.drawable.BitmapDrawable;
    import android.net.Uri;
    import android.provider.MediaStore;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.FloatMath;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.ScaleGestureDetector;
    import android.view.View;
    import android.view.animation.RotateAnimation;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.almeros.android.multitouch.MoveGestureDetector;
    import com.almeros.android.multitouch.RotateGestureDetector;
    import com.github.chrisbanes.photoview.PhotoView;
    import com.squareup.picasso.Picasso;

    public class MainActivity extends AppCompatActivity {

    private static final int PICK_IMAGE = 1;
    ImageView marker_iv;
    DrawImageView iv;
    TextView tvFloor;
    Button checkpointBt, selectBt;
    RelativeLayout rootLayout;

    private Matrix mMatrix = new Matrix();
    private float mScaleFactor = 1f;
    private float mRotationDegrees = 0.f;
    private float mFocusX = 0.f;
    private float mFocusY = 0.f;
    private int mAlpha = 255;
    private int mImageHeight, mImageWidth;

    private ScaleGestureDetector mScaleDetector;
    private RotateGestureDetector mRotateDetector;
    private MoveGestureDetector mMoveDetector;

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

    private void initViews() {
        iv = findViewById(R.id.main_iv);
        tvFloor = findViewById(R.id.floormain_tv);
        marker_iv = findViewById(R.id.marker_iv);
        checkpointBt = findViewById(R.id.check_bt);
        selectBt = findViewById(R.id.select_bt);
        rootLayout = findViewById(R.id.root);
        mFocusX = rootLayout.getWidth() / 2;
        mFocusY = rootLayout.getHeight() / 2;
        mScaleDetector = new ScaleGestureDetector(getApplicationContext(), new 
        ScaleListener());
        mRotateDetector = new RotateGestureDetector(getApplicationContext(), new 
        RotateListener());
        mMoveDetector = new MoveGestureDetector(getApplicationContext(), new 
        MoveListener());
    }

    private void setListeners() {

        iv.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mScaleDetector.onTouchEvent(event);
                mRotateDetector.onTouchEvent(event);
                mMoveDetector.onTouchEvent(event);
                float scaledImageCenterX = (mImageWidth * mScaleFactor) / 2;
                float scaledImageCenterY = (mImageHeight * mScaleFactor) / 2;

                mMatrix.reset();
                mMatrix.postScale(mScaleFactor, mScaleFactor);
                mMatrix.postRotate(mRotationDegrees, scaledImageCenterX, 
                scaledImageCenterY);
                mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - 
                scaledImageCenterY);

                ImageView view = (ImageView) v;
                view.setImageMatrix(mMatrix);
                view.setAlpha(mAlpha);
                return true;
            }
        });

        checkpointBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                iv.x = marker_iv.getX() + 45;
                iv.y = marker_iv.getY() + 45;


                iv.invalidate();
                iv.drawRect = true;

            }
        });

        selectBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, PICK_IMAGE);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable 
    Intent data) {
        if (requestCode == PICK_IMAGE & resultCode == RESULT_OK) {
            if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && null != 
    data) {
                Uri selectedImage = data.getData();
                Picasso.get().load(selectedImage).noPlaceholder().into(iv);
                mImageWidth = iv.getWidth();
                mImageHeight = iv.getWidth();
                float scaledImageCenterX = (mImageWidth * mScaleFactor) / 2;
                float scaledImageCenterY = (mImageHeight * mScaleFactor) / 2;
                mMatrix.postScale(mScaleFactor, mScaleFactor);
                mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - 
                scaledImageCenterY);
                iv.setImageMatrix(mMatrix);
                showTheDialog();
            }
        }
    }

    private void showTheDialog() {
        final Dialog d = new Dialog(this);
        d.setContentView(R.layout.floornamedialog);
        d.setCancelable(false);
        final EditText enteredFloor = d.findViewById(R.id.floor_et);
        Button dialogButton = d.findViewById(R.id.floor_bt);
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = enteredFloor.getText().toString();
                tvFloor.setText("Floor Name: " + str);
                d.dismiss();
            }
        });
        d.show();
    }

    private class ScaleListener extends 
    ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor(); // scale change since 
    previous event
            mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));
         /* iv.setScaleX(mScaleFactor);
            iv.setScaleX(mScaleFactor);*/
            return true;
        }
    }

    private class RotateListener extends 
    RotateGestureDetector.SimpleOnRotateGestureListener {
        @Override
        public boolean onRotate(RotateGestureDetector detector) {
            mRotationDegrees -= detector.getRotationDegreesDelta();
            // iv.setRotation(mRotationDegrees);
            return true;
        }
    }

    private class MoveListener extends 
    MoveGestureDetector.SimpleOnMoveGestureListener {
        @Override
        public boolean onMove(MoveGestureDetector detector) {
            PointF d = detector.getFocusDelta();
            mFocusX += d.x;
            mFocusY += d.y;
           /* iv.setX(mFocusX);
            iv.setY(mFocusY);*/
            return true;
        }
    }
}

DrawImageView.java

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;

@SuppressLint("AppCompatCustomView")
public class DrawImageView extends ImageView {

private Paint currentPaint;
public boolean drawRect = false;
public float x;
public float y;
public Bitmap newBitmap;
public Matrix matrix;


public DrawImageView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    currentPaint = new Paint();
    currentPaint.setDither(true);
    currentPaint.setColor(0xFF00CC00);
    currentPaint.setStyle(Paint.Style.STROKE);
    currentPaint.setStrokeJoin(Paint.Join.ROUND);
    currentPaint.setStrokeCap(Paint.Cap.ROUND);
    currentPaint.setStrokeWidth(60);
}

public void setNewBitmap(Bitmap bmp){
    newBitmap = bmp;
}

public Bitmap getNewBitmap(){
    return newBitmap;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if(drawRect){
       canvas.drawPoint(x,y,currentPaint);
       //canvas.drawBitmap(newBitmap,new Matrix(),null);
        //canvas.drawColor(0, PorterDuff.Mode.CLEAR);
    }
  }

}

图片-

  • 这是在我从图库中选择图像并将其加载到imageview之后的状态

This is right after I selected an image from gallery and load it into imageview

  • 这是在我按下“添加检查点按钮”之后。出现绿点

This is right after I press the 'Add checkpoint button'. The green dot appears

  • 这是我移动图像的时间。点留在那里。我希望它随位图一起移动

This is when I moved the image. The dot stays there. I want it to move with the bitmap

0 个答案:

没有答案