Android 图标拖动效果

本文详细介绍了如何优化图标拖动时的速率,并提供了相关代码实现。通过分析不同事件的动作,调整布局和视图可见性,实现了更流畅的用户体验。适合对Android UI交互优化感兴趣的开发者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

代码如下:

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值