The use of SeekBar in Android

The seekbar style of android is not very good-looking. We often need to beautify or rewrite it in project development.

SeekBar settings cannot be dragged. Rewrite SeekBar as follows

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;

/**
 * Created by Administrator on 2018/1/13.
 */

public class MySeekBar extends SeekBar {
    public MySeekBar(Context context) {
        super(context);
    }

    public MySeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //return super.onTouchEvent(event);
        // It's supposed to pass TouchEvent on. Let's not pass it on
        return  false;
    }
}

In fact, this change is relatively simple. The xml file is as follows:

<com.example.administrator.myapplication.MySeekBar
        android:id="@+id/my_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="30dp"
        android:max="100"
        android:maxHeight="2dip"
        android:minHeight="2dip"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:progressDrawable="@drawable/bg_seekbar"
        android:thumb="@drawable/point"
        />
<com.example.administrator.myapplication.MySeekBar
        android:id="@+id/my_seekbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="30dp"
        android:max="100"
        android:maxHeight="4dip"
        android:minHeight="4dip"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:progressDrawable="@drawable/bg_seekbar"
        android:thumb="@drawable/sun"
        />

android:thumb is a drag button on the slider.
android:progressDrawable is used here to create a color gradient effect for the progress of drag, as well as the fillet effect around the progress bar

The code of BG ﹣ seekbar.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <solid android:color="#C9CAC5" />

            <corners android:radius="10dip"/>
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="10dip"/>
                <solid android:color="#C9CAC5" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <bitmap android:src="@drawable/seekbar_bac_line" >
        </bitmap>
    </item>

</layer-list>

Where @ drawable / seekbar ﹣ BAC ﹣ line refers to the following figure:


@drawable/sun

Where @ drawable/point.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false">
    <solid android:color="#fcd601" />
    <stroke
        android:width="1dp"
        android:color="#fcd601" />
    <size
        android:width="10dp"
        android:height="10dp" />
</shape>

Refer to http://blog.csdn.net/huashanjuji/article/details/51396583

Tags: Android xml encoding

Posted on Mon, 04 May 2020 19:18:39 -0400 by fypstudent