android - 使用 Canvas 时禁用滚动
问题描述
我正在创建一个应用程序,允许用户通过将签名绘制到画布上来添加签名。根据我之前的研究,我决定使用我在 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;
}
预先感谢您的任何帮助。我希望这是有道理的,但如果您需要任何澄清或更多信息,请告诉我。
解决方案
以防有人偶然发现这一点。我的解决方案是为横向创建单独的布局,然后放置元素并调整其大小,这样滚动就不会成为问题。
推荐阅读
- javascript - 为 JSON 对象中的每个数组添加一个(子)按钮元素到 div,并为数组中的每个对象添加子按钮(到数组按钮)
- java - 新的 Runnable 和 Runnable 类型的功能接口有什么区别?
- spring-cloud-stream - Spring Cloud Stream中BindingServiceConfiguration.inputBindingLifecycle()方法有什么用?
- git - 当文件可用时,预推无此类文件
- excel - 如何从当前单元格和输入单元格中求和值
- amazon-web-services - EMR 上的 Spark 作业突然需要 30 小时(原为 5 小时)
- c++ - 将 int 分配给 int [x] 的不兼容类型
- c# - 如何使 Image.fillAmount 表现得像 unity.Slider?
- r - 如何使用 for 循环转换列
- macos - 产生 1 个错误。制作:*** [php_intl.lo] 错误 1 错误:`make' 失败