溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Android自定義一個(gè)圖形單點(diǎn)移動(dòng)縮小的效果

發(fā)布時(shí)間:2020-09-05 23:07:41 來(lái)源:腳本之家 閱讀:143 作者:Luck-yy 欄目:移動(dòng)開(kāi)發(fā)

先給大家展示下效果圖,如果大家感覺(jué)不錯(cuò),請(qǐng)參考實(shí)現(xiàn)代碼

效果圖如下所示:

Android自定義一個(gè)圖形單點(diǎn)移動(dòng)縮小的效果

代碼如下所示:

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;
  // 重寫(xiě)
  @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", "多指移動(dòng)");
      mode = SCALE;
      // 兩個(gè)手指開(kāi)始的長(zhǎng)度是多少呢?
      length = calc(event);
      break;
    case MotionEvent.ACTION_UP:
      length = 1;
      break;
    case MotionEvent.ACTION_MOVE:
      if (mode == DRAG) {
        // 1. 單個(gè)手指
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            width, height);
        params.setMargins(x, y, 0, 0);
        view.setLayoutParams(params);
      } else {
        // 2. 兩個(gè)手指
        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;
  }
  // 類(lèi) 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類(lèi)

<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中定義的一個(gè)圓的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的方式實(shí)現(xiàn)單點(diǎn)觸控時(shí)拖動(dòng)圖片,跟著拖動(dòng)點(diǎn)走

QiuView 類(lèi)

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

總結(jié)

以上所述是小編給大家介紹的Android自定義一個(gè)圖形單點(diǎn)移動(dòng)縮小的效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI