我的ImageView是普通屏幕高度的两倍(960倾角).我想在屏幕上上下滚动它.屏幕底部应包含一个按钮.我尝试过ScrollView和Imageview的各种组合,没有任何成功.我还使用: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; } });
我确信它可以稍微改进一下,但它的效果非常好.:)
我搜索了这段代码这么长时间,所以我想分享这个代码的伟大和平:
此代码来自一个Activity,后端有一个xml文件,包含一个名为'img' 的ImageView
@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; } }); }
我完美地完成了这项工作...... 包括水平和垂直滚动(已启用)
只有消极的一面是......你可以滚动图片的边缘......但这对我来说没问题..花一些时间你可以轻松实现这个功能:)
祝你好运&&玩得开心
这是我修复它的方式:p
@wirbly非常感谢您的代码,它完全按照我想要的方式工作.但是当我第一次阅读你的代码时,我对你忘记定义的四个变量感到有些困惑.
所以,我想添加代码的定义,使其更清晰.
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();
希望它有用.
最简单的方法是使用webview并通过本地html文件将图像加载到其中.这样,如果要使用它们,您还将自动获得缩放控件.对于大图像(即,如果宽度为1000或3000像素),您会注意到即使原始图像清晰且未压缩,Android(Coliris)也不能很好地显示非常清晰的大缩放图像.这是一个已知的问题.解决方案是将大图像分解为更小的图块,然后通过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");
这可能适用于大多数情况/"常规应用",但取决于您的具体情况.如果您将图像称为游戏的可滚动背景,则可能对您没有用.
您也可以直接加载图像(png,jpg),而不是html文件.如果您不想进行变焦控制,只需将其关闭即可.