达标进度条

1.效果

1.

在这里插入图片描述

2.代码与使用

1.自定义组合控件

kotlin

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.example.customview3.R

/**
 * 创建日期:2023/11/19 0:01
 * @author 唐门.西门吹雪
 * 类说明:
 */
class UpToParView(private val context: Context, attrs:AttributeSet):LinearLayout(context,attrs) {
    private lateinit var mDrawableEnd: Drawable
    private lateinit var mNoDrawable: Drawable
    private lateinit var mYesDrawable: Drawable
    private var mLineYesColor: Int = 0
    private var mLineNoColor: Int = 0
    private var mAll: Int=3
    private var mAttain: Int=0
    private var mWidth: Float=0f
    private var mLineHeight: Float=0f
    private var mLastMargin: Float=0f
    private var mLlView:LinearLayout
    private var mLlIv:LinearLayout
    init {
        initListener()
        LayoutInflater.from(context).inflate(R.layout.view_up_par,this,true)
        mLlView=findViewById(R.id.ll_view)
        mLlIv=findViewById(R.id.ll_iv)
    }

    private fun initListener() {

    }

    /**
     * 设置进度条达标的属性
     *
     * @param all 进度条总达标数
     * @param attain 已达标数
     * @param width 达标正方形宽度
     * @param lineHeight 线的高度
     * @param lastMarginLeft 最好一个的左边距
     */
    fun setParams(
        all: Int,
        attain: Int,
        width: Float,
        lineHeight: Float,
        lastMarginLeft: Float,
        lineNoColor: Int,
        lineYesColor: Int,
        noDrawable: Drawable,
        yesDrawable: Drawable,
        drawableEnd: Drawable
    ) {
        this.mAll=all
        this.mAttain=attain
        if (mAll<=mAttain||mAll<1||mAttain<0){
            visibility= View.GONE
            return
        }
        this.mWidth=width
        this.mLineHeight=lineHeight
        this.mLastMargin=lastMarginLeft

        this.mLineNoColor=lineNoColor
        this.mLineYesColor=lineYesColor
        this.mYesDrawable=yesDrawable
        this.mNoDrawable=noDrawable
        this.mDrawableEnd=drawableEnd
        //根据总长度设置达标控件数
        setViewAll()
    }

    private fun setViewAll() {
        setViewLine()
        setIv()
    }

    /**
     * 设置线
     */
    private fun setViewLine() {
        for (i in 0  until mAll){
            if (i>2&&i!=mAll-1){
                continue
            }
            val tv=TextView(context)
            tv.setBackgroundColor(if (i<mAttain){mLineYesColor}else{mLineNoColor})//是否达标来设置背景色
            //如果是第一个或者最后一个,设置长度为一半
            val lp=LayoutParams(if (i==0||i==mAll-1){mWidth/2}else{mWidth}.toInt(),mLineHeight.toInt())
            lp.setMargins((if (i==0){mWidth/2}else{0}).toInt(),0,0,0)
            lp.gravity= Gravity.CENTER_VERTICAL
            tv.layoutParams=lp
            mLlView.addView(tv)
        }
    }

    /**
     * 设置达标的圆圈
     */
    @SuppressLint("UseCompatLoadingForDrawables")
    private fun setIv() {
        for (i in 0  until mAll){
            if (i>2&&i!=mAll-1){
                continue
            }
            val iv=ImageView(context)
            iv.background=if (i==mAll-1){mDrawableEnd}else(if (i<mAttain){mYesDrawable}else{mNoDrawable})
            //如果是第一个或者最后一个,设置长度为一半
            val lp=LayoutParams(mWidth.toInt(),mWidth.toInt())
            lp.setMargins((if (i==mAll-1){mLastMargin}else{0}).toInt(),0,0,0)
            lp.gravity= Gravity.CENTER_VERTICAL
            iv.layoutParams=lp
            mLlIv.addView(iv)
        }
    }


}

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/ll_view"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="@id/ll_iv"
        app:layout_constraintStart_toStartOf="@id/ll_iv"
        app:layout_constraintTop_toTopOf="@id/ll_iv"
        app:layout_constraintBottom_toBottomOf="@id/ll_iv"
        android:gravity="center_vertical">
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </LinearLayout>



</androidx.constraintlayout.widget.ConstraintLayout>

2.Activity

kotlin

