Android多点触控视图调整闪烁

时间:2016-06-27 21:18:07

标签: android

我有一个自定义实现的视图 - 我可以使用它,我将这个视图在运行时添加到RelativeLayout布局中,如下所示:

  RelativeLayout paintRootView = (RelativeLayout) findViewById(R.id.root_view);
  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
  paintView.setLayoutParams(params);

  PaintView paintView = new PaintView(context);
  paintRootView.addView(paintView);

我有一些控件可以指定在onTouch方法上绘制视图/拖动视图或调整视图大小的时间。绘图和翻译视图工作正常,但当我尝试放大或缩小时,这个视图是不合适的。我想念一些吗?

video effect

使用过的代码:

private int mode = NONE;
private float oldDist;
private static final float MIN_ZOOM = 0.5f;
private static final float MAX_ZOOM = 3.0f;


 @Override
public boolean onTouch(View view, MotionEvent event) {
    if (IS_IN_MOOVING_STATE) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN: {
                // some code for drag
                mode = DRAG;
                break;
            }

            case MotionEvent.ACTION_POINTER_DOWN: {
                oldDist = getSpacing(event);
                mode = ZOOM;
                break;
            }

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP: {
                mode = NONE;
                break;
            }

            case MotionEvent.ACTION_MOVE: {
                if (mode == DRAG) {
                  // some code for drag

                } else if (mode == ZOOM) {
                    float newDist = getSpacing(event);

                    float scale = newDist / oldDist;

                    PointF pointF = getMidPoint(event);

                    if (scale >= MIN_ZOOM && scale < MAX_ZOOM) {
                        zoomPaintView(scale, scale, pointF);
                    }
                }

                break;
            }
        }

        paintRootView.invalidate();
        return true;
    }

    return false;
}

1 个答案:

答案 0 :(得分:0)

我希望能帮助你。这是伪代码,我可以通过MultiTouch事件

更改TextView大小
public class MainActivity extends Activity implements OnTouchListener {

    final static float STEP = 200;
    TextView mytv;
    float mRatio = 1.0f;
    int   mBaseDist;
    float mBaseRatio;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mytv = (TextView) findViewById(R.id.textView);
        mytv.setTextSize(mRatio + 13);
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() == 2) {
            int action     = event.getAction();
            int pureaction = action & MotionEvent.ACTION_MASK;
            if (pureaction == MotionEvent.ACTION_POINTER_DOWN) {
                mBaseDist = getDistance(event);
                mBaseRatio = mRatio;
            } else {
                float delta = (getDistance(event) - mBaseDist) / STEP;
                float multi = (float) Math.pow(2, delta);
                mRatio = Math.min(1024.0f, Math.max(0.1f, mBaseRatio * multi));
                mytv.setLayoutParams(new LinearLayout.LayoutParams((int) (mRatio + 13), (int) (mRatio + 13)));
            }
        }
        return true;
    }

    int getDistance(MotionEvent event) {
        int dx = (int) (event.getX(0) - event.getX(1));
        int dy = (int) (event.getY(0) - event.getY(1));
        return (int) (Math.sqrt(dx * dx + dy * dy));
    }

    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
}