0

I'm trying to create a custom ImageView, where the user can pinch to resize the view but the method doesn't do anything even the detector factor has always different values.

 public class Art extends AppCompatImageView  {

    private ScaleGestureDetector mScaleDetector;
    private  float mScaleFactor = 1.f;


    public Art(Context context) {
        super(context);
       mScaleDetector = new ScaleGestureDetector(context , new scaleListner());
    }

    public Art(Context context, AttributeSet attrs) {
        super(context, attrs);

       mScaleDetector = new ScaleGestureDetector(context , new scaleListner());
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        event.setLocation(event.getRawX(), event.getRawY());
        mScaleDetector.onTouchEvent(event);

        performClick();

        return true;
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

       canvas.save();
       canvas.scale(mScaleFactor, mScaleFactor);
       canvas.restore();

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int imageWidth = MeasureSpec.getSize(widthMeasureSpec);
        int imageHeight = MeasureSpec.getSize(heightMeasureSpec);

        int scaledWidth = Math.round(imageWidth * mScaleFactor);
        int scaledHeight = Math.round(imageHeight * mScaleFactor);

        setMeasuredDimension(
                Math.min(imageWidth , scaledWidth) ,
                Math.min(imageHeight , scaledHeight)
        );

    }


    private class scaleListner extends ScaleGestureDetector.SimpleOnScaleGestureListener {

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            return super.onScaleBegin(detector);
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {

            mScaleFactor *= detector.getScaleFactor();
            mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

            invalidate();
            return super.onScale(detector);
        }
    }
}

and the images are added using setImageResource in the MainActivity

Can anyone help me figure out the problem?

Pang
  • 9,564
  • 146
  • 81
  • 122
JAO
  • 1
  • call `super.onDraw(canvas);` after `canvas.scale(mScaleFactor, mScaleFactor);` but before `canvas.restore();` – pskink Feb 03 '18 at 07:20
  • This rescales the canvas inside the bounds of the View, but what I want is to resize the whole view when the user pinches in and out – JAO Feb 03 '18 at 08:56
  • so make your custom `ImageView` full screen, now it has a fixed size - with full screen view you can scale your image anywhere – pskink Feb 03 '18 at 09:00
  • The custom ImageView takes very various images inside it, and there's another ImageView behind it in a frameLayout, so it's size has to be fixed, but only the user can resize it, I don't insist on pinch to zoom, but after trials to make something different, it seemed to be the easiest way. – JAO Feb 03 '18 at 09:04
  • what actually do you want to achieve? whats your goal? what another `ImageView` are you talking about? – pskink Feb 03 '18 at 09:06
  • My goal is to achieve resizing functionality, that the user can add art or any image he wishes on another ImageView. something like in galleries or Instagram when the user adds vectors on his pictures – JAO Feb 03 '18 at 09:08
  • exactly like this : https://stackoverflow.com/questions/17971884/how-to-create-resizable-imageview-in-android – JAO Feb 03 '18 at 09:12
  • so you want multiple images on one view? something like [this](https://stackoverflow.com/a/21657145/2252830)? – pskink Feb 03 '18 at 09:16
  • yes, the lower one has fixed dimensions, the upper one dimensions can be modified, and can be translated. – JAO Feb 03 '18 at 09:20
  • what lower / upper ? – pskink Feb 03 '18 at 09:21
  • Lower: The bigger Image, which we add the another to it. – JAO Feb 03 '18 at 09:24
  • so i gave that code: all you have to do is to draw a "big" fixed bottom `Bitmap` inside `ViewPort#onDraw` method - do that before calling `for (Layer l : layers) {` loop – pskink Feb 03 '18 at 09:25
  • Thank you, I'm gonna try it – JAO Feb 03 '18 at 09:33
  • sure, use `Canvas#drawBitmap(Bitmap, Matrix, Paint)` method – pskink Feb 03 '18 at 09:57

0 Answers0