zoukankan      html  css  js  c++  java
  • Android随笔

    public class MainActivity extends AppCompatActivity {
    
        private SeekBar sb_normal;
        private TextView txt_cur;
        private Context mContext;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mContext = MainActivity.this;
            bindViews();
        }
    
        private void bindViews() {
            sb_normal = (SeekBar) findViewById(R.id.sb_normal);
            txt_cur = (TextView) findViewById(R.id.txt_cur);
            sb_normal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    txt_cur.setText("当前进度值:" + progress + "  / 100 ");
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    Toast.makeText(mContext, "触碰SeekBar", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    Toast.makeText(mContext, "放开SeekBar", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    2.简单SeekBar定制:

    本来还想着自定义下SeekBar的,后来想想,还是算了,涉及到自定义View的一些东西,可能初学者并 不了解,看起来也有点难度,关于自定义View的还是放到进阶那里吧,所以这里就只是简单的定制下SeekBar! 定制的内容包括滑块,以及轨道!

    代码实例:

    运行效果图:

    代码实现: 1.滑块状态Drawable:sb_thumb.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@mipmap/seekbar_thumb_pressed"/>
        <item android:state_pressed="false" android:drawable="@mipmap/seekbar_thumb_normal"/>
    </selector>

    贴下素材:

    seekbar_thumb_normal.pngseekbar_thumb_pressed.png

    2.条形栏Bar的Drawable:sb_bar.xml

    这里用到一个layer-list的drawable资源!其实就是层叠图片,依次是:背景,二级进度条,当前进度:

    <?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="#FFFFD042" />
            </shape>
        </item>
        <item android:id="@android:id/secondaryProgress">
            <clip>
                <shape>
                    <solid android:color="#FFFFFFFF" />
                </shape>
            </clip>
        </item>
        <item android:id="@android:id/progress">
            <clip>
                <shape>
                    <solid android:color="#FF96E85D" />
                </shape>
            </clip>
        </item>
    </layer-list>

    3.然后布局引入SeekBar后,设置下progressDrawable与thumb即可!

    <SeekBar
            android:id="@+id/sb_normal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxHeight="5.0dp"
            android:minHeight="5.0dp"
            android:progressDrawable="@drawable/sb_bar"
            android:thumb="@drawable/sb_thumb"/>
  • 相关阅读:
    CentOS7使用集群同步脚本对文件同步分发
    CentOS7安装jdk1.8
    CentOS7+CDH5.12.1集群搭建
    nginx通配符
    Nginx反向代理及配置
    一些好玩有用的书签
    linux操作小技巧锦集
    系统压测结果对比:tomcat/thinkphp/swoole/php-fpm/apache
    python修改linux日志(logtamper.py)
    【原创】给定随机数的取值范围(最小值、最大值),且要求多次取得的随机数最后的结果有一个固定的平均值
  • 原文地址:https://www.cnblogs.com/wrx166/p/14909590.html
Copyright © 2011-2022 走看看