scrollBy实现view随意移动并显示坐标

来源:互联网 发布:淘宝里权重是什么意思 编辑:程序博客网 时间:2024/06/05 00:32

本文通过FrameLayout和LinearLayout来布局,并通过捕捉onTouchEvent事件来实现画面的随意移动,并同时显示移动后画面坐标。

控制view移动的函数主要是scrollTo和scrollBy,两者的差别如下:

scrollTo让我们的layout视图相对于屏幕的左上角进行偏移;

scrollBy是相当于我们当前的坐标进行偏移,我们上面 的例子如果改成scrollBy的话,这个TextView文字会不断地向右下角移动,多 次后我 们就不看不到这个TextView的内容了,因为它已经进行了非Layout视图区,而如果我们用scrollTo的话,不管点多少次按钮,它永 远就就 上面显示的那个位置。

这里我们用scrollBy更合理一些。


1、先上布局文件:main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0"encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">
          
        <ImageView android:src="@drawable/icon1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
                   
    </LinearLayout>
      
    <LinearLayout android:id="@+id/showXY"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal">
          
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:text="当前坐标:"/>
          
        <TextView android:id="@+id/xyValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:text="0,0"/>                
    </LinearLayout>   
</FrameLayout>

2、Activity代码,MainActivity.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.shuxiang.test;
  
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
  
public class MainActivity extends Activity
{
    private LinearLayout container;
    private int currentX;
    private int currentY;
    private TextView xyValue;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
      
        container = (LinearLayout) findViewById(R.id.container);
        xyValue = (TextView) findViewById(R.id.xyValue);
          
    }
      
    @Override 
    public boolean onTouchEvent(MotionEvent event)
    {
      switch(event.getAction())
      {
          caseMotionEvent.ACTION_DOWN:
          {
              currentX = (int) event.getRawX();
              currentY = (int) event.getRawY();
              break;
          }  
          caseMotionEvent.ACTION_MOVE:
          {
              int x2 = (int) event.getRawX();
              int y2 = (int) event.getRawY();
              container.scrollBy(currentX - x2 , currentY - y2);
              currentX = x2;
              currentY = y2;
              xyValue.setText(x2 + ","+ y2);
              break;
          }   
          caseMotionEvent.ACTION_UP:
          {
              break;
          }
      }
        returntrue
    }
}


原文地址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0111/800.html