Android 生成图片,保存到本地

本文介绍了如何在Android中创建一个自定义视图类,使用XML布局生成图片,详细解释了测量、布局和绘制的过程,并展示了如何将生成的图片保存到本地。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先定义一个生成图片的工具类,根据xml文件生成图片

public class ShareView extends FrameLayout {

    private final int IMAGE_WIDTH = 720;
    private final int IMAGE_HEIGHT = 1280;

    private TextView tvInfo;

    public ShareView(@NonNull Context context) {
        super(context);
        init();
    }

    private void init() {
        View layout = View.inflate(getContext(), R.layout.share_view_layout, this);
        tvInfo = (TextView) layout.findViewById(R.id.tv_info);
    }

    /**
     * 设置相关信息
     *
     * @param info
     */
    public void setInfo(String info) {
        tvInfo.setText(info);
    }

    /**
     * 生成图片
     *
     * @return
     */
    public Bitmap createImage() {

        //由于直接new出来的view是不会走测量、布局、绘制的方法的,所以需要我们手动去调这些方法,不然生成的图片就是黑色的。

        int widthMeasureSpec = MeasureSpec.makeMeasureSpec(IMAGE_WIDTH, MeasureSpec.EXACTLY);
        int heightMeasureSpec = MeasureSpec.makeMeasureSpec(IMAGE_HEIGHT, MeasureSpec.EXACTLY);

        measure(widthMeasureSpec, heightMeasureSpec);
        layout(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
        Bitmap bitmap = Bitmap.createBitmap(IMAGE_WIDTH, IMAGE_HEIGHT, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        draw(canvas);

        return bitmap;
    }
}

定义了一个setInfo方法,可以从外部传入相关的信息,createImage方法用于生成最终的图片,由于直接new出来的view是不会走测量、布局、绘制的方法的,所以需要我们手动去调这些方法,不然生成的图片就是黑色的。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="720px"
    android:layout_height="1280px"
    android:orientation="vertical"
    android:background="#ffffff">

    <TextView
        android:id="@+id/tv_info"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="20px"
        android:layout_marginTop="20px"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:textSize="40px" />

</LinearLayout>

注意:在布局里面我们可以完全用ui给的px尺寸来布局,这样生成的图片就和效果图几乎完全一样了。

最后来看下调用:

    public void createShareImage(View view) {
        ShareView shareView = new ShareView(MainActivity.this);
        shareView.setInfo("自定义文本");
        final Bitmap image = shareView.createImage();
        final String path = saveImage(image);
        Log.e("xxx", path);

        if (image != null && !image.isRecycled()) {
            image.recycle();
        }
    }

    /**
     * 保存bitmap到本地
     *
     * @param bitmap
     * @return
     */
    private String saveImage(Bitmap bitmap) {

        File path = getCacheDir();

        String fileName = "shareImage.png";

        File file = new File(path, fileName);

        if (file.exists()) {
            file.delete();
        }

        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return file.getAbsolutePath();
    }

最后运行完会在本地保存一个png图片

可以在日志中看到打印出来最终的图片地址(不同手机可能会不一样):/data/data/com.vice.viewshotproject/cache/shareImage.png,用adb命令把它拷贝到电脑。

//root权限
adb root
//拷贝到桌面
adb pull /data/data/com.vice.viewshotproject/cache/shareImage.png D:/Desktop/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值