温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-09-05 23:07:41 来源:脚本之家 阅读:150 作者:Luck-yy 栏目:移动开发

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

效果图如下所示:

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自定义一个图形单点移动缩小的效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI