今天看書,看到了這個效果,以前也用過onTouchEvent(MotionEvent event)這個方法,但是沒有想到這麼用,感覺不錯,所以就自己寫了一下,感覺效果不錯,以後如果做遊戲用得到,點擊螢幕在螢幕上滑動,圖片跟著滑動!效果不錯啊!哈哈,分享一下給大家,希望給大家點啟迪,有問題的留言,想要源碼的留言,歡迎大家留言討論!轉載請標明出處:
http://blog.csdn.net/wdaming1986/article/details/6788097
程式開始介面: 點擊螢幕左上方,圖片移動過去
點擊螢幕左下角,圖片移動過去 點擊螢幕右下角,圖片移動過去
代碼說明一切:
在TouchDemo工程下,com.cn.daming的包下面:
一、MainActivity.java類的代碼:
package com.cn.daming;import android.app.Activity;import android.graphics.Color;import android.graphics.drawable.GradientDrawable;import android.graphics.drawable.GradientDrawable.Orientation;import android.os.Bundle;import android.util.DisplayMetrics;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.widget.AbsoluteLayout;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {/*聲明ImageView變數*/ private ImageView mImageView01; /*聲明相關變數作為儲存圖片寬高,位置使用*/ private int intWidth, intHeight, intDefaultX, intDefaultY; private float mX, mY; /*聲明儲存螢幕的解析度變數 */ private int intScreenX, intScreenY; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); drawBackground(); /* 取得螢幕對象 */ DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); /* 取得螢幕解析像素 */ intScreenX = dm.widthPixels; intScreenY = dm.heightPixels; /* 設定圖片的寬高 */ intWidth = 100; intHeight = 100; /*通過findViewById構造器建立ImageView對象*/ mImageView01 =(ImageView) findViewById(R.id.myImageView1); /*將圖片從Drawable賦值給ImageView來呈現*/ mImageView01.setImageResource(R.drawable.body); /* 初始化按鈕位置置中 */ RestoreButton(); /* 當點擊ImageView,還原初始位置 */ mImageView01.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { RestoreButton(); } }); } //載入背景顏色 public void drawBackground() { GradientDrawable grad = new GradientDrawable( Orientation.TL_BR, new int[] { Color.rgb(0, 0, 127), Color.rgb(0, 0, 255), Color.rgb(127, 0, 255), Color.rgb(127, 127, 255), Color.rgb(127, 255, 255), Color.rgb(255, 255, 255) } ); this.getWindow().setBackgroundDrawable(grad); } /*覆蓋觸控事件*/ public boolean onTouchEvent(MotionEvent event) { /*取得手指觸控螢幕的位置*/ float x = event.getX(); float y = event.getY(); try { /*觸控事件的處理*/ switch (event.getAction()) { /*點擊螢幕*/ case MotionEvent.ACTION_DOWN: picMove(x, y); break; /*移動位置*/ case MotionEvent.ACTION_MOVE: picMove(x, y); break; /*離開螢幕*/ case MotionEvent.ACTION_UP: picMove(x, y); break; } }catch(Exception e) { e.printStackTrace(); } return true; } /*移動圖片的方法*/ private void picMove(float x, float y) { /*預設微調圖片與指標的相對位置*/ mX=x-(intWidth/2); mY=y-(intHeight/2); /*防圖片超過螢幕的相關處理*/ /*防止螢幕向右超過螢幕*/ if((mX+intWidth)>intScreenX) { mX = intScreenX-intWidth; } /*防止螢幕向左超過螢幕*/ else if(mX<0) { mX = 0; } /*防止螢幕向下超過螢幕*/ else if ((mY+intHeight)>intScreenY) { mY=intScreenY-intHeight; } /*防止螢幕向上超過螢幕*/ else if (mY<0) { mY = 0; } /*通過log 來查看圖片位置*/ Log.i("jay", Float.toString(mX)+","+Float.toString(mY)); /* 以setLayoutParams方法,重新安排Layout上的位置 */ mImageView01.setLayoutParams ( new AbsoluteLayout.LayoutParams (intWidth,intHeight,(int) mX,(int)mY) ); } /* 還原ImageView位置的事件處理 */ public void RestoreButton() { intDefaultX = ((intScreenX-intWidth)/2); intDefaultY = ((intScreenY-intHeight)/2); /*Toast還原位置座標*/ mMakeTextToast ( "("+ Integer.toString(intDefaultX)+ ","+ Integer.toString(intDefaultY)+")",true ); /* 以setLayoutParams方法,重新安排Layout上的位置 */ mImageView01.setLayoutParams ( new AbsoluteLayout.LayoutParams (intWidth,intHeight,intDefaultX,intDefaultY) ); } /*自訂一發出資訊的方法*/ public void mMakeTextToast(String str, boolean isLong) { if(isLong==true) { Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show(); } }}
二、在main.xml布局檔案中的代碼:
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout android:id="@+id/widget27" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_x="80dip" android:text="@string/hello" /> <ImageView android:id="@+id/myImageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ImageView></AbsoluteLayout>