Skip to content

Commit

Permalink
代码精简,移除declare-styleable冗余;更新说明文档
Browse files Browse the repository at this point in the history
  • Loading branch information
liyujiang-gzu committed Nov 16, 2021
1 parent 0a6dbad commit db2375c
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 418 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# 更新日志

## 4.1.4 - 2021.11.16

- 时间选择器支持设置时分秒间隔,参阅 [issues#270](https://github.com/gzu-liyujiang/AndroidPicker/issues/270)
- 修复滚轮选择器设置选中项文字加大加粗可能导致错乱问题(注:建议通过`setStyle`定制样式设置文字加大,若通过`setSelectedTextSize`设置,该解决方案会导致选择器展示时跳动一下);
- 代码精简,移除`declare-styleable`冗余;

## 4.1.3 - 2021.11.07

Expand Down
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ picker.setOnOptionPickedListener(this);
//wheelLayout.setTextColor(0xFFFF00FF);
//wheelLayout.setSelectedTextColor(0xFFFF0000);
//wheelLayout.setTextSize(15 * view.getResources().getDisplayMetrics().scaledDensity);
//wheelLayout.setSelectedTextSize(17 * view.getResources().getDisplayMetrics().scaledDensity);
//wheelLayout.setSelectedTextBold(true);
//wheelLayout.setCurtainEnabled(true);
//wheelLayout.setCurtainColor(0xEEFF0000);
Expand Down Expand Up @@ -208,7 +207,6 @@ picker.setAddressMode(AddressMode.PROVINCE_CITY);
picker.setOnAddressPickedListener(this);
//LinkageWheelLayout wheelLayout = picker.getWheelLayout();
//wheelLayout.setTextSize(15 * view.getResources().getDisplayMetrics().scaledDensity);
//wheelLayout.setSelectedTextSize(17 * view.getResources().getDisplayMetrics().scaledDensity);
//wheelLayout.setSelectedTextBold(true);
//wheelLayout.setIndicatorEnabled(false);
//wheelLayout.setCurtainEnabled(true);
Expand Down Expand Up @@ -297,7 +295,17 @@ public class DemoApp extends Application {

#### 自定义 style

-`app/.../res/values/styles.xml`**重写`WheelDefault`覆盖** (所有选择器都会生效)
- **调用`setStyle`**(只作用于当前选择器,推荐)

`app/.../res/values/styles.xml`中参考`WheelDefault`写个style,然后设置。

```groovy
picker.getWheelView().setStyle(R.style.WheelStyleDemo);
```

- **重写`WheelDefault`覆盖** (所有选择器都会生效,不推荐)

`app/.../res/values/styles.xml`**重写`WheelDefault`覆盖**

```xml
<style name="WheelDefault">
Expand All @@ -320,12 +328,6 @@ public class DemoApp extends Application {
</style>
```

-`app/.../res/values/styles.xml`中参考`WheelDefault`写个style,然后**调用`setStyle`**(只作用于当前选择器)

```groovy
picker.getWheelView().setStyle(R.style.WheelStyleDemo);
```

#### 在Java中集成重写某一选择器样式及配色

```java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.StyleRes;
import androidx.annotation.StyleableRes;

import com.github.gzuliyujiang.dialog.DialogLog;
import com.github.gzuliyujiang.wheelpicker.R;
import com.github.gzuliyujiang.wheelview.annotation.CurtainCorner;
import com.github.gzuliyujiang.wheelview.annotation.ItemTextAlign;
Expand All @@ -47,49 +45,34 @@
@SuppressWarnings("unused")
public abstract class BaseWheelLayout extends LinearLayout implements OnWheelChangedListener {
private final List<WheelView> wheelViews = new ArrayList<>();
private AttributeSet attrs;

public BaseWheelLayout(Context context) {
super(context);
init(context, null);
TypedArray a = context.obtainStyledAttributes(null, provideStyleableRes(),
R.attr.WheelStyle, R.style.WheelDefault);
onAttributeSet(context, a);
init(context, null, R.attr.WheelStyle, R.style.WheelDefault);
}

public BaseWheelLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, provideStyleableRes(),
R.attr.WheelStyle, R.style.WheelDefault);
onAttributeSet(context, a);
a.recycle();
init(context, attrs, R.attr.WheelStyle, R.style.WheelDefault);
}

public BaseWheelLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, provideStyleableRes(),
defStyleAttr, R.style.WheelDefault);
onAttributeSet(context, a);
a.recycle();
init(context, attrs, defStyleAttr, R.style.WheelDefault);
}

public BaseWheelLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr);
init(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, provideStyleableRes(),
defStyleAttr, defStyleRes);
onAttributeSet(context, a);
a.recycle();
init(context, attrs, defStyleAttr, defStyleRes);
}

private void init(Context context, AttributeSet attrs) {
this.attrs = attrs;
private void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
setOrientation(VERTICAL);
inflate(context, provideLayoutRes(), this);
onInit(context);
wheelViews.clear();
wheelViews.addAll(provideWheelViews());
initAttrs(context, attrs, defStyleAttr, defStyleRes);
for (WheelView wheelView : wheelViews) {
wheelView.setOnWheelChangedListener(this);
}
Expand All @@ -99,26 +82,47 @@ protected void onInit(@NonNull Context context) {

}

protected void onAttributeSet(@NonNull Context context, @NonNull TypedArray typedArray) {
private void initAttrs(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
float density = context.getResources().getDisplayMetrics().density;
float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BaseWheelLayout, defStyleAttr, defStyleRes);
setVisibleItemCount(typedArray.getInt(R.styleable.BaseWheelLayout_wheel_visibleItemCount, 5));
setSameWidthEnabled(typedArray.getBoolean(R.styleable.BaseWheelLayout_wheel_sameWidthEnabled, false));
setMaxWidthText(typedArray.getString(R.styleable.BaseWheelLayout_wheel_maxWidthText));
setTextColor(typedArray.getColor(R.styleable.BaseWheelLayout_wheel_itemTextColor, 0xFF888888));
setSelectedTextColor(typedArray.getColor(R.styleable.BaseWheelLayout_wheel_itemTextColorSelected, 0xFF000000));
setTextSize(typedArray.getDimension(R.styleable.BaseWheelLayout_wheel_itemTextSize, 15 * scaledDensity));
setSelectedTextSize(typedArray.getDimension(R.styleable.BaseWheelLayout_wheel_itemTextSizeSelected, 15 * scaledDensity));
setSelectedTextBold(typedArray.getBoolean(R.styleable.BaseWheelLayout_wheel_itemTextBoldSelected, false));
setTextAlign(typedArray.getInt(R.styleable.BaseWheelLayout_wheel_itemTextAlign, ItemTextAlign.CENTER));
setItemSpace(typedArray.getDimensionPixelSize(R.styleable.BaseWheelLayout_wheel_itemSpace, (int) (20 * density)));
setCyclicEnabled(typedArray.getBoolean(R.styleable.BaseWheelLayout_wheel_cyclicEnabled, false));
setIndicatorEnabled(typedArray.getBoolean(R.styleable.BaseWheelLayout_wheel_indicatorEnabled, false));
setIndicatorColor(typedArray.getColor(R.styleable.BaseWheelLayout_wheel_indicatorColor, 0xFFC9C9C9));
setIndicatorSize(typedArray.getDimension(R.styleable.BaseWheelLayout_wheel_indicatorSize, 1 * density));
setCurvedIndicatorSpace(typedArray.getDimensionPixelSize(R.styleable.BaseWheelLayout_wheel_curvedIndicatorSpace, (int) (1 * density)));
setCurtainEnabled(typedArray.getBoolean(R.styleable.BaseWheelLayout_wheel_curtainEnabled, false));
setCurtainColor(typedArray.getColor(R.styleable.BaseWheelLayout_wheel_curtainColor, 0x88FFFFFF));
setCurtainCorner(typedArray.getInt(R.styleable.BaseWheelLayout_wheel_curtainCorner, CurtainCorner.NONE));
setCurtainRadius(typedArray.getDimension(R.styleable.BaseWheelLayout_wheel_curtainRadius, 0));
setAtmosphericEnabled(typedArray.getBoolean(R.styleable.BaseWheelLayout_wheel_atmosphericEnabled, false));
setCurvedEnabled(typedArray.getBoolean(R.styleable.BaseWheelLayout_wheel_curvedEnabled, false));
setCurvedMaxAngle(typedArray.getInteger(R.styleable.BaseWheelLayout_wheel_curvedMaxAngle, 90));
typedArray.recycle();
onAttributeSet(context, attrs);
}

protected void onAttributeSet(@NonNull Context context, @Nullable AttributeSet attrs) {

}

@LayoutRes
protected abstract int provideLayoutRes();

@StyleableRes
protected abstract int[] provideStyleableRes();

protected abstract List<WheelView> provideWheelViews();

public void setStyle(@StyleRes int style) {
if (attrs == null) {
DialogLog.print("Please use " + getClass().getSimpleName() + " in xml");
return;
}
TypedArray a = getContext().obtainStyledAttributes(attrs, provideStyleableRes(), R.attr.WheelStyle, style);
onAttributeSet(getContext(), a);
a.recycle();
initAttrs(getContext(), null, R.attr.WheelStyle, style);
requestLayout();
invalidate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
package com.github.gzuliyujiang.wheelpicker.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.gzuliyujiang.wheelpicker.impl.CarPlateProvider;

Expand Down Expand Up @@ -52,8 +52,7 @@ protected void onInit(@NonNull Context context) {
}

@Override
protected void onAttributeSet(@NonNull Context context, @NonNull TypedArray typedArray) {
super.onAttributeSet(context, typedArray);
protected void onAttributeSet(@NonNull Context context, @Nullable AttributeSet attrs) {
setFirstVisible(provider.firstLevelVisible());
setThirdVisible(provider.thirdLevelVisible());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.gzuliyujiang.wheelpicker.R;
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode;
import com.github.gzuliyujiang.wheelpicker.contract.DateFormatter;
import com.github.gzuliyujiang.wheelpicker.contract.OnDateSelectedListener;
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity;
import com.github.gzuliyujiang.wheelpicker.impl.SimpleDateFormatter;
import com.github.gzuliyujiang.wheelview.annotation.CurtainCorner;
import com.github.gzuliyujiang.wheelview.annotation.ItemTextAlign;
import com.github.gzuliyujiang.wheelview.annotation.ScrollState;
import com.github.gzuliyujiang.wheelview.contract.WheelFormatter;
import com.github.gzuliyujiang.wheelview.widget.NumberWheelView;
Expand Down Expand Up @@ -80,11 +79,6 @@ protected int provideLayoutRes() {
return R.layout.wheel_picker_date;
}

@Override
protected int[] provideStyleableRes() {
return R.styleable.DateWheelLayout;
}

@Override
protected List<WheelView> provideWheelViews() {
return Arrays.asList(yearWheelView, monthWheelView, dayWheelView);
Expand All @@ -101,35 +95,13 @@ protected void onInit(@NonNull Context context) {
}

@Override
protected void onAttributeSet(@NonNull Context context, @NonNull TypedArray typedArray) {
float density = context.getResources().getDisplayMetrics().density;
float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
setVisibleItemCount(typedArray.getInt(R.styleable.DateWheelLayout_wheel_visibleItemCount, 5));
setSameWidthEnabled(typedArray.getBoolean(R.styleable.DateWheelLayout_wheel_sameWidthEnabled, false));
setMaxWidthText(typedArray.getString(R.styleable.DateWheelLayout_wheel_maxWidthText));
setTextColor(typedArray.getColor(R.styleable.DateWheelLayout_wheel_itemTextColor, 0xFF888888));
setSelectedTextColor(typedArray.getColor(R.styleable.DateWheelLayout_wheel_itemTextColorSelected, 0xFF000000));
setTextSize(typedArray.getDimension(R.styleable.DateWheelLayout_wheel_itemTextSize, 15 * scaledDensity));
setSelectedTextSize(typedArray.getDimension(R.styleable.DateWheelLayout_wheel_itemTextSizeSelected, 15 * scaledDensity));
setSelectedTextBold(typedArray.getBoolean(R.styleable.DateWheelLayout_wheel_itemTextBoldSelected, false));
setTextAlign(typedArray.getInt(R.styleable.DateWheelLayout_wheel_itemTextAlign, ItemTextAlign.CENTER));
setItemSpace(typedArray.getDimensionPixelSize(R.styleable.DateWheelLayout_wheel_itemSpace, (int) (20 * density)));
setCyclicEnabled(typedArray.getBoolean(R.styleable.DateWheelLayout_wheel_cyclicEnabled, false));
setIndicatorEnabled(typedArray.getBoolean(R.styleable.DateWheelLayout_wheel_indicatorEnabled, false));
setIndicatorColor(typedArray.getColor(R.styleable.DateWheelLayout_wheel_indicatorColor, 0xFFC9C9C9));
setIndicatorSize(typedArray.getDimension(R.styleable.DateWheelLayout_wheel_indicatorSize, 1 * density));
setCurvedIndicatorSpace(typedArray.getDimensionPixelSize(R.styleable.DateWheelLayout_wheel_curvedIndicatorSpace, (int) (1 * density)));
setCurtainEnabled(typedArray.getBoolean(R.styleable.DateWheelLayout_wheel_curtainEnabled, false));
setCurtainColor(typedArray.getColor(R.styleable.DateWheelLayout_wheel_curtainColor, 0x88FFFFFF));
setCurtainCorner(typedArray.getInt(R.styleable.DateWheelLayout_wheel_curtainCorner, CurtainCorner.NONE));
setCurtainRadius(typedArray.getDimension(R.styleable.DateWheelLayout_wheel_curtainRadius, 0));
setAtmosphericEnabled(typedArray.getBoolean(R.styleable.DateWheelLayout_wheel_atmosphericEnabled, false));
setCurvedEnabled(typedArray.getBoolean(R.styleable.DateWheelLayout_wheel_curvedEnabled, false));
setCurvedMaxAngle(typedArray.getInteger(R.styleable.DateWheelLayout_wheel_curvedMaxAngle, 90));
protected void onAttributeSet(@NonNull Context context, @Nullable AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DateWheelLayout);
setDateMode(typedArray.getInt(R.styleable.DateWheelLayout_wheel_dateMode, DateMode.YEAR_MONTH_DAY));
String yearLabel = typedArray.getString(R.styleable.DateWheelLayout_wheel_yearLabel);
String monthLabel = typedArray.getString(R.styleable.DateWheelLayout_wheel_monthLabel);
String dayLabel = typedArray.getString(R.styleable.DateWheelLayout_wheel_dayLabel);
typedArray.recycle();
setDateLabel(yearLabel, monthLabel, dayLabel);
setDateFormatter(new SimpleDateFormatter());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.gzuliyujiang.wheelpicker.R;
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode;
Expand All @@ -30,8 +31,6 @@
import com.github.gzuliyujiang.wheelpicker.entity.DatimeEntity;
import com.github.gzuliyujiang.wheelpicker.impl.SimpleDateFormatter;
import com.github.gzuliyujiang.wheelpicker.impl.SimpleTimeFormatter;
import com.github.gzuliyujiang.wheelview.annotation.CurtainCorner;
import com.github.gzuliyujiang.wheelview.annotation.ItemTextAlign;
import com.github.gzuliyujiang.wheelview.annotation.ScrollState;
import com.github.gzuliyujiang.wheelview.widget.NumberWheelView;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
Expand Down Expand Up @@ -74,11 +73,6 @@ protected int provideLayoutRes() {
return R.layout.wheel_picker_datime;
}

@Override
protected int[] provideStyleableRes() {
return R.styleable.DatimeWheelLayout;
}

@Override
protected List<WheelView> provideWheelViews() {
List<WheelView> list = new ArrayList<>();
Expand All @@ -94,32 +88,8 @@ protected void onInit(@NonNull Context context) {
}

@Override
protected void onAttributeSet(@NonNull Context context, @NonNull TypedArray typedArray) {
float density = context.getResources().getDisplayMetrics().density;
float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
setVisibleItemCount(typedArray.getInt(R.styleable.DatimeWheelLayout_wheel_visibleItemCount, 5));
setSameWidthEnabled(typedArray.getBoolean(R.styleable.DatimeWheelLayout_wheel_sameWidthEnabled, false));
setMaxWidthText(typedArray.getString(R.styleable.DatimeWheelLayout_wheel_maxWidthText));
setSelectedTextColor(typedArray.getColor(R.styleable.DatimeWheelLayout_wheel_itemTextColorSelected, 0xFF000000));
setTextColor(typedArray.getColor(R.styleable.DatimeWheelLayout_wheel_itemTextColor, 0xFF888888));
setTextSize(typedArray.getDimension(R.styleable.DatimeWheelLayout_wheel_itemTextSize, 15 * scaledDensity));
setSelectedTextSize(typedArray.getDimension(R.styleable.DatimeWheelLayout_wheel_itemTextSizeSelected, 15 * scaledDensity));
setSelectedTextBold(typedArray.getBoolean(R.styleable.DatimeWheelLayout_wheel_itemTextBoldSelected, false));
setTextAlign(typedArray.getInt(R.styleable.DatimeWheelLayout_wheel_itemTextAlign, ItemTextAlign.CENTER));
setItemSpace(typedArray.getDimensionPixelSize(R.styleable.DatimeWheelLayout_wheel_itemSpace,
(int) (20 * density)));
setCyclicEnabled(typedArray.getBoolean(R.styleable.DatimeWheelLayout_wheel_cyclicEnabled, false));
setIndicatorEnabled(typedArray.getBoolean(R.styleable.DatimeWheelLayout_wheel_indicatorEnabled, false));
setIndicatorColor(typedArray.getColor(R.styleable.DatimeWheelLayout_wheel_indicatorColor, 0xFFC9C9C9));
setIndicatorSize(typedArray.getDimension(R.styleable.DatimeWheelLayout_wheel_indicatorSize, 1 * density));
setCurvedIndicatorSpace(typedArray.getDimensionPixelSize(R.styleable.DatimeWheelLayout_wheel_curvedIndicatorSpace, (int) (1 * density)));
setCurtainEnabled(typedArray.getBoolean(R.styleable.DatimeWheelLayout_wheel_curtainEnabled, false));
setCurtainColor(typedArray.getColor(R.styleable.DatimeWheelLayout_wheel_curtainColor, 0x88FFFFFF));
setCurtainCorner(typedArray.getInt(R.styleable.DatimeWheelLayout_wheel_curtainCorner, CurtainCorner.NONE));
setCurtainRadius(typedArray.getDimension(R.styleable.DatimeWheelLayout_wheel_curtainRadius, 0));
setAtmosphericEnabled(typedArray.getBoolean(R.styleable.DatimeWheelLayout_wheel_atmosphericEnabled, false));
setCurvedEnabled(typedArray.getBoolean(R.styleable.DatimeWheelLayout_wheel_curvedEnabled, false));
setCurvedMaxAngle(typedArray.getInteger(R.styleable.DatimeWheelLayout_wheel_curvedMaxAngle, 90));
protected void onAttributeSet(@NonNull Context context, @Nullable AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DatimeWheelLayout);
setDateMode(typedArray.getInt(R.styleable.DatimeWheelLayout_wheel_dateMode, DateMode.YEAR_MONTH_DAY));
setTimeMode(typedArray.getInt(R.styleable.DatimeWheelLayout_wheel_timeMode, TimeMode.HOUR_24_NO_SECOND));
String yearLabel = typedArray.getString(R.styleable.DatimeWheelLayout_wheel_yearLabel);
Expand All @@ -129,6 +99,7 @@ protected void onAttributeSet(@NonNull Context context, @NonNull TypedArray type
String hourLabel = typedArray.getString(R.styleable.DatimeWheelLayout_wheel_hourLabel);
String minuteLabel = typedArray.getString(R.styleable.DatimeWheelLayout_wheel_minuteLabel);
String secondLabel = typedArray.getString(R.styleable.DatimeWheelLayout_wheel_secondLabel);
typedArray.recycle();
setTimeLabel(hourLabel, minuteLabel, secondLabel);
setDateFormatter(new SimpleDateFormatter());
setTimeFormatter(new SimpleTimeFormatter(timeWheelLayout));
Expand Down
Loading

0 comments on commit db2375c

Please sign in to comment.