2014-10-18 3 views
0

следующий мой Основная деятельностьДобавить дважды нажмите на код

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.ActivityNotFoundException; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.CompressFormat; 
import android.graphics.PointF; 
import android.graphics.Typeface; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.speech.RecognizerIntent; 
import android.util.Log; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.Locale; 
import com.barr.survey.R.id; 
class Demo { 
public static String roomname; 
} 
public class DemoActivity extends Activity { 
static int TAKE_PICTURE = 3; 
Uri outputFileUri; 
LinearLayout L1; 
ImageView image; 
public static final int REQUEST_CODE = 0; 
private static final String STATE_SCALE = "state-scale"; 
private static final String STATE_CENTER_X = "state-center-x"; 
private static final String STATE_CENTER_Y = "state-center-y"; 
private GestureDetector detector; 
DBAdapter db; 
EditText mEdit1; 
EditText mEdit2; 
private ImageView mImage; 

public void onCreate(final Bundle savedInstanceState) { 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    mImage = (ImageView) findViewById(R.id.mImageView); 
    Button txt1 = (Button) findViewById(R.id.picbtn); 
    Typeface font2 = Typeface.createFromAsset(getAssets(), "fonts/heydings_edit.otf"); 
    txt1.setTypeface(font2); 
    Button txt2 = (Button) findViewById(R.id.savebtn); 
    txt2.setTypeface(font2); 
    Button txt3 = (Button) findViewById(R.id.emailbtn); 
    txt3.setTypeface(font2); 
    Button txt4 = (Button) findViewById(R.id.micbtn); 
    txt4.setTypeface(font2); 
    Button txt5 = (Button) findViewById(R.id.exitbtn); 
    txt5.setTypeface(font2); 
    String imagename = "DSC00277.png"; 
    String destDir = "/data/data/" + getPackageName() + 
      "/databases/"; 
    String destPath = destDir + "surveydata.db"; 
    File f1 = new File(destPath); 
    if (!f1.exists()) { 
     //---make sure directory exists--- 
     File directory = new File(destDir); 
     directory.mkdirs(); 
     //---copy the db from the assets folder into 
     // the databases folder--- 
     try { 
      CopyDB(getBaseContext().getAssets().open("surveydata.db"), 
        new FileOutputStream(destPath)); 
     } catch (FileNotFoundException e1) {   
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
    } 
    try { 
     SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)findViewById(id.imageView); 
     imageView.setImageAsset(imagename); 
     if (savedInstanceState != null && 
       savedInstanceState.containsKey(STATE_SCALE) && 
       savedInstanceState.containsKey(STATE_CENTER_X) && 
       savedInstanceState.containsKey(STATE_CENTER_Y)) { 
      imageView.setScaleAndCenter(savedInstanceState.getFloat(STATE_SCALE), new PointF(savedInstanceState.getFloat(STATE_CENTER_X), savedInstanceState.getFloat(STATE_CENTER_Y))); 
     } 
    } catch (IOException e) { 
     Log.e(DemoActivity.class.getSimpleName(), "Could not load asset", e); 
    }} 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)findViewById(id.imageView); 
    outState.putFloat(STATE_SCALE, imageView.getScale()); 
    PointF center = imageView.getCenter(); 
    if (center != null) { 
     outState.putFloat(STATE_CENTER_X, center.x); 
     outState.putFloat(STATE_CENTER_Y, center.y); 
    }} 
public void capscr(View view) { 
      Bitmap bitmap = takeScreenshot(); 
      saveBitmap(bitmap); 
      Toast.makeText(getBaseContext(), "Screenshot Saved", Toast.LENGTH_LONG).show(); 
    } 
      public Bitmap takeScreenshot() { 
       View rootView = findViewById(R.id.container).getRootView(); 
       rootView.setDrawingCacheEnabled(true); 
       return rootView.getDrawingCache(); 
      } 
      public void saveBitmap(Bitmap bitmap) { 
       String schname = "fred"; 
       String[] tokens = schname.split(" "); 
       String timeStamp = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date()); 
       String ref = "mark"; 
       final String imageFileName = "Room" + "-" + ref + "-" + timeStamp + ".jpg"; 
       File imagePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + imageFileName); 
       FileOutputStream fos; 
       try { 
        fos = new FileOutputStream(imagePath); 
        bitmap.compress(CompressFormat.JPEG, 100, fos); 
        fos.flush(); 
        fos.close(); 
        //Put up the Yes/No message box 
        AlertDialog.Builder builder = new AlertDialog.Builder(this); 
        builder 
        .setTitle("Send Screenshot") 
        .setMessage("Are you sure?") 
        .setIcon(android.R.drawable.ic_dialog_alert) 
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) {      
          emailtext2(imageFileName); 
         } 
        }) 
        .setNegativeButton("No", null)      //Do nothing on no 
        .show(); 
        } catch (FileNotFoundException e) { 
        Log.e("GREC", e.getMessage(), e); 
       } catch (IOException e) { 
        Log.e("GREC", e.getMessage(), e); 
       } 
      } 
      public void CopyDB(InputStream inputStream, OutputStream outputStream) 
       throws IOException { 
        //---copy 1K bytes at a time--- 
        byte[] buffer = new byte[1024]; 
        int length; 
        while ((length = inputStream.read(buffer)) > 0) { 
         outputStream.write(buffer, 0, length); 
        } 
        inputStream.close(); 
        outputStream.close(); 
       } 
