iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android编程实现图标拖动效果的方法
  • 349
分享到

Android编程实现图标拖动效果的方法

方法动效Android 2022-06-06 09:06:13 349人浏览 泡泡鱼
摘要

本文实例讲述了Android编程实现图标拖动效果的方法。分享给大家供大家参考,具体如下: 最近优化图标拖动时的速率,稍微有一点点效果,直接把代码贴出来,有兴趣一起讨论的朋友可以

本文实例讲述了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 {
 
 @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>

希望本文所述对大家Android程序设计有所帮助。

您可能感兴趣的文章:android 添加随意拖动的桌面悬浮窗口Android 仿淘宝、京东商品详情页向上拖动查看图文详情控件DEMO详解Android 可拖动的seekbar自定义进度值Android编程之控件可拖动的实现方法android 应用内部悬浮可拖动按钮简单实现代码Android实现ImageView图片缩放和拖动Android实现跟随手指拖动并自动贴边的View样式(实例demo)android Matrix实现图片随意放大缩小或拖动Android自定义View实现拖动选择按钮Android使用ViewDragHelper实现QQ聊天气泡拖动效果


--结束END--

本文标题: Android编程实现图标拖动效果的方法

本文链接: https://www.lsjlt.com/news/26323.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作