activity_main.xml
<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="fill_parent" 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> seekbar_img.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 背景图 --> <item android:id="@android:id/background" android:drawable="@drawable/bg"/> <!-- 进和能量图 --> <item android:id="@android:id/progress" android:drawable="@drawable/bg2"/> </layer-list> thumb.xml <?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 --> <item android:drawable="@drawable/bg3" android:state_pressed="true"/> <!-- 普通无焦点状态 --> <item android:drawable="@drawable/bg4" android:state_focused="false" android:state_pressed="false"/> </selector
MainActivity.java
package com.example.seekbardemo; import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; public class MainActivity extends Activity { private TextView num_tv; private SeekBar seekBar; private double width, fDensity; private String numbers; 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 - Utils.dip2px(this, 51)) / 100; seekBar = (SeekBar) findViewById(R.id.seekBar1); num_tv = (TextView) findViewById(R.id.num_tv); } private void initSeekBarProgress() { seekBar.setProgress(45); seekBar.setOnSeekBarChangeListener(mSeekChange); LinearLayout.LayoutParams paramsStrength = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); numbers = 45 + ""; paramsStrength.leftMargin = (int) (45 * 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 } }; }
Utils.java
package com.example.seekbardemo; import android.content.Context; public class Utils { /** * 根据手机分辨率从 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); } }
效果图
源码地址:http://www.apkbus.com/forum.php?mod=viewthread&tid=243597
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