public void savetext(View view) { 
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
    String name = "survey.csv"; 
    File file = new File(path, name); 
    String lineSep = System.getProperty("line.separator"); 
    String fileName = String.format("%s/" + "survey" + ".csv", path); 
    String site = "site"; 
    String building = "Building"; 
    String Floor = "Floor"; 
    String RoomRef = "RoomRef"; 
    mEdit2 = (EditText)findViewById(R.id.editText2); 
    String Notes = mEdit2.getText().toString(); 
    TextView data1 = (TextView) findViewById(R.id.textView1); 
    String filenamestr = data1.getText().toString(); 
    String Line = site + "," + building + "," + Floor + "," + RoomRef + "," + filenamestr + "," + Notes + lineSep; 
    try { 
      File myFile = new File(fileName); 
      if(!myFile.exists()){ 
      myFile.createNewFile(); 
      } 
      FileOutputStream fOut = new FileOutputStream(myFile,true); 
      OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
      myOutWriter.append(Line); 
      myOutWriter.close(); 
      fOut.close(); 
      } catch (Exception e) { 
      Log.e("ERRR", "Could not create file",e);}} 
public void testbtn(View view) { 
    room(Demovar.roomname); 
} 
public void takepic(View view) { 
     String schname = "fred"; 
     String[] tokens = schname.split(" "); 
     String timeStamp = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date()); 
     String imageFileName = tokens[0] + "-" + timeStamp + ".jpg"; 
     TextView detail = (TextView)findViewById(R.id.textView1); 
     detail.setText(imageFileName); 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
     File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
      String name = imageFileName; 
      File file = new File(path, name); 
      outputFileUri = Uri.fromFile(file); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
      startActivityForResult(intent, TAKE_PICTURE); 
      new SingleMediaScanner(this, file); 
    } 
private final int REQ_CODE_SPEECH_INPUT = 1; 
public void speech1(View view) { 
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
      RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 
      getString(R.string.speech_prompt)); 
    try { 
     startActivityForResult(intent, REQ_CODE_SPEECH_INPUT); 
    } catch (ActivityNotFoundException a) { 
     Toast.makeText(getApplicationContext(), 
       getString(R.string.speech_not_supported), 
       Toast.LENGTH_SHORT).show(); 
    } 
    } 
protected static final int RESULT_SPEECH = 1; 
static final int REQUEST_IMAGE_CAPTURE = 1; 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch(requestCode) { 
    case RESULT_SPEECH: { 
     if (resultCode == RESULT_OK && null != data) { 

      ArrayList<String> text = data 
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      EditText editText = (EditText)findViewById(R.id.editText2); 
      editText.setText(text.get(0), TextView.BufferType.EDITABLE); 
     } 
     break; 
    } 
    case 109: 
      if (resultCode == RESULT_OK) { 
       ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
       EditText editText = (EditText)findViewById(R.id.editText2); 
        editText.setText(result.get(0), TextView.BufferType.EDITABLE); 
      } 
    case 2: 
     if (resultCode == RESULT_OK){ 
      super.onActivityResult(requestCode, resultCode, data); 
      data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
     } 
    case 3: 
     if (resultCode == RESULT_OK){ 
      Bitmap bitmap = null; 
      try { 
       GetImageThumbnail getImageThumbnail = new GetImageThumbnail(); 
       bitmap = getImageThumbnail.getThumbnail(outputFileUri, this); 
      } catch (FileNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      // Setting image image icon on the imageview 
      ImageView imageView = (ImageView) this 
        .findViewById(R.id.mImageView); 
      imageView.setImageBitmap(bitmap); 
      break; 
     } 
      break; 
     } 
    } 
public void clk_exit(View view) { 
    System.exit(0); 
} 
public void emailtext(View view) { 
    File extStore = Environment.getExternalStorageDirectory(); 
    File myFile = new File(extStore.getAbsolutePath() + "/Pictures/survey.csv"); 
     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.putExtra(Intent.EXTRA_SUBJECT, "Survey Data"); 
     intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
     intent.putExtra(Intent.EXTRA_TEXT, "Survey Data Attached"); 
     intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(myFile.toString()))); 
     intent.setType("text/plain"); 
     this.startActivity(Intent.createChooser(intent, "Send mail...")); 
} 

