是否有可能使用标准的Andr​​oid API来在屏幕上移动组件? [英] Is it possible to move components around the screen using the standard android apis?

查看:124
本文介绍了是否有可能使用标准的Andr​​oid API来在屏幕上移动组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想产生一个Android用户界面,允许用户通过选择他们,然后拖拽他们在屏幕上移动添加的组件/构件。

I would like to produce an android user interface which allows the user to move added components/widgets around the screen by selecting them and then dragging them around.

这可能使用标准Android的API?

Is this possible using the standard android apis?

推荐答案

是的。这取决于你想要达到的目的。

Yes. It depends what you are trying to achieve.

这是可以做到的使用的标准API,但此功能不是的标准的API的的一部分。也就是说,没有 widget.DragOverHere()方法,除非你写一个。

It can be done using the standard APIs, but this functionality is not part of the standard APIs. That is, there is no widget.DragOverHere() method unless you write one.

这就是说,它不会被可怕复杂的事情。至少,你需要编写视图的自定义子类,并实现两个方法:的onDraw(帆布C) onTouch(MotionEvent五)。草图:

That said, it would not be terribly complicated to do. At a minimum, you would need to write a custom subclass of View and implement two methods: onDraw(Canvas c) and onTouch(MotionEvent e). A rough sketch:

class MyView extends View {

    int x, y;  //the x-y coordinates of the icon (top-left corner)
    Bitmap bitmap; //the icon you are dragging around

    onDraw(Canvas c) {
      canvas.drawBitmap(x, y, bitmap);
    }

    onTouch(MotionEvent e) {
      switch(e.getAction()) {
      case MotionEvent.ACTION_DOWN:
        //maybe use a different bitmap to indicate 'selected'
        break;
      case MotionEvent.ACTION_MOVE:
        x = (int)e.getX();
        y = (int)e.getY();
        break;
      case MotionEvent.ACTION_UP:
        //switch back to 'unselected' bitmap
        break;
      }
      invalidate(); //redraw the view
    }
}

这篇关于是否有可能使用标准的Andr​​oid API来在屏幕上移动组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