suwei95 2019-07-09 12:53 采纳率: 0%
浏览 421

android studio自定义滑动控件时,使用 ObjectAnimator.ofFloat滑动后,为什么getRight()值不变

android studio自定义滑动空间时,使用 ObjectAnimator.ofFloat滑动后,getRight()值不变
代码大概如下:

   <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <Button
                android:stateListAnimator="@null"
                android:layout_gravity="right"
                android:text="删除"
                android:gravity="center"
                android:id="@+id/delete"
                android:layout_width="30dp"
                android:layout_height="30dp"/>

        <com.example.myapplication.TestView   //自定义控件
                android:background="@color/colorAccent"
                android:layout_width="match_parent"
                android:layout_height="30dp">
        </com.example.myapplication.TestView>


    </FrameLayout>

空间重写代码

public class TestView extends LinearLayout {

    public TestView(Context context){
        super(context);
    }

    public TestView(Context context, AttributeSet attrs){
        super(context,attrs);
    }

    public TestView(Context context, AttributeSet attrs, int defStyleAttr){
        super(context,attrs,defStyleAttr);
    }

    int lastX;
    int lastY;
    int offsetX;

    int startX;
    int startY;

    int itemwidth;
    int buttonWidth;

    int flag=1;

    ViewGroup viewGroup;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
       int x = (int) event.getX();
        int y = (int) event.getY();

               switch ( event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("TestView", "ACTION_DOWN: ");
                flag =1;
                lastX=x;
                lastY=y;
                startX=getRight();
                itemwidth=getWidth();
                viewGroup=(ViewGroup) getParent();
                buttonWidth=viewGroup.getChildAt(0).getWidth();
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("TestView", "ACTION_MOVE: ");
                offsetX=lastX-x;
                if (offsetX>0)
                {
                    layout((getLeft()-offsetX),getTop(),(getRight()-offsetX),getBottom());
                }
                break;
            case MotionEvent.ACTION_UP:
                Log.d("TestView", "ACTION_UP: ");
                Log.d("TestView", "*****back star");
                Log.d("TestView", "1*****getRight ="+String.valueOf(getRight())+"getLeft"+
                        String.valueOf(getLeft()));
                ObjectAnimator.ofFloat(this,"translationX",0,
                        (itemwidth-getRight())).setDuration(300).start();
                Log.d("TestView", "2*****getRight ="+String.valueOf(getRight())+"getLeft"+ String.valueOf(getLeft()));
                                break;
                }
        return true;

    }
}

这个功能是让控件可以左滑,然后放手的时候动画的形式回到屏幕右边(x方向值为1440),拖动控件到x=1317,然后调用 ObjectAnimator.ofFloat,控件屏幕上显示回到右边了,但getRight()得到的数值和调用前得到的数值一样,为1317而不是1440,为什么会这样呢?

2019-07-09 12:41:46.123 11400-11400/com.example.myapplication D/TestView: 1*****getRight =1317getLeft-123
2019-07-09 12:41:46.124 11400-11400/com.example.myapplication D/TestView: 2*****getRight =1317getLeft-123

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?