【问题标题】:Move imageview smoothly平滑移动imageview
【发布时间】:2017-11-07 07:55:41
【问题描述】:

我正在尝试构建一个包含 imageview 的应用程序。用户可以使用以下代码将图像视图水平拖动到屏幕上的某个位置:

@Override
    public boolean onTouchEvent(MotionEvent event){ 
       img.getLocationOnScreen(viewCoords);
       newimgpos[0]=event.getX()-(viewCoords[0]-img.getX());
       img.setX(newimgpos[0]);
    }

现在,我希望当用户将手指从屏幕上抬起时,图像视图会移回 x=0。我可以直接将imageview的x坐标设置为零,但它看起来像一个非常突然的变化;我希望图像视图顺利移动到 x = 0。这可以做到吗?我尝试使用以下逻辑:

for(int i= (int)newimgpos[0]; i>=0; i--){
    img.setX(i);
}

它会移动,我可以看到实际的移动;还是不够流畅。

【问题讨论】:

    标签: android animation android-animation android-imageview touch-event


    【解决方案1】:

    你需要在这里使用动画。查看以下线程以获取更多信息:Android translate animation - permanently move View to new position using AnimationListener

    【讨论】:

    • 谢谢!看起来这就是我需要的。
    • 太棒了。祝您实施顺利
    猜你喜欢
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多