简体   繁体   中英

Get seekbar progress for calculation in another file Android

This may be a very stupid question (which it's got like 99% chance it is) but I can't figure this out for the life of me. I've got a seekbar in one activity, we'll call it Activity 1 , and I need to use it's constantly changing value (progress) in another activity, we'll call it Activity 2 , for calculations (or maybe I can do the calculations in the first activity and then just send the value over to the second one, IDK which would be better).

So here's my Activity 1 code:

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();
            }
        });
    }
}

and here's my RELEVANT Activity 2 code:

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
    }

I have tried to use curvebar.getProgress() on both the first activity and the second activity but I can't get it unless I'm within the OnSekbarChangeListener . I've tried setting public variables to the value but it only changes if i set the value within the listener, otherwise it doesn't (which makes perfect sense). I've tried setting the seekbar to be static (although I probably didn't do it right) and that didn't work either. I just can't figure out how i can get that value out and use it in the second file.

I would really appreciate some help

Thank you

Just create a public method in your SampleView class that can take the seekbar's progress as a parameter.

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

Then in your Activity you can call this method to update the SampleView with the latest progress value.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM