2

我开发了一个应用程序,可以全屏显示时间。小时、分钟和秒显示在屏幕中间。所以我的目标是,当我长按任何文本视图时,能够在整个屏幕上滚动它,并将其放置在我想要的位置......

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

任何人都知道一种能够在整个屏幕上移动文本视图的方法???这是我的文本视图之一

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:最后这是结果代码,可以吗?

 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;}
       }}
       }

而这个框架布局

      <?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>
4

2 回答 2

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;
               }
            }
于 2012-05-22T11:22:49.857 回答
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。如果没有相应地更改布局参数。

于 2012-05-21T20:06:32.820 回答