美文网首页
自定义控件之歌词RCL控件

自定义控件之歌词RCL控件

作者: XINHAO_HAN | 来源:发表于2017-09-20 16:51 被阅读0次

    最终效果图:

    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">
    
        <LinearLayout
            android:id="@+id/one"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_centerHorizontal="true">
    
            <TextView
                android: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>
    
        <RelativeLayout
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/one">
    
            <TextView
                android: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);
        }
    
    
        //开始测量
    
    
        @Override
        protected 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);
    
    
        }
    
        //开始绘画
    
        @Override
        protected 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">
    
        <LinearLayout
            android:id="@+id/one"
            android:layout_width="wrap_content"
            android:layout_height="25dp">
    
            <TextView
                android: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>
    
        <RelativeLayout
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_height="wrap_content">
    
            <TextView
                android: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.RCLView
    
            android: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>
    
    
        <SeekBar
            android: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控件

          本文链接:https://www.haomeiwen.com/subject/zdmosxtx.html