How to slide an ImageView from left to right smoothly in Android?

前端 未结 7 1660
小鲜肉
小鲜肉 2021-02-01 21:37

I need to make an ImageView slide from left to right of the screen with a smooth animation (I want the ImageView to be visible during the transition) I

7条回答
  •  鱼传尺愫
    2021-02-01 21:38

    public class MainActivity extends Activity {
    
           int windowwidth;
           int windowheight;
    
           private LayoutParams layoutParams;
    
           @Override
           public void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
    
                  windowwidth = getWindowManager().getDefaultDisplay().getWidth();
                  windowheight = getWindowManager().getDefaultDisplay().getHeight();
                  final ImageView img = (ImageView) findViewById(R.id.imageView1);
    
                  img.setOnTouchListener(new View.OnTouchListener() {
    
                         @Override
                         public boolean onTouch(View v, MotionEvent event) {
                               LayoutParams layoutParams = (LayoutParams) img
                                             .getLayoutParams();
                               switch (event.getAction()) {
                               case MotionEvent.ACTION_DOWN:
                                      break;
                               case MotionEvent.ACTION_MOVE:
                                      int x_cord = (int) event.getRawX();
                                      int y_cord = (int) event.getRawY();
    
                                      if (x_cord > windowwidth) {
                                             x_cord = windowwidth;
                                      }
                                      if (y_cord > windowheight) {
                                             y_cord = windowheight;
                                      }
    
                                      layoutParams.leftMargin = x_cord - 25;
                                      layoutParams.topMargin = y_cord - 75;
    
                                      img.setLayoutParams(layoutParams);
                                      break;
                               default:
                                      break;
                               }
                               return true;
                         }
                  });
           }
    }
    

提交回复
热议问题