无法将TouchImageView与ViewPager一起使用

时间:2013-07-05 12:34:14

标签: android android-viewpager android-imageview pinchzoom

在下面的代码中,我使用滑动操作显示图像,使用ViewPager和ImageView显示Tap按钮。但它不允许我缩放/平移图像。 为此,我尝试使用TouchImageView,bt没有成功。我原来的活动如下:

package com.pankajvatsa.testfeet;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ImageViewPager extends Activity {
// Declare Variable
int position;
Button bWallpaperButton;
Button bDownloadButton;
RelativeLayout mainLay;
int flagForButton = 0;
boolean gone = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set title for the ViewPager
    setTitle("ViewPager");
    // Get the view from view_pager.xml
    setContentView(R.layout.activity_image_view_pager);

    bWallpaperButton = (Button) findViewById(R.id.bSetWallpaper);
    bDownloadButton = (Button) findViewById(R.id.bSaveToGallery);

    mainLay = (RelativeLayout) findViewById(R.id.rl_view_pager);

    // Retrieve data from MainActivity on item click event
    Intent p = getIntent();
    position = p.getExtras().getInt("id");

    ImageAdapter imageAdapter = new ImageAdapter(this);
    List<ImageView> images = new ArrayList<ImageView>();

    // Retrieve all the images
    for (int i = 0; i < imageAdapter.getCount(); i++) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(imageAdapter.mThumbIds[i]);
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        images.add(imageView);
    }

    // Set the images into ViewPager
    ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
    ViewPager viewpager = (ViewPager) findViewById(R.id.image_pager);
    viewpager.setAdapter(pageradapter);
    // Show images following the position
    viewpager.setCurrentItem(position);

    viewpager.setOnTouchListener(new View.OnTouchListener() {


        private float pointX;
        private float pointY;
        private int tolerance = 50;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE: 
                return false;
            case MotionEvent.ACTION_DOWN:
                pointX = event.getX();
                pointY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                boolean sameX = pointX + tolerance > event.getX() && pointX - tolerance < event.getX();
                boolean sameY = pointY + tolerance > event.getY() && pointY - tolerance < event.getY();
                if(sameX && sameY){
                        //The user "clicked" certain point in the screen or just returned to the same position an raised the finger

                    if(gone == false){
                        bWallpaperButton.setVisibility(View.GONE);
                        bDownloadButton.setVisibility(View.GONE);
                        gone = true;
                    }else{
                        bWallpaperButton.setVisibility(View.VISIBLE);
                        bDownloadButton.setVisibility(View.VISIBLE);
                        gone = false;
                    }
                }
            }
            return false;

        }
    });


}

}

在此之后,我在名为“TouchImageView.java”的包中创建了一个新类,并在其中粘贴了TouchImageView的代码。然后我用TouchImageView替换了上面代码中的ImageView类型。 当我启动应用程序时,我仍然无法缩放图像,此外,以前出现的按钮也不会出现。

请帮助!!!

2 个答案:

答案 0 :(得分:2)

我知道这个问题已经过时但是在回答这个问题时Mike已经更新了TouchImageView并支持ViewPager(您可能需要切换到开发分支)

答案 1 :(得分:1)

我建议您使用 Android Touch Gallery 。看看图书馆。将ViewPager作为图库和Touch Image View,其中可以进行缩放缩放和平移。