类似图上的右下角日期,就是我们用程序动态画上去的,这个时间显示什么时间当然要根据我们自己的业务需求来定。
以我们的举列,我们的客户要求这个时间显示的是照片拍摄的日期。
这个分为两种情况
1.添加照片时候 用相机拍摄的图片,这个当然直接取当时的时间就可以了,具体代码如下://拍照
private void fromCarema(OnFilishedListener listener) {
if (hasSdcard()) {
String path = null;
if ( tempFile ==null || !tempFile.exists()) {
listener.onFilish(null);
return;
}
path = tempFile.getAbsolutePath();
if (TextUtils.isEmpty(path)) {
listener.onFilish(null);
return;
}
try {
Bitmap bitmap = BitmapUtils.scaleBitmap(path);
//+++++++++++++++++++画日期操作
bitmap = BitmapUtils.drawDate2Bitmap(bitmap);
File file = new File(path);
if (file.exists()) {
file.delete();
}
path = BitmapUtils.saveBitmap(bitmap);
listener.onFilish(path);
} catch (Exception e) {
e.printStackTrace();
Log.e("保存图片", "图片保存失败");
listener.onFilish(null);
}
} else {
if (fragment != null) {
Toast.makeText(fragment.getActivity(), "未找到存储卡,无法存储照片!", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(activity, "未找到存储卡,无法存储照片!", Toast.LENGTH_SHORT).show();
}
listener.onFilish(null);
}
}
public static Bitmap drawDate2Bitmap(Bitmap bitmap) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
String date = sdf.format(new Date());
Bitmap.Config bitmapConfig = bitmap.getConfig();
// set default bitmap config if none
if (bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig, true); // 获取可改变的位图
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.RED);
// text size in pixels
paint.setTextSize(30);
// text shadow
// paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
Rect bounds = new Rect();
paint.getTextBounds(date, 0, date.length(), bounds);
int x = (bitmap.getWidth() - bounds.width());
canvas.drawText(date, x - 10, bitmap.getHeight() - 10, paint);
canvas.save();
return bitmap;
} 2.如果是从图库选择的照片,我们需要先获取照片拍摄日期,然后再将日期画上去,代码如下://从图库选择
private void fromGallery(Intent data, OnFilishedListener listener) {
Uri uri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA , MediaStore.Images.Media.DATE_TAKEN};
Cursor cursor = null;
if (uri == null)
return;
if (uri.getScheme().contains("file")) {
Long fileTime = (new File(uri.getPath())).lastModified();
String dateTime = TimeUtil.longToDate1(fileTime);
Log.i("wtt","照片拍摄日期为dateTime: " + dateTime);
saveSelectPic( dateTime , uri.getPath(), listener);
} else if (uri.getScheme().contains("content")) {
if (fragment != null) {
cursor = fragment.getActivity().getContentResolver()
.query(uri, filePathColumn, null, null, null);
} else {
cursor = activity.getContentResolver().
query(uri,filePathColumn, null, null, null);
}
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
int dateIndex = cursor.getColumnIndexOrThrow(filePathColumn[1]);
String date = cursor.getString(dateIndex);
if (TextUtils.isEmpty(date)) {
date = TimeUtil.getStringDate1();
}else{
date = TimeUtil.longToDate1(Long.parseLong(date));
}
cursor.close();
saveSelectPic(date , picturePath, listener);
} else {
listener.onFilish(null);
}
}
}
/**
* 保存圖片
*
* @param picPath
* @param listener
*/
private void saveSelectPic(String date, String picPath, OnFilishedListener listener) {
if (TextUtils.isEmpty(picPath)) {
listener.onFilish(null);
return;
}
Bitmap bitmap = BitmapUtils.scaleBitmap(picPath);
bitmap = BitmapUtils.drawDate2Bitmap( date , bitmap);
try {
picPath = BitmapUtils.saveBitmap(bitmap);
listener.onFilish(picPath);
} catch (Exception e) {
e.printStackTrace();
Log.e("保存图片", "图片保存失败");
listener.onFilish(null);
}
}
public static Bitmap drawDate2Bitmap(String date , Bitmap bitmap) {
Bitmap.Config bitmapConfig = bitmap.getConfig();
// set default bitmap config if none
if (bitmapConfig == null) {
bitmapConfig = Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig, true); // 获取可改变的位图
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.RED);
// text size in pixels
paint.setTextSize(30);
// text shadow
// paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
Rect bounds = new Rect();
paint.getTextBounds(date, 0, date.length(), bounds);
int x = (bitmap.getWidth() - bounds.width());
canvas.drawText(date, x - 10, bitmap.getHeight() - 10, paint);
canvas.save();
return bitmap;
} 基本实现方式都在代码里面了,大家如果还有其他问题欢迎加入我的qq群讨论交流:开发一群:454430053 开发二群:537532956
|