Android:滚动Imageview

我有一个ImageView是一个normale屏幕的高度的两倍(960浸)。 我想在屏幕上很好地滚动它。 屏幕底部应该包含一个button。 我已经尝试过各种ScrollView和Imageviews的组合,没有任何成功。 我也使用:isScrollContainer属性进行了细化,但没有结果。 任何人都知道如何做到这一点? 干杯,卢卡

@ cV2非常感谢你的代码。 这让我走向了我所需要的方向。 这是我的修改版本,停止滚动在图像的边缘…

// set maximum scroll amount (based on center of image) int maxX = (int)((bitmapWidth / 2) - (screenWidth / 2)); int maxY = (int)((bitmapHeight / 2) - (screenHeight / 2)); // set scroll limits final int maxLeft = (maxX * -1); final int maxRight = maxX; final int maxTop = (maxY * -1); final int maxBottom = maxY; // set touchlistener ImageView_BitmapView.setOnTouchListener(new View.OnTouchListener() { float downX, downY; int totalX, totalY; int scrollByX, scrollByY; public boolean onTouch(View view, MotionEvent event) { float currentX, currentY; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); downY = event.getY(); break; case MotionEvent.ACTION_MOVE: currentX = event.getX(); currentY = event.getY(); scrollByX = (int)(downX - currentX); scrollByY = (int)(downY - currentY); // scrolling to left side of image (pic moving to the right) if (currentX > downX) { if (totalX == maxLeft) { scrollByX = 0; } if (totalX > maxLeft) { totalX = totalX + scrollByX; } if (totalX < maxLeft) { scrollByX = maxLeft - (totalX - scrollByX); totalX = maxLeft; } } // scrolling to right side of image (pic moving to the left) if (currentX < downX) { if (totalX == maxRight) { scrollByX = 0; } if (totalX < maxRight) { totalX = totalX + scrollByX; } if (totalX > maxRight) { scrollByX = maxRight - (totalX - scrollByX); totalX = maxRight; } } // scrolling to top of image (pic moving to the bottom) if (currentY > downY) { if (totalY == maxTop) { scrollByY = 0; } if (totalY > maxTop) { totalY = totalY + scrollByY; } if (totalY < maxTop) { scrollByY = maxTop - (totalY - scrollByY); totalY = maxTop; } } // scrolling to bottom of image (pic moving to the top) if (currentY < downY) { if (totalY == maxBottom) { scrollByY = 0; } if (totalY < maxBottom) { totalY = totalY + scrollByY; } if (totalY > maxBottom) { scrollByY = maxBottom - (totalY - scrollByY); totalY = maxBottom; } } ImageView_BitmapView.scrollBy(scrollByX, scrollByY); downX = currentX; downY = currentY; break; } return true; } }); 

我相信它可以被细化一下,但是它工作得很好。 🙂

我search了这么久的代码,所以我想分享这个伟大的代码和平:

这段代码来自一个Activity,在后台有一个xml文件 ,里面包含一个叫做'img'的ImageView,

 <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/img" android:scaleType="center" android:background="#fff" android:src="@drawable/picName" /> 

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.xml_name_layout); final ImageView switcherView = (ImageView) this.findViewById(R.id.img); switcherView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View arg0, MotionEvent event) { float curX, curY; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mx = event.getX(); my = event.getY(); break; case MotionEvent.ACTION_MOVE: curX = event.getX(); curY = event.getY(); switcherView.scrollBy((int) (mx - curX), (int) (my - curY)); mx = curX; my = curY; break; case MotionEvent.ACTION_UP: curX = event.getX(); curY = event.getY(); switcherView.scrollBy((int) (mx - curX), (int) (my - curY)); break; } return true; } }); } 

完成了我的工作… 水平和垂直滚动包括 (启用)

只有消极的一面是…你可以滚动的图片的边缘…但这对我来说没有问题..花费一些时间,你可以轻松地实现这个function:)

祝你好运&有乐趣

这是我如何解决它:页

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbarAlwaysDrawVerticalTrack="true" > <ImageView android:contentDescription="Specs" android:adjustViewBounds="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:src="@drawable/specs" > </ImageView> 

最简单的方法是使用webview并通过本地html文件加载图像。 这样你也可以自动获取缩放控件,如果你想使用它们。 对于一个大的图像(即如果它是1000或3000像素宽),你会注意到,即使原始图像是清晰的和未压缩的,Android(Coliris)也不太擅长显示大的缩放图像。 这是一个已知的问题。 解决scheme是将大图分解成更小的图块,然后通过html(div或table)把它们放在一起。 我用这种方法为用户提供了一个地铁地图(大于屏幕和可滚动的地图)。

  WebView webView = (WebView)findViewById(R.id.webView); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setUseWideViewPort(true); webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR); webView.loadUrl( "content://com.myapp.android.localfile/sdcard/myappdata/common/mtr_map.html"); 

这可能适用于大多数情况下/“常规应用程序”,虽然取决于您的具体情况。 如果您将图像作为游戏的可滚动背景来讨论,则可能对您没有任何用处。

而不是一个HTML文件,你也可以直接加载图像(PNG,JPG格式)。 如果您不想使用变焦控制,请将其closures。

@wirbly非常感谢你的代码,它的工作方式正是我想要的。 但是当我第一次读你的代码时,我对你忘记定义的四个variables有点困惑。

所以,我想添加代码的定义,使其更清晰。

 Resources res=getResources(); Bitmap mBitmap = BitmapFactory.decodeResource(res, R.drawable.p_1920x1080); BitmapDrawable bDrawable = new BitmapDrawable(res, mBitmap); //get the size of the image and the screen int bitmapWidth = bDrawable.getIntrinsicWidth(); int bitmapHeight = bDrawable.getIntrinsicHeight(); int screenWidth = this.getWindowManager().getDefaultDisplay().getWidth(); int screenHeight = this.getWindowManager().getDefaultDisplay().getHeight(); 

希望这是有帮助的。

另一种方法是创build一个HorizontalScrollView ,将imageView添加到其中,然后将HorizontalScrollView添加到ScrollView 。 这使您可以向上,向下,向左,向右滚动。

嘿家伙们发现了一个简单而100%可靠的解决scheme,正如Mr X上面提到的那样(正如其他代码所提到的在某些设备上工作不正常或破坏)只需使用由android提供的ScrollView,

像这样的东西

 <ScrollView android:layout_width="fill_parent" android:id="@+id/scrollView1" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_above="@+id/button1" android:layout_alignParentRight="true" android:scrollbarAlwaysDrawVerticalTrack="true"> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal"> <ImageView android:src="@android:drawable/ic_menu_report_image" android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_width="wrap_content"></ImageView> </LinearLayout> </ScrollView> 

并在创造这样的事情

 if (mImageName != null) { InputStream is = null; try { is = this.getResources().getAssets().open("mathematics/"+mImageName); } catch (IOException e) { } Bitmap image = BitmapFactory.decodeStream(is); mImageView.setImageBitmap(image); } 

干杯!

它适用于我

 <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/img" android:scaleType="centerCrop" android:adjustViewBounds="true"/> <Button style="@style/btn" android:id="@+id/btn" android:layout_height="60dp" android:layout_marginTop="20dp" android:background="@drawable/btn" android:onClick="click" android:text="text" /> </LinearLayout> </ScrollView>