欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义一个图形单点移动缩小的效果

程序员文章站 2023-10-19 22:10:30
先给大家展示下效果图,如果大家感觉不错,请参考实现代码 效果图如下所示: 代码如下所示: public class mainactivity extend...

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

效果图如下所示:

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));
  }
}

xml类

<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.lesson6_work1.mainactivity" >
  <view
    android:id="@+id/view"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:background="@drawable/oval" />
</framelayout>

自己在shape中定义的一个圆的oval.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
  <solid android:color="@android:color/holo_red_dark"/>
</shape>

用自定义view的方式实现单点触控时拖动图片,跟着拖动点走

qiuview 类

public class qiuview extends view {
  paint paint = new paint();
  pointf point = new pointf();
  public qiuview(context context) {
    super(context);
    paint.setcolor(color.red);
    paint.setantialias(true);
    paint.setdither(true);
  }
  @override
  protected void ondraw(canvas canvas) {
    super.ondraw(canvas);
    canvas.drawcircle(point.x, point.y, 50, paint);
  }
  //  触摸事件
  @override
  public boolean ontouchevent(motionevent event) {
    if (event.getaction() == motionevent.action_move) {
      point.set(event.getx(), event.gety());
      invalidate();
    }
    return true;
  }
}

总结

以上所述是小编给大家介绍的android自定义一个图形单点移动缩小的效果,希望对大家有所帮助