1.更新水印
DecimalFormat DF = new DecimalFormat("#");
DecimalFormat DF1 = new DecimalFormat("#.#");LocationManager LM = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, new LocationListener() {@Overridepublic void onLocationChanged(Location location) {latitude = location.getLatitude();longitude = location.getLongitude();altitude = location.getAltitude();speed = location.getSpeed();textView_longitude.setText("经度:" + DuFenMiao(longitude));textView_latitude.setText("纬度:" + DuFenMiao(latitude));textView_altitude.setText("高度:" + DF.format(altitude) + "米");if (speed * 3.6 < 10)textView_speed.setText("速度:" + DF1.format(speed * 3.6) + "千米/时");elsetextView_speed.setText("速度:" + DF.format(speed * 3.6) + "千米/时");}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}@Overridepublic void onProviderEnabled(String provider) {}@Overridepublic void onProviderDisabled(String provider) {}
});
2.设置水印
class ClickListener implements View.OnClickListener {@Overridepublic void onClick(View v) {int id = v.getId();switch (id) {......case R.id.button_watermark:String[] items = { "经纬度", "高度", "速度", "时间" };boolean watermark_geo = sharedPreferences.getBoolean("watermark_geo", true);boolean watermark_altitude = sharedPreferences.getBoolean("watermark_altitude", true);boolean watermark_speed = sharedPreferences.getBoolean("watermark_speed", true);boolean watermark_time = sharedPreferences.getBoolean("watermark_time", true);boolean[] check = { watermark_geo, watermark_altitude, watermark_speed, watermark_time };new AlertDialog.Builder(MainActivity.this).setTitle("水印").setMultiChoiceItems(items, check, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i, boolean b) {SharedPreferences.Editor editor = sharedPreferences.edit();switch (i) {case 0:if (b) {textView_longitude.setVisibility(View.VISIBLE);textView_latitude.setVisibility(View.VISIBLE);} else {textView_longitude.setVisibility(View.GONE);textView_latitude.setVisibility(View.GONE);}editor.putBoolean("watermark_geo", b);break;case 1:if (b)textView_altitude.setVisibility(View.VISIBLE);elsetextView_altitude.setVisibility(View.GONE);editor.putBoolean("watermark_altitude", b);break;case 2:if (b)textView_speed.setVisibility(View.VISIBLE);elsetextView_speed.setVisibility(View.GONE);editor.putBoolean("watermark_speed", b);break;case 3:editor.putBoolean("watermark_time", b);break;}editor.apply();}}).show();break;......}}
}
3.绘制水印
public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {@Overridepublic void onPictureTaken(byte[]data, Camera camera) {Log.e(Thread.currentThread().getStackTrace()[2] + "TakePicture", "PictureCallback");Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);Camera.CameraInfo cameraInfo = new Camera.CameraInfo();Camera.getCameraInfo(ccn, cameraInfo); Canvas canvas = new Canvas(bitmap);canvas.drawBitmap(bitmap, 0, 0, null);//水印Paint paint = new Paint();paint.setColor(Color.WHITE);paint.setTextSize(bitmap.getWidth() / 30);if (sharedPreferences.getBoolean("watermark_geo", true)) {canvas.drawText(textView_longitude.getText().toString(), 100, bitmap.getHeight() - 4 * bitmap.getWidth() / 20, paint);canvas.drawText(textView_latitude.getText().toString(), 100, bitmap.getHeight() - 3 * bitmap.getWidth() / 20, paint);}if (sharedPreferences.getBoolean("watermark_altitude", true))canvas.drawText(textView_altitude.getText().toString(), 100, bitmap.getHeight() - 2 * bitmap.getWidth() / 20, paint);if (sharedPreferences.getBoolean("watermark_speed", true))canvas.drawText(textView_speed.getText().toString(), 100, bitmap.getHeight() - bitmap.getWidth() / 20, paint);Date date = new Date();if (sharedPreferences.getBoolean("watermark_time", true))canvas.drawText(SDF1.format(date), 100, bitmap.getHeight() - 15, paint);......}
};