Android,如何在自定义进度条中禁用重复背景图像?

时间:2012-03-24 23:49:54

标签: android slider progress-bar customization

我有自定义进度条。 从ProgressBar继承的Horizo​​ntalSlider类是:

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ProgressBar;

public class HorizontalSlider extends ProgressBar {

    private OnProgressChangeListener listener;

    private static final int padding = 2;
    private static final int getMin = 11;


    public interface OnProgressChangeListener {
        void onProgressChanged(View v, int progress);
    }



    public HorizontalSlider(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, android.R.attr.progressBarStyleHorizontal);
    }

    public HorizontalSlider(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HorizontalSlider(Context context) {
        super(context);
    }

    public void setOnProgressChangeListener(OnProgressChangeListener l) {
        listener = l;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction();

        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
            float x_mouse = event.getX() - padding;
            float width = getWidth() - 2 * padding;
            int progress = Math.round((float) getMax() * (x_mouse / width));

            if (progress < getMin)
                progress = getMin;

            if (progress > getMax())
                progress = getMax();

            this.setProgress(progress);

            if (listener != null)
                listener.onProgressChanged(this, progress);
        }

        return true;
    }
}

xml格式的用户界面是:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="100dip"
    android:gravity="center_horizontal" >

    <com.astro.mania.classes.HorizontalSlider
        android:id = "@+id/slider"
        android:layout_width = "fill_parent"
        android:layout_height= "wrap_content"
        android:max="100"
        android:layout_centerHorizontal="true"
        style="?android:attr/progressBarStyleHorizontal" />

    <com.astro.mania.classes.HorizontalSlider 
        android:id="@+id/progressBar"
        android:progressDrawable="@drawable/progressbar"
        android:layout_width="fill_parent" 
        android:layout_height="24dip" 
        style="?android:attr/progressBarStyleHorizontal" 
        android:indeterminateOnly="false" 
        android:max="100"
        android:layout_below="@id/slider"
        android:layout_marginTop="20dip" />


</RelativeLayout>

进度条如下定制:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape>
            <gradient
                    android:startColor="#fffffe"
                    android:centerColor="#0b131e"
                    android:centerY="0.75"
                    android:endColor="#0d1522"
                    android:angle="270" />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <gradient
                        android:startColor="#234"
                        android:centerColor="#234"
                        android:centerY="0.75"
                        android:endColor="#a24"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <bitmap android:src="@drawable/btn_slide_arrow" android:dither="true" />
    </item>

</layer-list>

最后活动是这样的:

 public class LensaMania extends Activity {

    private HorizontalSlider slider, slider2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_lensamania);

        Log.i("LensaMania", "LensaMania activity started...");


        setProgressBarVisibility(true);

        slider = (HorizontalSlider) this.findViewById(R.id.slider);
        slider.setOnProgressChangeListener(changeListener);

        slider2 = (HorizontalSlider) this.findViewById(R.id.progressBar); 
        slider2.setVisibility(View.VISIBLE);
        slider2.setMax(100);
        slider2.setProgress(11);
        slider2.setOnProgressChangeListener(changeListener);
    }



    private OnProgressChangeListener changeListener = new OnProgressChangeListener() {
        public void onProgressChanged(View v, int progress) {
            setProgress(progress);
            Log.i("Progress is:", "=>" + progress);
        }
    };
}

当我运行应用程序并滑动progreebar时,图像会像下面的图像一样重复。我想只有一个图像,当我向左滑动时只想看到一个箭头。如何询问进度条不重复图像?

enter image description here

0 个答案:

没有答案