1

我正在使用 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" />

请帮忙 !

4

1 回答 1

1

经过调查,我发现了这个功能:

/**
 * 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;
}

我将名称更改drawableWidthdrawableSize代码潮汐,因为该函数也用于计算宽度和高度。

希望有人会觉得这很有帮助。

于 2015-02-05T14:19:44.713 回答