android TouchImageView 高度不变

android TouchImageView height don't change

我正在使用 TouchImageView (https://github.com/MikeOrtiz/TouchImageView) 全屏显示位图。当我放置普通位图时:

touchImageView.setImageBitmap(bm);

如果用户双击或两指捏合缩放图片缩放但 TouchImageView width/height 保持不变。

当我放置比屏幕大的位图时 width/height 一切都按预期工作:

Bitmap new_bm = Bitmap.createScaledBitmap(bm, 
            more_px_then_screen_width, more_px_then_screen_height, false);
touchImageView.setImageBitmap(bm);

这是 xml:

    <TouchImageView 
    android:id="@+id/picture_full_screen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />

请帮忙!

经过调查我发现了这个函数:

/**
 * Set view dimensions based on layout params
 * 
 * @param mode 
 * @param size
 * @param drawableWidth
 * @return
 */
private int setViewSize(int mode, int size, int drawableWidth) {
    int viewSize;
    switch (mode) {
    case MeasureSpec.EXACTLY:
        viewSize = size;
        break;

    case MeasureSpec.AT_MOST:
        viewSize = Math.min(drawableWidth, size);
        break;

    case MeasureSpec.UNSPECIFIED:
        viewSize = drawableWidth;
        break;

    default:
        viewSize = size;
        break;
    }
    return viewSize;
}

稍后将由 onMeasure 中的用户计算 viewWidthviewHeight 用于

setMeasuredDimension(viewWidth, viewHeight);

setViewSize 更改为这个得到了我想要的效果:

/**
 * Set view dimensions based on layout params
 * 
 * @param mode 
 * @param size
 * @param drawableSize
 * @return
 */
private int setViewSize(int mode, int size, int drawableSize) {
    int viewSize;
    switch (mode) {
    case MeasureSpec.EXACTLY:
        viewSize = (int) (size*Math.max(normalizedScale, 1.0));
        break;

    case MeasureSpec.AT_MOST:
        viewSize = (int) (size*Math.max(normalizedScale, 1.0));
        break;

    case MeasureSpec.UNSPECIFIED:
        viewSize = drawableSize;
        break;

    default:
        viewSize = (int) (size*Math.max(normalizedScale, 1.0));
        break;
    }
    return viewSize;
}

我把drawableWidth的名字改成drawableSize为了代码排版,因为这个函数也是用来计算宽高的

希望有人会觉得这有用。