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;
}
}
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);
}
}
<?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>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有