如何使文本视图沿进度条移动

时间:2019-03-12 01:17:55

标签: android android-canvas android-progressbar

上下文:我想构建一个带有某些值的水平进度条,并希望将一些沿该进度条移动的文本 。所以我的最终想法是这样的(我已经建立了进度栏,我只留下了下面的文本视图

Progress bar with text views below

我当前的代码如下:

这是我的 StackedHorizo​​ntalProgressBar类

public class StackedHorizontalProgressBar extends ProgressBar {
  private Paint paint;
  int primary_progress, max_value;

  public StackedHorizontalProgressBar(Context context) {
    super(context);
    init();
  }

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

  @Override public synchronized void setMax(int max) {
    this.max_value = max;
    super.setMax(max);
  }

  @Override public synchronized void setProgress(int progress) {
    if (progress > max_value) {
      progress = max_value;
    }
    this.primary_progress = progress;
    super.setProgress(progress);
  }



  @Override public synchronized void setSecondaryProgress(int secondaryProgress) {
    if ((primary_progress + secondaryProgress) > max_value) {
      secondaryProgress = (max_value - primary_progress);
    }
    super.setSecondaryProgress(primary_progress + secondaryProgress);
  }

  private void init() {
    paint = new Paint();
    paint.setColor(Color.BLACK);
    primary_progress = 0;
    max_value = 100;
  }
}

这是我的主要活动布局(在班级上方使用)

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

  <github.nisrulz.stackedhorizontalprogressbar.StackedHorizontalProgressBar
      android:id="@+id/stackedhorizontalprogressbar"
      style="?android:attr/progressBarStyleHorizontal"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="16dp"
      android:progressDrawable="@drawable/stacked_horizontal_progress"
      app:layout_constraintTop_toTopOf="parent"
      tools:layout_editor_absoluteX="16dp" />

</android.support.constraint.ConstraintLayout>

这是我的 MainActivity类

public class MainActivity extends AppCompatActivity {

    int max = 100;
    int countPrimary = 20;
    int countSecondary = 30;
    StackedHorizontalProgressBar stackedHorizontalProgressBar;

    TextView txt_primary, txt_secondary;

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

        stackedHorizontalProgressBar =
                (StackedHorizontalProgressBar) findViewById(R.id.stackedhorizontalprogressbar);
        stackedHorizontalProgressBar.setMax(max);

        stackedHorizontalProgressBar.setProgress(countPrimary);
        txt_primary.setText("Primary Value : " + countPrimary + "%");

        stackedHorizontalProgressBar.setSecondaryProgress(countSecondary);
        txt_secondary.setText("Secondary Value : " + countSecondary + "%");

    }

}

我认为我必须为此使用画布,因此,如果有任何有经验的人可以为我提供帮助。

1 个答案:

答案 0 :(得分:1)

public class ProgressBarAvtivity extends AppCompatActivity {
ProgressBar progressBar;
TextView tvProgress;
    int d = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_bar_avtivity);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.setProgress(d);
        tvProgress = (TextView)findViewById(R.id.tvProgress);
        tvProgress.setText(String.valueOf(d));
        if (d > 40) {
        tvProgress.setX(((width )* d / 100) - (d/2));
         } 
        else {
        tvProgress.setX(((width )* d / 100));

    }
 }}