自定义控件之歌词RCL控件

2023-10-25 15:51
文章标签 自定义 控件 歌词 rcl

本文主要是介绍自定义控件之歌词RCL控件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最终效果图:

2017-09-20-04mzAaaaaa.gif

控件的思路是2个TextView压在一起

-------注意更改第二版为可以居中版本,下载Demo后请覆盖内容

文件名:item_rcl.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:id="@+id/one"android:layout_width="wrap_content"android:layout_height="25dp"android:layout_centerHorizontal="true"><TextViewandroid:id="@+id/qqq"android:layout_width="wrap_content"android:layout_height="wrap_content"android:scrollbars="horizontal"android:text="XINHAO_HAN"android:textColor="#00ff00"android:textSize="20sp" /></LinearLayout><RelativeLayoutandroid:id="@+id/two"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_alignLeft="@+id/one"><TextViewandroid:id="@+id/zzzz"android:layout_width="wrap_content"android:layout_height="wrap_content"android:scrollbars="horizontal"android:text="XINHAO_HAN"android:textColor="#ff00ff"android:textSize="20sp" /></RelativeLayout></RelativeLayout>

TextView使用的属性如下:

XML:
android:scrollbars="horizontal"
java:textView.setMovementMethod(ScrollingMovementMethod.getInstance());textView.setHorizontallyScrolling(true); // 不让超出屏幕的文本自动换行,使用滚动条

全部代码 --JAVA

package com.example.downlist.view;import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.example.downlist.R;
import com.example.downlist.utils.UIUtlis;
/*** Created by Administrator on 2017/9/20.*/
//歌词控件
public class RCLView extends FrameLayout {private View one;private View two;private Context context;private View view;private int width;private TextView textView;private TextView qqq;public RCLView(Context context) {super(context);initView(context, null);}private String title = "XINHAO_HAN";private Paint paint;public RCLView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);initView(context, attrs);}public RCLView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initView(context, attrs);}//初始化public void setString(String string) {textView.setText(string);qqq.setText(string);}public void initView(Context context, AttributeSet attrs) {this.context = context;if (view == null)view = UIUtlis.getView(R.layout.item_rcl);if (one == null)one = view.findViewById(R.id.one);if (two == null)two = view.findViewById(R.id.two);textView = two.findViewById(R.id.zzzz);qqq = one.findViewById(R.id.qqq);textView.setMovementMethod(ScrollingMovementMethod.getInstance());textView.setHorizontallyScrolling(true); // 不让超出屏幕的文本自动换行,使用滚动条addView(view);if (attrs != null) {TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RCLView);int topColor = typedArray.getColor(R.styleable.RCLView_topColor, Color.RED);int boomColor = typedArray.getColor(R.styleable.RCLView_boomColor, Color.BLUE);String string = typedArray.getString(R.styleable.RCLView_text);int integer = typedArray.getInteger(R.styleable.RCLView_textSize, 40);setTopColor(topColor);setBoomColor(boomColor);setString(string);setTextSize(integer);}}//设置顶部颜色public void setTopColor(int color) {qqq.setTextColor(color);}//设置底部颜色public void setBoomColor(int color) {textView.setTextColor(color);}//设置字体大小public void setTextSize(float size) {qqq.setTextSize(size);textView.setTextSize(size);}//开始测量@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);width = one.getMeasuredWidth();// Log.e("宽:", "onMeasure: " + width );}public void setIndex(int index) {ViewGroup.LayoutParams layoutParams = two.getLayoutParams();double i = width * 1.0 / 100;Log.e("宽:", "setIndex: " + i + " index: " + index + " 总和:" + (i * index));layoutParams.width = (int) (i * index);two.setLayoutParams(layoutParams);}//开始绘画@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);}
}

//XML部分

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"><LinearLayoutandroid:id="@+id/one"android:layout_width="wrap_content"android:layout_height="25dp"><TextViewandroid:id="@+id/qqq"android:layout_width="wrap_content"android:layout_height="wrap_content"android:scrollbars="horizontal"android:text="这是自定义歌词的一种思路"android:textColor="#00ff00"android:textSize="20sp" /></LinearLayout><RelativeLayoutandroid:id="@+id/two"android:layout_width="0dp"android:layout_height="wrap_content"><TextViewandroid:id="@+id/zzzz"android:layout_width="wrap_content"android:layout_height="wrap_content"android:scrollbars="horizontal"android:text="这是自定义歌词的一种思路"android:textColor="#ff00ff"android:textSize="20sp" /></RelativeLayout></FrameLayout>

attrs部分

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="RCLView"><attr name="topColor" format="color" /><attr name="boomColor" format="color" /><attr name="textSize" format="integer"></attr><attr name="text" format="string"></attr></declare-styleable></resources>

//使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:hxh="http://schemas.android.com/apk/res-auto"android:id="@+id/mySheView"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><com.example.downlist.view.RCLViewandroid:id="@+id/rcView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="50dp"hxh:boomColor="#ff0000"hxh:text="曾经的一切,都是那么渺茫,曾经的曾经都是一切"hxh:textSize="15"hxh:topColor="#00ff00"></com.example.downlist.view.RCLView><SeekBarandroid:id="@+id/see"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="50dp"android:max="100" /></LinearLayout>

Demo下载:
链接: https://pan.baidu.com/s/1i5P6WML 密码: 3283

这篇关于自定义控件之歌词RCL控件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/283593

相关文章

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

dubbo3 filter(过滤器)如何自定义过滤器

《dubbo3filter(过滤器)如何自定义过滤器》dubbo3filter(过滤器)类似于javaweb中的filter和springmvc中的intercaptor,用于在请求发送前或到达前进... 目录dubbo3 filter(过滤器)简介dubbo 过滤器运行时机自定义 filter第一种 @A

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...