Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 类SwipeGetureDetector在我的应用程序中不工作_Android_Android Layout_Android Studio_Gridview_Swipe - Fatal编程技术网

Android 类SwipeGetureDetector在我的应用程序中不工作

Android 类SwipeGetureDetector在我的应用程序中不工作,android,android-layout,android-studio,gridview,swipe,Android,Android Layout,Android Studio,Gridview,Swipe,我已经用gridlist创建了android应用程序,并用鼠标轻扫有10张图片的图库或小册子 网格列表和图像视图成功打开,我能够启动应用程序 该功能也运行良好,但当我想从网格列表中滑动下一个图像时,我的应用程序关闭,并显示应用程序已停止并再次打开应用程序的消息 请检查我的代码并指导我如何解决此问题。 这是我的密码:- public class MainActivity extends ActionBarActivity { private GridView gv; privat

我已经用gridlist创建了android应用程序,并用鼠标轻扫有10张图片的图库或小册子

网格列表和图像视图成功打开,我能够启动应用程序

该功能也运行良好,但当我想从网格列表中滑动下一个图像时,我的应用程序关闭,并显示应用程序已停止并再次打开应用程序的消息

请检查我的代码并指导我如何解决此问题。 这是我的密码:-

public class MainActivity extends ActionBarActivity {

    private GridView gv;
    private Animator mCurrentAnimator;
    private int mShortAnimationDuration;
    private int j = 0;

    private GestureDetector detector;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    private int thumb[] = { R.drawable.img_android, R.drawable.img_chrome,
            R.drawable.img_facebook, R.drawable.img_firefox,
            R.drawable.img_gmail, R.drawable.img_google_plus,
            R.drawable.img_java, R.drawable.img_linux, R.drawable.img_skype,
            R.drawable.img_twitter, R.drawable.img_windows,
            R.drawable.img_wordpress };

    private ImageView expandedImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        detector = new GestureDetector(this, new SwipeGestureDetector());

        gv = (GridView) findViewById(R.id.grid_view);
        gv.setAdapter(new ImageAdapter(this));
        gv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int pos,
                    long id) {
                j = pos;
                zoomImageFromThumb(v, thumb[pos]);
            }
        });

        mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
    }

    class ImageAdapter extends BaseAdapter{

        private LayoutInflater layoutInflater;

        public ImageAdapter(MainActivity activity) {
            layoutInflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return thumb.length;
        }

        @Override
        public Object getItem(int pos) {
            return pos;
        }

        @Override
        public long getItemId(int pos) {
            return pos;
        }

        @Override
        public View getView(int pos, View convertView, ViewGroup parent) {

            View listItem = convertView;
            int p = pos;

            if(listItem == null) {
                listItem = layoutInflater.inflate(R.layout.single_grid_item, null);
            }

            ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);

            iv.setBackgroundResource(thumb[p]);

            return listItem;
        }
    }

    private void zoomImageFromThumb(final View thumbView, int imageResId) {

        if(mCurrentAnimator != null) {
            mCurrentAnimator.cancel();
        }

        expandedImageView = (ImageView) findViewById(R.id.expanded_image);

        expandedImageView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(detector.onTouchEvent(event)){
                    return true;
                } else {
                    return false;
                }
            }
        });

        expandedImageView.setImageResource(imageResId);

        final Rect startBounds = new Rect();
        final Rect finalBounds = new Rect();
        final Point globalOffset = new Point();

        thumbView.getGlobalVisibleRect(startBounds);
        findViewById(R.id.container).getGlobalVisibleRect(finalBounds, globalOffset);

        startBounds.offset(-globalOffset.x, -globalOffset.y);
        finalBounds.offset(-globalOffset.x, -globalOffset.y);

        float startScale;

        if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds
                .width() / startBounds.height()) {

            startScale = (float) startBounds.height() / finalBounds.height();
            float startWidth = startScale * finalBounds.width();
            float deltaWidth = (startWidth - startBounds.width()) / 2;
            startBounds.left -= deltaWidth;
            startBounds.right += deltaWidth;

        } else {

            startScale = (float) startBounds.width() / finalBounds.width();
            float startHeight = startScale * finalBounds.height();
            float deltaHeight = (startHeight = startBounds.height()) / 2;
            startBounds.top -= deltaHeight;
            startBounds.bottom += deltaHeight;
        }

        thumbView.setAlpha(0f);
        expandedImageView.setVisibility(View.VISIBLE);

        expandedImageView.setPivotX(0f);
        expandedImageView.setPivotY(0f);

        AnimatorSet set = new AnimatorSet();

        set.play(ObjectAnimator.ofFloat(expandedImageView, View.X, startBounds.left, finalBounds.left))
        .with(ObjectAnimator.ofFloat(expandedImageView, View.Y, startBounds.top, finalBounds.top))
        .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScale, 1f))
        .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScale, 1f));

        set.setDuration(mShortAnimationDuration);
        set.setInterpolator(new DecelerateInterpolator());
        set.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                mCurrentAnimator = null;
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                mCurrentAnimator = null;
            }
        });
        set.start();
        mCurrentAnimator = set;

        final float startScaleFinal = startScale;
        expandedImageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(mCurrentAnimator != null) {
                    mCurrentAnimator.cancel();
                }

                AnimatorSet set = new AnimatorSet();

                set.play(ObjectAnimator.ofFloat(expandedImageView, View.X, startBounds.left))
                .with(ObjectAnimator.ofFloat(expandedImageView, View.Y, startBounds.top))
                .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScaleFinal))
                .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScaleFinal));

                set.setDuration(mShortAnimationDuration);
                set.setInterpolator(new DecelerateInterpolator());
                set.addListener(new AnimatorListenerAdapter() {

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        thumbView.setAlpha(1f);
                        expandedImageView.setVisibility(View.GONE);
                        mCurrentAnimator = null;
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                        thumbView.setAlpha(1f);
                        expandedImageView.setVisibility(View.GONE);
                        mCurrentAnimator = null;
                    }
                });
                set.start();
                mCurrentAnimator = set;
            }
        });
    }

    private class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener{
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {

            try {

                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){

                    if(thumb.length > j) {
                        j++;

                        if(j < thumb.length) {
                            expandedImageView.setImageResource(thumb[j]);
                            return true;
                        } else {
                            j = 0;
                            expandedImageView.setImageResource(thumb[j]);
                            return true;
                        }
                    }
                } else if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                    if(j > 0) {
                        j--;
                        expandedImageView.setImageResource(thumb[j]);
                        return true;
                    } else {
                        j = thumb.length - 1;
                        expandedImageView.setImageResource(thumb[j]);
                        return true;
                    }
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
            return false;
        }
    }
}
公共类MainActivity扩展了ActionBarActivity{
私有网格视图gv;
私人动画师麦柯伦塔尼玛;
私有整数mShortAnimationDuration;
私有int j=0;
私人手势检测器;
专用静态最终整数滑动距离=120;
专用静态最终整数滑动\u阈值\u速度=200;
private int thumb[]={R.drawable.img_android,R.drawable.img_chrome,
R.drawable.img_facebook,R.drawable.img_firefox,
R.drawable.img_gmail,R.drawable.img_google_plus,
R.drawable.img_java、R.drawable.img_linux、R.drawable.img_skype、,
R.drawable.img_推特,R.drawable.img_窗口,
R.drawable.img_wordpress};
私有图像视图expandedImageView;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
detector=新的GestureDetector(这是新的SwipeGestureDetector());
gv=(GridView)findViewById(R.id.grid\u视图);
gv.setAdapter(新图像适配器(本));
gv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(适配器视图父视图、视图v、内部位置、,
长id){
j=位置;
zoomImageFromThumb(v,thumb[pos]);
}
});
mShortAnimationDuration=getResources().getInteger(android.R.integer.config\u shortAnimTime);
}
类ImageAdapter扩展BaseAdapter{
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
公共ImageAdapter(MainActivity活动){
layoutInflater=(layoutInflater)活动
.getSystemService(上下文布局\充气机\服务);
}
@凌驾
public int getCount(){
返回拇指长度;
}
@凌驾
公共对象getItem(int-pos){
返回pos;
}
@凌驾
公共长getItemId(int-pos){
返回pos;
}
@凌驾
公共视图getView(int pos、视图转换视图、视图组父视图){
查看列表项=转换视图;
int p=pos;
如果(listItem==null){
listItem=LayoutFlater.充气(R.layout.single\u grid\u item,空);
}
ImageView iv=(ImageView)listItem.findViewById(R.id.thumb);
iv.挫折资源(拇指[p]);
返回列表项;
}
}
私有void zoomImageFromThumb(最终视图thumbView,int-imageResId){
if(mCurrentAnimator!=null){
mCurrentAnimator.cancel();
}
expandedImageView=(ImageView)findViewById(R.id.expanded_image);
expandedImageView.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
if(探测器、通风孔(事件)){
返回true;
}否则{
返回false;
}
}
});
expandedImageView.setImageResource(imageResId);
final Rect startBounds=新Rect();
final Rect finalBounds=新Rect();
最终点全局偏移=新点();
thumbView.getGlobalVisibleRect(startBounds);
findViewById(R.id.container).getGlobalVisibleRect(finalBounds,globalOffset);
起始边界偏移(-globalOffset.x,-globalOffset.y);
最终边界偏移(-globalOffset.x,-globalOffset.y);
浮标;
如果((浮动)finalBounds.width()/finalBounds.height()>(浮动)起始边界
.width()/startBounds.height()){
startScale=(float)startBounds.height()/finalBounds.height();
float startWidth=startScale*finalBounds.width();
float deltaWidth=(startWidth-startBounds.width())/2;
startBounds.left-=三角洲宽度;
startBounds.right+=三角洲宽度;
}否则{
startScale=(float)startBounds.width()/finalBounds.width();
float startHeight=startScale*finalBounds.height();
float deltaHeight=(startHeight=startBounds.height())/2;
startBounds.top-=deltaHeight;
startBounds.bottom+=deltaHeight;
}
thumbView.setAlpha(0f);
expandedImageView.setVisibility(View.VISIBLE);
expandedImageView.setPivotX(0f);
expandedImageView.setPivotY(0f);
AnimatorSet=新的AnimatorSet();
set.play(ObjectAnimator.offload(expandedImageView,View.X,startBounds.left,finalBounds.left))
.with(ObjectAnimator.offload(expandedImageView、View.Y、startBounds.top、finalBounds.top))
.with(ObjectAnimator.offload(expandedImageView,View.SCALE_X,startScale,1f))
.with(ObjectAnimator.offload(expandedImageView,View.SCALE_Y,startScale,1f));
set.setDuration(mShortAnimationDuration);
set.setInterpolator(新减速器Interpolator());
set.addListener(新的AnimatorListenerAdapter(){
@凌驾
AnimationEnd上的公共无效(Animator动画){
mCurrentAnimator=null;
}
@凌驾
AnimationCancel上的公共无效(Animator动画){
mCurrentAnimator=null;
}
});
set.start();
mCurrentAnimator=设置;
最终浮动
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>
 CustomPagerAdapter mCustomPagerAdapter = new CustomPagerAdapter(this);

   ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCustomPagerAdapter);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView" />
</LinearLayout>
public class CustomPagerAdapter extends PagerAdapter {

   private int mResources[] = { R.drawable.img_android, R.drawable.img_chrome,
        R.drawable.img_facebook, R.drawable.img_firefox,
        R.drawable.img_gmail, R.drawable.img_google_plus,
        R.drawable.img_java, R.drawable.img_linux, R.drawable.img_skype,
        R.drawable.img_twitter, R.drawable.img_windows,
        R.drawable.img_wordpress };


    Context mContext;
    LayoutInflater mLayoutInflater;

    public CustomPagerAdapter(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mResources.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

        ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
        imageView.setImageResource(mResources[position]);

        container.addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);
    }
}
 gv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int pos,
                    long id) {
                Intent intent = new Intent(MainActivity.this,ViewPagerActivity.class);
                intent.putExtra("position",pos);
                startActivity(intent);
            }
        });
import android.support.v7.app.AppCompatActivity;
public class ViewPagerActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         CustomPagerAdapter mCustomPagerAdapter = new CustomPagerAdapter(this);

         Intent mIntent = getIntent();
         int position= mIntent.getIntExtra("position", 0);

         ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
         mViewPager.setAdapter(mCustomPagerAdapter);
         mViewPager.setCurrentItem(position);

    }
}