使用画布时禁用滚动

时间:2018-05-19 18:07:25

标签: android android-layout android-scrollview

我正在创建一个允许用户通过将其绘制到画布上来添加签名的应用。根据我之前的研究,我决定使用我在Github上发现的名为SignatureView的图书馆。

签名视图在启用滚动之前工作正常,此时,滚动仍然在用户绘图时发生,这意味着画布在输入过程中移动。

由于这个原因,我想在用户与签名视图交互时禁用滚动。到目前为止,我一直尝试将视图放在嵌套的滚动视图和多个xml属性中,但是这些属性中没有一个似乎有任何影响。

布局XML文件(忽略不相关的视图)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.jordan.promise.fragments.EnterSignature"
    android:configChanges="orientation|screenSize"
    android:name=".MainActivity"
    android:label="@string/app_name">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="56dp"
    android:paddingLeft="24dp"
    android:paddingRight="24dp">

    <com.kyanogen.signatureview.SignatureView
        xmlns:sign="http://schemas.android.com/apk/res-auto"
        android:id="@+id/signature_view"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_weight="1"
        sign:penSize="5dp"
        sign:backgroundColor="#ffffff"
        sign:penColor="#000000"
        sign:enableSignature="true"/>
</ScrollView>

OnCreate和OnCreateView方法(签名视图目前尚未在其他任何地方使用)

   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_enter_signature, container, false);

    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    String defaultValue = getResources().getString(R.string.error_name);
    String firstName = sharedPref.getString(getString(R.string.user_first), defaultValue);

    //Greet User and provide instructionsView for entering signature
    helloFirst = view.findViewById(R.id.hello_first);
    instructionsView = view.findViewById(R.id.sig_instructions);
    String hello = getResources().getString(R.string.hello);
    String instructions = getResources().getString(R.string.enter_signature_instruction);
    helloFirst.setText(String.format("%s %s!", hello, firstName)); //Title/Welcome text
    instructionsView.setText(instructions);
    sigBtn = view.findViewById(R.id.signatureBtn);//Continue button, changes text if signature is present

    isDirty = false; //Solves API bug were isDirty only detects if a gesture is taking place

//Signature Capture
clearSignature = view.findViewById(R.id.clearBtn);
clearSignature.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        signatureView.clearCanvas();
        isDirty = false;
    }
});

//Check That Signature Field is not Empty
thread = new Thread() {
    @Override
    public void run() {
        try {
            while(true) {
                sleep(1000);
                Log.i("handler" , "running");
                if(signatureView.isDirty() || isDirty) {
                    sigBtn.setText(getResources().getString(R.string.accept_sig));
                    isDirty = true;
                }
                else {
                    sigBtn.setText(getResources().getString(R.string.decline_sig));
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};

thread.start();

setHasOptionsMenu(true);

signatureView = view.findViewById(R.id.signature_view);


return view;
}

提前感谢您的帮助。我希望这是有道理的,但如果您需要任何澄清或更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

万一有人偶然发现。我的解决方案是为景观创建一个单独的布局,然后放置元素并调整其大小,以使滚动不会成为问题。

相关问题