当前位置: 首页 > news >正文

Android ImageView以及实现截图

实现效果

截图前

在这里插入图片描述

截图后

在这里插入图片描述

代码

package cn.jj.huaweiad;import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;import androidx.appcompat.app.AppCompatActivity;import cn.jj.sdkcomtools.utils.LogUtils;public class ImageViewActivity extends AppCompatActivity {private static final String TAG = "JJWorld.ImageViewActivity";private ImageView imageViewTest;private Button fitXYBtn;private Button fitStartBtn;private Button fitCenterBtn;private Button fitEndBtn;private Button centerBtn;private Button centerCropBtn;private Button centerInsideBtn;private Button mfitXYBtn;private Button layout;private ViewGroup rootView;private ViewGroup rootView1;private LinearLayout mRootLayout;private ImageView captureImageView;private Button captureBtn;private Handler mHandler;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_image_view);rootView = (ViewGroup) findViewById(android.R.id.content);initView();imageViewTest.setImageResource(R.drawable.tk_huawei_ad_splash_slogan_portait);printAllViewsInLayout((ViewGroup) findViewById(android.R.id.content), 0);mHandler = new Handler();rootView.setDrawingCacheEnabled(true);}@SuppressLint("LongLogTag")private void printAllViewsInLayout(ViewGroup layout, int level) {int childCount = layout.getChildCount();for (int i = 0; i < childCount; i++) {View childView = layout.getChildAt(i);Log.d(TAG, "View level:" + level + ", index " + i + ": " + childView.getClass().getSimpleName());// 如果子视图是ViewGroup,则递归地打印其子视图if (childView instanceof ViewGroup) {printAllViewsInLayout((ViewGroup) childView, level + 1);}}}private void initView() {imageViewTest = (ImageView) findViewById(R.id.image_view_test);fitXYBtn = (Button) findViewById(R.id.fitXY_btn);fitXYBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {imageViewTest.setScaleType(ImageView.ScaleType.FIT_XY);}});fitStartBtn = (Button) findViewById(R.id.fitStart_btn);fitStartBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {imageViewTest.setScaleType(ImageView.ScaleType.FIT_START);}});fitCenterBtn = (Button) findViewById(R.id.fitCenter_btn);fitCenterBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {imageViewTest.setScaleType(ImageView.ScaleType.FIT_CENTER);}});fitEndBtn = (Button) findViewById(R.id.fitEnd_btn);fitEndBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {imageViewTest.setScaleType(ImageView.ScaleType.FIT_END);}});centerBtn = (Button) findViewById(R.id.center_btn);centerBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {imageViewTest.setScaleType(ImageView.ScaleType.CENTER);}});centerCropBtn = (Button) findViewById(R.id.centerCrop_btn);centerCropBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {imageViewTest.setScaleType(ImageView.ScaleType.CENTER_CROP);}});centerInsideBtn = (Button) findViewById(R.id.centerInside_btn);centerInsideBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {imageViewTest.setScaleType(ImageView.ScaleType.CENTER_INSIDE);}});layout = (Button) findViewById(R.id.layout);layout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {rootView.removeAllViews();LogUtils.logI(TAG,"---------------------------");FrameLayout newRootView = new FrameLayout(ImageViewActivity.this);setContentView(newRootView);printAllViewsInLayout((ViewGroup) findViewById(android.R.id.content), 0);}});mRootLayout = (LinearLayout) findViewById(R.id.m_root_layout);captureImageView = (ImageView) findViewById(R.id.capture_image_view);captureBtn = (Button) findViewById(R.id.capture_btn);captureBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Bitmap bitmap = rootView.getDrawingCache();captureImageView.setImageBitmap(bitmap);mHandler.postDelayed(mRunnable,200);}});}private Runnable mRunnable = new Runnable(){@Overridepublic void run() {rootView.setDrawingCacheEnabled(false);rootView.setDrawingCacheEnabled(true);}};
}

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/m_root_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageViewandroid:id="@+id/image_view_test"android:layout_width="match_parent"android:layout_height="300dp" /><ImageViewandroid:id="@+id/capture_image_view"android:layout_width="match_parent"android:layout_height="200dp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/fitXY_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="fitXY" /><Buttonandroid:id="@+id/fitStart_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="fitStart" /><Buttonandroid:id="@+id/fitCenter_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="fitCenter" /><Buttonandroid:id="@+id/fitEnd_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="fitEnd" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/center_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="center" /><Buttonandroid:id="@+id/centerCrop_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="centerCrop" /><Buttonandroid:id="@+id/centerInside_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="centerInside" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="layout" /><Buttonandroid:id="@+id/capture_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="截图" /></LinearLayout>
</LinearLayout>

日志

应用启动,点击layout日志如下:

2024-03-28 20:54:08.051  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:0, index 0: LinearLayout
2024-03-28 20:54:08.051  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:1, index 0: AppCompatImageView
2024-03-28 20:54:08.051  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:1, index 1: AppCompatImageView
2024-03-28 20:54:08.051  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:1, index 2: LinearLayout
2024-03-28 20:54:08.051  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:2, index 0: MaterialButton
2024-03-28 20:54:08.051  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:2, index 1: MaterialButton
2024-03-28 20:54:08.051  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:2, index 2: MaterialButton
2024-03-28 20:54:08.051  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:2, index 3: MaterialButton
2024-03-28 20:54:08.052  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:1, index 3: LinearLayout
2024-03-28 20:54:08.052  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:2, index 0: MaterialButton
2024-03-28 20:54:08.052  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:2, index 1: MaterialButton
2024-03-28 20:54:08.052  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:2, index 2: MaterialButton
2024-03-28 20:54:08.052  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:1, index 4: LinearLayout
2024-03-28 20:54:08.052  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:2, index 0: MaterialButton
2024-03-28 20:54:08.052  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:2, index 1: MaterialButton
2024-03-28 20:54:14.092  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       I  ---------------------------
2024-03-28 20:54:14.093  3772-3772  JJWorld.Im...ewActivity cn.jj.huaweiad                       D  View level:0, index 0: FrameLayout
http://www.lryc.cn/news/330345.html

相关文章:

  • 剑指offer--数组中重复的数字
  • 【THM】SQL Injection(SQL注入)-初级渗透测试
  • 数码论坛系统的设计与实现|Springboot+ Mysql+Java+ B/S结构(可运行源码+数据库+设计文档)
  • vue3性能提升主要通过哪几方面?
  • 跨境电商IP防关联是什么?有什么作用?
  • git仓库太大只下载单个文件或文件夹
  • OpenHarmony实战:RK3568 开发板镜像烧录指南
  • Asp.net Core 中一键注入接口
  • 怎么让ChatGPT批量写作原创文章
  • 【SqlServer】Alwayson收缩日志
  • 视觉里程计之对极几何
  • 数据可视化高级技术(Echarts)
  • 设计模式——行为型——责任链模式Chain Of Responsibility
  • 设计模式之工厂方法模式精讲
  • JS实现省市区三级联动(json假数据)
  • Fastjson配置消息转换器(时间格式问题)
  • 安卓Android 架构模式及UI布局设计
  • 基于Spring Boot的在线学习系统的设计与实现
  • C++中重载和重写的区别
  • 二叉树 - 栈 - 计数 - leetcode 331. 验证二叉树的前序序列化 | 中等难度
  • Training language models to follow instructions with human feedback
  • Netty核心原理剖析与RPC实践11-15
  • 3.5网安学习第三阶段第五周回顾(个人学习记录使用)
  • kali常用命令功能简介记录
  • 低噪声、轨至轨运算放大器芯片—— D721、D722、D724,适合用于音频领域
  • 【统计】什么事 R 方
  • Maplesoft Maple 2024(数学科学计算)mac/win
  • 实战 | YOLOv8自定义数据集训练实现手势识别 (标注+训练+预测 保姆级教程--含数据集)
  • 从零学算法2810
  • Vue——案例01(查询用户)