亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

SwipeGestureDetector 將圖像從 int [] 更改為 Imageview

SwipeGestureDetector 將圖像從 int [] 更改為 Imageview

富國滬深 2022-07-20 20:16:05
當我從下到上和從上到下滑動時,我需要從 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 中顯示完成 MainActivityimport 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;@Overrideprotected 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);}@Overridepublic 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,    };
查看完整描述

2 回答

?
浮云間

TA貢獻1829條經驗 獲得超4個贊

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



查看完整回答
反對 回復 2022-07-20
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

定義一個“計數器”(整數變量)來存儲整數數組的索引?,F在基于檢測到的滑動,如果用戶從底部向上滑動,則增加計數器并將索引項的圖像設置為您的圖像視圖,反之亦然。


示例代碼:


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;

   }

}


查看完整回答
反對 回復 2022-07-20
  • 2 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號