import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.widget.Scroller;
import android.util.AttributeSet;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class WeekView extends LinearLayout {

    // ... other variables ...

    public WeekView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public WeekView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        // ... other initialization code ...

        if (attrs != null) {
            final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.WeekView, defStyleAttr, 0);
            try {
                isVerticalFlingEnabled = a.getBoolean(R.styleable.WeekView_verticalFlingEnabled, isVerticalFlingEnabled);
                allDayEventHeight = a.getDimensionPixelSize(R.styleable.WeekView_allDayEventHeight, allDayEventHeight);
                zoomFocusPoint = a.getFraction(R.styleable.WeekView_zoomFocusPoint, 1, 1, zoomFocusPoint);
                isZoomFocusPointEnabled = a.getBoolean(R.styleable.WeekView_zoomFocusPointEnabled, isZoomFocusPointEnabled);
                scrollDuration = a.getInt(R.styleable.WeekView_scrollDuration, scrollDuration);
                timeColumnResolution = a.getInt(R.styleable.WeekView_timeColumnResolution, timeColumnResolution);
                autoLimitTime = a.getBoolean(R.styleable.WeekView_autoLimitTime, autoLimitTime);
                mMinTime = a.getInt(R.styleable.WeekView_minTime, mMinTime);
                mMaxTime = a.getInt(R.styleable.WeekView_maxTime, mMaxTime);
                minOverlappingMinutes = a.getInt(R.styleable.WeekView_minOverlappingMinutes, minOverlappingMinutes);
                isScrollNumberOfVisibleDays = a.getBoolean(R.styleable.WeekView_isScrollNumberOfVisibleDays, isScrollNumberOfVisibleDays);
                enableDrawHeaderBackgroundOnlyOnWeekDays = a.getBoolean(R.styleable.WeekView_enableDrawHeaderBackgroundOnlyOnWeekDays, enableDrawHeaderBackgroundOnlyOnWeekDays);
                isUsingCheckersStyle = a.getBoolean(R.styleable.WeekView_isUsingCheckersStyle, isUsingCheckersStyle);
                headerWeekDayTitleTextSize = a.getDimension(R.styleable.WeekView_headerWeekDayTitleTextSize, headerWeekDayTitleTextSize);
                headerWeekDaySubtitleTextSize = a.getDimension(R.styleable.WeekView_headerWeekDaySubtitleTextSize, headerWeekDaySubtitleTextSize);
                spaceBetweenHeaderWeekDayTitleAndSubtitle = a.getDimensionPixelSize(R.styleable.WeekView_spaceBetweenHeaderWeekDayTitleAndSubtitle, spaceBetweenHeaderWeekDayTitleAndSubtitle);
                untitledEventText = a.getString(R.styleable.WeekView_untitledEventText) != null ? a.getString(R.styleable.WeekView_untitledEventText) : untitledEventText;
            } finally {
                a.recycle();
            }
        }

        //some one time initializations
        mHeaderWeekDayTitleTextPaint.setTextAlign(Paint.Align.CENTER);
        mHeaderWeekDaySubtitleTextPaint.setTextAlign(Paint.Align.CENTER);
        sideTitleTextPaint.setTextAlign(Paint.Align.CENTER);
        sideSubtitleTextPaint.setTextAlign(Paint.Align.CENTER);
        allDaySideTitleTextPaint.setTextAlign(Paint.Align.CENTER);
        timeTextPaint.setTextAlign(Paint.Align.RIGHT);
        mEventTextPaint.setStyle(Paint.Style.FILL);
        mHourSeparatorPaint.setStyle(Paint.Style.STROKE);
        mHeaderWeekDayTitleTodayTextPaint.setTextAlign(Paint.Align.CENTER);
        mHeaderWeekDaySubtitleTodayTextPaint.setTextAlign(Paint.Align.CENTER);
        init();
    }

    private void init() {
        resetHomeDate();

        // Scrolling initialization.
        mGestureDetector = new GestureDetectorCompat(context, mGestureListener);
        mScroller = new OverScroller(context, new FastOutLinearInInterpolator());

        mMinimumFlingVelocity = ViewConfiguration.get(context).getScaledMinimumFlingVelocity();
        mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

        // Measure settings for time column.
        timeTextPaint.setTextSize(textSize);
        timeTextPaint.setColor(headerColumnTextColor);
        timeTextPaint.setTypeface(typeface);

        Rect rect = new Rect();
        String exampleTime = timeColumnResolution % 60 != 0 ? '00:00 PM' : '00 PM';
        timeTextPaint.getTextBounds(exampleTime, 0, exampleTime.length(), rect);
        timeTextHeight = rect.height();
        initTextTimeWidth();

        //handle sideTitleTextPaint
        sideTitleTextPaint.setColor(headerColumnTextColor);
        sideTitleTextPaint.setTypeface(typeface);
        sideTitleTextPaint.setTextSize(headerWeekDayTitleTextSize);
        // handle sideSubtitleTextPaint
        sideSubtitleTextPaint.setTextSize(headerWeekDaySubtitleTextSize);
        sideSubtitleTextPaint.setColor(headerColumnTextColor);
        sideSubtitleTextPaint.setTypeface(typeface);

        //handle allDaySideTitleTextPaint
        allDaySideTitleTextPaint.setTextSize(textSize);
        allDaySideTitleTextPaint.setColor(allDaySideTitleTextColor);
        allDaySideTitleTextPaint.setTypeface(typeface);

        // Measure settings for header row.
        //TODO measure the text that will actually be used, based on the locale and dates. Important because various characters might look different.
        String sampleText = "ABCDEFGHIKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz0123456789";
        mHeaderWeekDayTitleTextPaint.setColor(headerColumnTextColor);
        mHeaderWeekDayTitleTextPaint.setTextSize(headerWeekDayTitleTextSize);
        mHeaderWeekDayTitleTextPaint.setTypeface(typeface);
        mHeaderWeekDayTitleTextPaint.getTextBounds(sampleText, 0, sampleText.length(), rect);
        headerWeekDayTitleTextHeight = rect.height();

        //measure settings for header subtitle
        mHeaderWeekDaySubtitleTextPaint.setColor(headerColumnTextColor);
        mHeaderWeekDaySubtitleTextPaint.setTextSize(headerWeekDaySubtitleTextSize);
        mHeaderWeekDaySubtitleTextPaint.setTypeface(typeface);
        mHeaderWeekDaySubtitleTextPaint.getTextBounds(sampleText, 0, sampleText.length(), rect);
        headerWeekDaySubtitleTextHeight = rect.height();

        // Prepare header background paint.
        mHeaderBackgroundPaint.setColor(headerRowBackgroundColor);

        // Prepare day background color paint.
        mDayBackgroundPaint.setColor(dayBackgroundColor);
        mFutureBackgroundPaint.setColor(futureBackgroundColor);
        mPastBackgroundPaint.setColor(pastBackgroundColor);
        mFutureWeekendBackgroundPaint.setColor(futureWeekendBackgroundColor);
        mPastWeekendBackgroundPaint.setColor(pastWeekendBackgroundColor);

        // Prepare hour separator color paint.
        mHourSeparatorPaint.setStrokeWidth(hourSeparatorHeight);
        mHourSeparatorPaint.setColor(hourSeparatorColor);

        // Prepare the "now" line color paint
        mNowLinePaint.setStrokeWidth(nowLineThickness);
        mNowLinePaint.setColor(nowLineColor);

        // Prepare today background color paint.
        mTodayColumnBackgroundPaint.setColor(todayColumnBackgroundColor);

        // Prepare today header text color paint.

        mHeaderWeekDayTitleTodayTextPaint.setTextSize(headerWeekDayTitleTextSize);
        mHeaderWeekDayTitleTodayTextPaint.setTypeface(typeface);
        mHeaderWeekDayTitleTodayTextPaint.setColor(todayHeaderTextColor);

        mHeaderWeekDaySubtitleTodayTextPaint.setTextSize(headerWeekDaySubtitleTextSize);
        mHeaderWeekDaySubtitleTodayTextPaint.setTypeface(typeface);
        mHeaderWeekDaySubtitleTodayTextPaint.setColor(todayHeaderTextColor);

        // Prepare event background color.
        mEventBackgroundPaint.setColor(Color.rgb(174, 208, 238));
        // Prepare empty event background color.
        mNewEventBackgroundPaint.setColor(Color.rgb(60, 147, 217));

        // Prepare event text size and color.
        mEventTextPaint.setColor(eventTextColor);
        mEventTextPaint.setTextSize(eventTextSize);
        mEventTextPaint.setTypeface(typeface);


        // Set default event color.
        defaultEventColor = 0xff9fc6e7;
        // Set default empty event color.
        newEventColor = 0xff3c93d9;
    }

    // ... other methods ...
}

原文地址: http://www.cveoy.top/t/topic/ftKy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录