码迷,mamicode.com
首页 > 移动开发 > 详细

android自定义Seekbar你还将你的拖动数值显示在旁边固定的TextView上么?

时间:2015-08-19 14:57:54      阅读:585      评论:0      收藏:0      [点我收藏+]

标签:android   seekbar   

转载请注明出处:王亟亟的大牛之路

实现并不复杂,理解透彻.setLayoutParams()方法就能很简单的实现了。

项目结构:
技术分享

上方显示的数值和位置会跟随你的拖动变化而变化

MainActivity

public class MainActivity extends Activity {
    private TextView num_tv;
    private SeekBar seekBar;
    private double width, fDensity;
    private int numbers=0;

    private DisplayMetrics displaysMetrics;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initSeekBarProgress();
    }

    //获取屏幕信息,以及初始化操作
    private void initView() {
        displaysMetrics = getResources().getDisplayMetrics();
        width = displaysMetrics.widthPixels;
        fDensity = (width - dip2px(this, 51)) / 100;
        seekBar = (SeekBar) findViewById(R.id.seekBar1);
        num_tv = (TextView) findViewById(R.id.num_tv);
    }
    //TextView跟随SeekBar移动
    private void initSeekBarProgress() {
        seekBar.setProgress(numbers);
        seekBar.setOnSeekBarChangeListener(mSeekChange);
        LinearLayout.LayoutParams paramsStrength = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        paramsStrength.leftMargin = (int) (numbers * fDensity);
        num_tv.setLayoutParams(paramsStrength);
        num_tv.setText(numbers+"");
    }

    private OnSeekBarChangeListener mSeekChange = new OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            numbers = progress;
            LinearLayout.LayoutParams paramsStrength = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            paramsStrength.leftMargin = (int) (progress * fDensity);
            num_tv.setLayoutParams(paramsStrength);
            num_tv.setText(numbers+"");

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }
    };

    /**
     * 根据手机分辨率从 px(像素) 单位 转成 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 根据手机分辨率从 dp 单位 转成 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/num_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/publish_seekbar_dialog"
            android:gravity="center_horizontal"
            android:paddingTop="1dp"
            android:text="0"
            android:textColor="#ffffff" />

        <SeekBar
            android:id="@+id/seekBar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="0"
            android:progressDrawable="@drawable/seekbar_img"
            android:thumb="@drawable/thumb" />
    </LinearLayout>

</RelativeLayout>

实现就是这么简单,伸手党可戳下面地址下载
源码:http://yunpan.cn/cdDLTGbzVNJtk 访问密码 1b03

版权声明:本文为博主原创文章,未经博主允许不得转载。

android自定义Seekbar你还将你的拖动数值显示在旁边固定的TextView上么?

标签:android   seekbar   

原文地址:http://blog.csdn.net/ddwhan0123/article/details/47779399

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!