ScrollView也是一个Layout布局,可以让它内部的数据显示不下的时候出现滚动条,要注意的是不能在ScrollView中放多个组 件,如果放了多个组件,会出现如下错误:ERROR/AndroidRuntime(271): Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child (ScrollView只能包裹一个直接子元素)
我们看一个例子:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/ScrollView01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:stretchColumns="0" xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:text="色彩透明度测试" android:textSize="18dip"
android:layout_span="2" android:layout_gravity="center"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#ff00ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#ff00ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#ee00ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#ee00ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#dd00ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#dd00ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#cc00ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#cc00ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#bb00ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#bb00ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#aa00ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#aa00ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#9900ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#9900ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#8800ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#8800ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#7700ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#7700ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#6600ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#6600ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#5500ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#5500ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#4400ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#4400ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#3300ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#3300ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#2200ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#2200ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#1100ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#1100ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="30dip">
<TextView android:background="#0000ff00"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
<TextView android:text="#0000ff00" android:background="#000"
android:textSize="30dip" android:textColor="#fff"
android:layout_width="fill_parent" android:layout_height="fill_parent">
</TextView>
</TableRow>
<TextView android:text="色彩透明度测试" android:textSize="18dip"
android:gravity="center_horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</TableLayout>
</ScrollView>
例子的显示效果:
向下滚屏后的截图:
来自: http://hi.baidu.com/xiechengfa/blog/item/5da7c803fbe6e313738da5cb.html
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class MulitPointTouchListener implements OnTouchListener {
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
//初始状态值
private final int NONE = 0;
private final int DRAG = 1;
private final int ZOOM = 2;
private int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();//放大或缩小中间点
float oldDist = 1f;
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
analyseEvent(event);//Log分析事件类型,屏幕每个点的坐标
//处理事件
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
matrix.set(view.getImageMatrix());
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
//Log.d("CDH", "mode=DRAG");
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
//Log.d("CDH", "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
//Log.d("CDH", "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
//Log.e(""CDH"", "W:"+view.getWidth()+" H:"+view.getHeight());
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
} else if (mode == ZOOM) {
float newDist = spacing(event);
//Log.d("CDH", "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
return true;
}
private void analyseEvent(MotionEvent event) {
String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
"POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
StringBuilder sb = new StringBuilder();
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount()) sb.append(";");
}
sb.append("]");
Log.d("CDH", sb.toString());
}
//两点间的距离
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
//从两点中间放大或缩小
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}
创建好上面的OnTouchListener实现类后,设置到ImageView上
ImageView mImageView = (ImageView)findViewById(R.id.image_view); mImageView.setOnTouchListener(new MulitPointTouchListener());
设置属性android:scaleType="matrix"
<ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="matrix" android:src="/blog_article/@drawable/landscape/index.html" />
http://www.iteye.com/topic/971782