`
yuanlanxiaup
  • 浏览: 859026 次
文章分类
社区版块
存档分类
最新评论

android : drag and drop ui

 
阅读更多

最近在忙一些ui特效,心想这个也不是我们开发的事情阿,设计师该管的事情。最近头痛阿,没有办法,还是得慢慢搞,搞开发得有点责任心,对把。这个东西呢,我在国内找了下,说的不是很清楚,大多数说的是利用WindowManager来管理要拖动的ui组件,但是我做了下,感觉麻烦,且不是我想要的效果。难道没有利用父容器来控制ui的拖拽实现吗?答案是有的,找了下资料,发觉效果不错,很有借鉴意义,就分享一下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>
复制代码

  代码很简单,主要是思路:对你想要拖拽的ui设置一个View.OntouchListener,然后在你想要实现拖拽容器(ViewGroup)里也设置自己的View.OntouchListener.当ui被移动的时候,获得位移数据,然后在包含ui的容器(ViewGroup)里重新设置ui的布局属性即可。代码如下:

package com.android.drag_drop;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
    private View selected_item = null;
    private int offset_x = 0;
    private int offset_y = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ViewGroup vg = (ViewGroup) findViewById(R.id.vg);
        vg.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                case MotionEvent.ACTION_MOVE:
                    int x = (int) event.getX() - offset_x;
                    int y = (int) event.getY() - offset_y;

                    int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
                    int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
                    if (x > w)
                        x = w;
                    if (y > h)
                        y = h;
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                            new ViewGroup.MarginLayoutParams(
                                    LinearLayout.LayoutParams.WRAP_CONTENT,
                                    LinearLayout.LayoutParams.WRAP_CONTENT));
                    lp.setMargins(x, y, 0, 0);

                    selected_item.setLayoutParams(lp);
                    break;
                default:
                    break;
                }
                return true;
            }
        });
        ImageView img = (ImageView) findViewById(R.id.img);
        img.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    offset_x = (int) event.getX();
                    offset_y = (int) event.getY();
                    selected_item = v;
                    break;
                default:
                    break;
                }

                return false;
            }
        });
    }
}
复制代码

效果图:

 

0
0
分享到:
评论

相关推荐

    Professional Android 4 Application Development

    Explains new and enhanced features such as drag and drop, fragments, the action bar, enhanced multitouch support, new environmental sensor support, major improvements to the animation framework, and a...

    Professional Android 4 Application Development(英文版)

    Explains new and enhanced features such as drag and drop, fragments, the action bar, enhanced multitouch support, new environmental sensor support, major improvements to the animation framework, and a...

    Pro Android 4

    Pro Android 4 shows you how to build real-world and fun mobile apps using the new ...Drag and Drop Using Sensors Understanding the Contacts API Deploying your Application: Android Market and Beyond

    CommonsWare.The.Busy.Coders.Guide.to.Android.Development.Version.8.2.2017

    Drag and Drop Keyboard and Mouse Input Home Screen App Widgets Adapter-Based App Widgets Content Provider Theory Content Provider Implementation Patterns The Loader Framework The ContactsContract and ...

    The Busy Coders Guide to Android Development最终版2019

    Drag and Drop Keyboard and Mouse Input Viewing PDFs Home Screen App Widgets Adapter-Based App Widgets Publishing Slices Hosting Slices Advanced Permissions Restricted Profiles and UserManager Device ...

    Pro.Android.5.1430246804

    Chapter 23 Drag and Drop Chapter 24 Using Sensors Chapter 25 Understanding Content Providers Chapter 26 Understanding the Contacts API Chapter 27 Loaders Chapter 28 Security and Permissions Chapter 29...

    Pro.Android.5.14302468

    Chapter 23 Drag and Drop Chapter 24 Using Sensors Chapter 25 Understanding Content Providers Chapter 26 Understanding the Contacts API Chapter 27 Loaders Chapter 28 Security and Permissions Chapter 29...

    Professional Android 4 Application Development PDF 含示例代码

    Explains new and enhanced features such as drag and drop, fragments, the action bar, enhanced multitouch support, new environmental sensor support, major improvements to the animation framework, and a...

    android-gridview-drag-and-drop:具有拖放功能的复杂网格视图

    android-gridview-拖放将 UI 内容组织为二维滚动网格时,gridview 会派上用场。 这个项目展示了一个更复杂的实现,它给了你更多:现在每个项目视图还显示一个具有双重状态的右上角图像,用作切换很有用,或者在我的...

    Android代码-RecyclerView 超强助手

    Android library that provides most common functions around recycler-view like Swipe to dismiss, Drag and Drop, Divider in the ui, events for when item selected and when not selected, on-click listener...

    Fingers - Touch Gestures for Unity 2.3.5

    Pan/rotate/scale and drag and drop is simple. Built in scripts for these scenarios, with demo scenes are provided. I've also added image recognition gestures. With a few minutes of tweaking, you can...

    selenium webdriver 3 practical guide 第二版

    Perform advanced actions, such as drag-and-drop and action builders on web page Learn to use Java 8 API and Selenium 3 together Explore remote WebDriver and discover how to use it Perform cross ...

    Selenium WebDriver Practical Guide 最新 原版

    Perform advanced actions such as drag-and-drop, double-click, right-click, and action builders on web pages Explore the advanced features of WebDriver such as handling cookies, taking ...

    recyclerviewhelper:[Android库]赋予RecyclerView权力

    Android库提供了围绕回收器视图的最常用功能,例如在ui中滑动以关闭,拖放,Divider,事件在选定项和未选定项时的事件,单击项的侦听器。由和打造的:red_heart: 注意:此库的androidx之前版本的开发已停止。 如果您...

    FastAdapter:防弹,快速且易于使用的适配器库,可将开发时间减至最少。

    快速适配器 FastAdapter此处用于简化为RecyclerViews创建适配器的过程。 不用担心适配器了。 只需编写视图/项目外观... 包括来自Android团队的建议 容易扩展 无限滚动( ) “留在后面”模式( ) 拆分项目视图和模

    史上最全的ios开发源码

    按钮 Drop Down Control 按钮-Circular Music Player Control 》》Flat Pill Button 按钮类--Fancy Menu 按钮之Custom Gradient Button 按钮之Flat Button 按钮之NIDropDown 按钮之Popup Menu 按钮之UIMenuItem ...

Global site tag (gtag.js) - Google Analytics