SwipeGestureDetector 将图像从 int [] 更改为 Imageview

当我从下到上和从上到下滑动时,我需要从 int Array 中获取图像,然后在 Imageview 中显示。


GestureDetector 运行良好,但我想从 int [] 中获取图像,然后在滑动时放入 Imageview 中。


例子 :


int [] imageBank = new int[] {

            R.drawable.image0,

            R.drawable.image1,

            R.drawable.image2,

            R.drawable.image3,

            R.drawable.image4, }; 

从上到下滑动,从 [ ] 中获取图像 0,在 Imageview 中显示


从上到下滑动,从 [ ] 中获取图像 1,在 Imageview 中显示


从下往上滑动,从 [ ] 中获取图像 0,在 Imageview 中显示


从下往上滑动,从 [ ] 中获取图像 4,在 Imageview 中显示


完成 MainActivity


import android.support.constraint.ConstraintLayout;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.MotionEvent;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.Toast;

import driss.moodtracker.R;

import driss.moodtracker.component.SwipeGestureDetector;



public class MainActivity extends AppCompatActivity {




private SwipeGestureDetector gestureDetector;



@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    gestureDetector = new SwipeGestureDetector(this);


    Button button_comment = (Button) findViewById(R.id.button1);

    Button button_calendar = (Button) findViewById(R.id.button2);

}




@Override

public boolean dispatchTouchEvent(MotionEvent event) {

    return gestureDetector.onTouchEvent(event);

}




public void onSwipe(SwipeGestureDetector.SwipeDirection direction) {


    ImageView imagePic = (ImageView) findViewById(R.id.imageview);

    ConstraintLayout currentLayout = (ConstraintLayout) findViewById(R.id.main_layout);



    // ARRAY OF SMILEYS LIST


    int [] arraySmileys = new int[] {

            R.drawable.smiley_super_happy,

            R.drawable.smiley_happy,

            R.drawable.smiley_normal,

            R.drawable.smiley_disappointed,

            R.drawable.smiley_sad,

    };


富国沪深
浏览 91回答 2
2回答

浮云间

最好使用ViewPageror RecyclerViewwith ViewPager-like行为而不是GestureDetector

月关宝盒

定义一个“计数器”(整数变量)来存储整数数组的索引。现在基于检测到的滑动,如果用户从底部向上滑动,则增加计数器并将索引项的图像设置为您的图像视图,反之亦然。示例代码:int counter = 0;public void swipe() {&nbsp; &nbsp; switch (direction) {&nbsp; &nbsp; case LEFT_TO_RIGHT:&nbsp; &nbsp; &nbsp; &nbsp; message = "Left to right swipe";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case RIGHT_TO_LEFT:&nbsp; &nbsp; &nbsp; &nbsp; message = "Right to left swipe";&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case TOP_TO_BOTTOM:&nbsp; &nbsp; &nbsp; &nbsp; if(counter > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imagePic.setImageResource(arraySmileys[counter]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case BOTTOM_TO_TOP:&nbsp; &nbsp; &nbsp; &nbsp; if(counter < arraySmileys.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imagePic.setImageResource(arraySmileys[counter]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java