andorid 将布局文件(layout)转换为图片(Bitmap)简单使用详解

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>
Android Studio中,如果你想要将一张图片作为布局的背景并让它衬于布局下方,你可以通过设置ImageView或者直接使用Drawable作为背景来实现。这里是一种常见的方法: 1. **使用Drawable资源作为背景**: - 打开你的`res/drawable`文件夹,创建一个新的XML文件(例如:background.xml),并将图片添加到其中。 ```xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <bitmap android:src="@drawable/your_image" /> <!-- replace with your image resource --> </item> <item android:bottom="0dp"> <shape android:shape="rectangle"> <solid android:color="#000000" /> <!-- 设置黑色背景,让图片居底 --> </shape> </item> </layer-list> ``` 2. **将Drawable应用到布局上**: 在你的XML布局文件中,给需要背景的View设置`android:background`属性,并引用刚才创建的Drawable资源: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" ...其他布局属性.../> ``` 3. **使用ImageView作为背景**: 如果你想保持图片可点击或者透明区域,可以使用`android:scaleType="fitXY"`来填充整个空间: ```xml <ImageView android:id="@+id/imageViewBackground" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/your_image" android:scaleType="fitXY" android:adjustViewBounds="true" /> ``` 然后将其放在Layout的底部,并覆盖其他内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安东尼肉店

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值