簡體   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