androidandroid-layouttouchimageview

Android: Square TouchImageView?


I'm currently using the library TouchImageView here:

https://github.com/MikeOrtiz/TouchImageView

This works perfectly fine when I fill the entire phone's screen with the TouchImageView, but how would I constrain the visible area to a square?

I've tried:

public class SquareTouchImageView extends TouchImageView {
    public SquareTouchImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public SquareTouchImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareTouchImageView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

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

        int width = getMeasuredWidth();
        setMeasuredDimension(width, width);
    }
}

But that doesn't let me scroll down to show the rest of the image (if it's taller than it is wide)

Is there a way I can enable a square TouchImageView?

If so how would I be able to do it?


Solution

  • I've solved it by doing the following and I hope this helps whoever else might have this issue in future.

    public class SquareTouchImageView extends TouchImageView {
        public SquareTouchImageView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }
    
        public SquareTouchImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public SquareTouchImageView(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            Drawable drawable = getDrawable();
            if (drawable == null || drawable.getIntrinsicWidth() == 0
                    || drawable.getIntrinsicHeight() == 0) {
                setMeasuredDimension(0, 0);
                return;
            }
    
            int drawableWidth = drawable.getIntrinsicWidth();
            //int drawableHeight = drawable.getIntrinsicHeight();
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            //int heightSize = widthSize;// MeasureSpec.getSize(heightMeasureSpec);
            //int heightMode = widthMode;// MeasureSpec.getMode(heightMeasureSpec);
            viewWidth = setViewSize(widthMode, widthSize, drawableWidth);
            viewHeight = viewWidth;// setViewSize(heightMode, heightSize,
                                    // drawableHeight);
    
            //
            // Set view dimensions
            //
            setMeasuredDimension(viewWidth, viewHeight);
    
            //
            // Fit content within view
            //
            fitImageToView();
        }
    }
    

    You may have to change some variables in TouchImageView from private to protected.

    And XML:

        <com.aura.app.widget.SquareTouchImageView
            android:id="@+id/scrollingImage"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />