毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> 开发语言 >> 正文

如何在seekbar的滑块上添加文字

更新时间:2015-11-11:  来源:毕业论文
这个可以自己定义一个布局,原生的是一张图,你可以写个布局上去,用framelayout的形式
个人觉得这个功能没什么用,用户滑动seekbar的时候必定会按到那个按钮,在上面显示多少数值用户根本看不到,难道要让用户调节的时候还要时不时的抬起手看看我调到了多少?这样的用户体验非常不好。莫不如在seekbar右上角加一个简单的 动态显示当前level的textview,再给他加一个淡入淡出的动画效果在用户不在调节时候隐掉来的实用,纯属个人观点哈
其实这样做,不如在seekbar的上方放一个TextView,用于实时显示用户滑动的数据,这样比你直接放在seekbar上面更好
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;
 
 
public class NumberProgressBar extends View {
 
    private Context mContext;
 
    /**
     * The max progress, default is 100
     */
    private int mMax = 100;
    
    /**
     * The min progress, default is 100
     */
    private int mMin = 0;
 
    /**
     * current progress, can not exceed the max progress.
     */
    private int mProgress = 0;
 
    /**
     * the progress area bar color
     */
    private int mReachedBarColor;
 
    /**
     * the bar unreached area color.
     */
    private int mUnreachedBarColor;
 
    /**
     * the progress text color.
     */
    private int mTextColor;
 
    /**
     * the vernier background color
     */
    private int mVernierColor;
 
    /**
     * the progress text size
     */
    private float mTextSize;
 
    /**
     * the height of the reached area
     */
    private float mReachedBarHeight;
 
    /**
     * the height of the unreached area
     */
    private float mUnreachedBarHeight;
 
 
    private final int default_text_color = Color.rgb(66, 145, 241);
    private final int default_reached_color = Color.rgb(66, 145, 241);
    private final int default_unreached_color = Color.rgb(204, 204, 204);
    private final float default_progress_text_offset;
    private final float default_text_size;
    private final float default_reached_bar_height;
    private final float default_unreached_bar_height;
 
    /**
     * for save and restore instance of progressbar.
     */
    private static final String INSTANCE_STATE = "saved_instance";
    private static final String INSTANCE_TEXT_COLOR = "text_color";
    private static final String INSTANCE_TEXT_SIZE = "text_size";
    private static final String INSTANCE_REACHED_BAR_HEIGHT = "reached_bar_height";
    private static final String INSTANCE_REACHED_BAR_COLOR = "reached_bar_color";
    private static final String INSTANCE_UNREACHED_BAR_HEIGHT = "unreached_bar_height";
    private static final String INSTANCE_UNREACHED_BAR_COLOR = "unreached_bar_color";
    private static final String INSTANCE_MAX = "max";
    private static final String INSTANCE_PROGRESS = "progress";
 
    private static final int PROGRESS_TEXT_INVISIBLE = 0;
    private static final int PROGRESS_TEXT_MIDDLE = 1;
    private static final int PROGRESS_TEXT_ABOVE = 2;
 
 
 
    /**
     * the width of the text that to be drawn
     */
    private float mDrawTextWidth;
 
    /**
     * The width of vernier
     */
    private float mDrawVerniewWidth;
    /**
     * The height of vernier
     */
    private float mDrawVerniewHeight;
 
    /**
     * the drawn text start
     */
    private float mDrawTextStart;
 
    /**
     *the drawn text end
     */
    private float mDrawTextEnd;
 
    /**
     * the text that to be drawn in onDraw()
     */
    private String mCurrentDrawText;
 
    /**
     * the Paint of the reached area.
     */
    private Paint mReachedBarPaint;
    /**
     * the Painter of the unreached area.
     */
    private Paint mUnreachedBarPaint;
    /**
     * the Painter of the progress text.
     */
    private Paint mTextPaint;
    /**
     * the Painter of the vernier
     */
    private Paint mVernierPaint;
 
    /**
     * The path between vernier and progressbar
     */
    private Path mVernierPath;
 
    /**
     * Unreached Bar area to draw rect.
     */
    private RectF mUnreachedRectF = new RectF(0,0,0,0);
    /**
     * reached bar area rect.
     */
    private RectF mReachedRectF = new RectF(0,0,0,0);
    /**
     * Vernier area rect
     */
    private RectF mVernierRectF = new RectF(0,0,0,0);
 
    /**
     * the progress text offset.
     */
    private float mOffset;
    /**
     * The offset between vernier and progress bar
     */
    private float mVernierOffset;
 
    /**
     * The vernier may exceed the area of progress bar
     * So set a padding to left and right side of progress bar
     */
    private float mProgressBarPadding = 0;
 
    /**
     * determine if need to draw unreached area
     */
    private boolean mDrawUnreachedBar = true;
 
    private boolean mDrawReachedBar = true;
 
    private boolean mIfDrawText = false;
 
    private boolean mIfDrawVernier = true;
 
    private String mShowText = null;
 
    public enum TEXT_MODE {
        NONE, ABOVE, MIDDLE
    }
 
    public NumberProgressBar(Context context) {
        this(context, null);
    }
 
    public NumberProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.numberProgressBarStyle);
    }
 
    public NumberProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
 
        mContext = context;
 
        default_reached_bar_height = dp2px(1.5f);
        default_unreached_bar_height = dp2px(1.0f);
        default_text_size = sp2px(10);
        default_progress_text_offset = dp2px(3.0f);
 
        //load styled attributes.
        final TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.NumberProgressBar,
                defStyleAttr, 0);
 
        mReachedBarColor = attributes.getColor(R.styleable.NumberProgressBar_progress_reached_color, default_reached_color);
        mUnreachedBarColor = attributes.getColor(R.styleable.NumberProgressBar_progress_unreached_color,default_unreached_color);
        mTextColor = attributes.getColor(R.styleable.NumberProgressBar_progress_text_color,default_text_color);
        mVernierColor = attributes.getColor(R.styleable.NumberProgressBar_progress_vernier_color,default_reached_color);
        mTextSize = attributes.getDimension(R.styleable.NumberProgressBar_progress_text_size, default_text_size);
 
        mReachedBarHeight = attributes.getDimension(R.styleable.NumberProgressBar_progress_reached_bar_height,default_reached_bar_height);
        mUnreachedBarHeight = attributes.getDimension(R.styleable.NumberProgressBar_progress_unreached_bar_height,default_unreached_bar_height);
        mOffset = attributes.getDimension(R.styleable.NumberProgressBar_progress_text_offset,default_progress_text_offset);
        mVernierOffset = getResources().getDimension(R.dimen.p8);
 
        int textMode = attributes.getInt(R.styleable.NumberProgressBar_progress_text_mode,PROGRESS_TEXT_INVISIBLE);
        switch (textMode){
            case PROGRESS_TEXT_INVISIBLE:
                mIfDrawText = false;
                mIfDrawVernier = false;
                break;
            case PROGRESS_TEXT_MIDDLE:
                mIfDrawText = true;
                mIfDrawVernier = false;
                break;
            case PROGRESS_TEXT_ABOVE:
                mIfDrawText = false;
                mIfDrawVernier = true;
                break;
        }
        mProgressBarPadding = mIfDrawVernier ? 2.0f : 0f;
        setProgress(attributes.getInt(R.styleable.NumberProgressBar_progress,0));
        setMax(attributes.getInt(R.styleable.NumberProgressBar_max, 100));
        //
        attributes.recycle(); 
        initializePainters();     }
设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。