Android App开发图像加工中卡片视图CardView和给图像添加装饰的讲解以及实战(附源码 简单易懂)

news2025/2/23 6:17:09

需要图片集和源码请点赞关注收藏后评论区留言~~~

一、卡片视图

随着手机越来越先进,开发者已经不满足简单地显示一张张图片,而要设计更多的花样,比如Android提供了一个卡片视图CardView,顾名思义它拥有卡片式的圆角边框,边框外缘有一圈阴影,边框内缘有一圈空白 使用卡片视图前要先修改build.gradle 引入以下依赖

implementation 'androidx.cardview:cardview:1.0.0'

常用方法如下

1:cardBackgroundColor 设置卡片边框的背景颜色

2:cardCornerRadius  设置卡片边框的圆角半径

3:cardElevation  设置卡片边缘的阴影高程

4:contentPadding  设置卡片边框的间隔

效果如下

可以调整圆角与阴影大小,改变视图效果。下拉框中可以选择

 

 

 

代码如下

Java类

package com.example.picture;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.example.picture.util.Utils;

public class CardViewActivity extends AppCompatActivity {
    private CardView cv_card; // 声明一个卡片视图对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_view);
        cv_card = findViewById(R.id.cv_card);
        initCardSpinner(); // 初始化卡片类型下拉框
    }

    // 初始化卡片类型下拉框
    private void initCardSpinner() {
        ArrayAdapter<String> cardAdapter = new ArrayAdapter<>(this,
                R.layout.item_select, cardArray);
        Spinner sp_card = findViewById(R.id.sp_card);
        sp_card.setPrompt("请选择卡片类型");
        sp_card.setAdapter(cardAdapter);
        sp_card.setOnItemSelectedListener(new CardSelectedListener());
        sp_card.setSelection(0);
    }

    private String[] cardArray = {"圆角与阴影均为3", "圆角与阴影均为6", "圆角与阴影均为10",
            "圆角与阴影均为15", "圆角与阴影均为20"};
    private int[] radiusArray = {3, 6, 10, 15, 20};
    class CardSelectedListener implements AdapterView.OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            int radius = Utils.dip2px(CardViewActivity.this, radiusArray[arg2]);
            cv_card.setRadius(radius); // 设置卡片视图的圆角半径
            cv_card.setCardElevation(radius); // 设置卡片视图的阴影长度
            MarginLayoutParams params = (MarginLayoutParams) cv_card.getLayoutParams();
            // 设置布局参数的四周空白
            params.setMargins(radius, radius, radius, radius);
            cv_card.setLayoutParams(params); // 设置卡片视图的布局参数
        }

        public void onNothingSelected(AdapterView<?> arg0) {}
    }

}

XML文件 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:paddingLeft="5dp"
            android:text="卡片视图样式:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <Spinner
            android:id="@+id/sp_card"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:spinnerMode="dialog" />
    </LinearLayout>

    <androidx.cardview.widget.CardView
        android:id="@+id/cv_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top"
        app:cardCornerRadius="3dp"
        app:cardElevation="3dp"
        app:contentPadding="20dp">

        <ImageView
            android:id="@+id/iv_scene"
            android:layout_width="wrap_content"
            android:layout_height="250dp"
            android:src="@drawable/ylxs" />
    </androidx.cardview.widget.CardView>

</LinearLayout>

 二、给图像添加装饰

虽然原样图片能够满足多数场合,但是有时需要给图片添加一些小装饰,比如添加图片边框,添加文字水印,添加图标水印等等,一般是利用画布工具Canvas来绘制图案,具体细节结合画笔工具Paint即可 Canvas常用方法如下

drawarc 绘制扇形或者弧形

drawBitmap 绘制位图

drawCircle 绘制圆形

drawLine 绘制直线

drawOval 绘制椭圆

drawPath 绘制路径

drawPoint 绘制点

drawRect 绘制矩形

drawRoundRect 绘制圆角矩形

drawText 绘制文字

效果如下

无装饰风格如下

添加了时间戳装饰如下

 

 

添加边框效果如下

 

 代码如下

Java类

package com.example.picture;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.example.picture.util.DateUtil;
import com.example.picture.widget.DecorateImageView;

