在整个屏幕上移动textview [英] Moving textview through whole screen

查看:107
本文介绍了在整个屏幕上移动textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个应用程序,可以全屏显示时间.小时的分钟和秒显示在屏幕中间.所以我的目标是,当我长时间单击任意一个文本视图时,便可以在整个屏幕上进行裁剪,然后将其放置在我想要的位置...

I have developed an app which shows the time in a full screeen. The hours minutes and seconds are displayed in the middle of the screen. So my objective is, when I long click any of the textviews be able to scrool it through all the screen, and place it where I want...

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

I tried to find a proper code to apply to my app, and I found a code which makes text to image an then move that image. But the problem is, texts are updating each a second, so I think creating images each second is not a good idea...

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

Anyone knows a method to be able to move textviews through all the screen??? This is one of my textviews

private TextView txtHour;

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

txtHour.setOnLongClickListener(new OnLongClickListener{
....

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

I dont know what to add to this.... :( Please HELP!

根据第一个答案,我的代码应该看起来像这样吗?

According to first answer, should my code look like this?

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

           }});

最终这是结果代码,可以吗?

EDIT 2: finnaly this is result code, is this ok?

 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>

推荐答案

将其添加到onCreate:更好地使用FrameLayout本身.

Add this to your onCreate: Better use FrameLayout itself.

 txtHour.setOnTouchListener(new OnTouchListener() {

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

这对您的Activity Class来说都是任何方法.

And this to your Activity Class ouutside any methods.

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

这篇关于在整个屏幕上移动textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