0

我在主要活动上有四个屏幕/布局,当我滑动时它可以工作,我已经实现了

@Override
public void onPageScrolled(int position, float positionOffset,
        int positionOffsetPixels){}

并且 t 有效(像立方体一样动画)。

对于布局之间的一次切换,我有十次调用 (I Log.i) onPageScrolled 函数并记录所有值。我正在尝试在单击菜单按钮时实现动画。

我试过了

public void automaticAnimate(final int next, final int current, final int direction) {
    Thread thread = new Thread() {
        public void run() {

            int steps = Math.abs(current - next);
            if (steps > 0) {
                if (direction == TO_LEFT) {
                    float leftPositionOffset[] = new float[] { 0.98796296f,
                            0.886111114f, 0.49444443f, 0.44537038f,
                            0.37314814f, 0.21018519f, 0.11388889f,
                            0.05925926f, 0.026851851f, 0.010185185f,
                            0.0037037036f, 0.000001f, 0.0f };
                    int leftPositionOffsetPixels[] = new int[] { 1067, 957,
                            534, 481, 403, 227, 123, 64, 29, 11, 4, 1, 0 };

                    for (int i = 0; i < leftPositionOffset.length; i++) {
                        int panel = (i == 0) ? current : current - 1;
                        onPageScrolled(panel, leftPositionOffset[i],
                                leftPositionOffsetPixels[i]);
                        try {
                            Thread.sleep(30);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                } else {
                    float rightPositionOffset[] = new float[] {
                            0.004629612f, 0.020370483f, 0.07407403f,
                            0.17314816f, 0.27592587f, 0.44537044f,
                            0.54444456f, 0.69722223f, 0.70370364f,
                            0.7907407f, 0.86018515f, 0.91203713f,
                            0.94537044f, 0.9694445f, 0.98333335f,
                            0.9916666f, 0.9972222f, 0.99999f, 0.0f };
                    int rightPositionOffsetPixels[] = new int[] { 4, 22,
                            79, 187, 297, 481, 588, 748, 753, 759, 854,
                            928, 985, 1021, 1047, 1062, 1070, 1077, 1078, 0 };

                    for (int i = 0; i < rightPositionOffset.length; i++) {
                        int panel = (i == (rightPositionOffset.length - 1)) ? current + 1
                                : current;
                        onPageScrolled(panel, rightPositionOffset[i],
                                rightPositionOffsetPixels[i]);
                        try {
                            Thread.sleep(30);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    };
    thread.start();
}

但我收到错误,因为我无法onPageScrollednot UI thread. 如何onPageScrolled从非 UI 线程调用(那一刻将布局放在好的位置)?

4

1 回答 1

1

您不能从主 (UI) 线程以外的线程触摸 UI 元素。这就是 AsyncTask 的用途:

public void automaticAnimate(final int next, final int current, final int direction) {
    new AsyncTask<Void, Object, Void>() {

        @Override
        protected void onProgressUpdate(Object... values) {
            int position = (Integer) values[0];
            float positionOffset = (Float) values[1];
            int positionOffsetPixels = (Integer) values[3];

            onPageScrolled(position, positionOffset,
                    positionOffsetPixels);
        }

        @Override
        protected Void doInBackground(Void... params) {


            int steps = Math.abs(current - next);
            if (steps > 0) {
                if (direction == TO_LEFT) {
                    float leftPositionOffset[] = new float[] { 0.98796296f,
                            0.886111114f, 0.49444443f, 0.44537038f,
                            0.37314814f, 0.21018519f, 0.11388889f,
                            0.05925926f, 0.026851851f, 0.010185185f,
                            0.0037037036f, 0.000001f, 0.0f };
                    int leftPositionOffsetPixels[] = new int[] { 1067, 957,
                            534, 481, 403, 227, 123, 64, 29, 11, 4, 1, 0 };

                    for (int i = 0; i < leftPositionOffset.length; i++) {
                        int panel = (i == 0) ? current : current - 1;
                        publishProgress(panel, leftPositionOffset[i],
                                leftPositionOffsetPixels[i]);
                        try {
                            Thread.sleep(30);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                } else {
                    float rightPositionOffset[] = new float[] {
                            0.004629612f, 0.020370483f, 0.07407403f,
                            0.17314816f, 0.27592587f, 0.44537044f,
                            0.54444456f, 0.69722223f, 0.70370364f,
                            0.7907407f, 0.86018515f, 0.91203713f,
                            0.94537044f, 0.9694445f, 0.98333335f,
                            0.9916666f, 0.9972222f, 0.99999f, 0.0f };
                    int rightPositionOffsetPixels[] = new int[] { 4, 22,
                            79, 187, 297, 481, 588, 748, 753, 759, 854,
                            928, 985, 1021, 1047, 1062, 1070, 1077, 1078, 0 };

                    for (int i = 0; i < rightPositionOffset.length; i++) {
                        int panel = (i == (rightPositionOffset.length - 1)) ? current + 1
                                : current;
                        publishProgress(panel, rightPositionOffset[i],
                                rightPositionOffsetPixels[i]);
                        try {
                            Thread.sleep(30);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
            return null;
        }

    }.execute();
}
于 2013-09-04T23:25:21.170 回答