1

我正在尝试使用图像背景和文本视图等小部件来缩放布局。我已经通过搜索和遵循一些教程实现了捏缩放。但问题是缩放布局时不平滑(闪烁)。

那么有没有人有关于这个问题的解决方案?或者有不同的方法如何平滑地缩放布局?

任何想法将不胜感激。

这是我的代码:

public class ZoomActivity extends Activity {

    View mainView = null;
    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;
    PointF oldDistPoint = new PointF();

    public static String TAG = "ZOOM";

    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.landingpage);
        mainView = (LinearLayout) findViewById(R.id.linear);

        mainView.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                Log.d(TAG, "mode=DRAG");
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    start.set(event.getX(), event.getY());
                    Log.d(TAG, "mode=DRAG");
                    mode = DRAG;

                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    oldDist = spacing(event);
                    oldDistPoint = spacingPoint(event);
                    Log.d(TAG, "oldDist=" + oldDist);
                    if (oldDist > 10f) {
                        midPoint(mid, event);
                        mode = ZOOM;
                        Log.d(TAG, "mode=ZOOM");
                    }
                    System.out.println("current time :"
                            + System.currentTimeMillis());
                    break;// return !gestureDetector.onTouchEvent(event);
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_UP:
                    Log.d(TAG, "mode=NONE");
                    mode = NONE;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (mode == DRAG) {

                    } else if (mode == ZOOM) {
                        PointF newDist = spacingPoint(event);
                        float newD = spacing(event);
                        Log.e(TAG, "newDist=" + newDist);
                        float[] old = new float[9];
                        float[] newm = new float[9];
                        Log.e(TAG, "x=" + old[0] + ":&:" + old[2]);
                        Log.e(TAG, "y=" + old[4] + ":&:" + old[5]);
                        float scale = newD / oldDist;
                        float scalex = newDist.x / oldDistPoint.x;
                        float scaley = newDist.y / oldDistPoint.y;
                        zoom(scale, scale, start);
                    }
                    break;
                }
                return true;
            }
        });
    }

    /**
     * zooming is done from here
     */
    public void zoom(Float scaleX, Float scaleY, PointF pivot) {
        mainView.setPivotX(pivot.x);
        mainView.setPivotY(pivot.y);
        mainView.setScaleX(scaleX);
        mainView.setScaleY(scaleY);
    }

    /**
     * space between the first two fingers
     */
    private float spacing(MotionEvent event) {
        // ...
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }

    private PointF spacingPoint(MotionEvent event) {
        PointF f = new PointF();
        f.x = event.getX(0) - event.getX(1);
        f.y = event.getY(0) - event.getY(1);
        return f;
    }

    /**
     * the mid point of the first two fingers
     */
    private void midPoint(PointF point, MotionEvent event) {
        // ...
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
    }

}
4

2 回答 2

1

你可以试试这个 zoomview -> https://code.google.com/p/android-zoom-view/

他们使用矩阵和画布来缩放您的视图。使用它非常简单,您可以将 java 文件从存储库复制到您的项目,然后您可以像这样将它添加到您的 xml 视图中

<yourpackagename.ZoomvView>
     //place your view that want to be zoomed here
</yourpackagename.ZoomvView>
于 2013-04-17T01:29:48.403 回答
0

很抱歉提出问题,但如果您遇到此错误“自定义视图 ZoomView 未使用 2 或 3 参数视图构造函数;XML 属性将不起作用”

只需在 java 类 ZoomView 中添加这些

公共 ZoomView(上下文上下文,AttributeSet 属性集)

{ 超级(上下文,属性集);

//TODO:

}

于 2014-03-12T15:06:17.847 回答