将ScrollView中的ImageView设置为屏幕的两倍

时间:2016-08-29 13:47:55

标签: java android

我试图在TwoDScrollView中使用ImageView创建简单的地图(所以我可以同时向两个方向滚动)。

这是我的xml布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:clickable="true" >

    <com.example.test.TwoDScrollView
        android:id="@+id/scrollView"
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </com.example.test.TwoDScrollView>

</FrameLayout>

TwoDScrollView取自此处: https://github.com/derekbrameyer/CustomScrollViews/blob/master/library/src/com/doomonafireball/customscrollviews/widget/TwoDScrollView.java

然后是这里的java代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_area, container, false);

    scrollView = (TwoDScrollView) root.findViewById(R.id.scrollView);

    MainActivity act = (MainActivity) getActivity();
    Drawable map = act.getResources().getDrawable(R.drawable.my_map);

    Bitmap bitmap = ((BitmapDrawable) map).getBitmap();

    int viewWidth = container.getWidth();
    int viewHeight = container.getHeight();

    int imageWidth = bitmap.getWidth();
    int imageHeight = bitmap.getHeight();

    int desiredHeight = viewHeight * 2;

    float r = desiredHeight / (float) imageHeight;

    int desiredWidth = (int) (imageWidth * r);

    ImageView imageView = new ImageView(act);
    imageView.setAdjustViewBounds(true);
    imageView.setImageBitmap(bitmap);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);

    scrollView.addView(imageView);

    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(desiredWidth, desiredHeight);
    imageView.setLayoutParams(params);
    imageView.invalidate();

    return root;
}

在我的情况下,值是:

  

viewWidth = 1440

     

viewHeight = 2308

     

imageWidth = 2000

     

imageHeight = 1603

     

desiredWidth = 5759

     

desiredHeight = 4616

     

r = 2.8796008

然而 - 它被忽略了。图像高度太小(我可以看到部分背景),宽度略微可滚动。

我该如何解决这个问题?

此致

1 个答案:

答案 0 :(得分:0)

所以,我没有使用TwoDScrollView,而是使用了ImageView,手势支持来自这里(我还是需要它):

https://github.com/thuytrinh/android-collage-views

我只是强制它在启动时重新调整我的imageView:

listener = new MultiTouchListener();
listener.minimumScale = 2.0f;
listener.maximumScale = 8.0f;
listener.isRotateEnabled = false;
TransformInfo info = new TransformInfo(); //had to extract this class to different file
info.deltaScale = 2.0f;
info.deltaAngle = 0.0f;
info.deltaX = 0.0f;
info.deltaY = 0.0f;
info.pivotX = 0.0f;
info.pivotY = 0.0f;
info.minimumScale = listener.minimumScale;
info.maximumScale = listener.maximumScale;
imageView.setOnTouchListener(listener);
MultiTouchListener.move(imageView, info); //had to make it public