public class ImageDecorateActivity extends AppCompatActivity {
    private DecorateImageView div_scene; // 声明一个装饰视图对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_decorate);
        div_scene = findViewById(R.id.div_scene);
        initDecorateSpinner(); // 初始化装饰类型下拉框
    }

    // 初始化装饰类型下拉框
    private void initDecorateSpinner() {
        ArrayAdapter<String> decorateAdapter = new ArrayAdapter<>(this,
                R.layout.item_select, decorateNameArray);
        Spinner sp_decorate = findViewById(R.id.sp_decorate);
        sp_decorate.setPrompt("请选择装饰类型");
        sp_decorate.setAdapter(decorateAdapter);
        sp_decorate.setOnItemSelectedListener(new DecorateSelectedListener());
        sp_decorate.setSelection(0);
    }

    private String[] decorateNameArray = {"无装饰", "时间戳装饰", "标志图装饰", "相框装饰"};
    class DecorateSelectedListener implements AdapterView.OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            if (arg2 == 0) { // 无装饰
                div_scene.showNone(); // 不显示任何装饰
            } else if (arg2 == 1) { // 时间戳装饰
                String text = DateUtil.getNowFullDateTime();
                div_scene.showText(text, true); // 显示装饰文本
            } else if (arg2 == 2) { // 标志图装饰
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.flower_lotus);
                div_scene.showLogo(bitmap, true); // 显示装饰标志
            } else if (arg2 == 3) { // 相框装饰
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.photo_frame1);
                div_scene.showFrame(bitmap, true); // 显示装饰相框
            }
        }

        public void onNothingSelected(AdapterView<?> arg0) {}
    }

}

XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="5dp"
            android:gravity="center"
            android:text="图像装饰类型:"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <Spinner
            android:id="@+id/sp_decorate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:spinnerMode="dialog" />
    </LinearLayout>

    <com.example.picture.widget.DecorateImageView
        android:id="@+id/div_scene"
        android:layout_width="match_parent"
        android:layout_height="270dp"
        android:src="@drawable/ylxs" />
</LinearLayout>

创作不易 觉得有帮助请点赞关注收藏~~~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/10502.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

[附源码]java毕业设计健身房管理系统论文2022

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

深度剖析 Vue3 如何通过虚拟DOM更新页面

上一讲我们主要介绍了 Vue 项目的首次渲染流程&#xff0c;在 mountComponent 中注册了effect 函数&#xff0c;这样&#xff0c;在组件数据有更新的时候&#xff0c;就会通知到组件的 update 方法进行更新 Vue 中组件更新的方式也是使用了响应式 虚拟 DOM 的方式&#xff0c…

git 命令行其实真的很好用

使用命令行操作git&#xff0c;我觉得是最简单、最直接的方式&#xff0c;最开始使用git的时候特别喜欢这种方式。后来&#xff0c;就不再使用命令行&#xff0c;而是选择了其他可视化的工具&#xff0c;如idea自带的插件、sourceTree、TortoiseGit、GitKraken。发生的转变的原…

AI绘画提示词创作指南:DALL·E 2、Midjourney和 Stable Diffusion最全大比拼

&#x1f4a1; 作者&#xff1a;韩信子ShowMeAI &#x1f4d8; 深度学习实战系列&#xff1a;https://www.showmeai.tech/tutorials/42 &#x1f4d8; 自然语言处理实战系列&#xff1a;https://www.showmeai.tech/tutorials/45 &#x1f4d8; 计算机视觉实战系列&#xff1a;h…

Jenkins 10 问 10 答,你想知道都在这

大家好啊&#xff0c;我是大田。 今天汇总一下近几周关于 Jenkins 问题。 1、如何安装 Jenkins&#xff1f; 答&#xff1a;一步一步教你安装部署 Jenkins&#xff0c;不信你安不上 2、忘记登录密码&#xff1f; 答&#xff1a;Jenkins 忘记登录密码解决办法 3、jenkins中缺少…

基于 IDEA 搭建 RocketMQ-4.6 源码环境

RocketMQ 架构 源码搭建前&#xff0c; 需要理解 RocketMQ 的四个重要组件&#xff0c; 以及 RocketMQ 的工作流程&#xff1a; NameServer是一个几乎无状态节点&#xff0c;可集群部署&#xff0c;节点之间无任何信息同步。 Broker部署相对复杂&#xff0c;Broker分为Master…

Java基础之《undertow容器》

一、什么是undertow 1、undertow是springboot默认支持的三种servlet容器之一。 tomcat、jetty、undertow 2、undertow怎么读 under-tow 3、undertow是RedHat&#xff08;红帽公司&#xff09;的开源产品&#xff0c;采用java开发&#xff0c;是一款灵活、高性能的web服务器&…

大学生游戏静态HTML网页设计-(北京冬奥会12页 带js 带视频 轮播图)

⛵ 源码获取 文末联系 ✈ Web前端开发技术 描述 网页设计题材&#xff0c;DIVCSS 布局制作,HTMLCSS网页设计期末课程大作业 | HTML期末大学生网页设计作业&#xff0c;Web大学生网页 HTML&#xff1a;结构 CSS&#xff1a;样式 在操作方面上运用了html5和css3&#xff0c; 采用…

stack容器、queue容器(20221116)

