3

我正在关注这个人,他编写了一个代码,该代码根据分配给 3 个不同搜索栏的 RGB 通道更改背景颜色,因此 R 到一个 sk,G 到另一个,B 到另一个。

问题是我无法让代码工作:

private int seekR, seekG, seekB;    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color_changer);

    SeekBar sbR = (SeekBar) findViewById(R.id.RedSeekBar);
    SeekBar sbG = (SeekBar) findViewById(R.id.GreenSeekBar);
    SeekBar sbB = (SeekBar) findViewById(R.id.BlueSeekBar);

    mySeekBarChangeListener msbclR = new mySeekBarChangeListener();
    mySeekBarChangeListener msbclG = new mySeekBarChangeListener();
    mySeekBarChangeListener msbclB = new mySeekBarChangeListener();

    sbR.setonseekBarChangeListener(msbclR);
    sbG.setonseekBarChangeListener(msbclG);
    sbB.setonseekBarChangeListener(msbclB);

    msbclR.onprogressChanged(sbR, sbR.getProgress(), false);
    msbclG.onprogressChanged(sbG, sbG.getProgress(), false);
    msbclB.onprogressChanged(sbB, sbB.getProgress(), false);

}

这段文字几乎在所有地方都给我一个错误,无论如何,似乎没有任何效果。我只是想知道为什么.. 我是否必须根据 mySeekBarChangeListener 设置一些东西?提前致谢!

4

1 回答 1

6

它应该工作:

private int seekR, seekG, seekB;    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color_changer);

    SeekBar sbR = (SeekBar) findViewById(R.id.RedSeekBar);
    SeekBar sbG = (SeekBar) findViewById(R.id.GreenSeekBar);
    SeekBar sbB = (SeekBar) findViewById(R.id.BlueSeekBar);

    sbR.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            seekR = progress;

            doSomethingWithColor();
        }
    });

    sbG.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            seekG = progress;

            doSomethingWithColor();
        }
    });

    sbB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            seekB = progress;

            doSomethingWithColor();
        }
    });

}

private void doSomethingWithColor() {
    int color = Color.rgb(seekR, seekG, seekB);
    // Do something with color
}

或更紧凑

private int seekR, seekG, seekB;    

private SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
        case R.id.RedSeekBar:
            seekR = progress;
            break;
        case R.id.GreenSeekBar:
            seekG = progress;
            break;
        case R.id.BlueSeekBar:
            seekB = progress;
            break;
        }

        doSomethingWithColor();
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color_changer);

    SeekBar sbR = (SeekBar) findViewById(R.id.RedSeekBar);
    SeekBar sbG = (SeekBar) findViewById(R.id.GreenSeekBar);
    SeekBar sbB = (SeekBar) findViewById(R.id.BlueSeekBar);

    sbR.setOnSeekBarChangeListener(onSeekBarChangeListener);
    sbG.setOnSeekBarChangeListener(onSeekBarChangeListener);
    sbB.setOnSeekBarChangeListener(onSeekBarChangeListener);

}

private void doSomethingWithColor() {
    int color = Color.rgb(seekR, seekG, seekB);
    // Do something with color
}

另一个

private int seekR, seekG, seekB;    

public class MySeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
        case R.id.RedSeekBar:
            seekR = progress;
            break;
        case R.id.GreenSeekBar:
            seekG = progress;
            break;
        case R.id.BlueSeekBar:
            seekB = progress;
            break;
        }

        doSomethingWithColor();
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color_changer);

    SeekBar sbR = (SeekBar) findViewById(R.id.RedSeekBar);
    SeekBar sbG = (SeekBar) findViewById(R.id.GreenSeekBar);
    SeekBar sbB = (SeekBar) findViewById(R.id.BlueSeekBar);

    MySeekBarChangeListener msbclR = new MySeekBarChangeListener();
    MySeekBarChangeListener msbclG = new MySeekBarChangeListener();
    MySeekBarChangeListener msbclB = new MySeekBarChangeListener();

    sbR.setOnSeekBarChangeListener(msbclR);
    sbG.setOnSeekBarChangeListener(msbclG);
    sbB.setOnSeekBarChangeListener(msbclB);

}

private void doSomethingWithColor() {
    int color = Color.rgb(seekR, seekG, seekB);
    // Do something with color
}
于 2014-05-27T18:24:47.650 回答