Java 在另一个文件中获取用于计算的seekbar进度
这可能是一个非常愚蠢的问题(有99%的几率是这样),但我一辈子都搞不懂这个问题。我在一个活动中有一个seekbar,我们称之为活动1,我需要在另一个活动中使用它不断变化的值(进度),我们称之为活动2,用于计算(或者我可以在第一个活动中进行计算,然后将值发送到第二个活动,IDK哪个更好) 下面是我的活动1代码:Java 在另一个文件中获取用于计算的seekbar进度,java,android,seekbar,Java,Android,Seekbar,这可能是一个非常愚蠢的问题(有99%的几率是这样),但我一辈子都搞不懂这个问题。我在一个活动中有一个seekbar,我们称之为活动1,我需要在另一个活动中使用它不断变化的值(进度),我们称之为活动2,用于计算(或者我可以在第一个活动中进行计算,然后将值发送到第二个活动,IDK哪个更好) 下面是我的活动1代码: public class Painting extends Activity { SeekBar curveBar; SampleView sampleView;
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();
}
});
}
}
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
}
公共类绘画扩展活动
{
SeekBar曲线;
采样视图采样视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_-Paint);
curveBar=(SeekBar)findViewById(R.id.curveBar);
sampleView=新的sampleView(本);
curveBar.setOnSeekBarChangeListener(新的SeekBar.OnSeekBarChangeListener(){
int progressChangedValue=0;
@凌驾
public void onProgressChanged(SeekBar-SeekBar、int-progress、boolean-fromUser){
if(progress>=50)//打印出50、60、70、80、90、100
{
progressChangedValue=进度;
}
else if(progress<50)//打印出-40,-30,-20,-10,0
{
progressChangedValue=(-1)*(进度);
}
}
@凌驾
开始跟踪触摸时的公共无效(SeekBar SeekBar){}
@凌驾
TopTrackingTouch(SeekBar SeekBar)上的公共无效{
Toast.makeText(Painting.this,“Value:+progressChangedValue,Toast.LENGTH_SHORT).show();
}
});
}
}
下面是我的相关活动2代码:
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();
}
});
}
}
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
}
public类SampleView扩展了视图
{
私人油漆;
私人浮动mX;
私人浮动[]MPO;
专用路径mPath;
私人涂料;
专用静态最终整数DY=30;
私有静态最终字符串TEXTONPATH=“沿路径”;
公共样本视图(上下文、属性集属性)
{
超级(上下文,attrs);
设置聚焦(真);
mPaint=新油漆();
mPaint.setAntiAlias(true);
mPaint.setTextSize(90);
mPaint.setTypeface(Typeface.SERIF);
mPath=新路径();
makePath(mPath);
mPathPaint=新油漆();
mPathPaint.setAntiAlias(真);
mPathPaint.setColor(Color.rgb(255,0,0));
MPATHPRAINT.setStyle(绘画风格笔划);
}
公共样本视图(上下文)
{
超级(上下文);
设置聚焦(真);
mPaint=新油漆();
mPaint.setAntiAlias(true);
mPaint.setTextSize(90);
mPaint.setTypeface(Typeface.SERIF);
mPath=新路径();
makePath(mPath);
mPathPaint=新油漆();
mPathPaint.setAntiAlias(真);
mPathPaint.setColor(Color.rgb(255,0,0));
MPATHPRAINT.setStyle(绘画风格笔划);
}
public void makePath(Path p)//只需在SampleView类中创建一个public方法,该方法可以将seekbar的进度作为参数
public class SampleView extends View {
...
public void updatePath(int seekBarProgress) {
// Do whatever you need to with the passed in value here.
}
...
}
然后在活动中可以调用此方法以使用最新进度值更新SampleView
public class Painting extends Activity {
...
curveBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
...
sampleView.updatePath(progress);
...
}
...
}
}
活动2不是一个活动,而是一个自定义视图。你需要通过膨胀视图将其正确添加到活动中,调用c'tor不是这样做的。然后你将makePath更改为一个整数作为参数,这样你就可以按你想要的方式使用它。@NeriaNachum我不明白你的意思。你能用一些代码详细说明一下吗?我很抱歉,我无法在SO评论中给您一个完整的答案。我的主要观点是,在一个活动中,您没有两个活动,而是有两个视图,其中一个是SeekBar
。因此,我建议您遵循以下教程:1.简单SeekBar
教程。2.以编程方式向活动添加自定义视图。谷歌it。此外,你没有提到你想要实现什么,但我非常怀疑你是否真的需要一个具有自定义行为的自定义视图。尝试寻找现有视图(无论是在Android开发者还是在Github上)这可能适合您的需要,您的工作将变得更加简单。我已经接受了您所说的内容,现在可以在我的SampleView中获得seekbar进度的价值。如何使用它不断更新我的makePath
函数?意思是:在我的新updatePath()中
函数我已将一个公共INT值设置为seekbarProgress
参数乘以6。如果我将函数更新为makePath(Path p,INT x3)
,其中x3
将是p.cubicTo()中的一个或多个值,我现在如何将该值放入makePath()
函数中
功能?