class MainActivity : AppCompatActivity() {
    private lateinit var binding:ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val all=4
        val attain=2
        val width=resources.getDimension(R.dimen.my_50dp)
        val lineHeight=resources.getDimension(R.dimen.my_5dp)
        val lastMarginLeft=resources.getDimension(R.dimen.my_5dp)
        binding.utp.setParams(all,attain,width,lineHeight,lastMarginLeft,
            resources.getColor(R.color.gray),resources.getColor(R.color.black),
            resources.getDrawable(R.drawable.ic_launcher_foreground),
            resources.getDrawable(R.drawable.ic_launcher_foreground_black),
            resources.getDrawable(R.drawable.ic_launcher_foreground_end)
        )

    }
}

xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="@dimen/my_300dp"
    tools:context=".MainActivity">

    <com.example.customview3.viewgroup.UpToParView
        android:id="@+id/utp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="50dp"
        android:text="Hello World!"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

3.

4.

5.

6.

3.总结

1.

4.遇到的问题

1.

2.

3.

4.

5.

6.

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Triage沙箱监控

Triage沙箱可以免费分析恶意软件样本。最先进的恶意软件分析沙箱&#xff0c;具有您需要的所有功能。 在可定制的环境中提交大量样本&#xff0c;并对许多恶意软件系列进行检测和配置提取。立即查看公开报告并对您的恶意软件进行分类&#xff01; 官方网址&#xff1a;https:…

ARM CoreLink CCN 互连总线介绍

NIC NOC CCI CMN CNN NI cmn-700 nic-700 ni-700 MLGB这都是啥玩意? 后期博文或视频将会更新这一系列。今天先温习一下CNN的概念,这是来自2014年的文章,然后稍微整理总结一番。 以下是正文… 现代主流和优质 ARM 片上系统 (SoC) 产品使用 CoreLink 缓存一致性网络 (CCN) 504…

整理低秩的理解

秩的定义是矩阵中非零特征值的个数。比如一个NxN的矩阵&#xff0c;它的秩为r&#xff0c;r远小于N&#xff0c;我们可以说它是低秩的。 但还有另一种情况&#xff1a;这个矩阵的的秩接近N&#xff0c;但它的特征值大多数接近于0&#xff0c;只有少数几个特征值特别大&#xf…

Windows10 MYSQL Installer 安装(mysql-installer-community-5.7.19.0.msi)

分类 编程技术 1.进入官网找到自己所需的安装包&#xff1a;https://dev.mysql.com/ &#xff0c;路径&#xff1a;DOWNLOAD-->MYSQL Community Edition(GRL)-->MYSQL on Windows (Installer & Tool) 或直接点击 MySQL :: Download MySQL Installer 查看最新版本。…

安卓中轻量级数据存储方案分析探讨

轻量级数据存储功能通常用于保存应用的一些常用配置信息&#xff0c;并不适合需要存储大量数据和频繁改变数据的场景。应用的数据保存在文件中&#xff0c;这些文件可以持久化地存储在设备上。需要注意的是&#xff0c;应用访问的实例包含文件所有数据&#xff0c;这些数据会一…

【开源】基于Vue.js的在线课程教学系统的设计和实现

项目编号&#xff1a; S 014 &#xff0c;文末获取源码。 \color{red}{项目编号&#xff1a;S014&#xff0c;文末获取源码。} 项目编号&#xff1a;S014&#xff0c;文末获取源码。 目录 一、摘要1.1 系统介绍1.2 项目录屏 二、研究内容2.1 课程类型管理模块2.2 课程管理模块2…

安卓源码-工程目录

1、程序启动配置及主要的权限声明 2、 界面渲染 3、 布局用 4、 常量等 5、 gradle构建

apply和call在Javascript中的使用与区别

apply和call在js中的使用与区别&#xff1a; 字符串格式化&#xff1a; ${占位符} name小帅 console.log(我是${name}) //我是小帅apply: 语法&#xff1a;function.apply(thisArg, [argsArray])thisArg&#xff1a;可选参数&#xff0c;指定函数执行时的上下文&#xff08…

国际知名商学院复旦大学EMBA荣登全球第8位,中文项目国内居首

2023年10月16日&#xff0c;英国《金融时报》&#xff08;FT&#xff09;发布全球EMBA项目排名。复旦大学EMBA位列全球8强&#xff0c;蝉联中文项目全球第一。学术研究、生源资历、商学院顾问委员会国际化程度、整体满意度等数个重要指标位列中文项目全球第 1位。    排名不…

