在另一个文件中获取用于计算的搜索栏进度 Android

Get seekbar progress for calculation in another file Android

这可能是一个非常愚蠢的问题(有 99% 的可能性是)但我一辈子都弄不明白。我在一个 activity 中有一个搜索栏,我们称之为 Activity 1,我需要在另一个中使用它不断变化的值(进度) activity,我们称它为 Activity 2,用于计算(或者我可以在第一个 activity 中进行计算,然后发送价值超过了第二个,IDK 哪个会更好)。

所以这是我的 Activity 1 代码:

public class Painting extends Activity
{
    SeekBar curveBar;
    SampleView sampleView;

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

        curveBar = (SeekBar)findViewById(R.id.curveBar);
        sampleView = new SampleView(this);

        curveBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int progressChangedValue = 0;
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress >= 50) //prints out 50, 60, 70, 80, 90, 100
                {
                    progressChangedValue = progress;
                }
                else if (progress < 50) //prints out -40, -30, -20, -10, 0
                {
                    progressChangedValue = (-1) * (progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(Painting.this, "Value: " +  progressChangedValue, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

这是我的相关 Activity 2 代码:

public class SampleView extends View
{
    private Paint mPaint;
    private float mX;
    private float[] mPos;

    private Path mPath;
    private Paint mPathPaint;

    private static final int DY = 30;
    private static final String TEXTONPATH = "Along a path";


public SampleView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        setFocusable(true);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(90);
        mPaint.setTypeface(Typeface.SERIF);

        mPath = new Path();
        makePath(mPath);

        mPathPaint = new Paint();
        mPathPaint.setAntiAlias(true);
        mPathPaint.setColor(Color.rgb(255,0,0));
        mPathPaint.setStyle(Paint.Style.STROKE);
    }


    public SampleView(Context context)
    {
        super(context);
        setFocusable(true);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(90);
        mPaint.setTypeface(Typeface.SERIF);

        mPath = new Path();
        makePath(mPath);

        mPathPaint = new Paint();
        mPathPaint.setAntiAlias(true);
        mPathPaint.setColor(Color.rgb(255,0,0));
        mPathPaint.setStyle(Paint.Style.STROKE);
    }

public void makePath(Path p)  //<----- this is where i need the seekbar's value
//so that i can make this take more variables so that the seekbar
//adjusts the values in the p.cubicTo's below
    {
//            p.moveTo(250, -300);
        p.moveTo(0,0);
//            p.cubicTo(-250, -550, 750, -550, 250, -300);

            p.cubicTo(0,-400,600,-400,600,0); //semi-circle?
//            p.cubicTo(-600, -400, 600, -400, 0, 0); //as far as the curve probably should allow
//        p.cubicTo(0, 0, 0, 0, 620,0); //flat line
    }

我尝试在第一个 activity 和第二个 activity 上使用 curvebar.getProgress(),但我无法使用它,除非我在 OnSekbarChangeListener 内.我已经尝试将 public 变量设置为该值,但只有当我在侦听器中设置该值时它才会改变,否则它不会(这很有意义)。我已经尝试将搜索栏设置为静态(虽然我可能没有做对)但也没有用。我只是不知道如何获取该值并在第二个文件中使用它。

非常感谢您的帮助

谢谢

只需在您的 SampleView class 中创建一个 public 方法,该方法可以将搜索栏的进度作为参数。

public class SampleView extends View {
    ...
    public void updatePath(int seekBarProgress) {
        // Do whatever you need to with the passed in value here.
    }
    ...
}

然后在你的Activity中你可以调用这个方法用最新的进度值更新SampleView。

public class Painting extends Activity {
    ...
    curveBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            ...
            sampleView.updatePath(progress);
            ...
        }
        ...     
    }
}