Android:Square TouchImageView?

时间:2014-06-10 17:54:43

标签: android android-layout touchimageview

我目前正在使用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?

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

1 个答案:

答案 0 :(得分: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中的某些变量从private更改为protected

和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" />