1. 程式人生 > >移動ImageView,並且儲存移動後的位置

移動ImageView,並且儲存移動後的位置

該篇部落格主要實現的功能:讓ImageView隨著手指的移動而移動,並且儲存移動後的位置,下次進入該頁面時還是移動後的位置,並且可以再次移動

首先來看一下效果圖:


然後貼出實現上述功能的·主要程式碼:

一.佈局檔案

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

</RelativeLayout>
二.程式碼
public class MainActivity extends Activity {
    ImageView iv;
    private int containerWidth;
    private int containerHeight;
    float lastX, lastY;
    RelativeLayout rl;
    private static final String TYPE_X = "type_x";
    private static final String TYPE_Y = "type_y";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rl = (RelativeLayout) findViewById(R.id.rl);


        iv = (ImageView) findViewById(R.id.iv);

        iv.setX(getInt(getApplicationContext(),TYPE_X,0));
        iv.setY(getInt(getApplicationContext(),TYPE_Y,0));
        iv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = event.getRawX();
                        lastY = event.getRawY();
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        //  不要直接用getX和getY,這兩個獲取的資料已經是經過處理的,容易出現圖片抖動的情況
                        float distanceX = lastX - event.getRawX();
                        float distanceY = lastY - event.getRawY();

                        float nextY = iv.getY() - distanceY;
                        float nextX = iv.getX() - distanceX;

                        // 不能移出螢幕
                        if (nextY < 0) {
                            nextY = 0;
                        } else if (nextY > containerHeight - iv.getHeight()) {
                            nextY = containerHeight - iv.getHeight();
                        }
                        if (nextX < 0)
                            nextX = 0;
                        else if (nextX > containerWidth - iv.getWidth())
                            nextX = containerWidth - iv.getWidth();

                        // 屬性動畫移動
                        ObjectAnimator y = ObjectAnimator.ofFloat(iv, "y", iv.getY(), nextY);
                        ObjectAnimator x = ObjectAnimator.ofFloat(iv, "x", iv.getX(), nextX);

                        AnimatorSet animatorSet = new AnimatorSet();
                        animatorSet.playTogether(x, y);
                        animatorSet.setDuration(0);
                        animatorSet.start();

                        lastX = event.getRawX();
                        lastY = event.getRawY();
                }
                return false;
            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        // 這裡來獲取容器的寬和高
        if (hasFocus) {
            containerHeight = rl.getHeight();
            containerWidth = rl.getWidth();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        putInt(getApplicationContext(),TYPE_X,lastX);
        putInt(getApplicationContext(),TYPE_Y,lastY);
    }


    public  static  void putInt(Context context, String key, float value){
        SharedPreferences sp=context.getSharedPreferences("config",Context.MODE_APPEND);
        SharedPreferences.Editor edit = sp.edit();
        edit.putFloat(key,value);
        edit.commit();
    }


    public  static  float getInt(Context context,String key,float defValue){
        SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_APPEND);
        return  sp.getFloat(key,defValue);
    }
}
有需要的可以進行下載喲~
完整Demo下載地址:http://download.csdn.net/detail/k2514091675/9902094