我在堆栈溢出中经历了很多帖子,但无法获得可能的解决方案。
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
上收到Touch事件。我测试了我的onTouchEvent
捏缩放而没有ViewPager
,它运行正常。它不适用于View Pager。我猜ViewPager
没有为子视图拍摄触摸事件。
我需要捏缩放到ImageView
内的ViewPager
?
答案 0 :(得分:0)
如果您的viewpager使用的是imageView,那么您可以使用touchImageView。通过以下链接,您可以使用源代码,并且您可以解决问题。
https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/example/touch/TouchImageView.java
最后,只需使用此自定义视图替换当前的imageview。如果在实施过程中遇到任何问题,请告诉我,我会尽力澄清一下。