Scrollview SignatureView 内部不起作用
Posted
技术标签:
【中文标题】Scrollview SignatureView 内部不起作用【英文标题】:Inside Scrollview SignatureView is not working 【发布时间】:2016-08-24 13:33:34 【问题描述】:我将Signatureview
放入android Scrollview
。
但是在SignatureView
上写东西时,上下滚动我无法写下我的标志。
当SignatureView
被触摸时如何禁用Scrollview
Link to referred signature
签名.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<ScrollView
android:id="@+id/scrollView1"
android:layout_
android:layout_
android:isScrollContainer="false"
android:paddingBottom="30dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:scrollbars="none">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<com.kyanogen.signatureview.SignatureView
xmlns:sign="http://schemas.android.com/apk/res-auto"
android:id="@+id/signature_view"
android:layout_
android:layout_
android:minHeight="250dp"
android:layout_weight="1"
sign:penSize="5dp"
sign:backgroundColor="#ffffff"
sign:penColor="#000000"
sign:enableSignature="true"/>
<LinearLayout
android:layout_
android:layout_
android:weightSum="2">
<Button
android:layout_
android:layout_
android:id="@+id/clearSignButton"
android:layout_weight="1"
android:text="clear"/>
<Button
android:layout_
android:layout_
android:id="@+id/saveSignButton"
android:layout_weight="1"
android:text="save"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</Scrolview>
Main.java
package com.example.parsaniahardik.signaturedemo;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.kyanogen.signatureview.SignatureView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity
Bitmap bitmap;
Button clear,save;
SignatureView signatureView;
String path;
private static final String IMAGE_DIRECTORY = "/signdemo";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signatureView = (SignatureView) findViewById(R.id.signature_view);
clear = (Button) findViewById(R.id.clear);
save = (Button) findViewById(R.id.save);
clear.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
signatureView.clearCanvas();
);
save.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
bitmap = signatureView.getSignatureBitmap();
path = saveImage(bitmap);
);
public String saveImage(Bitmap myBitmap)
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File wallpaperDirectory = new File(
Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY /*iDyme folder*/);
// have the object build the directory structure, if needed.
if (!wallpaperDirectory.exists())
wallpaperDirectory.mkdirs();
Log.d("hhhhh",wallpaperDirectory.toString());
try
File f = new File(wallpaperDirectory, Calendar.getInstance()
.getTimeInMillis() + ".jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(MainActivity.this,
new String[]f.getPath(),
new String[]"image/jpeg", null);
fo.close();
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
return f.getAbsolutePath();
catch (IOException e1)
e1.printStackTrace();
return "";
【问题讨论】:
signatureview 在 Linearlayout 而不是 Scrollview 内? 滚动视图在哪里?我在 Sign.xml 中看不到滚动视图 @ViramP 查看我更新的问题 抱歉,我检查了你的代码,一切正常。 【参考方案1】:当你要签名时需要拦截滚动事件当签名被触摸时。当你在签名区域之外时返回正常状态:scrollView拦截滚动事件。 在控制器的 onCreate 中添加以下内容:
signatureView.setOnTouchListener(new View.OnTouchListener()
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
int action = motionEvent.getAction();
switch (action)
case MotionEvent.ACTION_DOWN:
// Disable the scroll view to intercept the touch event
scrollView1.requestDisallowInterceptTouchEvent(true);
return false;
case MotionEvent.ACTION_UP:
// Allow scroll View to interceot the touch event
scrollView1.requestDisallowInterceptTouchEvent(false);
return true;
case MotionEvent.ACTION_MOVE:
scrollView1.requestDisallowInterceptTouchEvent(true);
return false;
default:
return true;
);
【讨论】:
【参考方案2】:您必须在此 SignatureView 中拦截触摸事件。最好的方法是继承它并覆盖 onTouchEvent:
public class MySignatureView extends SignatureView
@Override
public boolean onTouchEvent(MotionEvent event)
// intercept touch event here, so it's not passed to ScrollView
super.onTouchEvent(event);
return false;
然后,当然在您的布局中将 SignatureView 更改为 MySignatureView。
<com.your.package.name.MySignatureView
xmlns:sign="http://schemas.android.com/apk/res-auto"
android:id="@+id/signature_view"
android:layout_
android:layout_
android:minHeight="250dp"
android:layout_weight="1"
sign:penSize="5dp"
sign:backgroundColor="#ffffff"
sign:penColor="#000000"
sign:enableSignature="true"/>
【讨论】:
我必须在哪里保留该类 Mysignatureview? 随心所欲。您可以使用匿名类、内部类或完全独立的类来做到这一点。 你能告诉我吗,在我的代码中我必须修改你建议的内容,否则在scrollview中添加签名捕获的完整代码以上是关于Scrollview SignatureView 内部不起作用的主要内容,如果未能解决你的问题,请参考以下文章
android 中 scrollview 里面 又有一个子scrollview滑动事件冲突
属性 'scrollView' 需要定义方法 '-scrollView' - 使用 @synthesize、@dynamic 还是提供方法实现?