Android - move an ImageView on screen (Like dragging)

前端 未结 6 1211
灰色年华
灰色年华 2021-02-05 15:34

I\'m trying to create an app that can move an ImageView on your device like dragging and when I put like 75% of the ImageView out of the screen show a

6条回答
  •  一个人的身影
    2021-02-05 16:31

    You can achieve this via this code.

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int windowWidth = metrics.widthPixels;
    int windowHeight = metrics.heightPixels;
    

    Now in your onTouch method, calculate if the target location exceeds the above dimensions.

    if( currentXLocation + deltaX > windowWidth ){

    // this will ensure that target location 
    // is always <= windowHeight
    deltaX = windowWidth - currentXLocation; 
    
    } else if( currentXLocation + deltaX < 0){
    
    deltaX = -(currentXLocation);
    
    } else if (...){
    
    // perform similar calculations for the rest 
    
    }
    

提交回复
热议问题