Skip to content

Instantly share code, notes, and snippets.

@seLc7
Last active August 29, 2015 14:19
Show Gist options
  • Save seLc7/45d040b365b0d04c8d45 to your computer and use it in GitHub Desktop.
Save seLc7/45d040b365b0d04c8d45 to your computer and use it in GitHub Desktop.
获取屏幕触点坐标并在屏幕截图上标记。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cheng.touch" >
<!-- SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 向SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" />
</application>
</manifest>
package cheng.touch;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView labelView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
labelView = (TextView)findViewById(R.id.event_label);
TextView touchView = (TextView)findViewById(R.id.touch_area);
final TextView historyView = (TextView)findViewById(R.id.history_label);
touchView.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case (MotionEvent.ACTION_DOWN):
display("ACTION_DOWN", event);
break;
case (MotionEvent.ACTION_UP):
int historySize = processHistory(event);
historyView.setText("历史数据量:" + historySize);
display("ACTION_UP", event);
break;
case (MotionEvent.ACTION_MOVE):
display("ACTION_MOVE", event);
break;
}
return true;
}
});
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
});
}
private void display(String eventType, MotionEvent event){
int x = (int)event.getX();
int y = (int)event.getY();
float pressure = event.getPressure();
float size = event.getSize();
int RawX = (int)event.getRawX();
int RawY = (int)event.getRawY();
String msg = "";
msg += "事件类型:" + eventType + "\n";
msg += "相对坐标:"+String.valueOf(x)+","+String.valueOf(y)+"\n";
msg += "绝对坐标:"+String.valueOf(RawX)+","+String.valueOf(RawY)+"\n";
msg += "触点压力:"+String.valueOf(pressure)+", ";
msg += "触点尺寸:"+String.valueOf(size)+"\n";
labelView.setText(msg);
}
private int processHistory(MotionEvent event)
{
int historySize = event.getHistorySize();
for (int i = 0; i < historySize; i++) {
long time = event.getHistoricalEventTime(i);
float pressure = event.getHistoricalPressure(i);
float x = event.getHistoricalX(i);
float y = event.getHistoricalY(i);
float size = event.getHistoricalSize(i);
// 处理过程…
}
return historySize;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package cheng.touch;
import android.app.Service;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.IBinder;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* Created by Cheng on 2015/4/13.
*/
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i("out", "Service onCreate");
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/20150402_093240.png").copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setColor(Color.RED);// 设置红色
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(820, 720, 25, paint);
canvas.save(Canvas.ALL_SAVE_FLAG );
canvas.restore();
File f = new File("/sdcard/0.png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
Log.i("out", "done");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment