Android:滚动图像视图
我有一个ImageView,是正常屏幕高度的两倍(960倾角)。我想在屏幕上上下滚动。屏幕底部应该包含一个按钮。我已经试过ScrollView和Imageviews的各种组合,没有任何成功。我也用没有结果的isScrollContainer属性进行了细化。任何人都知道如何做到这一点? 欢呼声, LucaAndroid:滚动图像视图
@ 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;
}
});
我敢肯定,它可以细化一点,但它工作得很好。 :)
不客气; =))):=) – cV2 2012-08-23 08:27:06
@wirbly:我使用这段代码对我来说工作完美,但只是想在滚动左右的时候滚动一半的屏幕宽度。我们如何实现这一目标?我必须改变 – 2013-03-08 09:55:30
你能发布完整的工作代码吗? – 2016-05-11 09:05:06
最简单的方法是使用webview并通过本地html文件加载图像到其中。如果你想使用它们,你也可以自动获取缩放控件。 对于较大的图像(即,如果它是1000或3000像素宽),您会注意到Android(Coliris)不太擅长显示大幅缩放的图像,即使原始图像清晰且未压缩)。这是一个已知的问题。解决方案是将大图分解成更小的区块,并通过html(div或表格)将它们重新组合在一起。 我使用这种方法为用户提供地铁地图(大于屏幕和可滚动)。
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格式)。如果您不想使用变焦控制,请关闭它们。
我搜索了这个代码这么长时间,所以我想分享的代码,这个伟大的和平:
这个代码是从一个活动,这对含有的ImageView的后端一个xml文件称为 'IMG'
<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;
}
});
}
做的工作完美的我... 水平&垂直滚动包括(启用)
只有消极的一面是...你可以在图片... 的边缘滚动,但是这是没有问题的我..并且花一些时间,你可以很容易地实现此功能:)
好运& &乐趣
另一种方法是创建一个HorizontalScrollView
,添加imageView
进去,然后添加HorizontalScrollView
成ScrollView
。这允许你向上,向下,向左,向右滚动。
我刚刚尝试过,因为它看起来不错而且容易,但用户体验很不幸令人失望。滚动只发生在水平或垂直一次,不能发生在对角线上。更糟糕的是:假设垂直滚动是外部容器,那么如果你做了一个不接近完美水平的对角滑动,即使滑动的垂直分量比其水平分量小得多,滚动也会垂直发生。 – Balint 2014-02-01 21:03:22
嘿,伙计们发现了一个容易和100%可靠的解决方案作为X先生上面提到的(如所提及的其他代码不工作的工作在某些设备上或断裂) 简单地使用滚动型由Android提供的,它需要的东西护理
像这样
<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>
和OnCreate中像这样
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'只能垂直滚动。 – 2014-10-19 10:06:51
@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();
希望它有帮助。
这是我的固定它:P
<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>
简单的解决方案..更喜欢。 – Santanu 2016-10-11 11:17:46
它为我工作
<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>
它是:底部应该包含一个按钮...很抱歉的错字。 – luca 2010-06-17 00:37:37