簡體   English   中英

SwipeGestureDetector將圖像從int []更改為Imageview(頂部和底部)

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

我需要從int數組中獲取圖像,然后在從底部到頂部以及從頂部到底部滑動時在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,
    };


   //  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();
}


}

謝謝你的幫助

一個新學生。

定義一個“計數器”(整數變量)來存儲整數數組的索引。 現在,基於檢測到的滑動,如果用戶從下向上滑動,則計數器會遞增,並將該索引項的圖像設置為您的imageview,反之亦然,直到底部。

示例代碼:

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