android-无法将TouchImageView与ViewPager一起使用
作者:互联网
在下面的代码中,我使用滑动操作和使用ViewPager和ImageView的点击按钮来显示图像.但是它不允许我缩放/平移图像.
为此,我尝试使用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的代码.然后我将上面代码中的ImageView类型替换为TouchImageView.
当我启动该应用程序时,我仍然无法缩放图像,而且,以前出现在水龙头上的按钮现在也不会出现.
请帮忙!!!
解决方法:
我知道这个问题很旧,但是在回答此问题时,Mike已经使用ViewPager的支持更新了TouchImageView(您可能需要切换到dev分支)
标签:pinchzoom,android-viewpager,android-imageview,android 来源: https://codeday.me/bug/20191123/2064790.html