赞
踩
ps:需要将我的数据生成类似excel的格式的图片,就接触到了layout转换为bitmap 故此写了一个demo
直接上效果图:
代码:
要转换为bitmap的布局
view_photo.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ScrollView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!--封面图片 --> <ImageView android:layout_width="400dp" android:layout_height="400dp" android:background="@drawable/ic_launcher_background" android:layout_gravity="center_horizontal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文字 文字" android:layout_gravity="center_horizontal" /> <Button android:id="@+id/buttonaaa" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> </LinearLayout> </ScrollView> </LinearLayout>
主要是现代代码
public class FinalTest extends AppCompatActivity { private ImageView aaa; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_final_test); DisplayMetrics metric = new DisplayMetrics();//获取屏幕 getWindowManager().getDefaultDisplay().getMetrics(metric);//是获取到Activity的实际屏幕信息。 int width = metric.widthPixels; // 屏幕宽度(像素) int height = metric.heightPixels; // 屏幕高度(像素) View view = LayoutInflater.from(this).inflate(R.layout.view_photo, null, false); layoutView(view, width, height); Button buttons = view.findViewById(R.id.buttonaaa); buttons.setText("这就是二维码"); final ScrollView tv = (ScrollView) view.findViewById(R.id.textView); aaa = (ImageView) findViewById(R.id.aaa); final Runnable runnable = new Runnable() { @Override public void run() { viewSaveToImage(tv); } }; Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Handler().post(runnable); } }); } //然后View和其内部的子View都具有了实际大小,也就是完成了布局,相当与添加到了界面上。接着就可以创建位图并在上面绘制了: private void layoutView(View v, int width, int height) { // 整个View的大小 参数是左上角 和右下角的坐标 v.layout(0, 0, width, height); int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY); int measuredHeight = View.MeasureSpec.makeMeasureSpec(10000, View.MeasureSpec.AT_MOST); /** 当然,measure完后,并不会实际改变View的尺寸,需要调用View.layout方法去进行布局。 * 按示例调用layout函数后,View的大小将会变成你想要设置成的大小。 */ v.measure(measuredWidth, measuredHeight); v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); } public void viewSaveToImage(View view) { Bitmap cachebmp = loadBitmapFromView(view); aaa.setImageBitmap(cachebmp);//直接展示转化的bitmap } private Bitmap loadBitmapFromView(View v) { int w = v.getWidth(); int h = v.getHeight(); Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bmp); c.drawColor(Color.WHITE); /** 如果不设置canvas画布为白色,则生成透明 */ v.layout(0, 0, w, h); v.draw(c); return bmp; } }
activity_final_test.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" 转换" /> <ImageView android:id="@+id/aaa" android:layout_below="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。