论坛首页 移动开发技术论坛

定制BlackBerry上面的Slider Bar (原创)

浏览 3732 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-26   最后修改:2010-01-26

版权所有,欢迎转载,转载请注明 : SinFrancis  http://mdev.cc

 

由于BB项目需要,特定制了Sliderbar,BB上没有这个组件,所以只能自己动手。

 

请看代码 :

 

package cc.mdev.seek;

import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.TouchGesture;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.GaugeField;
import net.rim.device.api.ui.container.HorizontalFieldManager;

/**
 * Seekbar 可以拖动
 * 
 * @author Wang XinFeng
 * @site http://mdev.cc
 * 
 */
public class SeekBar2 extends Field {
	/**
	 * 水平滑動
	 */
	public static int HORIZONTAL_SLIDE = 0;

	/**
	 * 垂直滑動
	 */
	public static int VERTICAL_SLIDE = 1;

	/**
	 * 背景图片
	 */
	private EncodedImage background;

	/**
	 * 整个Field的高宽度
	 */
	private int width;
	/**
	 * 整个Field的高度
	 */
	private int height;

	/**
	 * 可以拖动的Image
	 */
	private EncodedImage barImage = null;

	/**
	 * x,y的坐标点
	 */
	private int x = 0, y = 0;

	/**
	 * 拖动时候的监听器
	 */
	private OnSlideListener listener = null;

	/**
	 * 刻度的最大值
	 */
	private int maxValue;

	/**
	 * Seekbar
	 * 
	 * @param width
	 *            宽度
	 * @param maxValue 刻度最大值
	 * @param barImage
	 *            可拖动的图片
	 * @param background
	 *            背景
	 * @param listener
	 *            监听器
	 */
	public SeekBar2(int width,int maxValue,EncodedImage barImage, EncodedImage background,
			OnSlideListener listener) {
		this(width, maxValue,barImage, background);
		this.listener = listener;
	}

	/**
	 * 
	 * @param width
	 *            宽度
	 *    @param maxValue 刻度最大值
	 * @param barImage
	 *            可拖动的图片
	 * @param slideStlye
	 *            滑动方式 垂直或者水平
	 * @param background
	 *            背景
	 */
	public SeekBar2(int width,int maxValue, EncodedImage barImage, EncodedImage background) {
		super(FOCUSABLE);
		this.width = width;
		this.height = barImage.getHeight();
		this.background = background.scaleImage32(Fixed32.div(background
				.getWidth(), width + barImage.getWidth()), Fixed32.div(
				background.getHeight(), height));
		this.barImage = barImage;
		this.maxValue = maxValue;
	}
	
	public int getCurrentValue(){
		return(this.x)/(width/maxValue);
	}
	
	/**
	 * 设置滑动监听器
	 * 
	 * @param listener
	 */
	public void setOnSlideListener(OnSlideListener listener) {
		this.listener = listener;
	}

	protected void layout(int width, int height) {
		setExtent(this.background.getWidth(), this.height);
	}

	protected void paint(Graphics graphics) {

		graphics.drawImage(x, 0, width, height, barImage, 0, 0, 0);
	}

	protected void paintBackground(Graphics arg0) {
		arg0.drawImage(getPaddingLeft(), getPaddingTop(), this.background
				.getWidth(), height, background, 0, 0, 0);
	}

	protected boolean touchEvent(TouchEvent message) {
		int e = message.getEvent();
		// 聚焦时候点击处理
		if (e == TouchEvent.CLICK && isFocus()) {
			this.x = message.getX(1);
			if (listener != null) {
				listener.onStart();
			}
			if (this.x > this.width) {
				this.x = width;
			}

			if (this.x < 0) {
				this.x = 0;
			}
			System.out.println("SeekBar2.touchEvent()Click x is :" + this.x);
			System.out.println("SeekBar2.touchEvent()getCurrentValue is :" + getCurrentValue());
			this.invalidate();
			return true;
		}
		// 聚焦时候弹起处理
		if (e == TouchEvent.UP && isFocus()) {
			if (listener != null) {
				listener.onEnd();
			}
			return true;
		}

		// 聚焦时候移动处理
		if (e == TouchEvent.MOVE && isFocus()) {
			int x2 = message.getX(1);
			this.x=x2;
			if (this.x > this.width) {
				this.x = width;
			}
			if (this.x < 0) {
				this.x = 0;
			}
			System.out.println("SeekBar2.touchEvent()MOVE x is :" + this.x);
			System.out.println("SeekBar2.touchEvent()getCurrentValue is :" + getCurrentValue());
			this.invalidate();
			if (listener != null) {
				listener.onSlide();
			}
			return true;
		}
		return false;
	}

	public static int RGB2HEX(int r, int g, int b) {
		return r << 16 | g << 8 << b;
	}

	// 禁用此方法,不讓系統畫出焦點
	protected void drawFocus(Graphics graphics, boolean on) {

	}

	/**
	 * 滑动监听器
	 * 
	 * @author Wang XinFeng
	 * 
	 */
	public static interface OnSlideListener {

		/**
		 * 开始滑动
		 */
		public void onStart();

		/**
		 * 滑动中
		 */
		public void onSlide();

		/**
		 * 滑动结束
		 */
		public void onEnd();
	}
}

 

此段代码只设置了横向滚动,纵向的并没有设置。

 

 

请看运行截图:

 


 


 

  • 大小: 2.6 KB
   发表时间:2010-12-01  
楼主能否讲得详细些???
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics