对于缩放和平移,最好的库是TouchImageView
,但是当我们在视图寻呼机上添加这个时会有一个问题,当我们缩放图像时滑动图像。即如果我缩放图像并拖动以查看右侧的另一个缩放部分,由于滑动视图寻呼机的属性,寻呼机将滑动到下一个图像。如果我们向右滑动也会发生。所以修复这个问题经过长时间的研发,我终于做了一些修复。为此,我们需要在TouchImageView.java中进行一些修改和浏览器
首先我们需要创建一个自定义的 Viewpager
MyViewPager.java
package com.example.gallery;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class MyViewPager extends ViewPager {
private boolean enabled = true;
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if(enabled)
return super.onInterceptTouchEvent(arg0);
return false;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
之后我们需要更改TouchImageView 类修改后的类如下所示
修改部分requestDisallowInterceptTouchEvent添加到触摸事件和onDoubleTap 部分也修改了,以便用户只有在缩小到其原始或正常位置后才能滑动到下一个图像。放大时用户无法更改当前图像
private class TouchImageViewListener implements OnTouchListener {
//
// Remember last point position for dragging
//
private PointF last = new PointF();
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
mGestureDetector.onTouchEvent(event);
PointF curr = new PointF(event.getX(), event.getY());
if (state == NONE || state == DRAG || state == FLING) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
last.set(curr);
if (fling != null)
fling.cancelFling();
setState(DRAG);
//Modified Portion
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
if (state == DRAG) {
float deltaX = curr.x - last.x;
float deltaY = curr.y - last.y;
float fixTransX = getFixDragTrans(deltaX, viewWidth,
getImageWidth());
float fixTransY = getFixDragTrans(deltaY, viewHeight,
getImageHeight());
matrix.postTranslate(fixTransX, fixTransY);
if (deltaX == 0 || normalizedScale == 1.0)
//Modified Portion
getParent().requestDisallowInterceptTouchEvent(false)
else
getParent().requestDisallowInterceptTouchEvent(true);
fixTrans();
last.set(curr.x, curr.y);
}
break;
case MotionEvent.ACTION_UP:
//Modified Portion
getParent().requestDisallowInterceptTouchEvent(false)
break;
case MotionEvent.ACTION_POINTER_UP:
setState(NONE);
break;
}
}
setImageMatrix(matrix);
//
// indicate event was handled
//
return true;
}
}//requestDisallowInterceptTouchEvent is Added When Touch Lister Occurs While Zoomed In
.......
private class GestureListener extends
GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return performClick();
}
@Override
public void onLongPress(MotionEvent e) {
performLongClick();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (fling != null) {
//
// If a previous fling is still active, it should be cancelled
// so that two flings
// are not run simultaenously.
//
fling.cancelFling();
}
fling = new Fling((int) velocityX, (int) velocityY);
compatPostOnAnimation(fling);
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
boolean consumed = false;
//Modified Portion
if (state == NONE) {
float targetZoom = (normalizedScale == minScale) ? maxScale
: minScale;
DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom,
e.getX(), e.getY(), false);
compatPostOnAnimation(doubleTap);
consumed = true;
}
else
{
float targetZoom = (normalizedScale == minScale) ? maxScale
: minScale;
DoubleTapZoom doubleTap = new DoubleTapZoom(targetZoom,
e.getX(), e.getY(), false);
compatPostOnAnimation(doubleTap);
}
return consumed;
}
}//Allow User To Double Tap To Orginal Position So that only he can Swipe to next image
从这里下载TouchImageView并修改以上部分
FullScreenImageAdapter.java
package com.example.gallery;
public class FullScreenImageAdapter extends PagerAdapter {
private Activity _activity;
private String[] mStrings;
private LayoutInflater inflater;
ImageLoader im;
// constructor
public FullScreenImageAdapter(Activity activity, String[] mStrings) {
this._activity = activity;
this.mStrings = mStrings;
im = new ImageLoader(_activity);
}
@Override
public int getCount() {
return this.mStrings.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
TouchImageView imgDisplay;
final Button btnClose;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image,
container, false);
RelativeLayout parentview = (RelativeLayout) viewLayout
.findViewById(R.id.rel);
imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
btnClose = (Button) viewLayout.findViewById(R.id.btnClose);
im.DisplayImage(mStrings[position], imgDisplay);
// close button click event
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_activity.finish();
}
});
((ViewPager) container).addView(viewLayout);
/**
* Setting the object for animation
* */
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
MainActivity.java
package com.example.gallery;
public class MainActivity extends Activity {
private FullScreenImageAdapter adapter;
private MyViewPager viewPager;
private String[] mStrings = {
"http://3rdbillion.net/wp-content/uploads/2013/12/9039b649b7cd0ee7f418e55416a8ea204.jpg",
"http://images.tentebranda.org/wp-content/uploads/animal-images_5514_1.jpg",
"http://3rdbillion.net/wp-content/uploads/2013/12/f6aa5d04531f8ff9416ace6e273405304.jpg",
"http://3rdbillion.net/wp-content/uploads/2013/12/fcbc6aaa7ccd1ebaea80f36bfad3c8f74.jpg"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);
viewPager = (MyViewPager) findViewById(R.id.pager);
adapter = new FullScreenImageAdapter(MainActivitys.this, mStrings);
viewPager.setAdapter(adapter);
viewPager.setPageMargin(30);
// displaying selected image first
viewPager.setCurrentItem(position);
}
}
使用从 Web 延迟加载的图像显示在此示例中,我使用的是LAzyLoad 库
请下载并将其添加到您的项目中
最后是 Xml 文件
layout_fullscreen_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rel" >
<!-- <YourPackagename where TouchImageView.java is Placed.TouchImageView
android:id="@+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter" />Example is given below My TouchImageView.java is placed in the com.example.gallery package
-->
<com.example.gallery.TouchImageView
android:id="@+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter" />
<Button
android:id="@+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:background="@drawable/ic_launcher"
android:textColor="#ffffff"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Close" />
activity_fullscreen_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#33BBFFFF" >
<!--<YourPackagename where MyViewPager.java is Placed.MyViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />Example is given below My MyViewPager.java is placed in the com.example.gallery package-->
<com.example.gallery.MyViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
还要在清单文件中添加 Internet 权限和写入存储权限然后你就完成了
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
对于从 Sd 卡显示图像仅替换延迟加载部分并添加必要的文件以从 Sd 卡显示图像并设置 Adpator
- - - - - - - - -更新 - - -
TouchImageview 的新链接在 这里