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

touchImageView.setImageBitmap(bm);


如果用户双击或2手指捏缩放,则图片会放大,但TouchImageView的宽度/高度保持不变。

当我放置更大的位图时,屏幕的宽度/高度将按预期工作:

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进行代码提示,因为该函数也用于计算宽度和高度。

希望有人会对此有所帮助。

10-08 17:07