Java 修改版面边距,然后从此版面生成图像
我有个奇怪的问题 我有一个活动,在这个活动中我有一个布局,我想把它做成一个形象,以便在社交网络上分享。 此布局包含不同的动态图像和文本。这就是为什么我不能把它作为一个静态图像存储并按需共享。我需要在用户点击共享按钮时生成图像 问题是我需要在分享之前调整布局,我该怎么做Java 修改版面边距,然后从此版面生成图像,java,android,android-relativelayout,Java,Android,Android Relativelayout,我有个奇怪的问题 我有一个活动,在这个活动中我有一个布局,我想把它做成一个形象,以便在社交网络上分享。 此布局包含不同的动态图像和文本。这就是为什么我不能把它作为一个静态图像存储并按需共享。我需要在用户点击共享按钮时生成图像 问题是我需要在分享之前调整布局,我该怎么做 ImageView profilPic = (ImageView)dashboardView.findViewById(R.id.profilePic); RelativeLayout.LayoutParams
ImageView profilPic = (ImageView)dashboardView.findViewById(R.id.profilePic);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)profilPic.getLayoutParams();
params.setMargins(10, 62, 0, 0);
profilPic.invalidate();
profilPic.requestLayout();
首先我修改版面的边距,然后制作一个图像
Bitmap bitmap = null;
try {
bitmap = Bitmap.createBitmap(dashboardView.getWidth(),
dashboardView.getHeight(), Bitmap.Config.ARGB_4444);
dashboardView.draw(new Canvas(bitmap));
} catch (Exception e) {
// Logger.e(e.toString());
}
FileOutputStream fileOutputStream = null;
File path = Environment
.getExternalStorageDirectory();
File file = new File(path, "wayzupDashboard" + ".png");
try {
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bitmap.compress(CompressFormat.PNG, 100, bos);
try {
bos.flush();
bos.close();
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
最后我分享它
基本上,它可以工作,除了它在使用修改的layoutParams重新绘制布局之前捕获布局。
我需要捕获这个布局一次,并且只有在重新绘制布局后,考虑到新的布局参数时才捕获
如果我删除了捕获代码,它会工作,当我触摸共享按钮时,我会看到布局在移动。但是当我有捕获代码时,它只是在修改页边距之前捕获布局
如何确保在捕获布局之前重新绘制布局 对于记录,要仅在设置视图后正确捕获布局,我需要首先设置视图,然后在延迟线程中捕获布局。这是我找到的唯一让它工作的方法
public void onShareButton() {
dashboardController.setupViewBeforeSharing();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Bitmap bitmap = null;
try {
bitmap = Bitmap.createBitmap(dashboardView.getWidth(),
dashboardView.getHeight(), Bitmap.Config.ARGB_4444);
dashboardView.draw(new Canvas(bitmap));
} catch (Exception e) {
// Logger.e(e.toString());
}
FileOutputStream fileOutputStream = null;
File path = Environment
.getExternalStorageDirectory();
File file = new File(path, "wayzupDashboard" + ".png");
try {
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bitmap.compress(CompressFormat.PNG, 100, bos);
try {
bos.flush();
bos.close();
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_TEXT, R.string.addPassengerButton);
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + file.getAbsolutePath()));
startActivity(Intent.createChooser(share, "Share image"));
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
dashboardController.setupViewAfterSharing();
}
});
}
}, 300);
}
更简单的方法是在
仪表板视图
视图上设置布局过程的侦听器使用View.addOnLayoutChangeListener()设置侦听器。
在更改布局参数之前设置侦听器,并在图像保存到持久化之后将其删除
希望它能带来任何改进