2016-08-24 97 views
2
  1. 我把Signatureview放在Android Scrollview的內部。Inside Scrollview SignatureView無法正常工作

  2. 雖然在SignatureView上寫了一些東西,上下滾動,我無法寫我的標誌。

如何禁用ScrollviewSignatureView被觸摸

Link to referred signature

Sign.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     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_width="match_parent" 
      android:layout_height="wrap_content" 
      android:isScrollContainer="false" 
      android:paddingBottom="30dp" 
      android:paddingLeft="2dp" 
      android:paddingRight="2dp" 
      android:scrollbars="none"> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"> 


     <com.kyanogen.signatureview.SignatureView 
      xmlns:sign="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/signature_view" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:minHeight="250dp" 
      android:layout_weight="1" 
      sign:penSize="5dp" 
      sign:backgroundColor="#ffffff" 
      sign:penColor="#000000" 
      sign:enableSignature="true"/> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="2"> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/clearSignButton" 
      android:layout_weight="1" 
      android:text="clear"/> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      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 ""; 

    } 
} 
+0

signatureview是在Linearlayout裏面不是滾動視圖? –

+0

scrollview在哪裏?我無法在Sign.xml中看到滾動視圖 – ViramP

+0

@ViramP查看我更新的問題 – Kumar

回答

0

您必須在此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_width="match_parent" 
     android:layout_height="100dp" 
     android:minHeight="250dp" 
     android:layout_weight="1" 
     sign:penSize="5dp" 
     sign:backgroundColor="#ffffff" 
     sign:penColor="#000000" 
     sign:enableSignature="true"/> 
+0

我必須保持那個類Mysignatureview? – Kumar

+0

無論你想要什麼。你可以使用匿名類,內部類或完全獨立的類來做到這一點。 – pawelo

+0

你可以讓我知道,在我的代碼中,我必須修改你的建議,否則在滾動視圖內添加Signature捕獲的完整代碼 – Kumar

0

當您想簽名時,您需要在簽名被觸摸時攔截滾動事件並且當你在簽名區域之外回到正常狀態時: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; 
      } 
     } 
    }); 

建議:如果你想達到你的代碼的一個更好的可讀性不使用像scrollView1使用有意義的名稱變量。