欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義View圓形和拖動(dòng)圓跟隨手指拖動(dòng)

 更新時(shí)間:2017年11月10日 09:10:02   作者:贏le  
這篇文章主要介紹了Android自定義View圓形和拖動(dòng)圓跟隨手指拖動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

單純的自定義一個(gè)圓非常簡(jiǎn)單 只需要幾步就完成 拖動(dòng)圓添加實(shí)現(xiàn)觸摸事件即可

我在第一次自定義View圓遇到的小問(wèn)題:

1.拖動(dòng)圓的話(huà)在xml里面設(shè)置的自定義圓的寬和高是它能活動(dòng)的空間的大小 不是圓控件的大小 如果你定義了100dp 拖動(dòng)它的時(shí)候超過(guò)100dp這個(gè)距離這個(gè)圓就會(huì)看不見(jiàn) 就像下面這樣 如果想活動(dòng)于整個(gè)屏幕直接給寬和高match_parent屬性就好了

2.在布局里自定的view會(huì)提示編譯 點(diǎn)擊Build編譯一下就好了

這里寫(xiě)圖片描述

下面開(kāi)始寫(xiě)代碼: 先是單純的創(chuàng)建一個(gè)圓形 創(chuàng)建一個(gè)類(lèi)繼承View 實(shí)現(xiàn)onDraw方法

public class CustomView extends View {
  //創(chuàng)建point對(duì)象 參數(shù)為x坐標(biāo)和y坐標(biāo)
  private PointF point = new PointF(100, 100);

  public CustomView(Context context) {
    super(context);
  }

  public CustomView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //參數(shù)為圓的橫坐標(biāo) ,縱坐標(biāo),半徑,創(chuàng)建 
    //如果圓形出不來(lái) 說(shuō)明你xml里定義的寬和高的空間不夠大 這里是圓在屏幕的坐標(biāo)位置 xml定義的是圓能夠顯示的區(qū)域 如果你定義的太小 圓的坐標(biāo)又超過(guò)了這個(gè)區(qū)域 就會(huì)顯示不出來(lái) xml里定義寬和高充滿(mǎn)屏幕就可以了  
    canvas.drawCircle(point.x,point.y, 50, new Paint());
  }

}

XML里、自己定義的view類(lèi)的名字:

 <ydtx.bwie.com.xiangmu_project02.CustomView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

一個(gè)圓就這樣創(chuàng)建好了 直接運(yùn)行就可以了 ManActivity里什么也不用改

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


  }


下面是添加拖動(dòng)圓的功能 非常簡(jiǎn)單 實(shí)現(xiàn)觸摸監(jiān)聽(tīng)即可 代碼非常少 如下:

public class CustomView extends View {
  //創(chuàng)建point對(duì)象 參數(shù)為x坐標(biāo)和y坐標(biāo)
  private PointF point = new PointF(100, 100);

  public CustomView(Context context) {
    super(context);
  }

  public CustomView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //參數(shù)為圓的橫坐標(biāo) ,縱坐標(biāo),半徑,創(chuàng)建
    //如果圓形出不來(lái) 說(shuō)明你xml里定義的寬和高的空間不夠大 這里是圓在屏幕的坐標(biāo)位置 xml定義的是圓能夠顯示的區(qū)域 如果你定義的太小 圓的坐標(biāo)又超過(guò)了這個(gè)區(qū)域 就會(huì)顯示不出來(lái) xml里定義寬和高充滿(mǎn)屏幕就可以了 
    canvas.drawCircle(point.x,point.y, 50, new Paint());
  }
  //觸摸事件
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    //獲得觸摸事件
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        break;
      //ACTION_MOVE不要設(shè)置break,否則圓形不會(huì)跟隨手指活動(dòng) 只會(huì)手指松開(kāi)屏幕的時(shí)候圓形直接到了屏幕停止的位置
      case MotionEvent.ACTION_MOVE:
      case MotionEvent.ACTION_UP:
        //獲取手指觸摸位置的x坐標(biāo)
        point.x = event.getX();
        //獲取手指觸摸位置的y坐標(biāo)
        point.y = event.getY();
        //啟動(dòng)
        postInvalidate();
        break;

    }
    return true;

  }
}

這樣圓就跟隨手指的活動(dòng)而動(dòng)了,快試試吧。

相關(guān)文章

最新評(píng)論