简体   繁体   English

SwipeGestureDetector将图像从int []更改为Imageview(顶部和底部)

[英]SwipeGestureDetector change image from int [] to Imageview (Top & Bottom)

I need to take an image from an int Array and show then in Imageview when i swipe from bottom to top and top to bottom. 我需要从int数组中获取图像,然后在从底部到顶部以及从顶部到底部滑动时在Imageview中显示。

The GestureDetector works well but i want to take an image from an int [] and put then in Imageview when swipe. GestureDetector效果很好,但是我想从int []中获取图像,然后在滑动时放入Imageview中。

Exemple : 范例:

int [] imageBank = new int[] {
            R.drawable.image0,
            R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4, }; 

Swipe top to bottom, take image 0 from [ ], show in Imageview 从上到下滑动,从[]拍摄图像0,在Imageview中显示

Swipe top to bottom, take image 1 from [ ], show in Imageview 从上到下滑动,从[]拍摄图像1,在Imageview中显示

Swipe bottom to top, take image 0 from [ ], show in Imageview 从下往上滑动,从[]拍摄图像0,在Imageview中显示

Swipe bottom to top, take image 4 from [ ], show in Imageview 从下往上滑动,从[]拍摄图像4,在Imageview中显示

Complete MainActivity 完成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,
    };


   //  HOW CAN I DO ?

    String message = "";
    switch (direction) {
        case LEFT_TO_RIGHT:
            message = "Left to right swipe";
            break;
        case RIGHT_TO_LEFT:
            message = "Right to left swipe";
            break;
        case TOP_TO_BOTTOM:
            imagePic.setImageResource(R.drawable.smiley_disappointed);
            currentLayout.setBackgroundResource(R.color.faded_red);
            message = "Top to bottom swipe";
            break;
        case BOTTOM_TO_TOP:
            currentLayout.setBackgroundResource(R.color.light_sage);
            imagePic.setImageResource(R.drawable.smiley_super_happy);
            message = " Bottom to top swipe";
            break;

    }
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}


}

Thanks you for your help 谢谢你的帮助

A new student. 一个新学生。

Define a 'counter' (integer variable) to store the index of your integer array. 定义一个“计数器”(整数变量)来存储整数数组的索引。 Now based on swipe detected, if user swipe from bottom to up increment the counter and set that index item's image to your imageview and vice versa for up to bottom. 现在,基于检测到的滑动,如果用户从下向上滑动,则计数器会递增,并将该索引项的图像设置为您的imageview,反之亦然,直到底部。

Example code : 示例代码:

int counter = 0;

public void swipe() {
    switch (direction) {
    case LEFT_TO_RIGHT:
        message = "Left to right swipe";
        break;

    case RIGHT_TO_LEFT:
        message = "Right to left swipe";
        break;

    case TOP_TO_BOTTOM:
        if(counter > 0) {
            counter--;
            imagePic.setImageResource(arraySmileys[counter]);
        }
        break;

    case BOTTOM_TO_TOP:
        if(counter < arraySmileys.length()) {
            counter++;
            imagePic.setImageResource(arraySmileys[counter]);
        }
        break;
   }
}

最好将ViewPagerRecyclerViewViewPager行为一起使用,而不要使用GestureDetector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM