让图片跟着手指动【Android Demo】

来源:互联网 发布:淘宝证书下载 编辑:程序博客网 时间:2024/04/28 00:53

IT女一名,边学边做,边做边学…… 害羞

 【PictureMoveDemoActivity.java】

package com.demo.picmove;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.Toast;

public class PictureMoveDemoActivity extends Activity {
 
 // 声明存储屏幕宽高分辨率的对象
 private int screenX, screenY;
 
 private ImageView imageView;
 // 初始化ImageView的默认位置
 private int defaultX, defaultY;
 // ImageView的宽高
 private int imgWidth, imgHeight;
 
 // 用来重新绘制手指所在位置图片的起始坐标
 private float mX, mY;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        // 取得屏幕对象
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        // 获取屏幕宽高
        screenX = dm.widthPixels;
        screenY = dm.heightPixels;
       
        // 设置图片大小为72*72
        imgWidth = 72;
        imgHeight = 72;
        // 设置图片资源
        imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setImageResource(R.drawable.ic_launcher);
        // 获取图片宽高
       
        // 初始化按钮位置居中
        restoreButton();
       
        imageView.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    restoreButton();
   }
  });
    }
   
    // 重写触控事件
    @Override
 public boolean onTouchEvent(MotionEvent event) {
     // 获取手指触摸的坐标
     float x = event.getX();
     float y = event.getY();
     
     switch(event.getAction()) {
     case MotionEvent.ACTION_DOWN:
      pictureMove(x, y);
      break;
  case MotionEvent.ACTION_MOVE:
   pictureMove(x, y);
   break;
  case MotionEvent.ACTION_UP:
   pictureMove(x, y);
   break;
     }
     
  return true;
 }
   
    // 关键:绘制 移动后的图片的方法
    private void pictureMove(float x, float y) {
     mX = x - (imgWidth/2);
     mY = y - (imgHeight/2);
     
     // 判断mX/mY有没有超出屏幕范围
     if(mX>(screenX-imgWidth)) {
      mX = screenX - imgWidth;
     } else if(mX<0) {
      mX = 0;
     } else if(mY>(screenY-imgHeight)) {
      mY = screenY - imgHeight;
     } else if(mY<0) {
      mY = 0;
     }
     
     // 通过Log来查看图片的位置
     Log.e("img_xy = ", "(" + Float.toString(mX) + ", " + Float.toString(mY) + ")");
     
     // setLayoutParams
     imageView.setLayoutParams(
       new AbsoluteLayout.LayoutParams(imgWidth, imgHeight, (int) mX, (int) mY));
     
    }

 private void restoreButton() {
     defaultX = (screenX - imgWidth)/2;
     defaultY = (screenY - imgHeight)/2;
     
     // 显示还原坐标的Toast
     String str = "(" + defaultX + ", " + defaultY + ")";
     makeToast(str, true);
     
     imageView.setLayoutParams(
       new AbsoluteLayout.LayoutParams(imgWidth, imgHeight, defaultX, defaultY));
    }
   
    private void makeToast(String str, boolean isLong) {
     if(isLong) {
      Toast.makeText(PictureMoveDemoActivity.this, str, Toast.LENGTH_LONG).show();
     } else {
      Toast.makeText(PictureMoveDemoActivity.this, str, Toast.LENGTH_SHORT).show();
     }
    }
   
}

【main.xml】

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true" />

</AbsoluteLayout>

 

【AndroidManifest.xml】

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo.picmove"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".PictureMoveDemoActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>