【发布时间】:2016-10-21 14:14:57
【问题描述】:
活动布局:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vertical">
<HorizontalScrollView
android:id="@+id/horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/linearparent"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tulips"/>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
Activity.java
public class MainActivity extends Activity {
enter code here
ScaleGestureDetector mScaleDetector;
float mScaleFactor = 1.f;
RelativeLayout parentLayout;
ImageView image;
HorizontalScrollView horizontal;
ScrollView vertical;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);
mScaleDetector = new ScaleGestureDetector(this, new ScaleListener());
parentLayout = (RelativeLayout)findViewById(R.id.linearparent);
horizontal = (HorizontalScrollView)findViewById(R.id.horizontal);
image = (ImageView)findViewById(R.id.imageView1);
vertical = (ScrollView)findViewById(R.id.vertical);
}
public class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener
{
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
image.setScaleX(mScaleFactor);
image.setScaleY(mScaleFactor);
ViewGroup vg = (ViewGroup) findViewById (R.id.horizontal);
vg.requestLayout();
return true;
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if(ev.getPointerCount() == 2){
mScaleDetector.onTouchEvent(ev);
return true;
}else{
Log.d("TOUCH", "dispatchTouchEvent to horizonal");
horizontal.dispatchTouchEvent(ev);
vertical.dispatchTouchEvent(ev);
return true;
}
}
}
如果图像视图的图像源尺寸较大,则默认情况下它也支持滚动和缩放。但是,如果图像与最初的设备屏幕相比较小,则缩放后无法滚动。那是最初不会有任何滚动支持,因为内容不大,并且缩放后内容也不会滚动。
【问题讨论】:
标签: scale gesture android-scrollview horizontalscrollview