public void emailtext2(String filenamestr) { 
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
     String name = "/" + filenamestr; 
     File myFile = new File(path, name); 
     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.putExtra(Intent.EXTRA_SUBJECT, "Screenshot"); 
     intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
     intent.putExtra(Intent.EXTRA_TEXT, "Map Screenshot Attached"); 
     intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(myFile.toString()))); 
     intent.setType("text/plain"); 
     this.startActivity(Intent.createChooser(intent, "Send mail...")); 
} 
public void room(String roomid){ 
db = new DBAdapter(this); 
    db.open(); 
    String strname = roomid; 
    Cursor c = db.getAsset(strname); 
    if (c.moveToFirst())   
     DisplayContact(c); 
db.close(); 
      } 
public void DisplayContact(Cursor c) 
{ 
String fontPath1 = "fonts/DistProTh.otf"; 
Typeface tf2 = Typeface.createFromAsset(getAssets(), fontPath1); 
TextView a = (TextView) findViewById(R.id.Sitename); 
a.setTypeface(tf2); 
a.setText(c.getString(1)); 
TextView b = (TextView) findViewById(R.id.Building); 
b.setTypeface(tf2); 
b.setText(c.getString(2)); 
TextView d = (TextView) findViewById(R.id.Floor); 
d.setTypeface(tf2); 
d.setText(c.getString(3)); 
TextView e = (TextView) findViewById(R.id.Roomno); 
e.setTypeface(tf2); 
e.setText(c.getString(4)); 
TextView f = (TextView) findViewById(R.id.Drawref); 
f.setTypeface(tf2); 
f.setText(c.getString(5)); 
}} 

Я хочу, чтобы иметь возможность дважды нажмите где-нибудь в ImageView и это то сделать что-то

Мой код для двойного крана следующего

detector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { 
     @Override 

      public boolean onDoubleTapEvent(MotionEvent ev) { 
      int x = (int)ev.getX(); 
      int y = (int)ev.getY(); 

      SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)findViewById(R.id.imageView); 
      PointF sCoord = imageView.viewToSourceCoord(ev.getX(), ev.getY()); 
      if(sCoord.x > 1940 && sCoord.x < 2119 && sCoord.y > 1558 && sCoord.y < 1719){Demovar.roomname="141"; }; 
      if(sCoord.x > 2120 && sCoord.x < 2322 && sCoord.y > 1558 && sCoord.y < 1719){Demovar.roomname="142";}; 
      if(sCoord.x > 2323 && sCoord.x < 2520 && sCoord.y > 1558 && sCoord.y < 1719){Demovar.roomname="143";}; 
      return true; 
      } 

Я наклоняю для жизни мне работать, где я могу добавить код, чтобы сделать его работу я продолжаю получать ошибки

Пожалуйста, простите меня, если это очевидный вопрос, я действительно новые жесты

любой помощь

Марком ОБНОВЛЕНО КОД

public void onCreate(final Bundle savedInstanceState) { 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    mImage = (ImageView) findViewById(R.id.mImageView); 

    final GestureDetector gestureDetector = new GestureDetector(this, new GestureListener()); 
    mImage.setOnTouchListener(new OnTouchListener() { 

        public boolean onTouch(View v, MotionEvent event) { 
         return gestureDetector.onTouchEvent(event); 
        } 
       }); 

этот код дает ошибку ниже Mark

+0

ли вы назначаете Читающий мысли к Teh ImageView ?? –

ответ

0

Вам необходимо зарегистрировать сенсорный прослушиватель, чтобы вызвать GestureDetector

GestureDetector gestureDetector = new GestureDetector(context, new GestureListener()); 
    imageView.setOnTouchListener(new OnTouchListener() { 

        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         return gestureDetector.onTouchEvent(event); 
        } 
       }); 

Итак, вы код будет выглядеть так:

public void onCreate(final Bundle savedInstanceState) { 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mImage = (ImageView) findViewById(R.id.mImageView); 

     final GestureDetector gestureDetector = new GestureDetector(this, 
       new GestureListener()); 
     mImage.setOnTouchListener(new OnTouchListener() { 

      public boolean onTouch(View v, MotionEvent event) { 
       return gestureDetector.onTouchEvent(event); 
      } 
     }); 
    } 

    private class GestureListener extends 
      GestureDetector.SimpleOnGestureListener { 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 

     // event when double tap occurs 
     @Override 
     public boolean onDoubleTap(MotionEvent e) { 
      float x = e.getX(); 
      float y = e.getY(); 

      Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")"); 

      return true; 
     } 
    } 

Reference Link

+0

Спасибо за это. Любая идея, в которую я вставляю ее в основную деятельность. Когда я ставлю ее, я получаю ошибки – user3422687

+0

в 'OnCreate'' MainActivity' –

+0

, какие ошибки ... Показать ошибки –

Смежные вопросы