簡體   English   中英

關於觸摸事件的Android Imageview

[英]Android Imageview on touch event

我正在創建一個Android應用程序。 我在這里有一個imageview。 我的目標是在圖像上移動手指時從數據庫中獲取下一張圖片

float nPicturePositionM = 0;

public boolean onTouch(View v, MotionEvent event) {
    boolean aHandledL = false;
    if (event.getAction() == MotionEvent.ACTION_MOVE) {
        float nNewPicturePosL = event.getX();
        if (nPicturePositionM < nNewPicturePosL) {
            nPicturePositionM = nNewPicturePosL;
            GetPictures(true);
        } else {
            nPicturePositionM = nNewPicturePosL;
            GetPictures(false);
        }
        aHandledL = true;
    }

    return aHandledL;

    //return false;
}

我如何使用觸摸事件處理它? 圖像應該像我們在圖庫中那樣滑動

首先,在xml中聲明一個imageview:

<ImageView
    android:id="@+id/imageview1"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="45dp"
    android:layout_centerHorizontal="true" />

然后在您的活動中初始化組件:

私人ImageView image1;

在onCreate:

this.image1= (ImageView)this.findViewById(R.id.imageview1);
this.image1.setImageResource(R.drawable.NAMEOFIMAGE);

下一步是檢測圖像是否“被點擊”。 然后,您可以使用在線數據庫或SQLite數據庫來保存圖像或圖像的路徑,數據庫的一部分很大程度上是從頭開始教您根據您想要使用的路線:

       this.image1.setOnClickListener(new View.OnClickListener() {        
        @Override
           public void onClick(View view) {
            if (view == findViewById(R.id.imageview1)) {     
                         //PUT IN CODE HERE TO GET NEXT IMAGE
            }
            }
        });

我希望這有幫助。 讓我知道事情的后續 :)

您必須使用數據庫中的圖像數組,並在Touch ...的基礎上設置它...

img.setOnTouchListener(new OnTouchListener() 
        {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) 
            {
                    img.setImageURI(uri[pos]);
                    pos++;
                    return false;
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM