Zatem nie ma najmniejszego problemu.
W dziedziczonym komponencie w metodzie onDraw(), wyciągaj wartość progress i wymalowuj ją używając progressu jako zmiennej x.
To z mojej starej kontrolki Slidera:
public class Slider extends ProgressBar implements OnTouchListener {
static public final String TAG = "Slider";
static public final int MAX_PROGRESS = 200;
static public final int PADDING = 3;
private int width;
private int progress = 100;
private Bitmap bitmap;
private IProgressListener progressListener;
private float progressCenter;
private boolean isTouchedCorrectly = false;
public Slider(Context context) {
super(context);
init(context);
}
public Slider(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public Slider(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
Log.v(TAG, "initialized");
this.setMax(MAX_PROGRESS);
this.setProgress(progress);
this.setOnTouchListener(this);
this.bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.circle2);
}
public void setProgressListener(IProgressListener listener) {
this.progressListener = listener;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
this.width = getWidth() - 6;
this.progressCenter = getProgressMarkerCenter(progress);
}
@Override
protected void onDraw(Canvas canvas) {
// Log.v(TAG, "onDraw");
super.onDraw(canvas);
canvas.drawBitmap(bitmap, progressCenter - 8f, 2, new Paint());
}
public float getProgressMarkerCenter(int progressValue) {
return Math.round( (float)(progressValue * width) / (float)MAX_PROGRESS );
}
@Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
final float x = event.getX();
// Log.v(TAG, "x: " + x + " progressCenter = " + progressCenter);
if( action == MotionEvent.ACTION_DOWN && validateLocation(x) ) {
isTouchedCorrectly = true;
return true;
}
if( action == MotionEvent.ACTION_UP ) {
isTouchedCorrectly = false;
return true;
}
if( action == MotionEvent.ACTION_MOVE && isTouchedCorrectly ) {
// Log.v(TAG, "ACTION_MOVE");
final int value = Math.round((float) MAX_PROGRESS * (x / (width)));
if( !isProgressChanged(value) ) {
return true;
}
progress = value;
if(progress < 0) {
progress = 0;
} else if(progress > 200) {
progress = 200;
}
this.setProgress(progress);
progressCenter = getProgressMarkerCenter( progress );
if( progressListener != null ) {
progressListener.progressChanged(this, progress);
}
}
return true;
}
public boolean isProgressChanged(final int value) {
if( value != progress ) {
return true;
}
return false;
}
private boolean validateLocation(float x) {
if( x < progressCenter + 20 && x > progressCenter - 20 ) {
return true;
}
return false;
}
public void setProgress(float scale) {
this.progress = Math.round( scale * 100 );
this.setProgress(progress);
this.progressCenter = getProgressMarkerCenter(progress);
// postInvalidate();
}
oraz w xmlu:
<pl.neurosoft.android.graph.components.Slider
android:id="@+id/slider"
android:layout_width="fill_parent"
android:layout_height="25dip"
android:indeterminateOnly="false"
android:progressDrawable="@android:drawable/progress_horizontal"
android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
/>
Wierzę, że zdołasz wywalić zbędne elementy 🙂