Android:如何创建可移动文本?

时间:2014-08-12 12:36:51

标签: android

大家好我只是好奇有没有办法创建一个类似的可移动文本吐司。我希望用户能够在屏幕上移动它并有一个关闭按钮(右上角的小“X”)。这可能吗?我想我必须创建一些自定义布局但不确定是什么。你能给我任何建议吗?我在哪里可以找到更多相关信息?

1 个答案:

答案 0 :(得分:0)

您可以使用多种方法实现此目的。其中之一是创建像Facebook聊天通知这样的服务。

        import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

public class ChatHeadService extends Service {

    private WindowManager.LayoutParams params;
    private WindowManager.LayoutParams paramstail;
    private WindowManager windowManager;
    private TextView chatHead;
    private ImageView chattail;
    @Override
    public IBinder onBind(Intent intent) {
        // Not used
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        chattail = new ImageView(this);
        chattail.setImageResource(R.drawable.ic_launcher);

        chatHead = new TextView(this);
        chatHead.setText("test");
        Log.d("aaaa", "aaaaaa");
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        paramstail = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        paramstail.gravity = Gravity.BOTTOM | Gravity.CENTER;
        paramstail.x = 0;
        paramstail.y = 0;

        params.gravity = Gravity.BOTTOM | Gravity.CENTER;
        params.x = 0;
        params.y = 200;

        chatHead.setOnTouchListener(new OnTouchListener() {

            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                chattail.setVisibility(View.VISIBLE);
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initialX = params.x;
                    initialY = params.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    return true;
                case MotionEvent.ACTION_UP:
                    chattail.setVisibility(View.INVISIBLE);
                    if(((params.x-paramstail.x)<50)&&((params.y-paramstail.y)<50))
                    {
                        if (chatHead != null)
                            windowManager.removeView(chatHead);
                        windowManager.removeView(chattail);
                    }
                    return true;
                case MotionEvent.ACTION_MOVE:
                    params.x = initialX
                            + (int) (event.getRawX() - initialTouchX);
                    params.y = initialY
                            - (int) (event.getRawY() - initialTouchY);
                    windowManager.updateViewLayout(chatHead, params);
                    return true;
                }
                return false;
            }
        });
        windowManager.addView(chattail, paramstail);
        windowManager.addView(chatHead, params);
        chattail.setVisibility(View.INVISIBLE);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (chatHead != null)
            windowManager.removeView(chatHead);
        windowManager.removeView(chattail);
    }
}

从您的活动中启动此服务。

startService(new Intent(MainActivity.this, ChatHeadService.class));

不要忘记给予此许可。

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

在manifest xml中添加您的服务

<service
        android:name=".ChatHeadService"
        android:label="@string/app_name" >

    </service>