3

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

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类型。当我启动该应用程序时,我仍然无法缩放图像,而且以前出现在水龙头上的按钮现在也没有出现。

请帮忙!!!

4

2 回答 2

2

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

于 2014-01-12T03:05:22.137 回答
1

我会建议你使用Android Touch Gallery。看看图书馆。作为画廊ViewPagerTouch Image View内部,可以进行缩放和平移。

于 2013-07-05T12:44:42.023 回答