我需要實現縮放,平移然後將ImageView重置爲原始比例/位置。我使用view.setImageMatrix(矩陣)在FrameLayout中執行縮放和移動。迄今爲止完美。要恢復到原始佈局,我嘗試保存原始矩陣並重置爲它,它不起作用,我有點卡住了。欣賞任何建議或提示!Android如何在放大並拖動後將ImageView佈局重置爲原始
下面是我的當前代碼:
- 一個ViewPager含有每圖片的片段
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
- 該片段包含的ImageView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/ivPage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:scaleType="matrix"
android:src="@drawable/catalogue_test" />
</FrameLayout>
- Java代碼做的縮放和平移工作就好了,這實際上是從一個教程頁原稿:
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
Log.d(tag, "ACTION_DOWN");
isDoubleTap = false;
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(tag, "mode=DRAG");
mode = DRAG;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
Log.d(tag, "ACTION_POINTER_UP");
mode = NONE;
Log.d(tag, "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
Log.d(tag, "ACTION_MOVE");
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
} else if (mode == ZOOM) {
double newDist = spacing(event);
Log.d(tag, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = (float) (newDist/oldDist);
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
if(isDoubleTap == false) {
view.setImageMatrix(matrix);
Log.d(tag, String.format("(%d, %d) - scale(%f, %f) xy(%f, %f)", view.getTop(), view.getLeft(), view.getScaleX(), view.getScaleY(), view.getX(), view.getY()));
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(tag, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(tag, "mode=ZOOM");
}
break;
}
gestureDetector.onTouchEvent(event);
return true; // indicate event was handled
}
嘗試使您的視圖無效。 – sat 2013-02-16 19:48:40
我試了下面,似乎沒有工作:final ViewPager vp =(ViewPager)getActivity()。findViewById(R.id.pager); vp.invalidate(); v.invalidate(); – Sean 2013-02-17 00:47:45
vp是包含imageView的viewPager。 v是imageView – Sean 2013-02-17 00:56:39