通过整个屏幕移动textview

时间:2012-05-21 19:59:57

标签: android textview screen onlongclicklistener

我开发了一款应用程序,可以显示完整的屏幕时间。小时分钟和秒显示在屏幕中间。所以我的目标是,当我长时间点击任何文本视图时,能够通过所有屏幕将其扫描出来,并将其放置在我想要的位置...

我试图找到一个适用于我的应用程序的正确代码,然后我找到了一个代码,它可以使文本成像,然后移动该图像。但问题是,文本每秒更新一次,所以我认为每秒创建图像并不是一个好主意......

任何人都知道一种能够通过所有屏幕移动文本视图的方法??? 这是我的观点之一

private TextView txtHour;

txtHour = (TextView)findViewById(R.id.TxtHour);

txtHour.setOnLongClickListener(new OnLongClickListener{
....

我不知道该添加什么.... :(请帮助!

编辑:根据第一个答案,我的代码应该是这样吗?

         txtHour.setOnLongClickListener(new OnLongClickListener() {
             public void onLongClick(View v){


               public void drag(MotionEvent event, View v)
                {

                    RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) v.getLayoutParams();

                    switch(event.getAction())
                    {
                       case MotionEvent.ACTION_MOVE:
                       {
                         params.topMargin = (int)event.getRawY() - (v.getHeight());
                         params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
                         v.setLayoutParams(params);
                         break;
                       }
                       case MotionEvent.ACTION_UP:
                       {
                         params.topMargin = (int)event.getRawY() - (v.getHeight());
                         params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
                         v.setLayoutParams(params);
                         break;
                       }
                       case MotionEvent.ACTION_DOWN:
                       {
                        v.setLayoutParams(params);
                        break;
                       }
                    }

           }});

编辑2:finnaly这是结果代码,这可以吗?

 package com.iamaner.T2Speech;
 //imports
 public class MAINActivity extends Activity{

 private TextView txtHour;
 private TextView txtMinutes;
 private TextView txtSeconds;

 @Override
 public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

txtHour = (TextView)findViewById(R.id.TxtHour);
txtMinutes = (TextView)findViewById(R.id.TxtMinute);
txtSeconds = (TextView)findViewById(R.id.TxtSeconds);

txtHour.setOnTouchListener(new OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        drag(event, v);
        return false;
    }});
 }
 public void drag(MotionEvent event, View v)
{

    FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) v.getLayoutParams();

    switch(event.getAction())
    {
       case MotionEvent.ACTION_MOVE:
       {params.topMargin = (int)event.getRawY() - (v.getHeight());
        params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
        v.setLayoutParams(params);
        break;}
       case MotionEvent.ACTION_UP:
       {params.topMargin = (int)event.getRawY() - (v.getHeight());
        params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
        v.setLayoutParams(params);
        break;}
       case MotionEvent.ACTION_DOWN:
       {v.setLayoutParams(params);
        break;}
       }}
       }

这个framelayout

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

<TextView
    android:id="@+id/TxtHour"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15mm"
    android:textColor="#000000"/>

<TextView
    android:id="@+id/TxtPoints1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/separator"
    android:textSize="15mm"
    android:textColor="#000000"
    android:layout_gravity="center"
    android:gravity="center"/>

<TextView
    android:id="@+id/TxtMinute"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15mm" 
    android:textColor="#000000"
    android:layout_gravity="center"
    android:gravity="center" />

 <TextView
    android:id="@+id/TxtPoints2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/separator"
    android:textSize="15mm"
    android:textColor="#000000"
    android:layout_gravity="center"
    android:gravity="center"/>

 <TextView
    android:id="@+id/TxtSeconds"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15mm"
    android:textColor="#000000"
    android:layout_gravity="bottom|right" />


  </FrameLayout>

2 个答案:

答案 0 :(得分:4)

将此添加到您的onCreate:更好地使用FrameLayout本身。

 txtHour.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            drag(event, v);
            return false;
        }
    });

除了任何方法,这都是你的活动类。

       public void drag(MotionEvent event, View v)
        {

            RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) v.getLayoutParams();

            switch(event.getAction())
            {
               case MotionEvent.ACTION_MOVE:
               {
                 params.topMargin = (int)event.getRawY() - (v.getHeight());
                 params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
                 v.setLayoutParams(params);
                 break;
               }
               case MotionEvent.ACTION_UP:
               {
                 params.topMargin = (int)event.getRawY() - (v.getHeight());
                 params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
                 v.setLayoutParams(params);
                 break;
               }
               case MotionEvent.ACTION_DOWN:
               {
                v.setLayoutParams(params);
                break;
               }
            }

答案 1 :(得分:0)

public void drag(MotionEvent event, View v)
{

    FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) v.getLayoutParams();

    switch(event.getAction())
    {
    case MotionEvent.ACTION_MOVE:
    {
        params.topMargin = (int)event.getRawY() - (v.getHeight());
        params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
        v.setLayoutParams(params);
        break;
    }
    case MotionEvent.ACTION_UP:
    {
        params.topMargin = (int)event.getRawY() - (v.getHeight());
         params.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
        v.setLayoutParams(params);
        break;
    }
    case MotionEvent.ACTION_DOWN:
    {
        v.setLayoutParams(params);
        break;
    }
    }
}

将此方法复制到您的类并在textview的ontouch()中调用它。在这里,我假设你使用Framelayout。如果不相应地更改布局参数。