android开发分享Android自定义view实现拖动小球移动

android应用界面中可以看得见的都是由一个个的view所组成的,几乎所有的可视的控件都是基于view写的。在view中提供了对touch也就是手势的捕获和传递,我们可以对view里面手势的重写来达

android应用界面中可以看得见的都是由一个个的view所组成的,几乎所有的可视的控件都是基于view写的。在view中提供了对touch也就是手势的捕获和传递,我们可以对view里面手势的重写来达到我们所需要的特性。比如说我们现在要做一款游戏,内容很简单,就是要实现让如图所示的一个黑色的小球在根据手指移动而在手机屏幕内移动。

Android自定义view实现拖动小球移动

我们可以重写view里面的public boolean ontouchevent(motionevent event)方法,来获取到所有的手势操作,再从中选择出所需要的手势进行操作。

所以可以得到如下的一段代码:

  /**   * created by obo on 15/8/21.   */  public class touchview extends view{       public static string tag = touchview.class.getcanonicalname();    //当前小球的位置    private pointf currrentposition = new pointf(100,100);    //手指触摸起点坐标    private pointf movestartposition = new pointf(0,0);    //当前手指位置坐标    private pointf moveendposition = new pointf(0,0);       private context context;       public touchview(context context, attributeset attrs) {      super(context, attrs);      this.context = context;    }       @override    public void ondraw(canvas canvas)    {      super.ondraw(canvas);      canvas.drawcircle(currrentposition.x + (moveendposition.x - movestartposition.x),currrentposition.y+(moveendposition.y - movestartposition.y),50,new paint());    }       @override    public boolean ontouchevent(motionevent event) {         switch (event.getactionmasked())      {        case motionevent.action_down:          movestartposition.x = event.getx();          movestartposition.y = event.gety();          break;        case motionevent.action_move:          moveendposition.x = event.getx();          moveendposition.y = event.gety();          //刷新          this.postinvalidate();          break;           case motionevent.action_up:          currrentposition.x += (moveendposition.x - movestartposition.x);          currrentposition.y += (moveendposition.y - movestartposition.y);          movestartposition.x = moveendposition.x;          movestartposition.y = moveendposition.y;          break;        default:      }      return true;    }  }

可以看到当前ontouchevent方法返回ture,表明这个view是要对当前手势操作进行捕获的,这里包括 按下、移动和抬起等相关操作,如果返回的是false的话,只会接收到第一次的一个action_down也就是按下的响应,之后的移动手势和抬起的手势都无法获取到。

方法ontouchevent里面做了三件事情:1.当用户手指按下的时候,初始化记录下开始按下的坐标,并立即返回不需要刷新界面。2.当用户移动手指的时候,记录用户手指的位置,并且重新刷新界面。3.当用户退出手势也就是抬起手指的时候,将位移赋值给基础坐标点,并让手势起点坐标和手势终点坐标x、y相等(清零)。

而如果使用matrix的话将会使整个过程变得更加简单,只需要事先得到小球的bitmap就能使用matrix对小球进行包括 位移、形变、旋转在内的变换,这里只使用matrix的位移变换效果,具体实现代码如下:

  /**   * created by obo on 15/8/26.   */  public class matrixview extends view {       public final static string tag = matrixview.class.getcanonicalname();    //bitmap运行矩阵    matrix matrix = new matrix();    //记录点    pointf startpoint = new pointf();    //自定义bitmap    bitmap bitmap = bitmap.createbitmap(100,100, bitmap.config.argb_8888);       public matrixview(context context, attributeset attrs) {      super(context, attrs);         canvas canvas = new canvas(bitmap);      //直接在bitmap上面绘制一个小球      canvas.drawcircle(50,50,50,new paint());    }       @override    public void ondraw(canvas canvas)    {      super.ondraw(canvas);         canvas.drawbitmap(bitmap, matrix, new paint());    }       @override    public boolean ontouchevent(motionevent event)    {      super.ontouchevent(event);         if (event.getactionmasked() == motionevent.action_move)      {        matrix.posttranslate(event.getx() - startpoint.x, event.gety() - startpoint.y );        //刷新        this.postinvalidate();      }         startpoint.x = event.getx();      startpoint.y = event.gety();         return true;       }  }

这一块的代码实现的效果和先前的一样,增加了matrix变量,少了一个pointf变量,同时在ontouchevent和ondraw方法中的代码量也降低了一些。matrix其实是一个3×3的矩阵,使用matrix可以一步步积累变换的操作,无论对matrix操作多少次,其对图片的处理复杂度都是固定不变的,并且能对图片进行快速的变换,这就是使用matrix的带来的好处。

以上就是android开发分享Android自定义view实现拖动小球移动的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/891472.html

(0)
上一篇 2021年10月19日
下一篇 2021年10月19日

精彩推荐