android横向刻度尺代码
时间: 2023-10-03 10:10:10 浏览: 150
android刻度尺
以下是一个简单的实现 Android 横向刻度尺的示例代码:
1. 首先,在布局文件中添加一个水平方向的 SeekBar,并将其隐藏起来:
```
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible" />
```
2. 在代码中,设置 SeekBar 的最大值、最小值和当前值,并添加一个 OnSeekBarChangeListener 来监听 SeekBar 的变化:
```
SeekBar seekBar = findViewById(R.id.seekbar);
seekBar.setMax(100);
seekBar.setMin(0);
seekBar.setProgress(50);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 当 SeekBar 的值发生变化时,更新刻度尺的显示
updateScale(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// 当开始拖动 SeekBar 时,暂停更新刻度尺的显示
stopUpdateScale();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// 当停止拖动 SeekBar 时,恢复更新刻度尺的显示
startUpdateScale();
}
});
```
3. 在 updateScale 方法中更新刻度尺的显示,例如使用一个横向的 LinearLayout 来显示刻度尺:
```
private void updateScale(int progress) {
LinearLayout scale = findViewById(R.id.scale);
// 清空原有的刻度
scale.removeAllViews();
// 添加新的刻度
for (int i = 0; i <= 100; i += 10) {
TextView textView = new TextView(this);
textView.setText(String.valueOf(i));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.weight = 1;
textView.setLayoutParams(layoutParams);
if (i == progress) {
textView.setTextColor(Color.RED);
}
scale.addView(textView);
}
}
```
4. 在 startUpdateScale 方法中启动一个定时器来更新刻度尺的显示:
```
private void startUpdateScale() {
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
updateScale(seekBar.getProgress());
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(runnable, 1000);
}
```
5. 在 stopUpdateScale 方法中停止定时器:
```
private void stopUpdateScale() {
handler.removeCallbacksAndMessages(null);
}
```
完整的示例代码如下:
```
public class MainActivity extends AppCompatActivity {
private SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = findViewById(R.id.seekbar);
seekBar.setMax(100);
seekBar.setMin(0);
seekBar.setProgress(50);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
updateScale(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
stopUpdateScale();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
startUpdateScale();
}
});
startUpdateScale();
}
private void updateScale(int progress) {
LinearLayout scale = findViewById(R.id.scale);
scale.removeAllViews();
for (int i = 0; i <= 100; i += 10) {
TextView textView = new TextView(this);
textView.setText(String.valueOf(i));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.weight = 1;
textView.setLayoutParams(layoutParams);
if (i == progress) {
textView.setTextColor(Color.RED);
}
scale.addView(textView);
}
}
private void startUpdateScale() {
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
updateScale(seekBar.getProgress());
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(runnable, 1000);
}
private void stopUpdateScale() {
handler.removeCallbacksAndMessages(null);
}
}
```
阅读全文