/*
* Copyright (C) 2011 Daniel Berndt - Codeus Ltd - DateSlider
*
* This class contains all the scrolling logic of the slidable elements
*
* 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.googlecode.android.widgets.DateSlider;
import java.lang.reflect.Constructor;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Scroller;
import com.googlecode.android.widgets.DateSlider.labeler.Labeler;
import com.googlecode.android.widgets.DateSlider.timeview.TimeView;
/**
* This is where most of the magic happens. This is a subclass of LinearLayout
* that display a collection of TimeViews and handles the scrolling, shuffling
* the TimeViews around to keep the display up-to-date, and managing the Labelers
* to populate the TimeViews with the correct data.
*
* This class is configured via xml attributes that specify the class of the
* labeler to use to generate views, the format string for the labeler to use
* to populate the views, and optionally width and height values to override
* the default width and height of the views.
*/
public class ScrollLayout extends LinearLayout {
private static String TAG = "SCROLLLAYOUT";
private Scroller mScroller;
/**
* Indicates if we are currently tracking touch events that are dragging
* (scrolling) us.
*/
private boolean mDragMode;
/**
* The aggregate width of all of our children
*/
private int childrenWidth;
/**
* The aggregate width of our children is very likely to be wider than the
* bounds of our view. Since we keep everything centered, we need to keep
* our view scrolled by enough to center our children, rather than
* left-aligning them. This variable tracks how much to scroll to achieve this.
*/
private int mInitialOffset;
private int mLastX, mLastScroll, mScrollX;
private VelocityTracker mVelocityTracker;
private int mMinimumVelocity, mMaximumVelocity;
/**
* The time that we are currently displaying
*/
private long currentTime = System.currentTimeMillis();
private long minTime = -1, maxTime = -1;
private int minuteInterval = 1;
/**
* The width of each child
*/
private int objWidth;
/**
* The height of each child
*/
private int objHeight;
private Drawable rightShadow, leftShadow;
private Labeler mLabeler;
private OnScrollListener listener;
private TimeView mCenterView;
public ScrollLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.setWillNotDraw(false);
rightShadow = getContext().getResources().getDrawable(R.drawable.right_shadow);
leftShadow = getContext().getResources().getDrawable(R.drawable.left_shadow);
mScroller = new Scroller(getContext());
setGravity(Gravity.CENTER_VERTICAL);
setOrientation(HORIZONTAL);
final ViewConfiguration configuration = ViewConfiguration.get(getContext());
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
// as mMaximumVelocity does not exist in API<4
float density = getContext().getResources().getDisplayMetrics().density;
mMaximumVelocity = (int)(4000 * 0.5f * density);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ScrollLayout,
0, 0);
// Get the labeler class and construct an instance
String className = a.getNonResourceString(R.styleable.ScrollLayout_labelerClass);
if (className == null) {
throw new RuntimeException("Must specify labeler class at " + a.getPositionDescription());
}
String labelerFormat = a.getString(R.styleable.ScrollLayout_labelerFormat);
if (labelerFormat == null) {
throw new RuntimeException("Must specify labelerFormat at " + a.getPositionDescription());
}
try {
Class<?> klazz = Class.forName(className);
Constructor<?> ctor = klazz.getConstructor(String.class);
mLabeler = (Labeler)ctor.newInstance(labelerFormat);
} catch (Exception e) {
throw new RuntimeException("Failed to construct labeler at " + a.getPositionDescription(), e);
}
// Determine the width and height of our children, using the labelers preferred
// values as defaults
objWidth = a.getDimensionPixelSize(R.styleable.ScrollLayout_childWidth,
mLabeler.getPreferredViewWidth(context));
objHeight = a.getDimensionPixelSize(R.styleable.ScrollLayout_childHeight,
mLabeler.getPreferredViewHeight(context));
a.recycle();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//
// We need to generate enough children to fill all of our desired space, and
// it needs to be an odd number of children because we treat the center view
// specially. So, first compute how many children we will need.
//
final Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
final int displayWidth = display.getWidth();
int childCount = displayWidth / objWidth;
// Make sure to round up
if (displayWidth % objWidth != 0) {
childCount++;
}
// Now make sure we have an odd number of children
if (childCount % 2 == 0) {
childCount++;
}
// We have an odd number of children, so childCount / 2 will round down to the
// index just before the center in 1-based indexing, meaning that it will be the
// center index in 0-based indexing.
final int centerIndex = (childCount / 2);
// Make sure we weren't inflated with any views for some odd reason
removeAllViews();
// Now add all of the child views, making sure to make the center view as such.
for (int i = 0; i < childCount; i++) {
LayoutParams lp = new LayoutParams(objWidth, objHeight);
TimeView ttv = mLabeler.createView(getContext(), i == centerIndex);
addView((View)ttv, lp);
}
// Now we need to set the times on all of the TimeViews. We start with the center
// view, work our way to the end, then starting from the center again, work our
// way back to the beginning.
mCenterView = (TimeView)getChildAt(centerIndex);
mCenterView.setVals(mLabeler.getElem(currentTime));
Log.v(TAG, "mCenter: " + mCenterView.getTimeText() + " minInterval " + minuteInterval);
// TODO: Do I need to use endTime, or can I just use the point time?
for (int i = centerIndex + 1; i < childCount; i++) {
TimeView lastView = (TimeView)getChildAt(i - 1);
TimeView thisView = (TimeView)getChildAt(i);
thisView.setVals(mLabeler.add(lastView.getEndTime(), 1));
}
for (int i = centerIndex - 1; i >= 0; i--) {
TimeVi
没有合适的资源?快使用搜索试试~ 我知道了~
小程序 非常漂亮的滚动选择日期控件.rar
共50个文件
java:27个
xml:17个
readme:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 153 浏览量
2023-02-03
11:44:58
上传
评论
收藏 75KB RAR 举报
温馨提示
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,本人不对所涉及的版权问题或内容负法律责任。如有侵权,请举报或通知本人删除。
资源推荐
资源详情
资源评论
收起资源包目录
小程序 非常漂亮的滚动选择日期控件.rar (50个子文件)
1-120R3211J00-L.jpg 24KB
Android 非常漂亮的滚动选择日期控件
bendemboski-DateSlider-3cbbd0e
README 3KB
src
com
googlecode
android
widgets
DateSlider
TimeSlider.java 2KB
timeview
TimeTextView.java 2KB
DayTimeLayoutView.java 3KB
TimeView.java 2KB
TimeLayoutView.java 4KB
SliderContainer.java 5KB
MinimalDemo.java 3KB
DefaultDateSlider.java 1KB
CustomDateSlider.java 1KB
labeler
MonthLabeler.java 659B
MinuteLabeler.java 841B
Labeler.java 4KB
HourLabeler.java 654B
DayLabeler.java 649B
Util.java 6KB
TimeLabeler.java 1KB
DayDateLabeler.java 844B
MonthYearLabeler.java 846B
YearLabeler.java 655B
WeekLabeler.java 3KB
AlternativeDateSlider.java 1KB
DateTimeSlider.java 2KB
ScrollLayout.java 20KB
DateSlider.java 6KB
TimeObject.java 456B
Demo.java 8KB
MonthYearDateSlider.java 2KB
LICENSE 635B
res
values
strings.xml 178B
attrs.xml 345B
styles.xml 352B
layout
dialogtitle.xml 514B
dialogbuttons.xml 768B
main.xml 2KB
customdateslider.xml 1KB
altdateslider.xml 1KB
timeslider.xml 975B
defaultdateslider.xml 1012B
datetimeslider.xml 1KB
simple_main.xml 811B
monthyeardateslider.xml 976B
drawable
right_shadow.xml 251B
left_shadow.xml 250B
slider_back.xml 538B
icon.png 4KB
proguard.cfg 1KB
default.properties 362B
AndroidManifest.xml 985B
共 50 条
- 1
资源评论
大富大贵7
- 粉丝: 322
- 资源: 8869
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功