发布于 2016-03-05 00:23:56 | 147 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android编程实现图标拖动效果的方法,涉及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 {
 /** 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>

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务