不断更新函数的参数

时间:2018-02-09 16:43:37

标签: java android function seekbar

我需要一些帮助来调整一个不断更新的函数(< - 这可能一开始没有意义,但希望稍微多一点解释就会清楚)。 这将是一个很长的帖子,但请耐心等待。

我有活动自定义类活动的 Java文件仅包含对其中seekbarOnSeekBarChangeListener的引用。 XML包含所说的搜索栏和参考?到自定义类(不确定它是否实际上称为引用)。以下是两者的代码:

爪哇:

public class Painting extends Activity
{
    static 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() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (progress >= 50
                {
                    sampleView.updatePath(progress);
                }
                else if (progress < 50)
                {
                    sampleView.updatePath((-1) * progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

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

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    tools:context="com.example.android.postvu.SampleView">

    <com.example.android.postvu.SampleView
        android:id="@+id/view"
        android:layout_height="300dp"
        android:layout_width="match_parent"/>

    <SeekBar
        android:id="@+id/curveBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/view"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="40dp"
        android:layout_marginEnd="40dp"
        android:elevation="50dp"/>

</RelativeLayout>

enter image description here

我的自定义类是实际使用上半部分的曲线绘制画布的内容。在这个课程中,我有2个构造函数:

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

        Log.d("LOGCAT", "" + value);

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

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

并最终我坚持使用的2个功能。第一个只是从活动中的seekbar调用,这样我就可以在移动时随时获取搜索栏的值。

public void updatePath(int seekbarProgress)
{
    value = seekbarProgress * 6;
}

第二个功能是根据贝塞尔曲线绘制曲线本身:

public void makePath(Path p)
    {
//            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
    }

我的实际问题

如何更改makePath函数以从updatePath函数中获取值变量,以便p.cubicTo看起来p.cubicTo(0,-400,600,-400,value,0)看起来像我makePath在两个构造函数中调用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(); // updatePath(value); makePath(mPath, value); 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(); // updatePath(value); makePath(mPath, value); Log.d("LOGTAG2", "" + value); mPathPaint = new Paint(); mPathPaint.setAntiAlias(true); mPathPaint.setColor(Color.rgb(255,0,0)); mPathPaint.setStyle(Paint.Style.STROKE); } public int updatePath(int seekbarProgress) { return value = seekbarProgress * 6; } public void makePath(Path p, int number) { p.moveTo(0,0); p.cubicTo(0, 0, 0, 0, number,0); //flat line }

**** **** EDIT

我的自定义类(及相关功能)中的2个构造函数现在看起来像这样:

updatePath

Android Studio突出显示Return value of the method is never used,当我将鼠标悬停在它上面时,它会告诉我makePath(mPath, value);。除非我做错了什么,否则两个构造函数中使用该值,并在其中显示class A { public override int GetHashCode() { Console.WriteLine(base.GetType().Name); return 0; } } 。我不知道此时还有什么可做的,因为它对我没有任何意义。我真的想知道1)如何解决它和2)导致它的原因(为什么它告诉我我没有使用它(我认为我是))。

0 个答案:

没有答案