自定义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>
还有那些测试数据。。。。。随便写点就行了
相关文章推荐
- 用switch语句制作菜单
- ACM输入问题以及矩阵最大路径累加和
- 常见数据库面试题目
- ARM-RTC的简单使用
- uboot.2016.03 在mini2440上norfalsh启动
- 用git写书
- jdk+tomcat+eclipse配置
- 电子小制作:一个家庭常见物理量显示装置
- 学习计划
- 数据类型TextView中添入int型的错误
- PE 001~010
- hibernate实体映射的一些可选优化/配置
- 软件工程第四个程序:结对项目 —— 电梯调度问题(分析)
- thinkphp3.2.3笔记-验证码
- 物联网: 深圳创客活动亲历之现状
- 第三周项目四(4)-三色球问题
- PHPStorm汉化
- 我理解的--java观察者模式
- Android Sparse*Array容器解析
- django访问数据库出错 OperationalError: attempt to write a readonly database