From 995146f60f9357743b809b61312888dc9ec916f0 Mon Sep 17 00:00:00 2001 From: seven332 Date: Tue, 23 Feb 2016 16:06:01 +0800 Subject: [PATCH] Add IndicatingScrollView --- .../hippo/widget/IndicatingScrollView.java | 92 +++++++++++++++++++ app/src/main/res/values/attrs.xml | 5 + app/src/main/res/values/styles.xml | 9 ++ 3 files changed, 106 insertions(+) create mode 100644 app/src/main/java/com/hippo/widget/IndicatingScrollView.java diff --git a/app/src/main/java/com/hippo/widget/IndicatingScrollView.java b/app/src/main/java/com/hippo/widget/IndicatingScrollView.java new file mode 100644 index 000000000..e02a89871 --- /dev/null +++ b/app/src/main/java/com/hippo/widget/IndicatingScrollView.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2015 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.widget; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Rect; +import android.support.annotation.NonNull; +import android.util.AttributeSet; +import android.widget.ScrollView; + +import com.hippo.ehviewer.R; + +public class IndicatingScrollView extends ScrollView { + + private int mIndicatorHeight; + + private final Paint mPaint = new Paint(); + private final Rect mTemp = new Rect(); + + public IndicatingScrollView(Context context, AttributeSet attrs) { + super(context, attrs); + init(context, attrs); + } + + public IndicatingScrollView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(context, attrs); + } + + private void init(Context context, AttributeSet attrs) { + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IndicatingScrollView); + mIndicatorHeight = a.getDimensionPixelOffset(R.styleable.IndicatingScrollView_indicatorHeight, 1); + mPaint.setColor(a.getColor(R.styleable.IndicatingScrollView_indicatorColor, Color.BLACK)); + mPaint.setStyle(Paint.Style.FILL); + a.recycle(); + } + + private void fillTopIndicatorDrawRect() { + mTemp.set(0, 0, getWidth(), mIndicatorHeight); + } + + private void fillBottomIndicatorDrawRect() { + mTemp.set(0, getHeight() - mIndicatorHeight, getWidth(), getHeight()); + } + + private boolean needShowTopIndicator() { + return canScrollVertically(-1); + } + + private boolean needShowBottomIndicator() { + return canScrollVertically(1); + } + + @Override + public void draw(@NonNull Canvas canvas) { + super.draw(canvas); + + final int restoreCount = canvas.save(); + canvas.translate(getScrollX(), getScrollY()); + + // Draw top indicator + if (needShowTopIndicator()) { + fillTopIndicatorDrawRect(); + canvas.drawRect(mTemp, mPaint); + } + // Draw bottom indicator + if (needShowBottomIndicator()) { + fillBottomIndicatorDrawRect(); + canvas.drawRect(mTemp, mPaint); + } + + canvas.restoreToCount(restoreCount); + } +} diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 5ce22a13c..8f8756db2 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -113,4 +113,9 @@ + + + + + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 085dceb63..456c636b5 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -120,4 +120,13 @@ true + + + +