您的位置:首页 > 其它

自定义ScrollView+自定义滚动条

2016-04-02 22:13 330 查看


主题思路就是,通过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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7A000000"
android:orientation="vertical" >

<LinearLayout
android:layout_centerInParent="true"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="@color/white"
android:orientation="horizontal" >

<com.example.demo.CustomeUserNoticeScrollView
android:id="@+id/user_notice_scroll"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scrollbars="none" >

<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:lineSpacingExtra="6dp"
android:text="@string/user_notice"
android:textColor="#333333"
android:textSize="16dp" />
</com.example.demo.CustomeUserNoticeScrollView>

<com.example.demo.CustomeUserNoticeCursor
android:id="@+id/user_notice_cursor"
android:layout_width="24dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:gravity="center_horizontal|top" >
</com.example.demo.CustomeUserNoticeCursor>
</LinearLayout>

</RelativeLayout>


还有那些测试数据。。。。。随便写点就行了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: