Android:随笔——对页面的View进行截图

简介: 转载请标明地址 QuincySx:[http://www.jianshu.com/p/71309b2bd0e7]我们在做项目时,往往有一个这样的需求:就是对视图的一部分进行截图然后分享出去这个功能很简单还是简单的看代码吧 ...

转载请标明地址 QuincySx:[http://www.jianshu.com/p/71309b2bd0e7]


我们在做项目时,往往有一个这样的需求:就是对视图的一部分进行截图然后分享出去
这个功能很简单还是简单的看代码吧

<android.support.constraint.ConstraintLayout
        android:id="@+id/layout_test"
        android:layout_width="90dp"
        android:layout_height="90dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.3">

        <ImageView
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:src="@drawable/icon_image"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="测试截图"
            android:textColor="#2b24c3"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0"/>
    </android.support.constraint.ConstraintLayout>

    <Button
        android:id="@+id/btn_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="点击测试"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/layout_test"/>

    <ImageView
        android:id="@+id/img_show"
        android:layout_width="90dp"
        android:layout_height="90dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_test"
        tools:src="@drawable/icon_image"/>

对 Activity 里面获取控件的代码已省略,直接展示业务代码

public void onClick(View view) {
        //控件可以进行缓存  
        mConstraintLayout.setDrawingCacheEnabled(true);
        //获取缓存的 Bitmap  
        Bitmap drawingCache = mConstraintLayout.getDrawingCache();
        //对获取的 Bitmap  进行复制
        drawingCache = drawingCache.createBitmap(drawingCache);
        //关闭视图的缓存
        mConstraintLayout.setDrawingCacheEnabled(false);

        if (drawingCache != null) {
            mImageView.setImageBitmap(drawingCache);
            Toast.makeText(this, "获取失败", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "获取成功", Toast.LENGTH_SHORT).show();
        }
    }

总结

看到这个功能感觉无从下手,其实也挺简单的,如果有需求不妨收藏一下,分享给有需求的朋友

目录
相关文章
|
23天前
|
缓存 测试技术 Android开发
深入探究Android中的自定义View绘制优化策略
【4月更文挑战第8天】 在Android开发实践中,自定义View的绘制性能至关重要,尤其是当涉及到复杂图形和动画时。本文将探讨几种提高自定义View绘制效率的策略,包括合理使用硬件加速、减少不必要的绘制区域以及利用缓存机制等。这些方法不仅能改善用户体验,还能提升应用的整体性能表现。通过实例分析和性能测试结果,我们将展示如何有效地实现这些优化措施,并为开发者提供实用的技术指南。
|
28天前
|
前端开发 Android开发 开发者
深入探究Android中的自定义View组件开发
【4月更文挑战第3天】 在现代Android应用程序的开发过程中,创建具有独特功能和高度定制化的用户界面是一个常见需求。为此,理解并掌握自定义View组件的开发成为了开发者必备的技能之一。本文将深入探讨如何在Android中创建自定义View,从基础的绘制原理到事件处理机制,再到性能优化技巧,旨在为读者提供一个全面的技术视角,并通过实例代码演示如何实现一个功能丰富、响应迅速的自定义View组件。
|
29天前
|
Android开发
Android实现页面渐变效果
Android实现页面渐变效果
19 1
|
4月前
|
Android开发 容器
Android UI设计: 什么是View和ViewGroup?
Android UI设计: 什么是View和ViewGroup?
36 0
|
19天前
|
XML 数据可视化 Android开发
深入探究Android中的自定义View组件开发
【4月更文挑战第12天】在安卓应用开发中,创建具有独特交互和视觉表现的自定义View组件是增强用户体验的重要手段。本文将详细阐述如何从头开始构建一个Android自定义View,包括理解View的工作原理、处理绘制流程、事件分发机制以及属性的自定义与管理。通过具体案例分析,我们将一步步实现一个可定制的动态进度条,不仅具备基础功能,还能根据业务需求进行扩展,体现高度的产品个性化。
|
23天前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
|
29天前
|
前端开发 Android开发 容器
Android View介绍
Android View介绍
20 0
|
29天前
|
XML Android开发 数据格式
Android注册登录页面2
Android注册登录页面
29 2
|
29天前
|
Java Android开发 数据安全/隐私保护
Android注册登录页面1
Android注册登录页面
10 1
|
5月前
|
XML API Android开发
Android 自定义View 之 Dialog弹窗
Android 自定义View 之 Dialog弹窗