我在点击按钮时生成图像。用于创建图像的布局当前在活动中不可见。矢量图像是布局的背景,但我看到的只是黑色图像。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="370dp"
android:layout_height="400dp"
android:background="@drawable/ic_background">
<TextView
android:id="@+id/address"
android:layout_width="match_parent"
android:textColor="@color/white"
android:textSize="40sp"
android:textStyle="bold"
android:gravity="center"
android:layout_height="wrap_content"/>
</LinearLayout>
生成图像
private void generateAndShareImage(){
final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.image_address, null);
final TextView addressView = (TextView) view.findViewById(R.id.address);
addressView.setText("Test Address");
final Bitmap cardImage = getBitmapFromView(view);
//store image in external storage
String savedImagePath = storeImage(cardImage);
if(savedFilePath != null){
scanGallery(savedImagePath);
}
}
private void scanGallery(final String path) {
try {
MediaScannerConnection.scanFile(getBaseContext(), new String[] { path },null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
} catch (Exception e) {
Log.e("Error scanning gallery",e.getMessage());
}
}
private Bitmap getBitmapFromView(@NonNull final View view){
final Bitmap bitmap = Bitmap.createBitmap(370, 400, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
有什么问题?
答案 0 :(得分:0)
您首先需要measure
和layout
您的观点:
int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
view.measure(spec, spec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
final Bitmap cardImage = getBitmapFromView(view);
以上代码应该有效。
最好传递getBitmapFromView(view, width, height)
中用于Bitmap.createBitmap()
的测量视图的维度,而不是使用硬编码值。
检查一下其他调整Converting a view to Bitmap without displaying it in Android?
顺便说一句,你不能使用370
,如果你真的需要使用硬编码的数字,那么请使用它:
float dp_370 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 370, context.getResources().getDisplayMetrics());
要了解密度独立像素在Android中的工作原理,请阅读: What is the difference between "px", "dp", "dip" and "sp" on Android?
和
https://material.io/guidelines/layout/units-measurements.html#units-measurements-scaleable-pixels-sp
修改强>
以下是我用来查看ImageView
中生成的位图的工作代码示例:
final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View test = inflater.inflate(R.layout.test, null);
int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
test.measure(spec, spec);
int measuredWidth = test.getMeasuredWidth();
int measuredHeight = test.getMeasuredHeight();
test.layout(0, 0, measuredWidth, measuredHeight);
final Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
test.draw(canvas);
//now show this inside an ImageView
ImageView image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(bitmap);