我在堆栈溢出中浏览了许多帖子,但找不到可能的解决方案。
package com.sourcebits.gallerygestures;
import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
/**
*
* @author Rajeev N B
*
*/
public class GalleryActivity extends Activity implements OnTouchListener {
/**
* Activity Context
*/
private GalleryActivity context;
/**
* List of drawables
*/
private int[] mImages = new int[6];
/**
* ViewPager for displaying images
*/
private ViewPager viewPager;
/**
* Matrices used for Pinch and Zoom
*/
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
context = GalleryActivity.this;
viewPager = (ViewPager) findViewById(R.id.pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
}
/**
*
* @author Rajeev N B Adapter for displaying the images
*
*/
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = { R.drawable.background, R.drawable.koskula,
R.drawable.wallpaper, R.drawable.wallpap, R.drawable.gr,
R.drawable.balck };
private int pos;
private ImageView imageView;
/**
* Default Constructor
*/
public ImagePagerAdapter() {
}
/**
* Get the Count of the Pages for the View Pager
*/
@Override
public int getCount() {
return mImages.length;
}
/**
* Get the View from the Object
*/
@Override
public boolean isViewFromObject(View view, Object object) {
pos = viewPager.getCurrentItem();
return view == ((ImageView) object);
}
/**
* Create a new Item in the viewPager
*/
@Override
public Object instantiateItem(ViewGroup container, final int position) {
Button prev = (Button) findViewById(R.id.button1);
Button next = (Button) findViewById(R.id.button2);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (pos < mImages.length)
viewPager.setCurrentItem(pos + 1);
Log.v("Next", "" + pos);
}
});
prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (pos > 0)
viewPager.setCurrentItem(pos - 1);
Log.v("Next", "" + pos);
}
});
imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
imageView.setOnTouchListener(context);
return imageView;
}
/**
* Destroy the Items
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
/**
* Pinch to zoom Functionality
*/
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == ZOOM) {
float newDist = spacing(event);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
/** Determine the space between the first two fingers */
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);
}
/** Calculate the mid point of the first two fingers */
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);
}
}
基本上是GalleryView
使用ViewPager
. 我正在根据可绘制数组中的位置膨胀图像视图。现在我想为我的应用程序实现 Pinch to Zoom 功能。但我无法ImageView
在ViewPager
. 我在没有 的情况下测试了我的onTouchEvent
捏缩放ViewPager
,它工作正常。它不适用于 View Pager。我猜这ViewPager
并没有为子视图进行触摸事件。我需要捏才能放大到ImageView
里面ViewPager
吗?