java - 如何将 float 操作按钮带出 Android 应用程序?

标签 java android translation

我想创建一个 Android 应用程序,可以翻译用户屏幕上的任何文本。它应该适用于整个 Android 手机的所有应用程序。我想在应用程序外部添加一个 float 操作按钮,并使其作为可见、可移动和可点击的后台服务运行。 float 按钮将获取复制的文本,然后使用谷歌翻译 API 进行翻译。

请帮助我,我需要一些指导和资源。

最佳答案

请检查下面的 Java 和 XML 文件以获取 float 操作按钮

JAVA

package com.example;

import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

public class MovableFloatingActionButton extends FloatingActionButton implements View.OnTouchListener {

    private final static float CLICK_DRAG_TOLERANCE = 10; // Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.

    private float downRawX, downRawY;
    private float dX, dY;

    public MovableFloatingActionButton(Context context) {
        super(context);
        init();
    }

    public MovableFloatingActionButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MovableFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent){

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)view.getLayoutParams();

        int action = motionEvent.getAction();
        if (action == MotionEvent.ACTION_DOWN) {

            downRawX = motionEvent.getRawX();
            downRawY = motionEvent.getRawY();
            dX = view.getX() - downRawX;
            dY = view.getY() - downRawY;

            return true; // Consumed

        }
        else if (action == MotionEvent.ACTION_MOVE) {

            int viewWidth = view.getWidth();
            int viewHeight = view.getHeight();

            View viewParent = (View)view.getParent();
            int parentWidth = viewParent.getWidth();
            int parentHeight = viewParent.getHeight();

            float newX = motionEvent.getRawX() + dX;
            newX = Math.max(layoutParams.leftMargin, newX); // Don't allow the FAB past the left hand side of the parent
            newX = Math.min(parentWidth - viewWidth - layoutParams.rightMargin, newX); // Don't allow the FAB past the right hand side of the parent

            float newY = motionEvent.getRawY() + dY;
            newY = Math.max(layoutParams.topMargin, newY); // Don't allow the FAB past the top of the parent
            newY = Math.min(parentHeight - viewHeight - layoutParams.bottomMargin, newY); // Don't allow the FAB past the bottom of the parent

            view.animate()
                    .x(newX)
                    .y(newY)
                    .setDuration(0)
                    .start();

            return true; // Consumed

        }
        else if (action == MotionEvent.ACTION_UP) {

            float upRawX = motionEvent.getRawX();
            float upRawY = motionEvent.getRawY();

            float upDX = upRawX - downRawX;
            float upDY = upRawY - downRawY;

            if (Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) { // A click
                return performClick();
            }
            else { // A drag
                return true; // Consumed
            }

        }
        else {
            return super.onTouchEvent(motionEvent);
        }

    }

}

XML

<com.example.MovableFloatingActionButton
        android:id="@+id/btnFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_navigate_next_white_24dp"/>

关于java - 如何将 float 操作按钮带出 Android 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61474295/

相关文章:

android - getTitle() 使用 Web URL,而不是 webView.getTitle();

android - 带有按钮单击的径向菜单android?

python - 字符串不会在 Django 中使用 Python 2.7 中可用的格式函数进行翻译

mysql - 如果找到,SQL 将字段内容替换为另一个表中的内容

ruby-on-rails - ActiveRecord Transaction并返回模型保存的id

java - 我如何将用户重定向到 TAI 中的身份提供商页面? WebSphere

java - 处理 - 帧率小数

android - com.google.android.gms.internal.firebase-perf.zzq.zzb(boolean)' 在空对象引用上

java - Android 中的 FatalShutdown 与 httpurlconnection

Java非递归文件系统遍历