ListIndexes

5432 ワード

リストクイックインデックス列public class ListIndexes extends ImageView{

	public int Alpha;
	private int intervalValue = 0;
	private int imageHeight;
	private float frontHeight;
	public OnscrollMoveEvent mOnscrollMoveEvent;
	public boolean isvisible = true;;
	Context con;
	char[] data = { 'A', 'D', 'G', 'J', 'M', 'O', 'R', 'U', 'X', 'Z' };
	float mCharGap = 0;

	public ListIndexes(Context context) {
		super(context);
		con = context;
		Alpha = 50;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		
		Bitmap contacts_left_srch_image = ImageTool.small(
				GetImageRouseTools
						.GetImageSource(this.getContext(),"left_search"), DensityUtil
						.dip2px(con, 14));
		Bitmap contacts_right_srch_focus_image = ImageTool.small(
				GetImageRouseTools
						.GetImageSource(this.getContext(),"left_search_focus"),
				DensityUtil.dip2px(con, 14));
		imageHeight = contacts_left_srch_image.getHeight();//  
		frontHeight = 22 / (this.getHeight() - imageHeight) + 0.5f;
		intervalValue = (int) (this.getHeight() - frontHeight * 20 - imageHeight) / 22;//  
		mCharGap = 2 * intervalValue + frontHeight;

		Paint p = new Paint();
		p.setAntiAlias(true);//  
		p.setTextSize(DensityUtil.dip2px(con, 13));
		p.setFakeBoldText(true);
		p.setAlpha(Alpha);
		
		p.setTextAlign(Paint.Align.CENTER);
		if (Alpha == 110) {
			p.setStyle(Paint.Style.FILL);
			p.setColor(Color.GRAY);
			RectF oval3 = new RectF(DensityUtil.dip2px(con, 3), 0,
					this.getWidth() * 2, this.getHeight());
			canvas.drawRoundRect(oval3, DensityUtil.dip2px(con, 12),
					DensityUtil.dip2px(con, 9), p);
			p.setColor(Color.WHITE);
			canvas.drawBitmap(contacts_right_srch_focus_image, DensityUtil.dip2px(con, 12),
					intervalValue, p);
		} else {
			p.setColor(Color.GRAY);
			canvas.drawBitmap(contacts_left_srch_image,
					DensityUtil.dip2px(con, 12), intervalValue, p);
		}
		
		for (int i = 0; i < data.length; i++) {
			canvas.drawText(Character.toString(data[i]), DensityUtil.dip2px(con, 18), (i * 2 + 2)
					* intervalValue + i * frontHeight + imageHeight, p);
			if (i != data.length - 1)
				canvas.drawText(".", DensityUtil.dip2px(con, 18), (i * 2 + 3)
						* intervalValue + i * frontHeight + imageHeight
						- DensityUtil.dip2px(con, 3), p);
		}

		canvas.drawText("#", DensityUtil.dip2px(con, 18),
				((data.length - 1) * 2 + 3) * intervalValue + (data.length - 1)
						* frontHeight + imageHeight+ DensityUtil.dip2px(con, 6), p);
		//  
		if (!contacts_left_srch_image.isRecycled())
			contacts_left_srch_image.recycle();
		if (!contacts_right_srch_focus_image.isRecycled())
			contacts_right_srch_focus_image.recycle();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (!isvisible) {
			return true;
		}
		float y = event.getY();

		float mfStartI = (y - imageHeight - 2 * intervalValue)
				/ (2 * intervalValue + frontHeight);

		int mStartI = (int) Math.round(mfStartI);

		char destChar = getDestChar(mStartI, y);
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mOnscrollMoveEvent.onScrollMove(destChar);
			Alpha = 110;
			this.invalidate();
			break;
		case MotionEvent.ACTION_MOVE:
			mOnscrollMoveEvent.onScrollMove(destChar);			
			break;
		case MotionEvent.ACTION_UP:
			mOnscrollMoveEvent.onScrollTouchEnd();
			Alpha = 50;
			this.invalidate();
			break;
		}
		return true;
	}

	private char getDestChar(int mStartI, float y) {
		char desChar = 'A';
		if (mStartI < 0) {
			return desChar = data[0];
		}
		if (mStartI >= data.length) {
			return '#';
		}

		desChar = data[mStartI];
		float dotStartY = ((mStartI * 2 + 3) * intervalValue + mStartI
				* frontHeight + imageHeight - DensityUtil.dip2px(con, 3));
		float upDotStartY = (((mStartI - 1) * 2 + 3) * intervalValue
				+ (mStartI - 1) * frontHeight + imageHeight - DensityUtil
				.dip2px(con, 3));
		float letterStartY = ((mStartI * 2 + 2) * intervalValue + mStartI
				* frontHeight + imageHeight);
		if (y >= (dotStartY - mCharGap / 4) && y <= (dotStartY + mCharGap / 6)) {
			desChar++;
		} else if (y >= upDotStartY && y <= (letterStartY - mCharGap / 4)) {
			desChar--;
		}
		if (desChar > 90) {
			desChar = '#';
		}
		return desChar;
	}

	public void setOnScrollMoveListener(OnscrollMoveEvent listener) {
		mOnscrollMoveEvent = listener;
	}

	public interface OnscrollMoveEvent {
		public void onScrollMove(char c);

		public void onScrollTouchDown();

		public void onScrollTouchEnd();
	}

	public void setNotVisible(boolean visible) {
		isvisible = visible;
	}
}