以下是我的主要活动
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[]{"mark.barr@sodexo.com"});
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[]{"mark.barr@sodexo.com"});
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);
}
});
此代码给出了以下错误 标记
答案 0 :(得分: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;
}
}