Android通过手势实现图片按照指定路径移动
转载网址:
http://blog.csdn.net/yuejingjiahong/article/details/6658877
Activity:
public class SignParallelActivity extends Activity { private RelativeLayout startRelative; private ImageView startKey; //private List<ViewPostion> list = new ArrayList<SignActivity.ViewPostion>(); private int screenWidth; private int screenHeight; private String TAG = "SignActivity"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.sign); DisplayMetrics dm = getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels - 50; startRelative = (RelativeLayout)findViewById(R.id.startRelative); startKey = (ImageView) findViewById(R.id.startKey); startKey.setOnTouchListener(touchListener); init(); } /**原点*/ private Sign centerSign; /**向上终点*/ private Sign topSign; /**向下终点*/ private Sign bottomSign; /**倾斜终点*/ private Sign biasSign; /**限制最小*/ private int minSize = 30; /**限制左右的距离*/ private int differSize = 25; /**限制最大*/ private int maxSize; /** 设置 */ private void init() { DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); Log.v("MainActivity.initApp", "屏幕尺寸(XxY):" + dm.widthPixels + "X"+ dm.heightPixels); Log.v("MainActivity.initApp", "屏幕分辨率float:" + dm.density); centerSign = new Sign(); if(dm.widthPixels == 240 && dm.heightPixels == 320){ //8500 topSign = new Sign(90, 36, 140, 87); bottomSign = new Sign(90, 194, 140, 245); biasSign = new Sign(15, 70, 67, 121); maxSize = 80; }else{ //MOTO topSign = new Sign(180, 155, 285, 255); bottomSign = new Sign(180, 480, 285, 575); biasSign = new Sign(70, 270, 175, 375); maxSize = 160; } } private OnTouchListener touchListener = new OnTouchListener() { private int lastX; private int lastY; public boolean onTouch(View view, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if(view.getId() == R.id.startKey){ startRelative.setVisibility(View.VISIBLE); //设置图片为移动的图片 startKey.setImageResource(R.drawable.png_nfsnum_down); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); vibrator.vibrate(60); //获取原点位置 int dx =(int)event.getRawX() - lastX; int dy =(int)event.getRawY() - lastY; int left = view.getLeft() + dx; int top = view.getTop() + dy; int right = view.getRight() + dx; int bottom = view.getBottom() + dy; centerSign.setLeft(left); centerSign.setBottom(bottom); centerSign.setRight(right); centerSign.setTop(top); } break; case MotionEvent.ACTION_UP: startRelative.setVisibility(View.INVISIBLE); //还原图片 startKey.setImageResource(R.drawable.png_nfsnum_up); //还原状态 fristAngle = null; break; case MotionEvent.ACTION_MOVE: String str = null; int dx =(int)event.getRawX() - lastX; int dy =(int)event.getRawY() - lastY; int left = view.getLeft() + dx; int top = view.getTop() + dy; int right = view.getRight() + dx; int bottom = view.getBottom() + dy; if(left < 0){ left = 0; right = left + view.getWidth(); } if(right > screenWidth){ right = screenWidth; left = right - view.getWidth(); } if(top < 0){ top = 0; bottom = top + view.getHeight(); } if(bottom > screenHeight){ bottom = screenHeight; top = bottom - view.getHeight(); } //指定方向 超出范围才触发指定的轨迹运动 if((Math.abs(left - centerSign.getLeft()) > minSize || Math.abs(right - centerSign.getRight()) > minSize || Math.abs(top - centerSign.getTop()) > minSize || Math.abs(bottom - centerSign.getBottom()) > minSize) && (Math.abs(left - centerSign.getLeft()) < maxSize && Math.abs(right - centerSign.getRight()) < maxSize && Math.abs(top - centerSign.getTop()) < maxSize && Math.abs(bottom - centerSign.getBottom()) < maxSize)){ //上 当前top减原点top < 0说明向上 if(Math.abs(left - topSign.getLeft()) <= differSize && Math.abs(right - topSign.getRight()) <= differSize && (top - centerSign.getTop()) < 0){ str = "向上"; left = topSign.getLeft(); right = topSign.getRight(); setFristAngle(ANGLE.ANGLE_TOP); } //下 当前top减原点top > 0说明向下 else if(Math.abs(left - bottomSign.getLeft()) <= differSize && Math.abs(right - bottomSign.getRight()) <= differSize && (top - centerSign.getTop()) > 0){ str = "向下"; left = bottomSign.getLeft(); right = bottomSign.getRight(); setFristAngle(ANGLE.ANGLE_BOTTOM); } //左上、右上 移动距离要在左上轴的一定范围内 else if(((left - centerSign.getLeft()) < 0 && //左上 (top - centerSign.getTop()) < 0) || ((left - centerSign.getLeft()) > 0 && //右上 (top - centerSign.getTop()) < 0) ){ if((left - centerSign.getLeft()) < 0 && //左上 (top - centerSign.getTop()) < 0){ str = "左上"; setFristAngle(ANGLE.ANGLE_LEFT_TOP); }else if((left - centerSign.getLeft()) > 0 && //右上 (top - centerSign.getTop()) < 0){ str = "右上"; setFristAngle(ANGLE.ANGLE_RIGHT_TOP); } //获取top 计算方式 未知 = 实际的top - (实际的left * 最大的top / 最大的left) top = centerSign.getTop() - (Math.abs(centerSign.getTop() - biasSign.getTop()) * (Math.abs(Math.abs(left - centerSign.getLeft()))) / Math.abs(biasSign.getLeft() - centerSign.getLeft())); bottom = top + view.getHeight(); } //右下 左下 移动距离要在左上轴的一定范围内 else if(((left - centerSign.getLeft()) > 0 && (top - centerSign.getTop()) > 0) || ((left - centerSign.getLeft()) < 0 && (top - centerSign.getTop()) > 0)){ if((left - centerSign.getLeft()) > 0 && //右下 (top - centerSign.getTop()) > 0){ str = "右下"; setFristAngle(ANGLE.ANGLE_RIGHT_BOTTOM); }else if((left - centerSign.getLeft()) < 0 && //左下 (top - centerSign.getTop()) > 0){ str = "左下"; setFristAngle(ANGLE.ANGLE_LEFT_BOTTOM); } //获取top 计算方式 未知 = 实际的top + (实际的left * 最大的top / 最大的left) top = centerSign.getTop() + (Math.abs(centerSign.getTop() - biasSign.getTop()) * (Math.abs(Math.abs(left - centerSign.getLeft()))) / Math.abs(biasSign.getLeft() - centerSign.getLeft())); bottom = top + view.getHeight(); }else{ //无效区域 return false; } view.layout(left, top, right, bottom); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); }else{ //Log.v(TAG, "超出范围,或者在最小范围以内"); //等于 if((Math.abs(left - centerSign.getLeft()) > minSize || Math.abs(right - centerSign.getRight()) > minSize || Math.abs(top - centerSign.getTop()) > minSize || Math.abs(bottom - centerSign.getBottom()) > minSize) && fristAngle != null && startRelative.getVisibility() == View.VISIBLE){ //隐藏选择层 startRelative.setVisibility(View.INVISIBLE); //设置播放音乐 playMusic(); Toast.makeText(SignParallelActivity.this, "" + fristAngle, Toast.LENGTH_SHORT).show(); //还原状态 fristAngle = null; } } //Log.e(TAG, "x:" + event.getX() + " ,Y:" + event.getY()); break; } return false; } private MediaPlayer mPlayer; /** * 播放音乐 */ private void playMusic() { if(mPlayer != null){ mPlayer.stop(); mPlayer.release(); mPlayer = null; } Log.v(TAG, "播放音乐:"); mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.unlock); //设置不可以重复播放 mPlayer.setLooping(false); mPlayer.start(); } }; public void click(View view){ } /**记录操作的位置*/ private ANGLE fristAngle = null; public void setFristAngle(ANGLE fristAngle) { if(this.fristAngle == null){ this.fristAngle = fristAngle; } } public enum ANGLE{ /**↗*/ ANGLE_RIGHT_TOP, /**↑*/ ANGLE_TOP, /**↖*/ ANGLE_LEFT_TOP, /**←*/ ANGLE_LEFT, /**→*/ ANGLE_RIGHT, /**↙*/ ANGLE_LEFT_BOTTOM, /**↓*/ ANGLE_BOTTOM, /**↘*/ ANGLE_RIGHT_BOTTOM } private class Sign{ private int left; private int right; private int top; private int bottom; public Sign(){ } /**所有参数的构造函数*/ public Sign(int left, int top, int right, int bottom){ this.left = left; this.top = top; this.right = right; this.bottom = bottom; } public int getLeft() { return left; } public void setLeft(int left) { this.left = left; } public int getRight() { return right; } public void setRight(int right) { this.right = right; } public int getTop() { return top; } public void setTop(int top) { this.top = top; } public int getBottom() { return bottom; } public void setBottom(int bottom) { this.bottom = bottom; } } }
xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layoutRacingNeedForSpeed" android:padding="0px" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:id="@+id/startRelative" android:visibility="invisible" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:src="@drawable/png_nfsnum_bg" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> <ImageView android:id="@+id/startKey" android:onClick="click" android:src="@drawable/title" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/startBtn" android:text="tuodongdeanniu" android:visibility="invisible" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>