我的ImageView是普通屏幕高度的两倍(960倾角)。我想在屏幕上上下滚动它。屏幕底部应包含一个按钮。我尝试过ScrollView和Imageview的各种组合,没有任何成功。我还使用:isScrollContainer属性进行了细化而没有结果。谁知道怎么做? 干杯, 卢卡
答案 0 :(得分:33)
@ 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;
}
});
我确信它可以稍微改进一下,但效果还不错。 :)
答案 1 :(得分:28)
我搜索了这段代码的时间很长,所以我想分享这段伟大的代码:
此代码来自一个Activity,其后端包含 xml文件 ImageView ,名为'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;
}
});
}
完美地完成了我的工作...... 横向&amp;包括垂直滚动(已启用)
唯一消极的一面是...你可以滚动图片的边缘...... 但这对我来说没问题..花一些时间你可以轻松实现这个功能:)
祝你好运&amp;&amp;玩得开心答案 2 :(得分:15)
这是我修复它的方式: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>
答案 3 :(得分:8)
@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();
希望它有用。
答案 4 :(得分:6)
最简单的方法是使用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文件。如果您不想使用变焦控制,只需将其关闭即可。
答案 5 :(得分:2)
另一种方法是创建HorizontalScrollView
,将imageView
添加到其中,然后将HorizontalScrollView
添加到ScrollView
。这样您就可以向上,向下,向左,向右滚动。
答案 6 :(得分:1)
它对我有用
<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>
答案 7 :(得分:0)
类似这样的事情
<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);
}
干杯!