一、stack容器 1、基本概念 先进后出的数据结构&#xff0c;只有一个出口&#xff08;栈顶&#xff09;。 栈不允许有遍历行为&#xff0c;可以判断是否为空(empty)&#xff0c;也可以知道其元素个数&#xff08;size&#xff09; 2、常用接口 构造函数&#xff1a; stac…

初始MySQL

目录 一、什么是数据库 二、SQL分类 三、库的操作 四、表的操作 五、数据类型 六、表的约束 什么是数据库 存储数据用文件就可以了&#xff0c;为什么还要有数据库&#xff1f; 文件保存数据有以下几个缺点&#xff1a; 文件的安全性问题文件不利于数据查询和管理 文件…

电脑视频怎么录制?好用的电脑录屏方法

在日常使用电脑的时候&#xff0c;很多小伙伴经常会遇到需要录制电脑视频的时候。但网上各种眼花缭乱的电脑录屏方法&#xff0c;很多小伙伴看了表示自己根本没有学会。今天就给大家分享2个简单好用的电脑录屏方法&#xff0c;看完后轻松掌握电脑录屏。 一&#xff0e;使用Wind…

主成分分析法在图像压缩和重建中的应用研究-含Matlab代码

目录一、引言二、主成分分析法概念及性质2.1 概念2.2 性质三、计算步骤3.1 计算相关系数矩阵3.2 计算特征值与特征向量3.3 计算主成分贡献率及累计贡献率3.4 计算主成分载荷3.5 各主成分的得分四、图像压缩与重建实验分析五、参考文献六、Matlab代码获取一、引言 主成分分析法…

【附源码】Python计算机毕业设计网上购物平台

项目运行 环境配置&#xff1a; Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术&#xff1a; django python Vue 等等组成&#xff0c;B/S模式 pychram管理等等。 环境需要 1.运行环境&#xff1a;最好是python3.7.7&#xff0c;…

最火后台管理系统 RuoYi 项目探秘,之二

上篇中&#xff0c;我们初步观察了 RuoYi 的项目结构&#xff0c;并在最后实际运行起了项目。我们也发现了作者不好的代码习惯&#xff0c;作为反例&#xff0c;我们应该要养成良好的编码习惯。本篇开始&#xff0c;我们会按照 Web 界面逐一对具体子项目的实现的功能进行探秘。…

Qt使用7z压缩和解压示例(支持文件夹递归、多文件不同位置)

1&#xff0c;简介 Qt自带的压缩处理类功能不太完善&#xff0c;也不支持中文路径。 这是我封装好的一个Qt调用7z处理压缩解压的工具类 ZipAPI&#xff0c;提供了几个简单易用的接口。 写压缩解压代码从此非常方便快捷&#xff01; 支持中文路径&#xff0c;支持常规的压缩解…

Cell:水平基因转移在昆虫中广泛存在,增强鳞翅目雄性昆虫求偶行为

期刊&#xff1a;Cell 影响因子&#xff1a;66.85 发表时间&#xff1a;2022年8月 一、研究背景 昆虫起源于约4.8亿年前&#xff0c;是地球上最繁盛的动物类群&#xff0c;已被描述种超过100万&#xff0c;占所有动物物种50%以上。这个古老的动物类群在…

插画、插图网站,免费(商用)

本期分享5个高质量插画网站&#xff0c;免费可商用&#xff0c;设计必备&#xff0c;建议收藏&#xff01;1、Undraw https://undraw.co/illustrationsUndraw是一个扁平风格插画图库&#xff0c;里面有大量的插画&#xff0c;可以支持在线更改配色&#xff0c;网站提供免费下载…

【JavaSE】类和对象(下)(访问限定符 包的概念 导入包中的类 自定义包 包的访问权限控制举例 常见的包 实例内部类 静态内部类 局部内部类 对象的打印)

文章目录六、 封装6.1 封装的概念6.2 访问限定符6.3 封装扩展之包6.3.1 包的概念6.3.2 导入包中的类6.3.3 自定义包6.3.4 包的访问权限控制举例6.3.5 常见的包七、内部类7.1 内部类7.1.1 实例内部类7.1.2 静态内部类7.2 局部内部类7.3 匿名内部类八、对象的打印六、 封装 6.1 …

人工智能-线性回归2--房价预测、欠拟合过拟合、正则化、模型保存加载

7&#xff0c;案例&#xff1a;波士顿房价预测 回归性能评估MSE from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression,SGDRegressor from sklearn.meyrics import mean_squa…

详解PHP解决swoole守护进程Redis假死 ,mysql断线重连问题

详解PHP解决swoole守护进程Redis假死 &#xff0c;mysql断线重连问题最近公司有个项目&#xff0c;要举办一个线上活动&#xff0c;我这边负责提供接口记录用户访问记录&#xff0c;与操作记录&#xff0c;由于活动参与人数可能比较多&#xff0c;为了不影响正常业务运行&#…