ClientDateSet:Cannot perform this operation on a closed dataset

一、问题表现 Delphi 三层DataSnap&#xff0c;使用AlphaControls控件优化界面&#xff0c;一窗口编辑时&#xff0c;出现下列错误提示&#xff1a; 编译通过&#xff0c;该窗口中&#xff0c;重新显示数据&#xff0c;下图&#xff1a; 相关代码&#xff1a; procedure…

memset和bzero性能分析

首先&#xff0c;我们需要了解bzero和memset的功能。bzero是一个非标准函数&#xff0c;用于将一块内存区域的内容设置为0。它的原型如下&#xff1a;void *bzero(void *s, size_t n);其中&#xff0c;s是要设置的内存区域的起始地址&#xff0c;n是要设置的字节数。memset是一…

ROS 学习应用篇(九)ROS中launch文件的实现

launch文件就好比一个封装好的命令库&#xff0c;我们按照在终端中输入的代码指令&#xff0c;全部按照launch语言格式封装在一个launch文件中&#xff0c;这样以后执行的时候&#xff0c;就可以不用开很多终端&#xff0c;一条一条输入代码指令。 lauch文件的语言风格很想我之…

通信原理板块——纠错编码最小码距与纠错能力的计算

微信公众号上线&#xff0c;搜索公众号小灰灰的FPGA,关注可获取相关源码&#xff0c;定期更新有关FPGA的项目以及开源项目源码&#xff0c;包括但不限于各类检测芯片驱动、低速接口驱动、高速接口驱动、数据信号处理、图像处理以及AXI总线等 对纠错编码的最小码距d0与编码的检…

数据结构【DS】图的基本概念

定义 完全图(简单完全图) 完全无向图&#xff1a;边数为&#x1d427;&#x1d427;−&#x1d7cf;&#x1d7d0;完全有向图&#xff1a;边数为 &#x1d427;(&#x1d427;−&#x1d7cf;) 子图、生成子图 G的子图&#xff1a;所有的顶点和边都属于图G的图 G的生成子图…

PHP字符串函数的解析

在PHP中&#xff0c;字符串是一种常见的数据类型&#xff0c;用于存储和操作文本数据。PHP提供了丰富的字符串函数&#xff0c;用于执行各种字符串操作&#xff0c;包括截取、连接、替换、搜索等。在这篇文章中&#xff0c;我们将深入解析一些常用的PHP字符串函数&#xff0c;以…

C++虚函数(定义,作用,原理,案例)

一.定义&#xff1a; C的虚函数是在父类(基类)中声明的的函数&#xff0c;它可在子类(派生类)中重写。二.作用 虚函数的目的是实现多态性&#xff0c;即在程序运行时根据对象的实际类型确定调用哪个函数。三.使用方法&#xff1a; 在基类中声明虚函数时&#xff0c;需要在函…

【图解算法】- 快乐数还能这么解?

一 - 前言 介绍&#xff1a;大家好啊&#xff0c;我是hitzaki辰。 社区&#xff1a;&#xff08;完全免费、欢迎加入&#xff09;日常打卡、学习交流、资源共享的知识星球。 自媒体&#xff1a;我会在b站/抖音更新视频讲解 或 一些纯技术外的分享&#xff0c;账号同名&#xff…

PPT基础:合并形状

目录 合并形状功能详解合并形状使用文字转形状图表转形状 合并形状功能详解 形状&#xff1a;并不局限于ppt内给定的图形&#xff0c;也并不全是图形 &#xff08;1&#xff09;所在位置&#xff1a;选中图形后>>>形状格式>>>最左边 &#xff08;2&#x…

Canal+Kafka实现MySQL与Redis数据同步(二)

CanalKafka实现MySQL与Redis数据同步&#xff08;二&#xff09; 创建MQ消费者进行同步 在application.yml配置文件加上kafka的配置信息&#xff1a; spring:kafka:# Kafka服务地址bootstrap-servers: 127.0.0.1:9092consumer:# 指定一个默认的组名group-id: consumer-group…

Bootloader——预编程流程

预编程目录 前言一、预编程步骤1.1 切换到扩展会话1.2 检查刷写前提条件整车ECU进入扩展会话(补充)1.3 停用故障码存储功能1.4 停止通信(一般报文或网络管理报文)前言 刷写过程定义了刷写前、刷写中、刷写后三个阶段。 一、预编程步骤 预编程步骤用来做刷写前的CAN网络准…
最新文章