Way to change progress color of a custom vertical seekbar on android, not working from a thread
I'm trying to change the progress color of a custom vertical SeekBar How can I get a working vertical SeekBar in Android? and it works fine if I put this code in a View.OnTouchListener:
VerticalSeekBar verticalSeekBar1 = (VerticalSeekBar)verticalSeekBar;
Rect bounds = verticalSeekBar1.getProgressDrawable().getBounds();
int progr = verticalSeekBar1.getProgress();
int max = verticalSeekBar1.getMax();
int width = verticalSeekBar1.getWidth();
int height = verticalSeekBar1.getHeight();
prDr = verticalSeekBar1.getProgressDrawable();
verticalSeekBar1.setProgressDrawable(r.getDrawable(R.drawable.myprogress));
verticalSeekBar1.getProgressDrawable().setBounds(bounds);
verticalSeekBar1.setMax(max);
verticalSeekBar1.setProgress(1);
verticalSeekBar1.setProgress(progr);
verticalSeekBar1.onSizeChanged(width, height);
verticalSeekBar1开发者_Go百科.refreshDrawableState();
However, if I put this same code in another thread it works partially since the thumb of the seekbar does not go to the right position and instead goes down but the color of the progress works well and gets to the last position. I figured that the method onSizeChanged is probably causing this. I debugged it and found out that in both scenarios the onSizeChanged method has the appropriate arguments but in the code from the thread it does not update the seekbar.
I already tried moving this code to UI's thread but does not work either.
LinearLayout linearLayout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.FILL_PARENT);
// linearLayout.setOrientation(LinearLayout.VERTICAL);
VerticalSeekBar_1 verticalSeekBar_1 = new VerticalSeekBar_1(
getApplicationContext());
// ------------ custom seekbar
LayerDrawable layer = (LayerDrawable) verticalSeekBar_1
.getProgressDrawable();
Drawable draw1 = (Drawable) layer
.findDrawableByLayerId(android.R.id.progress);
Bitmap bitmap1 = BitmapFactory.decodeResource(getApplicationContext()
.getResources(), R.drawable.scroll_on);
draw1 = new ClipDrawable(new BitmapDrawable(bitmap1),
Gravity.AXIS_PULL_BEFORE, ClipDrawable.HORIZONTAL);
layer.setDrawableByLayerId(android.R.id.progress, draw1);
Drawable draw2 = (Drawable) layer
.findDrawableByLayerId(android.R.id.background);
Bitmap bitmap2 = BitmapFactory.decodeResource(getApplicationContext()
.getResources(), R.drawable.scroll_off);
draw2 = new BitmapDrawable(bitmap2);
layer.setDrawableByLayerId(android.R.id.background, draw2);
// --------------
verticalSeekBar_1.setLayoutParams(rlp);
verticalSeekBar_1.setMax(100);
verticalSeekBar_1.setProgress(30);
verticalSeekBar_1
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
linearLayout.addView(verticalSeekBar_1);
setContentView(linearLayout);
I read that using Handler or AsyncTask can do the work for updating UI and that did the trick.
uiHandler.post(new Runnable() {
VerticalSeekBar vs;
Rect bounds;
int progr;
int max;
int width;
int height;
{
vs = verticalSeekBars.get(currentNote);
bounds = vs.getProgressDrawable().getBounds();
progr = vs.getProgress();
max = vs.getMax();
width = vs.getWidth();
height = vs.getHeight();
}
public void run() {
if(currentBytes != 0) {
VerticalSeekBar vsBack = verticalSeekBars.get(currentNote-1);
Rect bounds = vsBack.getProgressDrawable().getBounds();
int progr = vsBack.getProgress();
int max = vsBack.getMax();
int width = vsBack.getWidth();
int height = vsBack.getHeight();
vsBack.setProgressDrawable(prDrInactive);
vsBack.getProgressDrawable().setBounds(bounds);
vsBack.setMax(max);
vsBack.setProgress(1);
vsBack.setProgress(progr);
vsBack.onSizeChanged(width, height);
vsBack.refreshDrawableState();
}
painting note.
prDrInactive = vs.getProgressDrawable();
vs.setProgressDrawable(prDrActive);
vs.getProgressDrawable().setBounds(bounds);
vs.setMax(max);
vs.setProgress(1);
vs.setProgress(progr);
vs.onSizeChanged(width, height);
vs.refreshDrawableState();
}
});
精彩评论