Android在螢幕任意點移動圖片【大明進化二十四】

來源:互聯網
上載者:User

                 今天看書,看到了這個效果,以前也用過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>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.