我正在尝试创建和共享1920x1920的图像,其中打印了应用程序的前半截屏幕截图和图像的简单阴影。在下半部分,将打印应用程序图标和一些多行文字。这是应用程序中的共享图像。
我已经降低了压缩时的图像质量,但是第一次运行此代码仍需要花费大量时间(约1秒)。任何形式的帮助将不胜感激,并提出一些更好的方法。
private void shareScreenShot() {
String text="Download Sarkari Bank App from play store";
String link="https://play.google.com/store/apps/details?id=com.sarkaribank";
View rootView = findViewById(R.id.layoutView).getRootView();
rootView.setDrawingCacheEnabled(true);
Bitmap ss = rootView.getDrawingCache();
int statusBarHeight = getStatusBarHeight();
//adding ss and shadow
Bitmap bitmap = Bitmap.createBitmap(ss, 0, statusBarHeight, ss.getWidth(), ss.getHeight()-statusBarHeight-getNavBarHeight(this) );
final int canvasWidth = 1920;
final int canvasHeight = 1920;
Bitmap result = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(result);
canvas.drawColor(Color.WHITE);
Paint shadowPaint = new Paint();
shadowPaint.setColor(Color.parseColor("#00cbff"));
Paint border = new Paint();
border.setColor(Color.parseColor("#095baf"));
border.setStyle(Paint.Style.STROKE);
border.setStrokeWidth(10);
canvas.drawRect(new Rect(0, 0, canvasWidth, canvasHeight), border);
border.setStrokeWidth(5);
int nh = (int) (bitmap.getHeight() * (768.0 / bitmap.getWidth()));
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 768, nh, true);
Rect shadow = new Rect(0, 0, scaled.getWidth(), scaled.getHeight());
//small distance between shadow and SS
float sd = 96 * scaled.getWidth() / scaled.getHeight();
float translateX = (canvasWidth / 2 - scaled.getWidth()) / 2;
float translateY = (canvasHeight - scaled.getHeight()) / 2;
canvas.save();
canvas.translate(translateX + sd, translateY + sd);
canvas.drawRect(shadow, shadowPaint);
canvas.restore();
canvas.save();
canvas.translate(translateX, translateY);
canvas.drawBitmap(scaled, 0, 0, null);
canvas.drawRect(shadow, border);
canvas.restore();
//add icon
int left = canvasWidth / 2;
int right = canvasWidth;
int blockHeight = canvasHeight / 12;
Rect iconRect = new Rect(left, 3 * blockHeight, right, 5 * blockHeight);
Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logo);
int iconX = (iconRect.left + (iconRect.width() / 2)) - blockHeight;
int iconY = (iconRect.top + (iconRect.height() / 2)) - blockHeight;
iconRect.left = iconX;
iconRect.top = iconY;
iconRect.right = iconX + 2 * blockHeight;
iconRect.bottom = iconY + 2 * blockHeight;
canvas.drawBitmap(icon, null, iconRect, null);
//add text
String t1 = "SarkariBank.com",
t2 = "These details are shared by sarkaribank mobile app. You can download it from playstore.",
t3 = "Go on playstore and search sarkaribank then install it.",
t4 = "Thanks for sharing!";
TextPaint headingPaint = new TextPaint();
headingPaint.setTextSize(blockHeight / 2);
TextPaint textPaint = new TextPaint();
textPaint.setTextSize(blockHeight / 4);
headingPaint.setColor(Color.parseColor("#095baf"));
Rect t1Rect = new Rect(left, 5 * blockHeight, right, 6 * blockHeight);
Rect t2Rect = new Rect(left + (int) translateX, 6 * blockHeight, right - (int) translateX, 7 * blockHeight);
Rect t3Rect = new Rect(left + (int) translateX, 7 * blockHeight, right - (int) translateX, 8 * blockHeight);
Rect t4Rect = new Rect(left, 8 * blockHeight, right, 9 * blockHeight);
drawText(canvas, t1Rect, t1, headingPaint);
drawText(canvas, t2Rect, t2, textPaint);
drawText(canvas, t3Rect, t3, textPaint);
drawText(canvas, t4Rect, t4, headingPaint);
Uri uri = saveBitmap(result);
Log.i("start time",stopwatch.toString());
rootView.setDrawingCacheEnabled(false);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/*");
String shareBody = text + "\n" + link;
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
private int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
private void drawText(Canvas canvas, Rect rect, String textOnCanvas, TextPaint textPaint){
StaticLayout sl = new StaticLayout(textOnCanvas, textPaint, rect.width(), Layout.Alignment.ALIGN_CENTER, 1, 1, true);
canvas.save();
canvas.translate(rect.left, rect.top);
sl.draw(canvas);
canvas.restore();
}
public int getNavBarHeight(Context c) {
int result = 0;
boolean navBarExists = getResources().getBoolean(getResources().getIdentifier("config_showNavigationBar", "bool", "android"));
if(navBarExists) {
//The device has a navigation bar
Resources resources = c.getResources();
int orientation = resources.getConfiguration().orientation;
int resourceId;
if (isTablet(c)){
resourceId = resources.getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_height_landscape", "dimen", "android");
} else {
resourceId = resources.getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_width", "dimen", "android");
}
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
}
return result;
}
private boolean isTablet(Context c) {
return (c.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
public Uri saveBitmap(Bitmap bitmap) {
File imagesFolder = new File(getCacheDir(), "images");
Uri uri = null;
try {
imagesFolder.mkdirs();
File file = new File(imagesFolder, "shared_image.jpeg");
FileOutputStream stream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, stream);
stream.flush();
stream.close();
uri = FileProvider.getUriForFile(this, "com.sarkaribank.fileprovider", file);
} catch (IOException e) {
Log.d("oops", "IOException while trying to write file for sharing: " + e.getMessage());
}
return uri;
}