0

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?

David Xu
  • 5,555
  • 3
  • 28
  • 50
  • Have you tried simply putting an "android:onclick=[function()]" directly on the XML for the imageView? – durbnpoisn Jun 10 '14 at 18:04
  • Yeah, that didn't help, Basically the image in the TouchImageView is anchored in the wrong place for some reason. – David Xu Jun 10 '14 at 18:15
  • As an alternate idea, you can make a PNG that is totally transparent, and anchor that wherever you want - effectively making a sensor. – durbnpoisn Jun 10 '14 at 18:28

1 Answers1

0

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" />
David Xu
  • 5,555
  • 3
  • 28
  • 50