将view转换成bitmap保存

 /**
                 * setDrawingCacheEnabled
                 * 保留我们的绘制副本
                 * 1.重新测量
                 * 2.重新布局
                 * 3.得到我们的DrawingCache
                 * 4.转换成Bitmap
                 */
                 // ll_content  要保存的view控件
                ll_content.setDrawingCacheEnabled(true);

                ll_content.measure(
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

                ll_content.layout(0, 0, ll_content.getMeasuredWidth(),
                        ll_content.getMeasuredHeight());

                Bitmap mBitmap = ll_content.getDrawingCache();

                if (mBitmap != null) {
                    FileHelper.getInstance().saveBitmapToAlbum(this, mBitmap);
                    mLodingView.hide();
                }


  public boolean saveBitmapToAlbum(Context mContext, Bitmap mBitmap) {
        //根布局
        File rootPath = new File(Environment.getExternalStorageDirectory() + "/Meet/");

        if (!rootPath.exists()) {
            rootPath.mkdirs();
        }

        File file = new File(rootPath, System.currentTimeMillis() + ".png");
        try {
            FileOutputStream out = new FileOutputStream(file);
            //自带的保存方法
            mBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            Toast.makeText(mContext, "保存成功", Toast.LENGTH_SHORT).show();
            updateAlnum(mContext, file.getPath());
            return true;
        } catch (IOException e) {
            LogUtils.i("e:" + e.toString());
            Toast.makeText(mContext, "保存失败", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
将Android View转换成视频的过程需要以下步骤: 1. 将View绘制到Bitmap上 2. 将Bitmap保存为图片文件 3. 将图片文件转换为视频文件 以下是实现的代码示例: ``` // 将View绘制到BitmapBitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); // 将Bitmap保存为图片文件 FileOutputStream fileOutputStream = new FileOutputStream("/sdcard/image.jpg"); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); // 将图片文件转换为视频文件 MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setDataSource("/sdcard/image.jpg"); int width = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)); int height = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)); int duration = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)); File videoFile = new File("/sdcard/video.mp4"); MediaCodec codec = MediaCodec.createEncoderByType("video/avc"); MediaFormat format = MediaFormat.createVideoFormat("video/avc", width, height); format.setInteger(MediaFormat.KEY_BIT_RATE, 125000); format.setInteger(MediaFormat.KEY_FRAME_RATE, 30); format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface); format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5); codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); Surface surface = codec.createInputSurface(); codec.start(); MediaMuxer muxer = new MediaMuxer(videoFile.getAbsolutePath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4); int trackIndex = -1; boolean sawEOS = false; MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo(); ByteBuffer[] codecOutputBuffers = codec.getOutputBuffers(); while (!sawEOS) { int inputBufferIndex = codec.dequeueInputBuffer(-1); if (inputBufferIndex >= 0) { long presentationTimeUs = 0; Bitmap frame = BitmapFactory.decodeFile("/sdcard/image.jpg"); Canvas surfaceCanvas = surface.lockCanvas(null); surfaceCanvas.drawBitmap(frame, 0, 0, null); surface.unlockCanvasAndPost(surfaceCanvas); ByteBuffer inputBuffer = codec.getInputBuffer(inputBufferIndex); inputBuffer.clear(); inputBuffer.put(frame); codec.queueInputBuffer(inputBufferIndex, 0, inputBuffer.capacity(), presentationTimeUs, 0); presentationTimeUs += 1000000 / 30; } int outputBufferIndex = codec.dequeueOutputBuffer(bufferInfo, 0); if (outputBufferIndex >= 0) { ByteBuffer outputBuffer = codecOutputBuffers[outputBufferIndex]; if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0) { codec.releaseOutputBuffer(outputBufferIndex, false); continue; } if (bufferInfo.size != 0) { outputBuffer.position(bufferInfo.offset); outputBuffer.limit(bufferInfo.offset + bufferInfo.size); if (trackIndex == -1) { MediaFormat outFormat = codec.getOutputFormat(); trackIndex = muxer.addTrack(outFormat); muxer.start(); } muxer.writeSampleData(trackIndex, outputBuffer, bufferInfo); } codec.releaseOutputBuffer(outputBufferIndex, false); if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { sawEOS = true; } } } muxer.stop(); muxer.release(); codec.stop(); codec.release(); ``` 需要注意的是,这段代码使用了MediaCodec来对Bitmap进行编码,然后将编码后的视频数据写入到视频文件中。同时,由于Android系统不支持直接将View转换为视频,因此需要将View先绘制到Bitmap上,再进行后续的视频转换过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值