自定义ScrollView+自定义滚动条

自定义ScrollView+自定义滚动条_第1张图片

主题思路就是,通过scrollView的onScrollChanged的事件,去设置重绘cursor...  当然也可以直接用ScrollTo

先看主界面UserNoticeActivity

这里面有个自定义的控件,很简单。。。

package com.example.demo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;


import java.io.InvalidClassException;

public class UserNoticeActivity extends Activity  {
	CustomeUserNoticeScrollView user_notice_scroll;
	CustomeUserNoticeCursor user_notice_cursor;
	RelativeLayout start_virtual;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_notice);
		initRes();
		user_notice_scroll.setCursor(user_notice_cursor);
	}

	@SuppressLint("WrongViewCast")
	private void initRes() {
		user_notice_scroll = (CustomeUserNoticeScrollView) findViewById(R.id.user_notice_scroll);
		user_notice_cursor = (CustomeUserNoticeCursor) findViewById(R.id.user_notice_cursor);

	}

}

CustomeUserNoticeScrollView

package com.example.demo;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ScrollView;

public class CustomeUserNoticeScrollView extends ScrollView {

	CustomeUserNoticeCursor cursor;
	private float noticeScrollViewTotleHeight;
	private float noticeScrollViewVisibleHeight;
	private Context context;

	public CustomeUserNoticeScrollView(Context context) {
		this(context, null);
	}

	public CustomeUserNoticeScrollView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;

	}

	public CustomeUserNoticeScrollView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		noticeScrollViewTotleHeight = this.getChildAt(0).getMeasuredHeight();
		noticeScrollViewVisibleHeight = this.getHeight();

		// 3683 900
		Log.i("TAG", "totle:" + noticeScrollViewTotleHeight + ",visible"
				+ noticeScrollViewVisibleHeight);
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		float currentScrollY = this.getScrollY();
		float proportionTotalHeight = currentScrollY
				/ (noticeScrollViewTotleHeight - noticeScrollViewVisibleHeight);
		float cursorMoveY = proportionTotalHeight
				* (noticeScrollViewVisibleHeight - dp2px(context, 30));
		Log.i("TAG", "currentScrollY:" + currentScrollY
				+ ",proportionVisibleHeight--" + cursorMoveY);

		// cursor.scrollTo(0, -(int) proportionVisibleHeight);

		cursor.SetOffSet(cursorMoveY);
	}

	public void setCursor(CustomeUserNoticeCursor user_notice_cursor) {
		this.cursor = user_notice_cursor;

	}

	public static int dp2px(Context context, float dpValue) {
		float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
}


CustomeUserNoticeCursor

package com.example.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


public class CustomeUserNoticeCursor extends View {

	Paint mPaint;

	private Context context;

	private float userNoticeCursorWidth;

	public CustomeUserNoticeCursor(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		initRes();
	}

	private void initRes() {
		mPaint = new Paint();
		mPaint.setColor(getResources().getColor(R.color.contentPressColor));
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setAntiAlias(true);

		userNoticeCursorWidth = dp2px(context, 24);
	}

	public CustomeUserNoticeCursor(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public CustomeUserNoticeCursor(Context context) {
		this(context, null);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		RectF rect3 = new RectF(userNoticeCursorWidth / 4, cursorMoveY,
				userNoticeCursorWidth / 4 * 2, cursorMoveY + dp2px(context, 30));
		canvas.drawRoundRect(rect3, 10, 10, mPaint);

	}

	private float cursorMoveY;

	public void SetOffSet(float cursorMoveY) {
		this.cursorMoveY = cursorMoveY;
		Log.i("TAG", ",proportionVisibleHeight:" + cursorMoveY);
		invalidate();
	}

	//
	public static int dp2px(Context context, float dpValue) {
		float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
}



user_notice.xml




        

            

                
            

            
            
        




还有那些测试数据。。。。。随便写点就行了

你可能感兴趣的:(Android控件实例)