0

我目前在这里使用库 TouchImageView:

https://github.com/MikeOrtiz/TouchImageView

当我用 TouchImageView 填充整个手机的屏幕时,这非常有效,但是我如何将可见区域限制为正方形?

我试过了:

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);
    }
}

但这并不能让我向下滚动以显示图像的其余部分(如果它高于它的宽度)

有没有办法可以启用方形 TouchImageView?

如果是这样,我怎么能做到?

4

1 回答 1

0

我已经通过执行以下操作解决了它,我希望这对将来可能遇到此问题的其他人有所帮助。

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();
    }
}

您可能需要将 TouchImageView 中的一些变量从 更改privateprotected

和 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" />
于 2014-06-10T18:46:19.647 回答