Android 图标拖动效果

最近优化图标拖动时的速率,稍微有一点点效果,直接把代码贴出来,有兴趣一起讨论的朋友可以给我留言。

代码如下:

DragView.java

package com.android.dragtest;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

public class DragView extends FrameLayout {
    private static final String TAG = "DragView";
    private float X;
    private float Y;
    
    private View mDragView;
    
    public DragView(Context context) {
        this(context, null);
    }

    public DragView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    
    public DragView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        
        mDragView = new View(context);
        mDragView.setLayoutParams(new LayoutParams(60, 60));
        mDragView.setBackgroundDrawable(getResources().getDrawable(R.drawable.gamecenter));
        mDragView.setVisibility(View.INVISIBLE);
        addView(mDragView);
    }
    
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            Log.d(TAG, "===============>onInterceptTouchEvent ACTION_DOWN");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.d(TAG, "===============>onInterceptTouchEvent ACTION_MOVE");
            break;
        case MotionEvent.ACTION_UP:
            Log.d(TAG, "===============>onInterceptTouchEvent ACTION_UP");
            break;
        }
        return true;
    }
    
    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        X = ev.getX();
        Y = ev.getY();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            Log.d(TAG, "onTouchEvent ACTION_DOWN");
            mDragView.layout((int)X - 30, (int)Y - 30, (int)X + 30, (int)Y + 30);
            mDragView.setVisibility(View.VISIBLE);
            break;
        case MotionEvent.ACTION_MOVE:
            Log.d(TAG, "onTouchEvent ACTION_MOVE x:" + X + " Y:" + Y);
            mDragView.layout((int)X - 30, (int)Y - 30, (int)X + 30, (int)Y + 30);
            break;
        case MotionEvent.ACTION_UP:
            Log.d(TAG, "onTouchEvent ACTION_UP");
            mDragView.setVisibility(View.INVISIBLE);
            break;
        }
        
        return true;
    }
}

DragTestActivity.java


package com.android.dragtest;

import android.app.Activity;
import android.os.Bundle;

public class DragTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <com.android.dragtest.DragView 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.android.dragtest.DragView>
</LinearLayout>


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Android 示例,演示如何在应用程序中实现图标拖动: 1. 建立一个新的 Android 项目。 2. 在布局文件中添加一个 ImageView 控件,用于显示图标。 ```xml <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_marginTop="50dp" android:layout_marginLeft="50dp"/> ``` 3. 在 MainActivity 中设置 ImageView 可以拖动。 ```java public class MainActivity extends AppCompatActivity implements View.OnTouchListener { private ImageView icon; private int xDelta; private int yDelta; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); icon = (ImageView) findViewById(R.id.icon); icon.setOnTouchListener(this); } @Override public boolean onTouch(View view, MotionEvent event) { final int x = (int) event.getRawX(); final int y = (int) event.getRawY(); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); xDelta = x - lParams.leftMargin; yDelta = y - lParams.topMargin; break; case MotionEvent.ACTION_MOVE: RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view .getLayoutParams(); layoutParams.leftMargin = x - xDelta; layoutParams.topMargin = y - yDelta; view.setLayoutParams(layoutParams); break; } return true; } } ``` 4. 运行应用程序,并尝试拖动 ImageView。 以上示例演示了如何在 Android 应用程序中实现图标拖动。在实际应用程序中,您可能需要更多的逻辑来处理拖动,如限制拖动范围、拖动时更改图标拖动时显示边界等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值