繁体   English   中英

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

[英]Get seekbar progress for calculation in another file Android

这可能是一个非常愚蠢的问题(它有 99% 的可能性),但我一生都无法弄清楚。 我在一个活动中有一个搜索栏,我们称之为Activity 1 ,我需要在另一个活动中使用它不断变化的值(进度),我们称之为Activity 2 ,用于计算(或者我可以做第一个活动中的计算,然后将值发送到第二个活动,IDK 会更好)。

所以这是我的活动 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();
            }
        });
    }
}

这是我的相关活动 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
    }

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

我真的很感激一些帮助

谢谢

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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