成都创新互联网站制作重庆分公司

Android自定义一个图形单点移动缩小的效果-创新互联

先给大家展示下效果图,如果大家感觉不错,请参考实现代码

创新互联建站专注于下花园网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供下花园营销型网站建设,下花园网站制作、下花园网页设计、下花园网站官网定制、微信平台小程序开发服务,打造下花园网络公司原创品牌,更为您提供下花园网站排名全网营销落地服务。

效果图如下所示:

代码如下所示:

public class MainActivity extends Activity {
  View view;
  public static final int DRAG = 1;
  public static final int SCALE = 2;
  int mode = 1;
  int height = 10, width = 10;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    view = findViewById(R.id.view);
  }
  float length = 1;
  // 重写
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();
    // 多指触控
    switch (event.getAction() & event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
      mode = DRAG;
      break;
    case MotionEvent.ACTION_POINTER_DOWN:
      Log.e("TAG", "多指移动");
      mode = SCALE;
      // 两个手指开始的长度是多少呢?
      length = calc(event);
      break;
    case MotionEvent.ACTION_UP:
      length = 1;
      break;
    case MotionEvent.ACTION_MOVE:
      if (mode == DRAG) {
        // 1. 单个手指
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            width, height);
        params.setMargins(x, y, 0, 0);
        view.setLayoutParams(params);
      } else {
        // 2. 两个手指
        float beilv = calc(event) / length;
        width = (int) (view.getWidth() * beilv);
        height = (int) (view.getHeight() * beilv);
        Log.e("TAG", beilv + " " + width + " " + height);
        FrameLayout.LayoutParams params = (LayoutParams) view
            .getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
      }
      break;
    }
    return true;
  }
  // 类 Ponint
  public float calc(MotionEvent event) {
    float x1 = event.getX();
    float y1 = event.getY();
    float x2 = event.getX(1);
    float y2 = event.getY(1);
    return (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  }
}

网站名称:Android自定义一个图形单点移动缩小的效果-创新互联
分享地址:http://cxhlcq.com/article/dsdpjd.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部