* 数据来源:
* 1、国家民政局:http://www.mca.gov.cn/article/sj/xzqh
* 2、国家统计局:http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm
* 3、台湾维基数据:https://zh.wikipedia.org/wiki/中华人民共和国行政区划代码_(7区)
* 4、港澳维基数据:https://zh.wikipedia.org/wiki/中华人民共和国行政区划代码_(8区)
* 5、数据抓取转化:https://github.com/small-dream/China_Province_City
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/15 12:17
*/
@SuppressWarnings({"unused"})
public class AddressPicker extends LinkagePicker implements AddressReceiver {
private AddressLoader addressLoader;
private AddressParser addressParser;
private int addressMode;
private OnAddressPickedListener onAddressPickedListener;
private OnAddressLoadListener onAddressLoadListener;
public AddressPicker(@NonNull Activity activity) {
super(activity);
}
public AddressPicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@Override
protected void initData() {
super.initData();
if (addressLoader == null || addressParser == null) {
return;
}
wheelLayout.showLoading();
if (onAddressLoadListener != null) {
onAddressLoadListener.onAddressLoadStarted();
}
DialogLog.print("Address data loading");
addressLoader.loadJson(this, addressParser);
}
@Override
public void onAddressReceived(@NonNull List data) {
DialogLog.print("Address data received");
wheelLayout.hideLoading();
if (onAddressLoadListener != null) {
onAddressLoadListener.onAddressLoadFinished(data);
}
wheelLayout.setData(new AddressProvider(data, addressMode));
}
@Deprecated
@Override
public void setData(@NonNull LinkageProvider data) {
throw new UnsupportedOperationException("Use setAddressMode or setAddressLoader instead");
}
@Deprecated
@Override
public void setOnLinkagePickedListener(OnLinkagePickedListener onLinkagePickedListener) {
throw new UnsupportedOperationException("Use setOnAddressPickedListener instead");
}
@Override
protected void onOk() {
if (onAddressPickedListener != null) {
ProvinceEntity province = wheelLayout.getFirstWheelView().getCurrentItem();
CityEntity city = wheelLayout.getSecondWheelView().getCurrentItem();
CountyEntity county = wheelLayout.getThirdWheelView().getCurrentItem();
onAddressPickedListener.onAddressPicked(province, city, county);
}
}
public void setOnAddressPickedListener(@NonNull OnAddressPickedListener onAddressPickedListener) {
this.onAddressPickedListener = onAddressPickedListener;
}
public void setOnAddressLoadListener(@NonNull OnAddressLoadListener onAddressLoadListener) {
this.onAddressLoadListener = onAddressLoadListener;
}
public void setAddressLoader(@NonNull AddressLoader loader, @NonNull AddressParser parser) {
this.addressLoader = loader;
this.addressParser = parser;
}
public void setAddressMode(@AddressMode int addressMode) {
setAddressMode("china_address.json", addressMode);
}
public void setAddressMode(@NonNull String assetPath, @AddressMode int addressMode) {
setAddressMode(assetPath, addressMode, new AddressJsonParser());
}
public void setAddressMode(@NonNull String assetPath, @AddressMode int addressMode,
@NonNull AddressJsonParser jsonParser) {
this.addressMode = addressMode;
setAddressLoader(new AssetAddressLoader(getContext(), assetPath), jsonParser);
}
public final WheelView getProvinceWheelView() {
return wheelLayout.getFirstWheelView();
}
public final WheelView getCityWheelView() {
return wheelLayout.getSecondWheelView();
}
public final WheelView getCountyWheelView() {
return wheelLayout.getThirdWheelView();
}
public final TextView getProvinceLabelView() {
return wheelLayout.getFirstLabelView();
}
public final TextView getCityLabelView() {
return wheelLayout.getSecondLabelView();
}
public final TextView getCountyLabelView() {
return wheelLayout.getThirdLabelView();
}
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/annotation/AddressMode.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* 地址模式
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/15 12:19
*/
@Retention(RetentionPolicy.SOURCE)
public @interface AddressMode {
/**
* 省级、市级、县级
*/
int PROVINCE_CITY_COUNTY = 0;
/**
* 省级、市级
*/
int PROVINCE_CITY = 1;
/**
* 市级、县级
*/
int CITY_COUNTY = 2;
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/AddressLoader.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
/**
* 地址数据加载器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 16:42
*/
public interface AddressLoader {
@MainThread
void loadJson(@NonNull AddressReceiver receiver, @NonNull AddressParser parser);
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/AddressParser.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import com.github.gzuliyujiang.wheelpicker.entity.ProvinceEntity;
import java.util.List;
/**
* 地址数据解析器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/12 16:53
*/
public interface AddressParser {
@WorkerThread
@NonNull
List parseData(@NonNull String text);
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/AddressReceiver.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.wheelpicker.entity.ProvinceEntity;
import java.util.List;
/**
* 地址数据接收器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 16:42
*/
public interface AddressReceiver {
@MainThread
void onAddressReceived(@NonNull List data);
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnAddressLoadListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.wheelpicker.entity.ProvinceEntity;
import java.util.List;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/30 15:20
*/
public interface OnAddressLoadListener {
void onAddressLoadStarted();
void onAddressLoadFinished(@NonNull List data);
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnAddressPickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
import com.github.gzuliyujiang.wheelpicker.entity.CityEntity;
import com.github.gzuliyujiang.wheelpicker.entity.CountyEntity;
import com.github.gzuliyujiang.wheelpicker.entity.ProvinceEntity;
/**
* 地址选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 18:23
*/
public interface OnAddressPickedListener {
/**
* 联动选择回调
*
* @param province 选中的省/直辖市/自治区
* @param city 选中的地级市/自治州
* @param county 选中的区/县/县级市
*/
void onAddressPicked(ProvinceEntity province, CityEntity city, CountyEntity county);
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/entity/AddressEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.entity;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.wheelview.contract.TextProvider;
import java.io.Serializable;
import java.util.Objects;
/**
* 地址数据实体
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 11:47
*/
class AddressEntity implements TextProvider, Serializable {
private String code;
private String name;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String provideText() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AddressEntity that = (AddressEntity) o;
return Objects.equals(code, that.code) &&
Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(code, name);
}
@NonNull
@Override
public String toString() {
return "AddressEntity{" +
"code='" + code + '\'' +
", name='" + name + '\'' +
'}';
}
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/entity/CityEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.entity;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
/**
* 市级数据实体
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 11:47
*/
public class CityEntity extends AddressEntity {
private List countyList;
@NonNull
public List getCountyList() {
if (countyList == null) {
countyList = new ArrayList<>();
}
return countyList;
}
public void setCountyList(List countyList) {
this.countyList = countyList;
}
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/entity/CountyEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.entity;
/**
* 县级数据实体
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 11:47
*/
public class CountyEntity extends AddressEntity {
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/entity/ProvinceEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.entity;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
/**
* 省级数据实体
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 11:47
*/
public class ProvinceEntity extends AddressEntity {
private List cityList;
@NonNull
public List getCityList() {
if (cityList == null) {
cityList = new ArrayList<>();
}
return cityList;
}
public void setCityList(List cityList) {
this.cityList = cityList;
}
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/impl/AddressProvider.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.impl;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode;
import com.github.gzuliyujiang.wheelpicker.contract.LinkageProvider;
import com.github.gzuliyujiang.wheelpicker.entity.CityEntity;
import com.github.gzuliyujiang.wheelpicker.entity.CountyEntity;
import com.github.gzuliyujiang.wheelpicker.entity.ProvinceEntity;
import java.util.ArrayList;
import java.util.List;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/7 11:55
*/
public class AddressProvider implements LinkageProvider {
private final List data;
private final int mode;
public AddressProvider(@NonNull List data, int mode) {
this.data = data;
this.mode = mode;
}
@Override
public boolean firstLevelVisible() {
return mode == AddressMode.PROVINCE_CITY_COUNTY || mode == AddressMode.PROVINCE_CITY;
}
@Override
public boolean thirdLevelVisible() {
return mode == AddressMode.PROVINCE_CITY_COUNTY || mode == AddressMode.CITY_COUNTY;
}
@NonNull
@Override
public List provideFirstData() {
return data;
}
@NonNull
@Override
public List linkageSecondData(int firstIndex) {
if (data.size() == 0) {
return new ArrayList<>();
}
if (firstIndex == INDEX_NO_FOUND) {
firstIndex = 0;
}
return data.get(firstIndex).getCityList();
}
@NonNull
@Override
public List linkageThirdData(int firstIndex, int secondIndex) {
List data = linkageSecondData(firstIndex);
if (data.size() == 0) {
return new ArrayList<>();
}
if (secondIndex == INDEX_NO_FOUND) {
secondIndex = 0;
}
return data.get(secondIndex).getCountyList();
}
@Override
public int findFirstIndex(Object firstValue) {
if (firstValue == null) {
return INDEX_NO_FOUND;
}
if (firstValue instanceof ProvinceEntity) {
return data.indexOf(firstValue);
}
for (int i = 0, n = data.size(); i < n; i++) {
ProvinceEntity entity = data.get(i);
if (entity.getCode().equals(firstValue.toString()) ||
entity.getName().contains(firstValue.toString())) {
return i;
}
}
return INDEX_NO_FOUND;
}
@Override
public int findSecondIndex(int firstIndex, Object secondValue) {
if (secondValue == null) {
return INDEX_NO_FOUND;
}
List cityList = linkageSecondData(firstIndex);
if (secondValue instanceof CityEntity) {
return cityList.indexOf(secondValue);
}
for (int i = 0, n = cityList.size(); i < n; i++) {
CityEntity entity = cityList.get(i);
if (entity.getCode().equals(secondValue.toString()) ||
entity.getName().contains(secondValue.toString())) {
return i;
}
}
return INDEX_NO_FOUND;
}
@Override
public int findThirdIndex(int firstIndex, int secondIndex, Object thirdValue) {
if (thirdValue == null) {
return INDEX_NO_FOUND;
}
List countyList = linkageThirdData(firstIndex, secondIndex);
if (thirdValue instanceof CountyEntity) {
return countyList.indexOf(thirdValue);
}
for (int i = 0, n = countyList.size(); i < n; i++) {
CountyEntity entity = countyList.get(i);
if (entity.getCode().equals(thirdValue.toString()) ||
entity.getName().contains(thirdValue.toString())) {
return i;
}
}
return INDEX_NO_FOUND;
}
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/impl/AssetAddressLoader.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.impl;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import com.github.gzuliyujiang.wheelpicker.contract.AddressLoader;
import com.github.gzuliyujiang.wheelpicker.contract.AddressParser;
import com.github.gzuliyujiang.wheelpicker.contract.AddressReceiver;
import com.github.gzuliyujiang.wheelpicker.entity.ProvinceEntity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
/**
* 从APK包中的“assets”目录下加载地址数据
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 17:15
*/
@SuppressWarnings("unused")
public class AssetAddressLoader implements AddressLoader {
private final Context context;
private final String path;
public AssetAddressLoader(@NonNull Context context, @NonNull String path) {
this.context = context;
this.path = path;
}
@Override
public void loadJson(@NonNull final AddressReceiver receiver, @NonNull final AddressParser parser) {
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
String text = loadFromAssets();
final List data;
if (TextUtils.isEmpty(text)) {
data = new ArrayList<>();
} else {
data = parser.parseData(text);
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
receiver.onAddressReceived(data);
}
});
}
});
}
@WorkerThread
private String loadFromAssets() {
StringBuilder stringBuilder = new StringBuilder();
AssetManager am = context.getAssets();
try (BufferedReader bf = new BufferedReader(new InputStreamReader(am.open(path)))) {
String line;
while ((line = bf.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
return "";
}
return stringBuilder.toString();
}
}
================================================
FILE: AddressPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/utility/AddressJsonParser.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.utility;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.dialog.DialogLog;
import com.github.gzuliyujiang.wheelpicker.contract.AddressParser;
import com.github.gzuliyujiang.wheelpicker.entity.CityEntity;
import com.github.gzuliyujiang.wheelpicker.entity.CountyEntity;
import com.github.gzuliyujiang.wheelpicker.entity.ProvinceEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* 使用系统自带的“org.json”包转换地址数据
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 17:15
*/
public class AddressJsonParser implements AddressParser {
private final Builder builder;
public AddressJsonParser() {
this(new Builder());
}
public AddressJsonParser(Builder builder) {
this.builder = builder;
}
@NonNull
@Override
public List parseData(@NonNull String text) {
try {
JSONArray provinceArray = new JSONArray(text);
return parseProvince(provinceArray);
} catch (JSONException e) {
DialogLog.print(e);
}
return new ArrayList<>();
}
private List parseProvince(JSONArray provinceArray) {
List data = new ArrayList<>();
for (int i = 0, x = provinceArray.length(); i < x; i++) {
ProvinceEntity provinceEntity = new ProvinceEntity();
JSONObject provinceObject = provinceArray.optJSONObject(i);
provinceEntity.setCode(provinceObject.optString(builder.provinceCodeField));
provinceEntity.setName(provinceObject.optString(builder.provinceNameField));
provinceEntity.setCityList(new ArrayList<>());
JSONArray cityArray = provinceObject.optJSONArray(builder.provinceChildField);
parseCity(provinceEntity, cityArray);
data.add(provinceEntity);
}
return data;
}
private void parseCity(ProvinceEntity provinceEntity, JSONArray cityArray) {
//台湾的第二级可能没数据
if (cityArray == null || cityArray.length() == 0) {
return;
}
for (int j = 0, y = cityArray.length(); j < y; j++) {
CityEntity cityEntity = new CityEntity();
JSONObject cityObject = cityArray.optJSONObject(j);
cityEntity.setCode(cityObject.optString(builder.cityCodeField));
cityEntity.setName(cityObject.optString(builder.cityNameField));
cityEntity.setCountyList(new ArrayList<>());
provinceEntity.getCityList().add(cityEntity);
JSONArray countyArray = cityObject.optJSONArray(builder.cityChildField);
parseCounty(cityEntity, countyArray);
}
}
private void parseCounty(CityEntity cityEntity, JSONArray countyArray) {
//港澳台的第三级可能没数据
if (countyArray == null || countyArray.length() == 0) {
return;
}
for (int k = 0, z = countyArray.length(); k < z; k++) {
CountyEntity countyEntity = new CountyEntity();
JSONObject countyObject = countyArray.optJSONObject(k);
countyEntity.setCode(countyObject.optString(builder.countyCodeField));
countyEntity.setName(countyObject.optString(builder.countyNameField));
cityEntity.getCountyList().add(countyEntity);
}
}
@SuppressWarnings("unused")
public static class Builder {
private String provinceCodeField = "code";
private String provinceNameField = "name";
private String provinceChildField = "cityList";
private String cityCodeField = "code";
private String cityNameField = "name";
private String cityChildField = "areaList";
private String countyCodeField = "code";
private String countyNameField = "name";
public Builder provinceCodeField(String provinceCodeField) {
if (TextUtils.isEmpty(provinceCodeField)) {
return this;
}
this.provinceCodeField = provinceCodeField;
return this;
}
public Builder provinceNameField(String provinceNameField) {
if (TextUtils.isEmpty(provinceNameField)) {
return this;
}
this.provinceNameField = provinceNameField;
return this;
}
public Builder provinceChildField(String provinceChildField) {
if (TextUtils.isEmpty(provinceChildField)) {
return this;
}
this.provinceChildField = provinceChildField;
return this;
}
public Builder cityCodeField(String cityCodeField) {
if (TextUtils.isEmpty(cityCodeField)) {
return this;
}
this.cityCodeField = cityCodeField;
return this;
}
public Builder cityNameField(String cityNameField) {
if (TextUtils.isEmpty(cityNameField)) {
return this;
}
this.cityNameField = cityNameField;
return this;
}
public Builder cityChildField(String cityChildField) {
if (TextUtils.isEmpty(cityChildField)) {
return this;
}
this.cityChildField = cityChildField;
return this;
}
public Builder countyCodeField(String countyCodeField) {
if (TextUtils.isEmpty(countyCodeField)) {
return this;
}
this.countyCodeField = countyCodeField;
return this;
}
public Builder countyNameField(String countyNameField) {
if (TextUtils.isEmpty(countyNameField)) {
return this;
}
this.countyNameField = countyNameField;
return this;
}
public AddressJsonParser build() {
return new AddressJsonParser(this);
}
}
}
================================================
FILE: CalendarPicker/README.md
================================================
# 日历选择器
基于 [双向日期选择日历控件](https://github.com/oxsource/calendar) 开发。
## 示例
```groovy
CalendarPicker picker = new CalendarPicker(this);
picker.setRangeDateOnFuture(3);
if (singleTimeInMillis == 0) {
singleTimeInMillis = System.currentTimeMillis();
}
picker.setSelectedDate(singleTimeInMillis);
picker.setOnSingleDatePickListener(new OnSingleDatePickListener() {
@Override
public void onSingleDatePicked(@NonNull Date date) {
singleTimeInMillis = date.getTime();
}
});
picker.show();
```
## 配色
```groovy
CalendarPicker picker = new CalendarPicker(this);
picker.setColorScheme(new ColorScheme()
.daySelectBackgroundColor(0xFF0000FF)
.dayStressTextColor(0xFF0000DD));
picker.show();
```
================================================
FILE: CalendarPicker/build.gradle
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
apply from: "${rootDir}/gradle/library.gradle"
apply from: "${rootDir}/gradle/publish.gradle"
dependencies {
implementation androidxLibrary.annotation
implementation androidxLibrary.core
implementation androidxLibrary.recyclerview
api project(':Common')
}
================================================
FILE: CalendarPicker/consumer-rules.pro
================================================
# 本库模块专用的混淆规则
================================================
FILE: CalendarPicker/src/main/AndroidManifest.xml
================================================
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/CalendarPicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.recyclerview.widget.RecyclerView;
import com.github.gzuliyujiang.calendarpicker.core.CalendarAdapter;
import com.github.gzuliyujiang.calendarpicker.core.CalendarView;
import com.github.gzuliyujiang.calendarpicker.core.ColorScheme;
import com.github.gzuliyujiang.calendarpicker.core.DateUtils;
import com.github.gzuliyujiang.calendarpicker.core.FestivalProvider;
import com.github.gzuliyujiang.calendarpicker.core.ItemViewProvider;
import com.github.gzuliyujiang.calendarpicker.core.OnDateSelectedListener;
import com.github.gzuliyujiang.calendarpicker.listener.OnPageChangeCallback;
import com.github.gzuliyujiang.calendarpicker.listener.ScrollEventAdapter;
import com.github.gzuliyujiang.dialog.DialogConfig;
import com.github.gzuliyujiang.dialog.DialogStyle;
import com.github.gzuliyujiang.dialog.ModalDialog;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* 日历日期选择器,基于`https://github.com/oxsource/calendar`
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/4/30 13:36
*/
@SuppressWarnings({"unused", "deprecation"})
public class CalendarPicker extends ModalDialog implements OnDateSelectedListener {
private CalendarView calendarView;
private CalendarAdapter calendarAdapter;
private ScrollEventAdapter mScrollEventAdapter;
private ColorScheme colorScheme;
private boolean singleMode = false;
private FestivalProvider festivalProvider;
private ItemViewProvider itemViewProvider;
private Date minDate, maxDate;
private Date selectDate, startDate, endDate;
private String noteFrom, noteTo;
private OnSingleDatePickListener onSingleDatePickListener;
private OnRangeDatePickListener onRangeDatePickListener;
private boolean initialized = false;
public CalendarPicker(Activity activity) {
super(activity);
}
public CalendarPicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@NonNull
@Override
protected View createBodyView() {
calendarView = new CalendarView(activity);
return calendarView;
}
@Override
protected void initView() {
super.initView();
setHeight((int) (activity.getResources().getDisplayMetrics().heightPixels * 0.6f));
switch (DialogConfig.getDialogStyle()) {
case DialogStyle.Default:
case DialogStyle.Two:
headerView.setVisibility(View.VISIBLE);
break;
default:
headerView.setVisibility(View.GONE);
break;
}
}
@Override
protected void initData() {
super.initData();
initialized = true;
if (minDate == null && maxDate == null) {
Date currentDate = new Date(System.currentTimeMillis());
Calendar minCalendar = DateUtils.calendar(currentDate);
minCalendar.add(Calendar.MONTH, -12);
minCalendar.set(Calendar.DAY_OF_MONTH, DateUtils.maxDaysOfMonth(minCalendar.getTime()));
minDate = minCalendar.getTime();
Calendar maxCalendar = DateUtils.calendar(currentDate);
maxCalendar.setTime(currentDate);
maxCalendar.add(Calendar.MONTH, 12);
maxCalendar.set(Calendar.DAY_OF_MONTH, DateUtils.maxDaysOfMonth(maxCalendar.getTime()));
maxDate = maxCalendar.getTime();
}
if (mScrollEventAdapter == null) {
mScrollEventAdapter = new ScrollEventAdapter(calendarView.getBodyView());
calendarView.getBodyView().addOnScrollListener(mScrollEventAdapter);
}
calendarAdapter = calendarView.getAdapter();
calendarAdapter.setOnCalendarSelectedListener(this);
mScrollEventAdapter.setOnPageChangeCallback(new OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
if (calendarAdapter != null) {
Date cDate = calendarAdapter.getDateValue(position);
if (onSingleDatePickListener != null) {
onSingleDatePickListener.onMonthChanged(cDate);
}
if (onRangeDatePickListener != null) {
onRangeDatePickListener.onMonthChanged(cDate);
}
}
}
});
refreshData();
}
@Override
protected void onCancel() {
}
@Override
protected void onOk() {
if (singleMode && selectDate == null) {
return;
}
boolean rangeNotSelected = startDate == null || endDate == null;
if (!singleMode && rangeNotSelected) {
return;
}
dismiss();
if (onSingleDatePickListener != null) {
onSingleDatePickListener.onSingleDatePicked(selectDate);
}
if (onRangeDatePickListener != null) {
onRangeDatePickListener.onRangeDatePicked(startDate, endDate);
}
}
@Override
public void onSingleSelected(@NonNull Date date) {
selectDate = date;
}
@Override
public void onRangeSelected(@NonNull Date start, @NonNull Date end) {
startDate = start;
endDate = end;
}
/**
* 启用横向滑动模式
*/
public void enablePagerSnap() {
setHeight(WRAP_CONTENT);
calendarView.enablePagerSnap();
calendarView.getBodyView().addOnScrollListener(new RecyclerView.OnScrollListener() {
@SuppressLint("NotifyDataSetChanged")
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
if (newState != RecyclerView.SCROLL_STATE_IDLE) {
return;
}
calendarView.getAdapter().notifyDataSetChanged();
}
});
}
/**
* 设置配色方案
*/
public void setColorScheme(ColorScheme colorScheme) {
if (colorScheme == null) {
colorScheme = new ColorScheme();
}
this.colorScheme = colorScheme;
if (initialized) {
refreshData();
}
}
/**
* 设置日期范围选择回调
*/
public void setOnRangeDatePickListener(OnRangeDatePickListener onRangeDatePickListener) {
this.singleMode = false;
this.onRangeDatePickListener = onRangeDatePickListener;
if (initialized) {
refreshData();
}
}
/**
* 设置单个日期选择回调
*/
public void setOnSingleDatePickListener(OnSingleDatePickListener onSingleDatePickListener) {
this.singleMode = true;
this.onSingleDatePickListener = onSingleDatePickListener;
if (initialized) {
refreshData();
}
}
/**
* 设置日期范围
*/
public void setRangeDate(Date minDate, Date maxDate) {
this.minDate = DateUtils.min(minDate, maxDate);
this.maxDate = DateUtils.max(minDate, maxDate);
if (initialized) {
refreshData();
}
}
/**
* 设置日期范围为当前年月之后的几个月
*/
public void setRangeDateOnFuture(int offsetMonth) {
if (offsetMonth < 0) {
offsetMonth = 0;
}
minDate = new Date(System.currentTimeMillis());
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MONTH, offsetMonth);
calendar.set(Calendar.DAY_OF_MONTH, DateUtils.maxDaysOfMonth(calendar.getTime()));
maxDate = calendar.getTime();
if (initialized) {
refreshData();
}
}
/**
* 设置默认选择的日期时间戳(单个日期选择模式)
*/
public void setSelectedDate(long timeInMillis) {
setSelectedDate(new Date(timeInMillis));
}
/**
* 设置默认选择的日期(单个日期选择模式)
*/
public void setSelectedDate(Date date) {
this.selectDate = date;
if (initialized) {
refreshData();
}
}
/**
* 设置默认选择的日期时间戳(日期范围选择模式)
*/
public void setSelectedDate(long timeInMillisStart, long timeInMillisEnd) {
setSelectedDate(new Date(Math.min(timeInMillisStart, timeInMillisEnd)),
new Date(Math.max(timeInMillisStart, timeInMillisEnd)));
}
/**
* 设置默认选择的日期(日期范围选择模式)
*/
public void setSelectedDate(Date startDate, Date endDate) {
this.startDate = startDate;
this.endDate = endDate;
if (initialized) {
refreshData();
}
}
/**
* 设置选择区间提示语
*/
public void setIntervalNotes(String noteFrom, String noteTo) {
this.noteFrom = noteFrom;
this.noteTo = noteTo;
if (initialized) {
refreshData();
}
}
/**
* 设置节日文本提供者
*/
public void setFestivalProvider(FestivalProvider festivalProvider) {
this.festivalProvider = festivalProvider;
if (initialized) {
refreshData();
}
}
/**
* 设置条目视图提供者
*/
public void setItemViewProvider(ItemViewProvider itemViewProvider) {
this.itemViewProvider = itemViewProvider;
if (initialized) {
refreshData();
}
}
private void refreshData() {
calendarView.setColorScheme(colorScheme);
calendarAdapter.notify(false);
calendarAdapter.single(singleMode);
calendarAdapter.festivalProvider(festivalProvider);
calendarAdapter.itemViewProvider(itemViewProvider);
if (singleMode) {
startDate = selectDate;
endDate = selectDate;
}
calendarAdapter.valid(minDate, maxDate);
calendarAdapter.select(startDate, endDate);
calendarAdapter.range(minDate, maxDate);
if (!TextUtils.isEmpty(noteFrom) && !TextUtils.isEmpty(noteTo)) {
calendarAdapter.intervalNotes(noteFrom, noteTo);
}
calendarAdapter.refresh();
scrollToSelectedPosition();
}
private void scrollToSelectedPosition() {
calendarView.post(new Runnable() {
@Override
public void run() {
int position = calendarAdapter.getDatePosition(startDate);
position = Math.max(position, 0);
position = Math.min(position, calendarAdapter.getItemCount() - 1);
calendarView.getLayoutManager().scrollToPositionWithOffset(position, 0);
}
});
}
/**
* 获取日历视图
*/
public final CalendarView getCalendarView() {
return calendarView;
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mScrollEventAdapter != null && calendarView != null) {
mScrollEventAdapter.setOnPageChangeCallback(null);
calendarView.getBodyView().removeOnScrollListener(mScrollEventAdapter);
mScrollEventAdapter = null;
}
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/OnRangeDatePickListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker;
import androidx.annotation.NonNull;
import java.util.Date;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/4/30 13:36
*/
public interface OnRangeDatePickListener {
void onRangeDatePicked(@NonNull Date startDate, @NonNull Date endDate);
void onMonthChanged(Date date);
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/OnSingleDatePickListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker;
import androidx.annotation.NonNull;
import java.util.Date;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/4/30 13:36
*/
public interface OnSingleDatePickListener {
void onSingleDatePicked(@NonNull Date date);
void onMonthChanged(Date date);
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/CalendarAdapter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* Created by peng on 2017/8/3.
*/
@SuppressWarnings("UnusedReturnValue")
public class CalendarAdapter extends RecyclerView.Adapter implements OnDateClickListener {
public static String DATE_FORMAT = "yyyy年MM月";
private boolean notify = true;
private ColorScheme colorScheme = new ColorScheme();
private final List dates = new ArrayList<>();
private final Interval valid = new Interval<>();
private final Interval select = new Interval<>();
private final Interval selectNote = new Interval<>();
private boolean singleMode = false;
private FestivalProvider festivalProvider;
private ItemViewProvider itemViewProvider;
private Date lastClickDate = null;
private OnDateSelectedListener onDateSelectedListener;
static {
if (!Locale.getDefault().getDisplayLanguage().contains("中文")) {
DATE_FORMAT = "MMM, yyyy";
}
}
public CalendarAdapter notify(boolean notify) {
this.notify = notify;
return this;
}
public CalendarAdapter colorScheme(ColorScheme colorScheme) {
if (colorScheme == null) {
colorScheme = new ColorScheme();
}
this.colorScheme = colorScheme;
return this;
}
public CalendarAdapter single(boolean value) {
singleMode = value;
if (notify) {
refresh();
}
return this;
}
public CalendarAdapter festivalProvider(FestivalProvider value) {
festivalProvider = value;
if (notify) {
refresh();
}
return this;
}
public CalendarAdapter itemViewProvider(ItemViewProvider value) {
itemViewProvider = value;
if (notify) {
refresh();
}
return this;
}
public CalendarAdapter valid(Date from, Date to) {
valid.left(from);
valid.right(to);
if (notify) {
refresh();
}
return this;
}
/**
* 选择区间提示语
*
* @param noteFrom 开始日期提示语
* @param noteTo 结束日期提示语
*/
public CalendarAdapter intervalNotes(String noteFrom, String noteTo) {
selectNote.left(noteFrom);
selectNote.right(noteTo);
if (notify) {
refresh();
}
return this;
}
/**
* 设置选择范围
*
* @param fromInMillis 开始日期
* @param toInMillis 结束日期
*/
public CalendarAdapter select(long fromInMillis, long toInMillis) {
return select(new Date(fromInMillis), new Date(toInMillis));
}
/**
* 设置选择范围
*
* @param from 开始日期
* @param to 结束日期
*/
public CalendarAdapter select(Date from, Date to) {
select.left(from);
select.right(to);
if (notify) {
refresh();
}
return this;
}
/**
* 设置选择日期范围
*
* @param startDate 开始时间
* @param endDate 结束时间
*/
public CalendarAdapter range(Date startDate, Date endDate) {
return range(startDate, endDate, true);
}
public CalendarAdapter range(Date startDate, Date endDate, boolean clear) {
List dates = DateUtils.fillDates(startDate, endDate);
return range(dates, clear);
}
public CalendarAdapter range(List list, boolean clear) {
if (clear) {
dates.clear();
}
if (null != list && list.size() > 0) {
dates.addAll(list);
}
if (notify) {
refresh();
}
return this;
}
/**
* @deprecated 使用 {@link #notify(boolean)} 及 {@link #range(Date, Date, boolean)} 代替
*/
@Deprecated
public CalendarAdapter setRange(Date startDate, Date endDate, boolean clear, boolean notify) {
List dates = DateUtils.fillDates(startDate, endDate);
this.notify = notify;
return range(dates, clear);
}
/**
* @deprecated 使用 {@link #notify(boolean)} 及 {@link #range(List, boolean)} 代替
*/
@Deprecated
public CalendarAdapter setRange(List list, boolean clear, boolean notify) {
this.notify = notify;
return range(list, clear);
}
@SuppressLint("NotifyDataSetChanged")
public void refresh() {
notifyDataSetChanged();
}
public void setOnCalendarSelectedListener(OnDateSelectedListener onDateSelectedListener) {
this.onDateSelectedListener = onDateSelectedListener;
}
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TextView titleView = itemViewProvider == null ? null : itemViewProvider.provideTitleView(context);
if (titleView == null) {
titleView = new TextView(context);
titleView.setGravity(Gravity.CENTER);
titleView.setTextSize(14);
titleView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
int padding = (int) (parent.getResources().getDisplayMetrics().density * 10);
titleView.setPadding(padding, padding, padding, padding);
}
linearLayout.addView(titleView, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
MonthView monthView = itemViewProvider == null ? null : itemViewProvider.provideMonthView(context);
if (monthView == null) {
monthView = new MonthView(context);
}
monthView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.addView(monthView);
return new VH(linearLayout, titleView, monthView);
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
holder.titleView.setBackgroundColor(colorScheme.monthTitleBackgroundColor());
holder.titleView.setTextColor(colorScheme.monthTitleTextColor());
holder.titleView.setText(TimeUtils.dateText(getDateValue(position).getTime(), DATE_FORMAT));
holder.monthView.setOnDayInMonthClickListener(this);
holder.monthView.setValue(MonthEntity.obtain(valid, select)
.date(dates.get(position))
.singleMode(singleMode)
.festivalProvider(festivalProvider)
.note(selectNote), colorScheme);
}
@Override
public int getItemCount() {
return dates.size();
}
public final int getDatePosition(Date date) {
int size = dates.size();
if (size <= 1) {
return 0;
}
if (date == null) {
return 0;
}
long time = date.getTime();
if (time <= dates.get(0).getTime()) {
return 0;
}
int lastPosition = size - 1;
if (time >= dates.get(lastPosition).getTime()) {
return lastPosition;
}
for (int i = 0; i <= lastPosition; i++) {
Calendar minDate = DateUtils.calendar(dates.get(i).getTime());
minDate.set(Calendar.DAY_OF_MONTH, 1);
minDate.set(Calendar.HOUR_OF_DAY, 0);
minDate.set(Calendar.MINUTE, 0);
minDate.set(Calendar.SECOND, 0);
minDate.set(Calendar.MILLISECOND, 0);
Calendar maxDate = DateUtils.calendar(dates.get(i).getTime());
maxDate.set(Calendar.DAY_OF_MONTH, DateUtils.maxDaysOfMonth(maxDate.getTime()));
maxDate.set(Calendar.HOUR_OF_DAY, 23);
maxDate.set(Calendar.MINUTE, 59);
maxDate.set(Calendar.SECOND, 59);
maxDate.set(Calendar.MILLISECOND, 999);
if (time >= minDate.getTime().getTime() && time <= maxDate.getTime().getTime()) {
return i;
}
}
return -1;
}
public Date getDateValue(int position) {
if (position >= 0 && position < dates.size()) {
return dates.get(position);
}
return new Date(0);
}
@Override
public void onCalendarDayClick(Date date) {
if (null == date) {
return;
}
if (singleMode || null == lastClickDate || lastClickDate.getTime() >= date.getTime()) {
lastClickDate = date;
select(date, date).refresh();
if (null != onDateSelectedListener) {
onDateSelectedListener.onSingleSelected(date);
}
if (!singleMode && null != onDateSelectedListener) {
onDateSelectedListener.onRangeSelected(date, date);
}
return;
}
select(lastClickDate, date).refresh();
if (null != onDateSelectedListener) {
onDateSelectedListener.onRangeSelected(lastClickDate, date);
}
lastClickDate = null;
}
static class VH extends RecyclerView.ViewHolder {
TextView titleView;
MonthView monthView;
VH(View itemView, TextView titleView, MonthView monthView) {
super(itemView);
this.titleView = titleView;
this.monthView = monthView;
}
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/CalendarView.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.widget.GridView;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import com.github.gzuliyujiang.calendarpicker.R;
/**
* 日历控件
* Created by peng on 2017/8/3.
*/
@SuppressWarnings("unused")
public class CalendarView extends LinearLayout {
private final CalendarAdapter calendarAdapter = new CalendarAdapter();
private final WeekAdapter weekAdapter = new WeekAdapter();
private final GridView weekView;
private final RecyclerView bodyView;
public CalendarView(Context context) {
this(context, null);
}
public CalendarView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CalendarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOrientation(VERTICAL);
inflate(context, R.layout.calendar_body, this);
weekView = findViewById(R.id.calendar_body_week);
weekView.setNumColumns(weekAdapter.getCount());
weekView.setAdapter(weekAdapter);
weekView.setSelector(new ColorDrawable(Color.TRANSPARENT));
bodyView = findViewById(R.id.calendar_body_content);
bodyView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
bodyView.setAdapter(calendarAdapter);
}
public void enablePagerSnap() {
bodyView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
new PagerSnapHelper().attachToRecyclerView(bodyView);
}
public void setColorScheme(ColorScheme colorScheme) {
weekAdapter.setColorScheme(colorScheme);
calendarAdapter.colorScheme(colorScheme);
}
public final GridView getWeekView() {
return weekView;
}
public final RecyclerView getBodyView() {
return bodyView;
}
public final LinearLayoutManager getLayoutManager() {
RecyclerView.LayoutManager layoutManager = bodyView.getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
return (LinearLayoutManager) layoutManager;
}
throw new IllegalArgumentException("Layout manager must instance of LinearLayoutManager");
}
public final CalendarAdapter getAdapter() {
return calendarAdapter;
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/ColorScheme.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import android.graphics.Color;
import androidx.annotation.ColorInt;
import java.io.Serializable;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/9/17 12:28
*/
public class ColorScheme implements Serializable {
private int weekTextColor = 0xFF343434;
private int weekBackgroundColor = Color.TRANSPARENT;
private int monthTitleTextColor = 0xFF343434;
private int monthTitleBackgroundColor = Color.TRANSPARENT;
private int monthBackgroundColor = Color.TRANSPARENT;
private int monthDividerColor = Color.TRANSPARENT;
private int dayNormalTextColor = 0xFF343434;
private int dayInvalidTextColor = 0xFFCCCCCC;
private int dayStressTextColor = 0xFFFF6600;
private int daySelectTextColor = 0xFFFFFFFF;
private int dayNormalBackgroundColor = Color.TRANSPARENT;
private int dayInvalidBackgroundColor = Color.TRANSPARENT;
private int daySelectBackgroundColor = 0xFFE75051;
public ColorScheme weekTextColor(@ColorInt int color) {
this.weekTextColor = color;
return this;
}
@ColorInt
public int weekTextColor() {
return weekTextColor;
}
public ColorScheme weekBackgroundColor(@ColorInt int color) {
this.weekBackgroundColor = color;
return this;
}
@ColorInt
public int weekBackgroundColor() {
return weekBackgroundColor;
}
public ColorScheme monthTitleTextColor(@ColorInt int color) {
this.monthTitleTextColor = color;
return this;
}
@ColorInt
public int monthTitleTextColor() {
return monthTitleTextColor;
}
public ColorScheme monthTitleBackgroundColor(@ColorInt int color) {
this.monthTitleBackgroundColor = color;
return this;
}
@ColorInt
public int monthTitleBackgroundColor() {
return monthTitleBackgroundColor;
}
public ColorScheme monthBackgroundColor(@ColorInt int color) {
this.monthBackgroundColor = color;
return this;
}
@ColorInt
public int monthBackgroundColor() {
return monthBackgroundColor;
}
public ColorScheme monthDividerColor(@ColorInt int color) {
this.monthDividerColor = color;
return this;
}
@ColorInt
public int monthDividerColor() {
return monthDividerColor;
}
public ColorScheme dayNormalTextColor(@ColorInt int color) {
this.dayNormalTextColor = color;
return this;
}
@ColorInt
public int dayNormalTextColor() {
return dayNormalTextColor;
}
public ColorScheme dayInvalidTextColor(@ColorInt int color) {
this.dayInvalidTextColor = color;
return this;
}
@ColorInt
public int dayInvalidTextColor() {
return dayInvalidTextColor;
}
public ColorScheme dayStressTextColor(@ColorInt int color) {
this.dayStressTextColor = color;
return this;
}
@ColorInt
public int dayStressTextColor() {
return dayStressTextColor;
}
public ColorScheme daySelectTextColor(@ColorInt int color) {
this.daySelectTextColor = color;
return this;
}
@ColorInt
public int daySelectTextColor() {
return daySelectTextColor;
}
public ColorScheme dayNormalBackgroundColor(@ColorInt int color) {
this.dayNormalBackgroundColor = color;
return this;
}
@ColorInt
public int dayNormalBackgroundColor() {
return dayNormalBackgroundColor;
}
public ColorScheme dayInvalidBackgroundColor(@ColorInt int color) {
this.dayInvalidBackgroundColor = color;
return this;
}
@ColorInt
public int dayInvalidBackgroundColor() {
return dayInvalidBackgroundColor;
}
public ColorScheme daySelectBackgroundColor(@ColorInt int color) {
this.daySelectBackgroundColor = color;
return this;
}
@ColorInt
public int daySelectBackgroundColor() {
return daySelectBackgroundColor;
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/DateUtils.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* Created by peng on 2017/8/2.
*/
public class DateUtils {
public static Calendar calendar(long timeInMillis) {
return calendar(new Date(timeInMillis));
}
public static Calendar calendar(Date date) {
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.setTime(date);
return calendar;
}
/**
* @param date 日期
* @return 当月最大天数
*/
public static int maxDaysOfMonth(Date date) {
return calendar(date).getActualMaximum(Calendar.DATE);
}
/**
* @param date 日期
* @return 当月第一天在月份表中的索引
*/
public static int firstDayOfMonthIndex(Date date) {
Calendar calendar = calendar(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.get(Calendar.DAY_OF_WEEK) - 1;
}
/**
* 给定日期是否是今天所在的月份
*
* @param date 日期
* @return 今天是当月几号
*/
public static int isTodayOfMonth(Date date) {
Calendar current = calendar(new Date());
Calendar calendar = calendar(date);
if (diverse(current, calendar, Calendar.YEAR)) {
return -1;
}
if (diverse(current, calendar, Calendar.MONTH)) {
return -1;
}
return current.get(Calendar.DAY_OF_MONTH) - 1;
}
/**
* @param calendarA 开始日历
* @param calendarB 结束日历
* @param field 字段
* @return 是否相同:不相--true,相同--false
*/
public static boolean diverse(Calendar calendarA, Calendar calendarB, int field) {
boolean same;
try {
same = calendarA.get(field) == calendarB.get(field);
} catch (Exception e) {
same = false;
}
return !same;
}
/**
* 区间内有多少个月
*
* @param sDate 开始日期
* @param eDate 结束日期
* @return 月数
*/
public static int months(Date sDate, Date eDate) {
Calendar before = calendar(min(sDate, eDate));
Calendar after = calendar(max(sDate, eDate));
int diffYear = after.get(Calendar.YEAR) - before.get(Calendar.YEAR);
int diffMonth = after.get(Calendar.MONTH) - before.get(Calendar.MONTH);
return diffYear * 12 + diffMonth;
}
public static Date max(Date sDate, Date eDate) {
return sDate.getTime() > eDate.getTime() ? sDate : eDate;
}
public static Date min(Date sDate, Date eDate) {
return sDate.getTime() > eDate.getTime() ? eDate : sDate;
}
/**
* 获取区间内各月的Date
*
* @param sDate 开始日期
* @param eDate 结束日期
* @return 区间内各月的Date
*/
public static List fillDates(Date sDate, Date eDate) {
List dates = new ArrayList<>();
if (null == sDate || null == eDate) {
dates.add(new Date());
} else {
Calendar calendar = calendar(min(sDate, eDate));
int months = months(sDate, eDate);
for (int i = 0; i <= months; i++) {
dates.add(calendar.getTime());
calendar.add(Calendar.MONTH, 1);
}
}
return dates;
}
/**
* 目标月份有哪些天在区间内,返回索引值区间
* 不在范围内时返回(-1,-1)
*
* @param month 目标月份
* @param dateInterval 开始,结束日期区间
* @return 起始位置区间
*/
public static NumInterval daysInterval(Date month, Interval dateInterval) {
final NumInterval range = new NumInterval();
if (null == month || null == dateInterval) {
return range;
}
final int maxDaysOfMonth = maxDaysOfMonth(month);
Date sDay;
Date eDay;
//保证sDay和eDay不为空
if (null == dateInterval.left()) {
Calendar safeCalendar = calendar(month);
safeCalendar.set(Calendar.DAY_OF_MONTH, 1);
sDay = safeCalendar.getTime();
} else {
sDay = new Date(dateInterval.left().getTime());
}
if (null == dateInterval.right()) {
Date date = max(sDay, month);
Calendar safeCalendar = calendar(date);
safeCalendar.set(Calendar.DAY_OF_MONTH, maxDaysOfMonth);
eDay = safeCalendar.getTime();
} else {
eDay = new Date(dateInterval.right().getTime());
}
//保证日期顺序
sDay = min(sDay, eDay);
eDay = max(sDay, eDay);
//以最小年份为基础
Calendar[] calendars = new Calendar[]{calendar(month), calendar(sDay), calendar(eDay)};
Calendar miniYearCalendar = calendars[0];
for (int i = 1; i < calendars.length; i++) {
if (miniYearCalendar.get(Calendar.YEAR) > calendars[i].get(Calendar.YEAR)) {
miniYearCalendar = calendars[i];
}
}
final long miniDate = miniYearCalendar.getTime().getTime();
long[] diffDays = new long[calendars.length];
for (int i = 0; i < calendars.length; i++) {
Calendar cal = calendar(new Date(miniDate));
int diffYear = calendars[i].get(Calendar.YEAR) - cal.get(Calendar.YEAR);
for (int j = 0; j < diffYear; j++) {
diffDays[i] += cal.getActualMaximum(Calendar.DAY_OF_YEAR);
cal.add(Calendar.YEAR, 1);
}
}
calendars[0].set(Calendar.DAY_OF_MONTH, 1);
final long dayIndex = diffDays[0] + calendars[0].get(Calendar.DAY_OF_YEAR);
final long limitA = diffDays[1] + calendars[1].get(Calendar.DAY_OF_YEAR);
final long limitB = diffDays[2] + calendars[2].get(Calendar.DAY_OF_YEAR);
long temp;
for (int i = 0; i < maxDaysOfMonth; i++) {
temp = dayIndex + i;
boolean contain = (temp >= limitA) && (temp <= limitB);
if (!contain) {
continue;
}
if (range.left() < 0) {
range.left(i);
}
range.right(i);
if (limitA == temp) {
range.lBound(i);
}
if (limitB == temp) {
range.rBound(i);
}
}
return range;
}
/**
* @param month 月份
* @param index 索引
* @return 根据月份及日期索引计算出指定日期
*/
public static Date specialDayInMonth(Date month, int index) {
Calendar calendar = calendar(month);
calendar.set(Calendar.DAY_OF_MONTH, index + 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 获取某月最后一天日期
*
* @param date 月份
* @return date月最后一天日期
*/
public static Date getLastDayFromMonth(Date date) {
Calendar calendar = calendar(date);
calendar.set(Calendar.DAY_OF_MONTH, maxDaysOfMonth(date));
return calendar.getTime();
}
/**
* 获取指定Date一年前的某月第一天日期
*
* @param date 制定日期
* @return 指定Date一年前的某月第一天日期
*/
public static Date getDayYearAgo(Date date) {
Calendar calendar = calendar(date);
calendar.add(Calendar.MONTH, -11);
calendar.set(Calendar.DAY_OF_MONTH, 0);
return calendar.getTime();
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/DayEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Created by peng on 2017/8/4.
*/
public class DayEntity implements Serializable {
private static final List pools = new ArrayList<>();
@DayStatus
private int status;
private int value;
@DayStatus
private int valueStatus;
private String desc;
@DayStatus
private int descStatus;
private String note;
private DayEntity() {
super();
}
@DayStatus
public int status() {
return status;
}
public DayEntity status(@DayStatus int status) {
this.status = status;
return this;
}
public DayEntity value(int value) {
this.value = value;
return this;
}
public String value() {
return value < 0 || value > MonthEntity.MAX_DAYS_OF_MONTH ? "" : String.valueOf(value + 1);
}
public int intValue() {
return value;
}
@DayStatus
public int valueStatus() {
return valueStatus;
}
public DayEntity valueStatus(@DayStatus int valueStatus) {
this.valueStatus = valueStatus;
return this;
}
public String desc() {
return null == desc ? "" : desc;
}
public DayEntity desc(String desc) {
this.desc = desc;
return this;
}
@DayStatus
public int descStatus() {
return descStatus;
}
public DayEntity descStatus(@DayStatus int descStatus) {
this.descStatus = descStatus;
return this;
}
public String note() {
return null == note ? "" : note;
}
public DayEntity note(String note) {
this.note = note;
return this;
}
public void recycle() {
if (!pools.contains(this)) {
this.status = DayStatus.NORMAL;
this.value = -1;
this.valueStatus = DayStatus.NORMAL;
this.descStatus = DayStatus.NORMAL;
this.desc = "";
pools.add(this);
}
}
public static DayEntity obtain(@DayStatus int status, int value, String desc) {
DayEntity entity = 0 == pools.size() ? new DayEntity() : pools.remove(0);
entity.status = status;
entity.value = value;
entity.valueStatus = status;
entity.descStatus = status;
entity.desc = desc;
return entity;
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/DayStatus.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Created by peng on 2017/8/2.
*/
@Retention(RetentionPolicy.SOURCE)
public @interface DayStatus {
//正常
int NORMAL = 0;
//不可用
int INVALID = 1;
//范围内
int RANGE = 2;
//左边界
int BOUND_L = 3;
//单选
int BOUND_M = 4;
//右边界
int BOUND_R = 5;
//强调
int STRESS = 6;
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/DayView.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.graphics.ColorUtils;
/**
* 日期控件
* Created by peng on 2017/8/2.
*/
public final class DayView extends LinearLayout {
private TextView tvDesc;
private TextView tvDay;
private DayEntity entity;
public DayView(@NonNull Context context) {
super(context);
initialize(context);
}
public DayView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public DayView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public DayView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize(context);
}
private void initialize(Context context) {
setOrientation(VERTICAL);
setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
int padding = (int) (getResources().getDisplayMetrics().density * 3);
setPadding(0, padding, 0, padding);
tvDay = new TextView(context);
tvDay.setGravity(Gravity.CENTER);
tvDay.setTextSize(15);
addView(tvDay, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tvDesc = new TextView(context);
tvDesc.setGravity(Gravity.CENTER);
tvDesc.setTextSize(12);
addView(tvDesc, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
public void setValue(DayEntity entity, ColorScheme scheme) {
if (null != getValue()) {
getValue().recycle();
}
this.entity = entity;
//内容
tvDay.setText(entity.value());
setTextStatusColor(tvDay, entity.valueStatus(), scheme);
//描述
tvDesc.setText(entity.desc());
setTextStatusColor(tvDesc, entity.descStatus(), scheme);
//背景
setBackgroundStatus(entity, scheme);
}
public DayEntity getValue() {
return entity;
}
private void setTextStatusColor(TextView tv, @DayStatus int status, ColorScheme scheme) {
switch (status) {
//正常
case DayStatus.NORMAL:
tv.setTextColor(scheme.dayNormalTextColor());
break;
//不可用
case DayStatus.INVALID:
tv.setTextColor(scheme.dayInvalidTextColor());
break;
//强调
case DayStatus.STRESS:
tv.setTextColor(scheme.dayStressTextColor());
break;
//范围内、左边界
case DayStatus.RANGE:
case DayStatus.BOUND_L:
case DayStatus.BOUND_M:
case DayStatus.BOUND_R:
tv.setTextColor(scheme.daySelectTextColor());
break;
default:
break;
}
}
private void setBackgroundStatus(DayEntity entity, ColorScheme scheme) {
switch (entity.status()) {
//正常、强调
case DayStatus.NORMAL:
case DayStatus.STRESS:
setBackgroundColor(scheme.dayNormalBackgroundColor());
setEnabled(true);
break;
//不可用
case DayStatus.INVALID:
tvDay.setTextColor(scheme.dayInvalidTextColor());
setBackgroundColor(scheme.dayInvalidBackgroundColor());
setEnabled(false);
break;
//范围内
case DayStatus.RANGE:
setBackgroundColor(ColorUtils.setAlphaComponent(scheme.daySelectBackgroundColor(), 200));
setEnabled(true);
break;
//左边界、单选、右边界
case DayStatus.BOUND_L:
case DayStatus.BOUND_M:
case DayStatus.BOUND_R:
tvDay.setTextColor(scheme.daySelectTextColor());
tvDesc.setTextColor(scheme.daySelectTextColor());
tvDesc.setText(entity.note());
setBackgroundColor(scheme.daySelectBackgroundColor());
break;
default:
break;
}
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/FestivalProvider.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import java.util.Date;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/10/28 9:52
*/
public interface FestivalProvider {
String provideText(Date date);
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/Interval.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
/**
* Created by peng on 2017/8/4.
*/
public class Interval {
private int lBound;
private T left;
private int rBound;
private T right;
public int lBound() {
return lBound;
}
public void lBound(int lBound) {
this.lBound = lBound;
}
public T left() {
return left;
}
public Interval left(T left) {
this.left = left;
return this;
}
public int rBound() {
return rBound;
}
public void rBound(int rBound) {
this.rBound = rBound;
}
public T right() {
return right;
}
public Interval right(T right) {
this.right = right;
return this;
}
public boolean bothNoNull() {
return null != left() && null != right();
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/ItemViewProvider.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import android.content.Context;
import android.widget.TextView;
/**
* @author liyuj
* @since 2022/1/5 16:03
*/
public interface ItemViewProvider {
TextView provideTitleView(Context context);
MonthView provideMonthView(Context context);
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/MonthEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* Created by peng on 2017/8/4.
*/
public class MonthEntity implements Serializable {
public static int WEEK_DAYS = 7;
public static int MAX_HORIZONTAL_LINES = 6;
public static int MAX_DAYS_OF_MONTH = 31;
public static String STR_TODAY = "今天";
private final static List pools = new ArrayList<>();
private Date date;
private Interval valid;
private Interval select;
private Interval note;
private boolean singleMode = false;
private FestivalProvider festivalProvider;
static {
if (!Locale.getDefault().getDisplayLanguage().contains("中文")) {
STR_TODAY = "Today";
}
}
public static MonthEntity obtain(Interval valid, Interval select) {
MonthEntity entity = pools.size() == 0 ? new MonthEntity() : pools.remove(0);
entity.valid = valid;
entity.select = select;
return entity;
}
private MonthEntity() {
super();
}
public Date date() {
return date;
}
public MonthEntity date(Date date) {
this.date = date;
return this;
}
public Interval valid() {
return valid;
}
public MonthEntity valid(Interval valid) {
this.valid = valid;
return this;
}
public Interval select() {
return select;
}
public MonthEntity select(Interval select) {
this.select = select;
return this;
}
public MonthEntity singleMode(boolean singleMode) {
this.singleMode = singleMode;
return this;
}
public boolean singleMode() {
return this.singleMode;
}
public MonthEntity festivalProvider(FestivalProvider festivalProvider) {
this.festivalProvider = festivalProvider;
return this;
}
public FestivalProvider festivalProvider() {
return this.festivalProvider;
}
public Interval note() {
return note;
}
public MonthEntity note(Interval note) {
this.note = note;
return this;
}
public void recycle() {
if (!pools.contains(this)) {
date = null;
valid = null;
select = null;
note = null;
pools.add(this);
}
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/MonthView.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import java.util.Date;
/**
* 月份控件
* Created by peng on 2017/8/2.
*/
public class MonthView extends ViewGroup {
private ColorScheme colorScheme = new ColorScheme();
private final DayView[] dayViews = new DayView[MonthEntity.MAX_DAYS_OF_MONTH];
private final View[] dividerViews = new View[MonthEntity.MAX_HORIZONTAL_LINES];
private int dividerHeight;
private DividerLayoutControl dividerLayoutControl;
private MonthEntity monthEntity;
private int isTodayOfMonth = -1;
//location
private int position = 0;
private int offset = 0;
//child width and height
private int childWidth = 0;
private int childHeight = 0;
private OnDateClickListener onDayInMonthClickListener;
public MonthView(Context context) {
super(context);
initialize(context);
}
public MonthView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public MonthView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MonthView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize(context);
}
private void initialize(Context context) {
for (int i = 0, m = dayViews.length; i < m; i++) {
dayViews[i] = new DayView(context);
addView(dayViews[i]);
}
dividerHeight = (int) (getResources().getDisplayMetrics().density * 0.5f);
for (int j = 0, n = dividerViews.length; j < n; j++) {
View view = new View(getContext());
addView(view);
dividerViews[j] = view;
}
dividerLayoutControl = new DividerLayoutControl(dividerViews);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (null == getValue()) {
return;
}
int totalWidth = MeasureSpec.getSize(widthMeasureSpec);
dayViews[0].measure(widthMeasureSpec, heightMeasureSpec);
int childrenHeight = 0;
//calc need rows
int amount = position + offset;
int dayRows = (amount / MonthEntity.WEEK_DAYS) + (((amount % MonthEntity.WEEK_DAYS) != 0) ? 1 : 0);
//measure container
childrenHeight += dayViews[0].getMeasuredHeight() * dayRows;
childrenHeight += (dayRows) * dividerHeight;
setMeasuredDimension(totalWidth, childrenHeight);
//measure DayViews
childWidth = totalWidth / MonthEntity.WEEK_DAYS;
childHeight = dayViews[0].getMeasuredHeight();
int childWidthSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
for (DayView dayView : dayViews) {
dayView.measure(childWidthSpec, childHeightSpec);
}
//measure horizontal lines
for (View line : dividerViews) {
line.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(dividerHeight, MeasureSpec.EXACTLY));
}
}
@Override
protected void onLayout(boolean b, int left, int top, int right, int bottom) {
if (null == getValue()) {
return;
}
int offsetX = 0, offsetY = 0;
for (int i = 0; i < position; i++) {
offsetX += childWidth;
}
int childBottom = offsetY + childHeight;
boolean lastIsRightBound = false;//上一个是否是右边界
NumInterval validRange = DateUtils.daysInterval(monthEntity.date(), monthEntity.valid());
NumInterval selectRange = null;
if (monthEntity.select().bothNoNull()) {
selectRange = DateUtils.daysInterval(monthEntity.date(), monthEntity.select());
}
for (int index = 0, move = position + 1; index < dayViews.length; index++, move++) {
boolean rightBound = move % MonthEntity.WEEK_DAYS == 0;
DayEntity dayEntity;
if (index < offset) {
//set state
boolean isToday = index == isTodayOfMonth;
dayEntity = DayEntity.obtain(DayStatus.NORMAL, index, isToday ? MonthEntity.STR_TODAY : toDayDesc(index))
.valueStatus((lastIsRightBound || rightBound) ? DayStatus.STRESS : DayStatus.NORMAL)
.descStatus(isToday ? DayStatus.STRESS : DayStatus.NORMAL);
//valid
if (validRange.contain(index)) {
if (null != selectRange && selectRange.contain(index)) {
if (index == selectRange.lBound()) {
if (monthEntity.singleMode()) {
dayEntity.status(DayStatus.BOUND_M).note(monthEntity.note().left());
} else {
dayEntity.status(DayStatus.BOUND_L).note(monthEntity.note().left());
}
} else if (index == selectRange.rBound()) {
dayEntity.status(DayStatus.BOUND_R).note(monthEntity.note().right());
} else {
dayEntity.status(DayStatus.RANGE);
dayEntity.valueStatus(DayStatus.RANGE);
dayEntity.descStatus(DayStatus.RANGE);
}
}
} else {
//不响应选择事件
dayEntity.status(DayStatus.INVALID)
.valueStatus(DayStatus.INVALID)
.descStatus(DayStatus.INVALID);
}
dayViews[index].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!(v instanceof DayView)) {
return;
}
if (null == onDayInMonthClickListener) {
return;
}
try {
DayEntity entity = ((DayView) v).getValue();
Date dayDate = DateUtils.specialDayInMonth(monthEntity.date(), entity.intValue());
onDayInMonthClickListener.onCalendarDayClick(dayDate);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} else {
dayEntity = DayEntity.obtain(DayStatus.INVALID, -1, "");
dayViews[index].setOnClickListener(null);
}
dayViews[index].setValue(dayEntity, colorScheme);
dayViews[index].layout(offsetX, offsetY, offsetX + childWidth, childBottom);
if (rightBound) {
offsetX = 0;
offsetY += childHeight;
//draw horizontal line
offsetY = dividerLayoutControl.layout(offsetY);
childBottom = offsetY + childHeight;
} else {
offsetX += childWidth;
}
lastIsRightBound = rightBound;
}
dividerLayoutControl.layout(offsetY + childHeight);
}
@NonNull
protected String toDayDesc(int index) {
FestivalProvider festivalProvider = monthEntity.festivalProvider();
if (festivalProvider == null) {
return "";
}
Date date = DateUtils.specialDayInMonth(monthEntity.date(), index);
String festival = festivalProvider.provideText(date);
if (festival == null) {
festival = "";
}
return festival;
}
public void setValue(@NonNull MonthEntity entity, @NonNull ColorScheme colorScheme) {
if (null != monthEntity) {
monthEntity.recycle();
}
this.monthEntity = entity;
position = DateUtils.firstDayOfMonthIndex(entity.date());
offset = DateUtils.maxDaysOfMonth(entity.date());
isTodayOfMonth = DateUtils.isTodayOfMonth(entity.date());
setBackgroundColor(colorScheme.monthBackgroundColor());
for (View view : dividerViews) {
view.setBackgroundColor(colorScheme.monthDividerColor());
}
this.colorScheme = colorScheme;
requestLayout();
}
public MonthEntity getValue() {
return monthEntity;
}
public void setOnDayInMonthClickListener(OnDateClickListener listener) {
onDayInMonthClickListener = listener;
}
/**
* 分割线布局控制器
*/
private static class DividerLayoutControl {
private final int width;
private final int height;
private final View[] view;
private int count = 0;
DividerLayoutControl(@NonNull View[] views) {
this.view = views;
width = views[0].getMeasuredWidth();
height = views[0].getMeasuredHeight();
}
public int layout(int offsetY) {
if (count >= view.length) {
return offsetY;
}
int bottom = offsetY + height;
view[count].layout(0, offsetY, width, bottom);
count += 1;
return bottom;
}
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/NumInterval.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
/**
* Created by peng on 2017/8/4.
*/
public class NumInterval extends Interval {
public NumInterval() {
left(-1);
lBound(-1);
right(-1);
rBound(-1);
}
public boolean contain(int index) {
return index >= left() && index <= right();
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/OnDateClickListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import java.util.Date;
/**
* Created by peng on 2017/8/4.
*/
public interface OnDateClickListener {
void onCalendarDayClick(Date date);
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/OnDateSelectedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import androidx.annotation.NonNull;
import java.util.Date;
/**
* Created by peng on 2017/8/4.
*/
public interface OnDateSelectedListener {
void onSingleSelected(@NonNull Date date);
void onRangeSelected(@NonNull Date start, @NonNull Date end);
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/TimeUtils.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import androidx.annotation.NonNull;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* 时间工具
*/
public class TimeUtils {
private final static Map dateMap = new HashMap<>();
private static void ensureDateFormatMap(@NonNull String format) {
if (!dateMap.containsKey(format)) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
dateMap.put(format, sdf);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static Date date(String dateText, @NonNull String format) throws Exception {
ensureDateFormatMap(format);
if (dateMap.containsKey(format)) {
SimpleDateFormat sdf = dateMap.get(format);
if (sdf != null) {
return sdf.parse(dateText);
}
}
return null;
}
public static String dateText(long date, @NonNull String format) {
String value = "";
ensureDateFormatMap(format);
if (dateMap.containsKey(format)) {
SimpleDateFormat sdf = dateMap.get(format);
if (sdf != null) {
value = sdf.format(new Date(date));
}
}
return value;
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/core/WeekAdapter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.calendarpicker.core;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.Locale;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/9/17 14:36
*/
public class WeekAdapter extends BaseAdapter {
public static String[] DATA = new String[]{
"日", "一", "二", "三", "四", "五", "六"
};
private ColorScheme colorScheme = new ColorScheme();
static {
if (!Locale.getDefault().getDisplayLanguage().contains("中文")) {
DATA = new String[]{
"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"
};
}
}
public void setColorScheme(ColorScheme colorScheme) {
if (colorScheme == null) {
colorScheme = new ColorScheme();
}
this.colorScheme = colorScheme;
notifyDataSetChanged();
}
@Override
public int getCount() {
return DATA.length;
}
@Override
public Object getItem(int position) {
return DATA[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(parent.getContext());
textView.setGravity(Gravity.CENTER);
textView.setTextSize(15);
textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
int padding = (int) (parent.getResources().getDisplayMetrics().density * 10);
textView.setPadding(0, padding, 0, padding);
textView.setText(DATA[position]);
textView.setBackgroundColor(colorScheme.weekBackgroundColor());
textView.setTextColor(colorScheme.weekTextColor());
return textView;
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/listener/OnPageChangeCallback.java
================================================
package com.github.gzuliyujiang.calendarpicker.listener;
import androidx.annotation.Px;
public abstract class OnPageChangeCallback {
/**
* This method will be invoked when the current page is scrolled, either as part
* of a programmatically initiated smooth scroll or a user initiated touch scroll.
*
* @param position Position index of the first page currently being displayed.
* Page position+1 will be visible if positionOffset is nonzero.
* @param positionOffset Value from [0, 1) indicating the offset from the page at position.
* @param positionOffsetPixels Value in pixels indicating the offset from position.
*/
public void onPageScrolled(int position, float positionOffset,
@Px int positionOffsetPixels) {
}
/**
* This method will be invoked when a new page becomes selected. Animation is not
* necessarily complete.
*
* @param position Position index of the new selected page.
*/
public void onPageSelected(int position) {
}
/**
* Called when the scroll state changes. Useful for discovering when the user begins
* dragging, when a fake drag is started, when the pager is automatically settling to the
* current page, or when it is fully stopped/idle. {@code state} can be one of {@link
*/
public void onPageScrollStateChanged(int state) {
}
}
================================================
FILE: CalendarPicker/src/main/java/com/github/gzuliyujiang/calendarpicker/listener/ScrollEventAdapter.java
================================================
/*
* Copyright 2018 The Android Open Source Project
*
* 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.github.gzuliyujiang.calendarpicker.listener;
import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_DRAGGING;
import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE;
import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_SETTLING;
import static java.lang.annotation.RetentionPolicy.SOURCE;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.lang.annotation.Retention;
import java.util.Locale;
/**
* Recy页面滑动的监控
*
* 改自ViewPager2
*
*/
public final class ScrollEventAdapter extends RecyclerView.OnScrollListener {
@Retention(SOURCE)
@IntDef({STATE_IDLE, STATE_IN_PROGRESS_MANUAL_DRAG, STATE_IN_PROGRESS_SMOOTH_SCROLL,
STATE_IN_PROGRESS_IMMEDIATE_SCROLL, STATE_IN_PROGRESS_FAKE_DRAG})
private @interface AdapterState {
}
private static final int STATE_IDLE = 0;
private static final int STATE_IN_PROGRESS_MANUAL_DRAG = 1;
private static final int STATE_IN_PROGRESS_SMOOTH_SCROLL = 2;
private static final int STATE_IN_PROGRESS_IMMEDIATE_SCROLL = 3;
private static final int STATE_IN_PROGRESS_FAKE_DRAG = 4;
private static final int NO_POSITION = -1;
private OnPageChangeCallback mCallback;
private final @NonNull RecyclerView mRecyclerView;
private final @NonNull LinearLayoutManager mLayoutManager;
// state related fields
private @AdapterState int mAdapterState;
private int mScrollState;
private ScrollEventValues mScrollValues;
private int mDragStartPosition;
private int mTarget;
private boolean mDispatchSelected;
private boolean mScrollHappened;
private boolean mDataSetChangeHappened;
private boolean mFakeDragging;
public ScrollEventAdapter(@NonNull RecyclerView recyclerView) {
mRecyclerView = recyclerView;
if (mRecyclerView.getLayoutManager() instanceof LinearLayoutManager) {
mLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
} else {
throw new RuntimeException("need RecyclerView use LinearLayoutManager!!!!!!!!!!!!");
}
mScrollValues = new ScrollEventValues();
resetState();
}
private void resetState() {
mAdapterState = STATE_IDLE;
mScrollState = SCROLL_STATE_IDLE;
mScrollValues.reset();
mDragStartPosition = NO_POSITION;
mTarget = NO_POSITION;
mDispatchSelected = false;
mScrollHappened = false;
mFakeDragging = false;
mDataSetChangeHappened = false;
}
/**
* This method only deals with some cases of {@link AdapterState} transitions. The rest of
* the state transition implementation is in the {@link #onScrolled} method.
*/
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
// User started a drag (not dragging -> dragging)
if ((mAdapterState != STATE_IN_PROGRESS_MANUAL_DRAG
|| mScrollState != SCROLL_STATE_DRAGGING)
&& newState == SCROLL_STATE_DRAGGING) {
startDrag(false);
return;
}
// Drag is released, RecyclerView is snapping to page (dragging -> settling)
// Note that mAdapterState is not updated, to remember we were dragging when settling
if (isInAnyDraggingState() && newState == SCROLL_STATE_SETTLING) {
// Only go through the settling phase if the drag actually moved the page
if (mScrollHappened) {
dispatchStateChanged(SCROLL_STATE_SETTLING);
// Determine target page and dispatch onPageSelected on next scroll event
mDispatchSelected = true;
}
return;
}
// Drag is finished (dragging || settling -> idle)
if (isInAnyDraggingState() && newState == SCROLL_STATE_IDLE) {
boolean dispatchIdle = false;
updateScrollEventValues();
if (!mScrollHappened) {
// Pages didn't move during drag, so either we're at the start or end of the list,
// or there are no pages at all.
// In the first case, ViewPager's contract requires at least one scroll event.
// In the second case, don't send that scroll event
if (mScrollValues.mPosition != RecyclerView.NO_POSITION) {
dispatchScrolled(mScrollValues.mPosition, 0f, 0);
}
dispatchIdle = true;
} else if (mScrollValues.mOffsetPx == 0) {
// Normally we dispatch the selected page and go to idle in onScrolled when
// mOffsetPx == 0, but in this case the drag was still ongoing when onScrolled was
// called, so that didn't happen. And since mOffsetPx == 0, there will be no further
// scroll events, so fire the onPageSelected event and go to idle now.
// Note that if we _did_ go to idle in that last onScrolled event, this code will
// not be executed because mAdapterState has been reset to STATE_IDLE.
dispatchIdle = true;
if (mDragStartPosition != mScrollValues.mPosition) {
dispatchSelected(mScrollValues.mPosition);
}
}
if (dispatchIdle) {
// Normally idle is fired in last onScrolled call, but either onScrolled was never
// called, or we were still dragging when the last onScrolled was called
dispatchStateChanged(SCROLL_STATE_IDLE);
resetState();
}
}
if (mAdapterState == STATE_IN_PROGRESS_SMOOTH_SCROLL
&& newState == SCROLL_STATE_IDLE && mDataSetChangeHappened) {
updateScrollEventValues();
if (mScrollValues.mOffsetPx == 0) {
if (mTarget != mScrollValues.mPosition) {
dispatchSelected(
mScrollValues.mPosition == NO_POSITION ? 0 : mScrollValues.mPosition);
}
dispatchStateChanged(SCROLL_STATE_IDLE);
resetState();
}
}
}
/**
* This method only deals with some cases of {@link AdapterState} transitions. The rest of
* the state transition implementation is in the {@link #onScrollStateChanged} method.
*/
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
mScrollHappened = true;
updateScrollEventValues();
if (mDispatchSelected) {
// Drag started settling, need to calculate target page and dispatch onPageSelected now
mDispatchSelected = false;
boolean scrollingForward = dy > 0 || (dy == 0 && dx < 0 == (mLayoutManager.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL));
// "&& values.mOffsetPx != 0": filters special case where we're scrolling forward and
// the first scroll event after settling already got us at the target
mTarget = scrollingForward && mScrollValues.mOffsetPx != 0
? mScrollValues.mPosition + 1 : mScrollValues.mPosition;
if (mDragStartPosition != mTarget) {
dispatchSelected(mTarget);
}
} else if (mAdapterState == STATE_IDLE) {
// onScrolled while IDLE means RV has just been populated after an adapter has been set.
// Contract requires us to fire onPageSelected as well.
int position = mScrollValues.mPosition;
// Contract forbids us to send position = -1 though
dispatchSelected(position == NO_POSITION ? 0 : position);
}
// If position = -1, there are no items. Contract says to send position = 0 instead.
dispatchScrolled(mScrollValues.mPosition == NO_POSITION ? 0 : mScrollValues.mPosition,
mScrollValues.mOffset, mScrollValues.mOffsetPx);
// Dispatch idle in onScrolled instead of in onScrollStateChanged because RecyclerView
// doesn't send IDLE event when using setCurrentItem(x, false)
if ((mScrollValues.mPosition == mTarget || mTarget == NO_POSITION)
&& mScrollValues.mOffsetPx == 0 && !(mScrollState == SCROLL_STATE_DRAGGING)) {
// When the target page is reached and the user is not dragging anymore, we're settled,
// so go to idle.
// Special case and a bit of a hack when mTarget == NO_POSITION: RecyclerView is being
// initialized and fires a single scroll event. This flags mScrollHappened, so we need
// to reset our state. However, we don't want to dispatch idle. But that won't happen;
// because we were already idle.
dispatchStateChanged(SCROLL_STATE_IDLE);
resetState();
}
}
/**
* Calculates the current position and the offset (as a percentage and in pixels) of that
* position from the center.
*/
private void updateScrollEventValues() {
ScrollEventValues values = mScrollValues;
values.mPosition = mLayoutManager.findFirstVisibleItemPosition();
if (values.mPosition == RecyclerView.NO_POSITION) {
values.reset();
return;
}
View firstVisibleView = mLayoutManager.findViewByPosition(values.mPosition);
if (firstVisibleView == null) {
values.reset();
return;
}
int leftDecorations = mLayoutManager.getLeftDecorationWidth(firstVisibleView);
int rightDecorations = mLayoutManager.getRightDecorationWidth(firstVisibleView);
int topDecorations = mLayoutManager.getTopDecorationHeight(firstVisibleView);
int bottomDecorations = mLayoutManager.getBottomDecorationHeight(firstVisibleView);
LayoutParams params = firstVisibleView.getLayoutParams();
if (params instanceof MarginLayoutParams) {
MarginLayoutParams margin = (MarginLayoutParams) params;
leftDecorations += margin.leftMargin;
rightDecorations += margin.rightMargin;
topDecorations += margin.topMargin;
bottomDecorations += margin.bottomMargin;
}
int decoratedHeight = firstVisibleView.getHeight() + topDecorations + bottomDecorations;
int decoratedWidth = firstVisibleView.getWidth() + leftDecorations + rightDecorations;
boolean isHorizontal = mLayoutManager.getOrientation() == RecyclerView.HORIZONTAL;
int start, sizePx;
if (isHorizontal) {
sizePx = decoratedWidth;
start = firstVisibleView.getLeft() - leftDecorations - mRecyclerView.getPaddingLeft();
if (mLayoutManager.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL) {
start = -start;
}
} else {
sizePx = decoratedHeight;
start = firstVisibleView.getTop() - topDecorations - mRecyclerView.getPaddingTop();
}
values.mOffsetPx = -start;
if (values.mOffsetPx < 0) {
/* // We're in an error state. Figure out if this might have been caused
// by animateLayoutChanges and throw a descriptive exception if so
if (new AnimateLayoutChangeDetector(mLayoutManager).mayHaveInterferingAnimations()) {
throw new IllegalStateException("Page(s) contain a ViewGroup with a "
+ "LayoutTransition (or animateLayoutChanges=\"true\"), which interferes "
+ "with the scrolling animation. Make sure to call getLayoutTransition()"
+ ".setAnimateParentHierarchy(false) on all ViewGroups with a "
+ "LayoutTransition before an animation is started.");
}*/
// Throw a generic exception otherwise
throw new IllegalStateException(String.format(Locale.US, "Page can only be offset by a "
+ "positive amount, not by %d", values.mOffsetPx));
}
values.mOffset = sizePx == 0 ? 0 : (float) values.mOffsetPx / sizePx;
}
private void startDrag(boolean isFakeDrag) {
mFakeDragging = isFakeDrag;
mAdapterState = isFakeDrag ? STATE_IN_PROGRESS_FAKE_DRAG : STATE_IN_PROGRESS_MANUAL_DRAG;
if (mTarget != NO_POSITION) {
// Target was set means we were settling to that target
// Update "drag start page" to reflect the page that ViewPager2 thinks it is at
mDragStartPosition = mTarget;
// Reset target because drags have no target until released
mTarget = NO_POSITION;
} else if (mDragStartPosition == NO_POSITION) {
// ViewPager2 was at rest, set "drag start page" to current page
mDragStartPosition = getPosition();
}
dispatchStateChanged(SCROLL_STATE_DRAGGING);
}
void notifyDataSetChangeHappened() {
mDataSetChangeHappened = true;
}
/**
* Let the adapter know a programmatic scroll was initiated.
*/
void notifyProgrammaticScroll(int target, boolean smooth) {
mAdapterState = smooth
? STATE_IN_PROGRESS_SMOOTH_SCROLL
: STATE_IN_PROGRESS_IMMEDIATE_SCROLL;
// mFakeDragging is true when a fake drag is interrupted by an a11y command
// set it to false so endFakeDrag won't fling the RecyclerView
mFakeDragging = false;
boolean hasNewTarget = mTarget != target;
mTarget = target;
dispatchStateChanged(SCROLL_STATE_SETTLING);
if (hasNewTarget) {
dispatchSelected(target);
}
}
/**
* Let the adapter know that a fake drag has started.
*/
void notifyBeginFakeDrag() {
mAdapterState = STATE_IN_PROGRESS_FAKE_DRAG;
startDrag(true);
}
/**
* Let the adapter know that a fake drag has ended.
*/
void notifyEndFakeDrag() {
if (isDragging() && !mFakeDragging) {
// Real drag has already taken over, no need to post process the fake drag
return;
}
mFakeDragging = false;
updateScrollEventValues();
if (mScrollValues.mOffsetPx == 0) {
// We're snapped, so dispatch an IDLE event
if (mScrollValues.mPosition != mDragStartPosition) {
dispatchSelected(mScrollValues.mPosition);
}
dispatchStateChanged(SCROLL_STATE_IDLE);
resetState();
} else {
// We're not snapped, so dispatch a SETTLING event
dispatchStateChanged(SCROLL_STATE_SETTLING);
}
}
public void setOnPageChangeCallback(OnPageChangeCallback callback) {
mCallback = callback;
}
int getScrollState() {
return mScrollState;
}
/**
* @return {@code true} if there is no known scroll in progress
*/
boolean isIdle() {
return mScrollState == SCROLL_STATE_IDLE;
}
/**
* @return {@code true} if the ViewPager2 is being dragged. Returns {@code false} from the
* moment the ViewPager2 starts settling or goes idle.
*/
boolean isDragging() {
return mScrollState == SCROLL_STATE_DRAGGING;
}
boolean isFakeDragging() {
return mFakeDragging;
}
/**
* Checks if the adapter state (not the scroll state) is in the manual or fake dragging state.
*
* @return {@code true} if {@link #mAdapterState} is either {@link
* #STATE_IN_PROGRESS_MANUAL_DRAG} or {@link #STATE_IN_PROGRESS_FAKE_DRAG}
*/
private boolean isInAnyDraggingState() {
return mAdapterState == STATE_IN_PROGRESS_MANUAL_DRAG
|| mAdapterState == STATE_IN_PROGRESS_FAKE_DRAG;
}
/**
* Calculates the scroll position of the currently visible item of the ViewPager relative to its
* width. Calculated by adding the fraction by which the first visible item is off screen to its
* adapter position. E.g., if the ViewPager is currently scrolling from the second to the third
* page, the returned value will be between 1 and 2. Thus, non-integral values mean that the
* the ViewPager is settling towards its {ViewPager2#getCurrentItem() current item}, or
* the user may be dragging it.
*
* @return The current scroll position of the ViewPager, relative to its width
*/
double getRelativeScrollPosition() {
updateScrollEventValues();
return mScrollValues.mPosition + (double) mScrollValues.mOffset;
}
private void dispatchStateChanged(int state) {
// Callback contract for immediate-scroll requires not having state change notifications,
// but only when there was no smooth scroll in progress.
// By putting a suppress statement in here (rather than next to dispatch calls) we are
// simplifying the code of the class and enforcing the contract in one place.
if (mAdapterState == STATE_IN_PROGRESS_IMMEDIATE_SCROLL
&& mScrollState == SCROLL_STATE_IDLE) {
return;
}
if (mScrollState == state) {
return;
}
mScrollState = state;
if (mCallback != null) {
mCallback.onPageScrollStateChanged(state);
}
}
private void dispatchSelected(int target) {
if (mCallback != null) {
mCallback.onPageSelected(target);
}
}
private void dispatchScrolled(int position, float offset, int offsetPx) {
if (mCallback != null) {
mCallback.onPageScrolled(position, offset, offsetPx);
}
}
private int getPosition() {
return mLayoutManager.findFirstVisibleItemPosition();
}
private static final class ScrollEventValues {
int mPosition;
float mOffset;
int mOffsetPx;
// to avoid a synthetic accessor
ScrollEventValues() {
}
void reset() {
mPosition = RecyclerView.NO_POSITION;
mOffset = 0f;
mOffsetPx = 0;
}
}
}
================================================
FILE: CalendarPicker/src/main/res/layout/calendar_body.xml
================================================
================================================
FILE: ChangeLog.md
================================================
# 更新日志
## 4.1.12 - 2023.07.22
- 避免内存泄露,参阅 [issues#337](https://github.com/gzu-liyujiang/AndroidPicker/issues/337) 。
- 地址选择器内置的省市区数据更新(2022版)。
## 4.1.11 - 2022.11.09
- Fix [issues#322](https://github.com/gzu-liyujiang/AndroidPicker/issues/322) 。
## 4.1.10 - 2022.11.04
- Fix [issues#324](https://github.com/gzu-liyujiang/AndroidPicker/issues/324) 。
- Opt [issues#294](https://github.com/gzu-liyujiang/AndroidPicker/issues/294) [issues#296](https://github.com/gzu-liyujiang/AndroidPicker/issues/296) 。
- `XXXPicker`和`DialogFragment`结合使用示例。
## 4.1.9 - 2022.08.31
- Fix [issues#292](https://github.com/gzu-liyujiang/AndroidPicker/issues/292) 。
- Fix [issues#290](https://github.com/gzu-liyujiang/AndroidPicker/issues/290) 。
- 增加 API 说明文档。
## 4.1.8 - 2022.07.12
- Fix [issues#313](https://github.com/gzu-liyujiang/AndroidPicker/issues/313) 。
- Fix [issues#304](https://github.com/gzu-liyujiang/AndroidPicker/issues/304) 。
- Fix `setStyle`只能在其他设置项之前调用,否则会导致其他设置项失效。
- Fix [issues#293](https://github.com/gzu-liyujiang/AndroidPicker/issues/293) 。
## 4.1.7 - 2022.01.21
- 添加可直接安卓体验的 demo.apk ,参阅 [gitee#note_7765234](https://gitee.com/li_yu_jiang/AndroidPicker#note_7765234)。
- 修复时间选择器未来小时数计算错误问题。
- 日历选择器支持启用左右翻页,参阅 [issues#288](https://github.com/gzu-liyujiang/AndroidPicker/issues/288) 。
- 时间选择器上午下午联动优化,参阅 [issues#284](https://github.com/gzu-liyujiang/AndroidPicker/issues/284) 。
## 4.1.6 - 2021.12.20
- 修复滚轮选择器部分XML属性设置无效问题,参阅 [issues#281](https://github.com/gzu-liyujiang/AndroidPicker/issues/281);
- 修复滚轮选择器设置了选中项文字加大加粗时滑动会导致其他条目错位问题,参阅 [issues#279](https://github.com/gzu-liyujiang/AndroidPicker/issues/279);
## 4.1.5 - 2021.12.05
- 修复日期时间选择器未来日期时间计算错误问题,参阅 [issues#276](https://github.com/gzu-liyujiang/AndroidPicker/issues/276);
- 修复滚轮无法设置白色背景及选中项加粗属性可能无效问题,参阅 [issues#274](https://github.com/gzu-liyujiang/AndroidPicker/issues/274) [issues#268](https://github.com/gzu-liyujiang/AndroidPicker/issues/268);
- 日历日期选择器范围选择允许选择单个日期,参阅 [issues#271](https://github.com/gzu-liyujiang/AndroidPicker/issues/271);
## 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
- 滚轮选择器条目文字过长截取优化;
- 日期时间选择器支持滚动联动不重置开关;
- 滚轮选择器条目滚动展示效果优化;
## 4.1.2 - 2021.11.01
- 修复 Android 7.x 上滚轮选择器选中项重叠问题;
## 4.1.1 - 2021.10.28
- 滚轮选择器选中项文字支持加大加粗;
- 部分细节优化,部分效果图更新;
## 4.1.0 - 2021.10.28
- 支持滚轮选择器选中项设置圆角纯色背景;
- 所以选择器文本国际化,内置支持中文及英文;
- 手机号码前缀选择器数据完善,支持全球的国家及地区;
- 车牌、星座及性别等选择器调整,民族、星座及性别等默认值设置优化;
- 日历选择器支持自定义节日文本;
## 4.0.1 - 2021.09.26
- 优化选择器标题设置,避免设置不生效问题;
## 4.0.0 - 2021.09.18
- 支持设置弹窗样式,内置四种弹窗模式,支持全局弹窗配色,效果请运行Demo;
- 日历日期选择器重构,日历默认选中项展示到当前可见位置,Demo日历日期选择器自定义配色示例;
- 滚轮选择器滚动变化监听器优化,Demo滚轮选择器实时滚动变化监听示例;
- 二三级联动选择器默认值设置优化;
- 文件目录选择器优化及界面调整;
- 颜色选择器优化及界面调整;
- 其他已知的小缺陷修复及代码优化;
## 3.1.2 - 2021.09.13
- 选择器弹窗效果优化,支持启用顶部圆角背景;
- 修正联动选择器当第二三级数据为空时滑动一下会导致滑动失效问题,参阅 [issues#263](https://github.com/gzu-liyujiang/AndroidPicker/issues/263);
- 日历日期选择器默认选中项设置优化,参阅 [CSDN#comments_18122565](https://blog.csdn.net/waplyj/article/details/117857191#comments_18122565);
## 3.1.1 - 2021.08.31
- 图片选择器增加自定义文件类型选择及取消回调;
- 默认值选中设置优化;
- 修复日历日期选择器月份可能缺失问题,参阅 [issues#262](https://github.com/gzu-liyujiang/AndroidPicker/issues/262);
- 日期时间选择器联动滚动优化;
## 3.1.0 - 2021.07.30
- 新增图片选择器(相机+相册+裁剪),可用于头像选择裁剪;
- 修复个别已知的可能的异常;
- 改进12小时制的上下午选择,参阅 [252#issuecomment-888115423](https://github.com/gzu-liyujiang/AndroidPicker/issues/252#issuecomment-888115423);
- 民族选择器支持按民族代码、民族中文名及罗马拼写设置默认值;
## 3.0.8 - 2021.07.21
- 联动/日期/时间选择器有滚轮在滑动中时不允许另外的滚轮滑动,参阅 [issues#251](https://github.com/gzu-liyujiang/AndroidPicker/issues/251);
- 修复日期/时间选择器模式设置可能不生效问题;
- 修复时间选择器12小时制时间显示不正确问题,参阅 [issues#252](https://github.com/gzu-liyujiang/AndroidPicker/issues/252);
## 3.0.7 - 2021.07.15
- 时间选择器设置显示秒无效问题,参阅 [issues#249](https://github.com/gzu-liyujiang/AndroidPicker/issues/249);
## 3.0.6 - 2021.07.08
- 滚轮控件弯曲模式增加指示线间隔控制;
## 3.0.5 - 2021.07.02
- 日期/时间选择器确保设置的日期时间范围有效,参阅 [issues#244](https://github.com/gzu-liyujiang/AndroidPicker/issues/244);
## 3.0.4 - 2021.06.30
- 联动选择器增加初始数据加载进度控制,地址选择器数据加载过程避免卡顿感;
- 以日期选择器为例示范选择器界面个性化用法,参阅 [issues#243](https://github.com/gzu-liyujiang/AndroidPicker/issues/243);
- 地址选择器新增国家统计局省市区数据示例;
## 3.0.3 - 2021.06.25
- 滚轮控件默认值设置优化;
- 小数选择器格式化示例;
## 3.0.2 - 2021.06.12
- 发布遗漏了的地址选择器模块;
## 3.0.1 - 2021.06.12
- 增加民族选择器;
- 抽取地址选择器模块,默认附带来源于国家统计局的省市区数据;
- 地址选择器演示添加新的省市区JSON数据;
## 3.0.0 - 2021.06.11
- 时隔两年,大刀阔斧的重构回归,全新的滚轮选择器、文件选择器、颜色选择器、日历选择器;
## 2.0.0 - 2020.10.20
- 从 Support v4/v7 迁移到 AndroidX ;
## 1.5.6.20181018 - 2018.10.18
- 兼容 Android 9.0(COMPILE_SDK_VERSION=28);
## ~~1.5.6 - 2018.06.01~~
- 避免因 Android9.0 导致的编译问题;
- WheelView 优化……
## ~~1.5.5 - 2017.09.01~~
- 代码优化,新增一些设置方法;
## ~~1.5.4 - 2017.08.19~~
- 修复时间选择器 bug;
- 联动选择器支持设置宽度填充;
## ~~1.5.3 - 2017.07.01~~
- 修复默认无法选中第一项的问题;
- 新增日期时间选择器联动时是否重置下一级的索引的控制方法;
- 修复日期选择器 setSelectedItem 传值错误可能导致的奔溃问题;
- 新增多项选择器;
## ~~1.5.2 - 2017.05.14~~
- 有童鞋反应动画太慢,移除选择器默认的动画;
- DoublePicker 支持设置前缀及后缀标签;
- 修复联动选择器不滑动(使用默认项)就确定时的奔溃问题;
- 修复 setXXRangeStart()及 setXXRangeEnd()二者调用顺序颠倒出现的问题;
## ~~1.5.1 - 2017.05.03~~
- 日期时间选择器支持设置各项平分布局;
- 顶部按钮文字颜色默认调整为青蓝风格;
- 修复日期时间选择联动适时取值不对问题;
- 支持设置选中项的背景色及透明度;
- 增加港澳台的县级城市数据;
- 添加双项选择器,选择两项,数据不联动;
## ~~1.5.0 - 2017.05.01~~
- 基于 View 重构 WheelView;
- 联动选择器支持直接传入对象;
- 动画默认为 500 毫秒,应该不会太慢了;
## ~~1.4.6 - 2017.04.10~~
- 弹窗内部部分代码重构;
- 使用透明渐变位移动画,缓解初始化时默认选中项显示跳动问题;
- 日期时间选择联动时重置联动项的索引为 0;
## ~~1.4.5 - 2017.03.19~~
- - 解决选择器部分选项概率性不显示问题,感谢亮亮同学;
## ~~1.4.4 - 2017.03.8~~
- 解决滑动选项闪烁问题;修复已知的数组越界异常问题;
## ~~1.4.3 - 2017.01.16~~
- 解决默认选项选中失效问题;
## ~~1.4.2 - 2017.01.09~~
- 滑轮选择器默认禁用循环滚动;
- 日期时间选择器宽度兼容 480x800;
- demo 的地址选择器重构,方便回调;
## ~~1.4.1 - 2017.01.08~~
- 修复选择器在 Android4.X 版本上奔溃问题;
- 改进文件目录选择器的路径指示效果;
## ~~1.4.0 - 2017.01.07~~
- 重构 WheelView,大批量数据时滑动性能显著提升;
- 改进颜色选择器的颜色选择预览效果;
- 滑轮选择器可设置阴影效果;
## ~~1.3.5 - 2017.01.04~~
- 选择器引用的图片改成字节数组形式;
- 解决默认主题设置为 Material 时顶部按钮内边距过宽问题;
- demo 添加沉浸式状态栏功能
## ~~1.3.4 - 2017.01.01~~
- 重构日期时间选择器,默认选中当前日期时间;
- 支持设置顶部的左右边距;
- 优化多级选择器的数据联动;
- 支持将库打包为纯 jar 包,通过 jar 依赖;
## ~~1.3.3 - 2016.12.21~~
- 修复日期选择器月份范围设置可能无效的 bug;
- 支持设置顶部按钮按下状态的文字颜色;
- 选择器条目默认显示由 3 条改为 5 条,并修改条目文字的内边距;
## ~~1.3.2 - 2016.12.19~~
- 支持设置选中项分割线的宽比例、透明度、粗度;
- 修复小数选择器设置默认选择项可能无效的 bug;
- 添加中国大陆车牌号码选择器;
## ~~1.3.1 - 2016.12.17~~
- 对 1.2.\*版本作兼容处理。
## ~~1.3.0 - 2016.12.15~~
- 支持设置选择器主体背景颜色;
- 所有选择器支持滑动实时监听;
- 所有选择器支持内嵌到其他视图容器;
- 重构单项选择器,数字选择器支持小数;
## ~~1.2.4 - 2016.11.23~~
- 修复默认选中第一项时颜色不高亮问题;
- 修复联动选择器数组越界问题;
- 多级联动选择器支持设置各列比例;
- 时期及时间选择器内部逻辑修改;
## ~~1.2.3 - 2016.10.13~~
- 修复日期选择器中当开始年份和结束年份相同时的 Bug;
- 修复年月日时分选择器的设置默认值时分没有补零的 Bug;
- 支持所有选择器顶部高度及字号的设置;
- 修复日期选择器选中项显示有误问题,感谢@msdx;
## ~~1.2.2 - 2016.09.01~~
- 日期选择器支持年份正序和逆序;
- 时间选择器可以限定时分范围;
- 选择器可指定弹框显示位置,如居中;
- 日期选择器可以限定年月日范围;
- 修复几个已知的 bug;
## ~~1.2.1 - 2016.07.23~~
- 地址选择器增加获取城市编码,感谢@weishd;
- 修改注释,优化部分代码,修复一个已知的 bug;
## ~~1.1.3 - 2016.06.14~~
- 添加日期时间选择器,感谢@dongzhaoqi;
## ~~1.1.2 - 2016.05.06~~
- 添加二三级联动选择器;
- 文件选择器布局调整;
## ~~1.1.1 - 2016.04.23~~
- 地址选择器支持只选择省和市、不能混淆某些类,感谢@Wastrel 及@lutas2000;
## ~~1.1.0 - 2016.01.29~~
- 添加注解约束,如“setOffset()”只能是 1 至 4;
- 所有枚举类改为常量来表示,据说这样可以节约内存;
- 支持自定义选择器的顶部及底部的视图;
- 支持使用第三方动画库来实现窗口动画;
## ~~1.0.3 - 2016.01.19~~
- 日期时间、地址、单项、数字等选择器支持伪循环滚动。
## ~~1.0.2 - 2016.01.15~~
- 年或月变动时,保持之前选择的日不动:如果之前选择的日是之前年月的最大日,则日自动为该年月的最大日。
## ~~1.0.1 - 2016.01.14~~
- 精简文件选择器的数据适配器;
- 添加选择器顶部确定、取消按钮所在容器的背景色设置。
## ~~1.0.0 - 2016.01.13~~
- 发布到 jcenter,支持远程 maven 依赖。
================================================
FILE: ColorPicker/README.md
================================================
# 颜色选择器
颜色选择面板,改自[AndroidColorPicker](https://github.com/jbruchanov/AndroidColorPicker)
================================================
FILE: ColorPicker/build.gradle
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
apply from: "${rootDir}/gradle/library.gradle"
apply from: "${rootDir}/gradle/publish.gradle"
dependencies {
implementation androidxLibrary.annotation
api project(':Common')
}
================================================
FILE: ColorPicker/consumer-rules.pro
================================================
# 本库模块专用的混淆规则
================================================
FILE: ColorPicker/src/main/AndroidManifest.xml
================================================
================================================
FILE: ColorPicker/src/main/java/com/github/gzuliyujiang/colorpicker/BrightnessGradientView.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.colorpicker;
import android.content.Context;
import android.util.AttributeSet;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/10 12:59
*/
public class BrightnessGradientView extends ColorGradientView {
public BrightnessGradientView(Context context) {
super(context);
asBrightnessGradient();
}
public BrightnessGradientView(Context context, AttributeSet attrs) {
super(context, attrs);
asBrightnessGradient();
}
public BrightnessGradientView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
asBrightnessGradient();
}
}
================================================
FILE: ColorPicker/src/main/java/com/github/gzuliyujiang/colorpicker/ColorGradientView.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.colorpicker;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.DrawableRes;
/**
* 颜色选择面板。Adapted from https://github.com/jbruchanov/AndroidColorPicker
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2015/7/20
*/
@SuppressWarnings("unused")
public class ColorGradientView extends View {
private static final int[] GRAD_COLORS = new int[]{Color.RED, Color.YELLOW, Color.GREEN,
Color.CYAN, Color.BLUE, Color.MAGENTA, Color.RED};
private static final int[] GRAD_ALPHA = new int[]{Color.WHITE, Color.TRANSPARENT};
private ColorGradientView gradientView;
private Shader mShader;
private Drawable mPointerDrawable;
private Paint mPaint;
private Paint mPaintBackground;
private final RectF mGradientRect = new RectF();
private final float[] mHSV = new float[]{1f, 1f, 1f};
private final int[] mSelectedColorGradient = new int[]{0, Color.BLACK};
private float mRadius = 0;
private int mSelectedColor = 0;
private boolean brightnessGradient = false;
private int mLastX = Integer.MIN_VALUE;
private int mLastY;
private int mPointerHeight;
private int mPointerWidth;
private boolean mLockPointerInBounds = false;
private OnColorChangedListener mOnColorChangedListener;
public ColorGradientView(Context context) {
super(context);
init();
}
public ColorGradientView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ColorGradientView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setClickable(true);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintBackground = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintBackground.setColor(Color.WHITE);
setLayerType(View.LAYER_TYPE_SOFTWARE, isInEditMode() ? null : mPaint);
setPointerDrawable(R.mipmap.color_picker_pointer);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int desiredWidth = 0;
int desiredHeight = 0;
if (mPointerDrawable != null) {
desiredHeight = mPointerDrawable.getIntrinsicHeight();
//this is nonsense, but at least have something than 0
desiredWidth = mPointerDrawable.getIntrinsicWidth();
}
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
//Measure Width
if (widthMode == MeasureSpec.EXACTLY) {
//Must be this size
width = widthSize;
} else if (widthMode == MeasureSpec.AT_MOST) {
//Can't be bigger than...
width = Math.min(desiredWidth, widthSize);
} else {
//Be whatever you want
width = desiredWidth;
}
//Measure Height
if (heightMode == MeasureSpec.EXACTLY) {
//Must be this size
height = heightSize;
} else if (heightMode == MeasureSpec.AT_MOST) {
//Can't be bigger than...
height = Math.min(desiredHeight, heightSize);
} else {
//Be whatever you want
height = desiredHeight;
}
//MUST CALL THIS
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mShader != null) {
canvas.drawRoundRect(mGradientRect, mRadius, mRadius, mPaintBackground);
canvas.drawRoundRect(mGradientRect, mRadius, mRadius, mPaint);
}
onDrawPointer(canvas);
}
private void onDrawPointer(Canvas canvas) {
if (mPointerDrawable != null) {
int vh = getHeight();
int pwh = mPointerWidth >> 1;
int phh = mPointerHeight >> 1;
float tx, ty;
if (!brightnessGradient) {
// fixed: 17-1-7 Math operands should be cast before assignment
tx = (float) (mLastX - pwh);
ty = (float) (mLastY - phh);
if (mLockPointerInBounds) {
tx = Math.max(mGradientRect.left, Math.min(tx, mGradientRect.right - mPointerWidth));
ty = Math.max(mGradientRect.top, Math.min(ty, mGradientRect.bottom - mPointerHeight));
} else {
tx = Math.max(mGradientRect.left - pwh, Math.min(tx, mGradientRect.right - pwh));
ty = Math.max(mGradientRect.top - pwh, Math.min(ty, mGradientRect.bottom - phh));
}
} else {//vertical lock
tx = (float) (mLastX - pwh);
ty = mPointerHeight != mPointerDrawable.getIntrinsicHeight() ? (vh >> 1) - phh : 0;
if (mLockPointerInBounds) {
tx = Math.max(mGradientRect.left, Math.min(tx, mGradientRect.right - mPointerWidth));
ty = Math.max(mGradientRect.top, Math.min(ty, mGradientRect.bottom - mPointerHeight));
} else {
tx = Math.max(mGradientRect.left - pwh, Math.min(tx, mGradientRect.right - pwh));
ty = Math.max(mGradientRect.top - pwh, Math.min(ty, mGradientRect.bottom - phh));
}
}
canvas.translate(tx, ty);
mPointerDrawable.draw(canvas);
canvas.translate(-tx, -ty);
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
mGradientRect.set(getPaddingLeft(), getPaddingTop(), right - left - getPaddingRight(), bottom - top - getPaddingBottom());
if (changed) {
buildShader();
}
if (mPointerDrawable != null) {
int h = (int) mGradientRect.height();
int ph = mPointerDrawable.getIntrinsicHeight();
int pw = mPointerDrawable.getIntrinsicWidth();
mPointerHeight = ph;
mPointerWidth = pw;
if (h < ph) {
mPointerHeight = h;
mPointerWidth = (int) (pw * (h / (float) ph));
}
mPointerDrawable.setBounds(0, 0, mPointerWidth, mPointerHeight);
updatePointerPosition();
}
}
private void buildShader() {
if (brightnessGradient) {
mShader = new LinearGradient(mGradientRect.left, mGradientRect.top, mGradientRect.right, mGradientRect.top, mSelectedColorGradient, null, Shader.TileMode.CLAMP);
} else {
LinearGradient gradientShader = new LinearGradient(mGradientRect.left, mGradientRect.top, mGradientRect.right, mGradientRect.top, GRAD_COLORS, null, Shader.TileMode.CLAMP);
LinearGradient alphaShader = new LinearGradient(0, mGradientRect.top + (mGradientRect.height() / 3), 0, mGradientRect.bottom, GRAD_ALPHA, null, Shader.TileMode.CLAMP);
mShader = new ComposeShader(alphaShader, gradientShader, PorterDuff.Mode.MULTIPLY);
}
mPaint.setShader(mShader);
}
public void setRadius(float radius) {
// fixed: 17-1-7 Equality tests should not be made with floating point values.
if ((int) radius != (int) mRadius) {
mRadius = radius;
invalidate();
}
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
mLastX = (int) event.getX();
mLastY = (int) event.getY();
onUpdateColorSelection(mLastX, mLastY);
invalidate();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return super.onTouchEvent(event);
}
protected void onUpdateColorSelection(int x, int y) {
x = (int) Math.max(mGradientRect.left, Math.min(x, mGradientRect.right));
y = (int) Math.max(mGradientRect.top, Math.min(y, mGradientRect.bottom));
if (brightnessGradient) {
float b = pointToValueBrightness(x);
mHSV[2] = b;
mSelectedColor = Color.HSVToColor(mHSV);
} else {
float hue = pointToHue(x);
float sat = pointToSaturation(y);
mHSV[0] = hue;
mHSV[1] = sat;
mHSV[2] = 1f;
mSelectedColor = Color.HSVToColor(mHSV);
}
dispatchColorChanged(mSelectedColor);
}
protected void dispatchColorChanged(int color) {
if (gradientView != null) {
gradientView.setColor(color, false);
}
if (mOnColorChangedListener != null) {
mOnColorChangedListener.onColorChanged(this, color);
}
}
public final void asBrightnessGradient() {
this.brightnessGradient = true;
}
public void setBrightnessGradientView(ColorGradientView gradientView) {
if (this.gradientView != gradientView) {
this.gradientView = gradientView;
if (this.gradientView != null) {
this.gradientView.asBrightnessGradient();
this.gradientView.setColor(mSelectedColor);
}
}
}
public int getSelectedColor() {
return mSelectedColor;
}
public void setColor(int selectedColor) {
setColor(selectedColor, true);
}
protected void setColor(int selectedColor, boolean updatePointers) {
Color.colorToHSV(selectedColor, mHSV);
if (brightnessGradient) {
mSelectedColorGradient[0] = getColorForGradient(mHSV);
mSelectedColor = Color.HSVToColor(mHSV);
buildShader();
if (mLastX != Integer.MIN_VALUE) {
mHSV[2] = pointToValueBrightness(mLastX);
}
selectedColor = Color.HSVToColor(mHSV);
}
if (updatePointers) {
updatePointerPosition();
}
mSelectedColor = selectedColor;
invalidate();
dispatchColorChanged(mSelectedColor);
}
private int getColorForGradient(float[] hsv) {
// fixed: 17-1-7 Equality tests should not be made with floating point values.
if ((int) hsv[2] != 1) {
float oldV = hsv[2];
hsv[2] = 1;
int color = Color.HSVToColor(hsv);
hsv[2] = oldV;
return color;
} else {
return Color.HSVToColor(hsv);
}
}
private void updatePointerPosition() {
if (mGradientRect.width() != 0 && mGradientRect.height() != 0) {
if (!brightnessGradient) {
mLastX = hueToPoint(mHSV[0]);
mLastY = saturationToPoint(mHSV[1]);
} else {
mLastX = brightnessToPoint(mHSV[2]);
}
}
}
public void setOnColorChangedListener(OnColorChangedListener onColorChangedListener) {
mOnColorChangedListener = onColorChangedListener;
}
//region HSL math
private float pointToHue(float x) {
x = x - mGradientRect.left;
return x * 360f / mGradientRect.width();
}
private int hueToPoint(float hue) {
return (int) (mGradientRect.left + ((hue * mGradientRect.width()) / 360));
}
private float pointToSaturation(float y) {
y = y - mGradientRect.top;
return 1 - (1.f / mGradientRect.height() * y);
}
private int saturationToPoint(float sat) {
sat = 1 - sat;
return (int) (mGradientRect.top + (mGradientRect.height() * sat));
}
private float pointToValueBrightness(float x) {
x = x - mGradientRect.left;
return 1 - (1.f / mGradientRect.width() * x);
}
private int brightnessToPoint(float val) {
val = 1 - val;
return (int) (mGradientRect.left + (mGradientRect.width() * val));
}
//endregion HSL math
public void setPointerDrawable(Drawable pointerDrawable) {
if (pointerDrawable == null) {
return;
}
if (mPointerDrawable != pointerDrawable) {
mPointerDrawable = pointerDrawable;
requestLayout();
}
}
public void setPointerDrawable(@DrawableRes int pointerDrawable) {
setPointerDrawable(getResources().getDrawable(pointerDrawable));
}
public void recycle() {
mPaint = null;
mPaintBackground = null;
if (mPointerDrawable instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable) mPointerDrawable).getBitmap();
if (null != bitmap && !bitmap.isRecycled()) {
bitmap.recycle();
}
}
}
public void setLockPointerInBounds(boolean lockPointerInBounds) {
if (lockPointerInBounds != mLockPointerInBounds) {
mLockPointerInBounds = lockPointerInBounds;
invalidate();
}
}
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.isBrightnessGradient = brightnessGradient;
ss.color = mSelectedColor;
return ss;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
brightnessGradient = ss.isBrightnessGradient;
setColor(ss.color, true);
}
}
================================================
FILE: ColorPicker/src/main/java/com/github/gzuliyujiang/colorpicker/ColorPicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.colorpicker;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.CallSuper;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.dialog.DialogLog;
import com.github.gzuliyujiang.dialog.ModalDialog;
import java.util.Locale;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/10 10:54
*/
@SuppressWarnings("unused")
public class ColorPicker extends ModalDialog implements OnColorChangedListener {
protected ColorGradientView colorGradientView;
protected BrightnessGradientView brightnessGradientView;
private boolean initialized = false;
private int initColor = Color.YELLOW;
private OnColorPickedListener onColorPickedListener;
public ColorPicker(@NonNull Activity activity) {
super(activity);
}
public ColorPicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@NonNull
@Override
protected View createBodyView() {
return View.inflate(activity, R.layout.color_picker_content, null);
}
@CallSuper
@Override
protected void initView() {
super.initView();
colorGradientView = contentView.findViewById(R.id.color_picker_panel);
brightnessGradientView = contentView.findViewById(R.id.color_picker_bright);
}
@CallSuper
@Override
protected void initData() {
super.initData();
initialized = true;
if (cancelView != null) {
cancelView.setOnClickListener(this);
}
if (okView != null) {
okView.setOnClickListener(this);
}
colorGradientView.setOnColorChangedListener(this);
brightnessGradientView.setOnColorChangedListener(this);
colorGradientView.setBrightnessGradientView(brightnessGradientView);
//将触发onColorChanged,故必须先待其他控件初始化完成后才能调用
colorGradientView.setColor(initColor);
}
@Override
protected void onCancel() {
}
@Override
protected void onOk() {
if (onColorPickedListener != null) {
onColorPickedListener.onColorPicked(getCurrentColor());
}
}
@Override
public void onColorChanged(ColorGradientView gradientView, @ColorInt int color) {
updateCurrentColor(color);
}
private void updateCurrentColor(@ColorInt int color) {
titleView.setText(Utils.toHexString(color, false).toUpperCase(Locale.PRC));
titleView.setTextColor(color);
titleView.setShadowLayer(10, 5, 5, Utils.reverseColor(color));
}
/**
* 设置初始默认颜色
*
* @param initColor 颜色值,如:0xFFFF00FF
*/
public void setInitColor(@ColorInt int initColor) {
this.initColor = initColor;
if (initialized) {
colorGradientView.setColor(initColor);
}
}
/**
* 设置颜色选择监听器
*/
public void setOnColorPickListener(OnColorPickedListener onColorPickedListener) {
this.onColorPickedListener = onColorPickedListener;
}
/**
* 获取当前选择的颜色
*/
@ColorInt
public final int getCurrentColor() {
try {
return Color.parseColor("#" + titleView.getText());
} catch (Exception e) {
DialogLog.print(e);
return initColor;
}
}
/**
* 获取16进制颜色值视图
*/
public final TextView getHexView() {
return getTitleView();
}
/**
* 获取颜色面板视图
*/
public final ColorGradientView getColorGradientView() {
return colorGradientView;
}
/**
* 获取亮度面板视图
*/
public final BrightnessGradientView getBrightnessGradientView() {
return brightnessGradientView;
}
}
================================================
FILE: ColorPicker/src/main/java/com/github/gzuliyujiang/colorpicker/OnColorChangedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.colorpicker;
import androidx.annotation.ColorInt;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2015/7/20
*/
public interface OnColorChangedListener {
void onColorChanged(ColorGradientView gradientView, @ColorInt int color);
}
================================================
FILE: ColorPicker/src/main/java/com/github/gzuliyujiang/colorpicker/OnColorPickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.colorpicker;
import androidx.annotation.ColorInt;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2015/7/20
*/
public interface OnColorPickedListener {
void onColorPicked(@ColorInt int pickedColor);
}
================================================
FILE: ColorPicker/src/main/java/com/github/gzuliyujiang/colorpicker/SavedState.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.colorpicker;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.View;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/10 10:56
*/
class SavedState extends View.BaseSavedState {
int color;
boolean isBrightnessGradient;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
super(in);
color = in.readInt();
isBrightnessGradient = in.readInt() == 1;
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(color);
out.writeInt(isBrightnessGradient ? 1 : 0);
}
public static final Creator CREATOR =
new Creator() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
================================================
FILE: ColorPicker/src/main/java/com/github/gzuliyujiang/colorpicker/Utils.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.colorpicker;
import android.graphics.Color;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.dialog.DialogLog;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/10 11:30
*/
class Utils {
@ColorInt
public static int reverseColor(@ColorInt int color) {
int alpha = Color.alpha(color);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb(alpha, 255 - red, 255 - green, 255 - blue);
}
/**
* 转换为6位十六进制颜色代码,不含“#”
*/
@NonNull
public static String toHexString(@ColorInt int color, boolean includeAlpha) {
String alpha = Integer.toHexString(Color.alpha(color));
String red = Integer.toHexString(Color.red(color));
String green = Integer.toHexString(Color.green(color));
String blue = Integer.toHexString(Color.blue(color));
if (alpha.length() == 1) {
alpha = "0" + alpha;
}
if (red.length() == 1) {
red = "0" + red;
}
if (green.length() == 1) {
green = "0" + green;
}
if (blue.length() == 1) {
blue = "0" + blue;
}
String colorString;
if (includeAlpha) {
colorString = alpha + red + green + blue;
DialogLog.print(String.format("%s to color string is %s", color, colorString));
} else {
colorString = red + green + blue;
DialogLog.print(String.format("%s to color string is %s%s%s%s, exclude alpha is %s", color, alpha, red, green, blue, colorString));
}
return colorString;
}
}
================================================
FILE: ColorPicker/src/main/res/layout/color_picker_content.xml
================================================
================================================
FILE: Common/README.md
================================================
# 基础选择器
选择器底部弹窗。
================================================
FILE: Common/build.gradle
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
apply from: "${rootDir}/gradle/library.gradle"
apply from: "${rootDir}/gradle/publish.gradle"
dependencies {
implementation androidxLibrary.annotation
implementation androidxLibrary.core
api androidxLibrary.activity
}
================================================
FILE: Common/consumer-rules.pro
================================================
# 本库模块专用的混淆规则
================================================
FILE: Common/src/main/AndroidManifest.xml
================================================
================================================
FILE: Common/src/main/java/com/github/gzuliyujiang/dialog/BaseDialog.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.CallSuper;
import androidx.annotation.ColorInt;
import androidx.annotation.Dimension;
import androidx.annotation.DrawableRes;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
/**
* 屏幕底部弹出对话框
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2017/4/12
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public abstract class BaseDialog extends Dialog implements DialogInterface.OnShowListener, DialogInterface.OnDismissListener, LifecycleEventObserver {
public static final int MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT;
public static final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
protected Activity activity;
protected View contentView;
public BaseDialog(@NonNull Activity activity) {
this(activity, R.style.DialogTheme_Base);
}
public BaseDialog(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
init(activity);
}
public final View getContentView() {
return contentView;
}
private void init(Activity activity) {
if (activity instanceof LifecycleOwner) {
((LifecycleOwner) activity).getLifecycle().addObserver(this);
}
this.activity = activity;
//触摸屏幕取消窗体
setCanceledOnTouchOutside(false);
//按返回键取消窗体
setCancelable(false);
super.setOnShowListener(this);
super.setOnDismissListener(this);
Window window = super.getWindow();
if (window != null) {
//requestFeature must be called before adding content
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setLayout(activity.getResources().getDisplayMetrics().widthPixels, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
window.getDecorView().setPadding(0, 0, 0, 0);
}
onInit(null);
// 调用create或show才能触发onCreate
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
super.create();
} else {
readyView();
}
}
@Override
public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
if (event.equals(Lifecycle.Event.ON_DESTROY)) {
DialogLog.print("dismiss dialog when " + source.getClass().getName() + " on destroy");
dismiss();
source.getLifecycle().removeObserver(this);
}
}
/**
* @deprecated 使用 {@link #onInit(Bundle)} 代替
*/
@Deprecated
@CallSuper
protected void onInit(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
DialogLog.print("dialog onInit");
}
@CallSuper
protected void onInit(@Nullable Bundle savedInstanceState) {
//noinspection deprecation
onInit(activity, savedInstanceState);
}
@Override
protected final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DialogLog.print("dialog onCreate");
if (contentView == null) {
readyView();
}
}
private void readyView() {
contentView = createContentView();
contentView.setFocusable(true);
contentView.setFocusableInTouchMode(true);
setContentView(contentView);
initView();
}
@NonNull
protected abstract View createContentView();
/**
* @deprecated 使用 {@link #initView()} 代替
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
@CallSuper
protected void initView(View contentView) {
DialogLog.print("dialog initView");
}
@CallSuper
protected void initView() {
//noinspection deprecation
initView(contentView);
}
public final void disableCancel() {
setCancelable(false);
setCanceledOnTouchOutside(false);
}
public final void setBackgroundColor(@ColorInt int color) {
setBackgroundColor(CornerRound.No, color);
}
public final void setBackgroundColor(@CornerRound int cornerRound, @ColorInt int color) {
setBackgroundColor(cornerRound, 20, color);
}
public final void setBackgroundColor(@CornerRound int cornerRound, @Dimension(unit = Dimension.DP) int radius, @ColorInt int color) {
if (contentView == null) {
return;
}
float radiusInPX = contentView.getResources().getDisplayMetrics().density * radius;
contentView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Drawable drawable;
switch (cornerRound) {
case CornerRound.Top:
float[] outerRadii = new float[]{radiusInPX, radiusInPX, radiusInPX, radiusInPX, 0, 0, 0, 0};
ShapeDrawable shapeDrawable = new ShapeDrawable(new RoundRectShape(outerRadii, null, null));
shapeDrawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
drawable = shapeDrawable;
break;
case CornerRound.All:
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setCornerRadius(radiusInPX);
gradientDrawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
drawable = gradientDrawable;
break;
default:
drawable = new ColorDrawable(color);
break;
}
contentView.setBackground(drawable);
}
public final void setBackgroundResource(@DrawableRes int resId) {
if (contentView == null) {
return;
}
contentView.setBackgroundResource(resId);
}
public final void setBackgroundDrawable(Drawable drawable) {
if (contentView == null) {
return;
}
contentView.setBackground(drawable);
}
public final void setLayout(int width, int height) {
getWindow().setLayout(width, height);
}
public final void setWidth(int width) {
getWindow().setLayout(width, getWindow().getAttributes().height);
}
public final void setHeight(int height) {
getWindow().setLayout(getWindow().getAttributes().width, height);
}
public final void setGravity(int gravity) {
getWindow().setGravity(gravity);
}
public final void setDimAmount(@FloatRange(from = 0, to = 1) float amount) {
getWindow().setDimAmount(amount);
}
public final void setAnimationStyle(@StyleRes int animRes) {
getWindow().setWindowAnimations(animRes);
}
@Override
public void setOnShowListener(@Nullable OnShowListener listener) {
if (listener == null) {
return;
}
final OnShowListener current = this;
super.setOnShowListener(dialog -> {
current.onShow(dialog);
listener.onShow(dialog);
});
}
@Override
public void setOnDismissListener(@Nullable OnDismissListener listener) {
if (listener == null) {
return;
}
final OnDismissListener current = this;
super.setOnDismissListener(dialog -> {
current.onDismiss(dialog);
listener.onDismiss(dialog);
});
}
@CallSuper
@Override
public void show() {
if (isShowing()) {
return;
}
showSafe();
}
protected void showSafe() {
try {
super.show();
DialogLog.print("dialog show");
} catch (Throwable e) {
//...not attached to window manager
//...Unable to add window...is your activity running?
//...Activity...has leaked window...that was originally added here
DialogLog.print(e);
}
}
@CallSuper
@Override
public void dismiss() {
if (!isShowing()) {
return;
}
dismissSafe();
}
protected void dismissSafe() {
try {
super.dismiss();
DialogLog.print("dialog dismiss");
} catch (Throwable e) {
//...not attached to window manager
//...Activity...has leaked window...that was originally added here
DialogLog.print(e);
}
}
@CallSuper
@Override
public void onAttachedToWindow() {
DialogLog.print("dialog attached to window");
super.onAttachedToWindow();
initData();
}
@CallSuper
protected void initData() {
DialogLog.print("dialog initData");
}
@CallSuper
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
DialogLog.print("dialog detached from window");
}
/**
* @see #onAttachedToWindow()
*/
@CallSuper
@Override
public void onShow(DialogInterface dialog) {
DialogLog.print("dialog onShow");
}
/**
* @see #onDetachedFromWindow()
*/
@CallSuper
@Override
public void onDismiss(DialogInterface dialog) {
DialogLog.print("dialog onDismiss");
}
}
================================================
FILE: Common/src/main/java/com/github/gzuliyujiang/dialog/BottomDialog.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.dialog;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
/**
* 屏幕底部弹出对话框
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/4/15 20:54
*/
public abstract class BottomDialog extends BaseDialog {
protected View maskView;
public BottomDialog(@NonNull Activity activity) {
super(activity, R.style.DialogTheme_Sheet);
}
public BottomDialog(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@Override
public void onInit(@Nullable Bundle savedInstanceState) {
super.onInit(savedInstanceState);
setCancelable(true);
setCanceledOnTouchOutside(true);
setWidth(activity.getResources().getDisplayMetrics().widthPixels);
setGravity(Gravity.BOTTOM);
}
@Override
public void onShow(DialogInterface dialog) {
super.onShow(dialog);
if (enableMaskView()) {
addMaskView();
}
}
protected boolean enableMaskView() {
return true;
}
protected void addMaskView() {
// 通过自定义遮罩层视图解决自带弹窗遮罩致使系统导航栏背景过暗不一体问题
try {
// 取消弹窗遮罩效果 android:backgroundDimEnabled=false
getWindow().setDimAmount(0);
// 自定义遮罩层视图
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
Point screenRealSize = new Point();
activity.getWindowManager().getDefaultDisplay().getRealSize(screenRealSize);
int navBarIdentifier = activity.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
params.height = screenRealSize.y - activity.getResources().getDimensionPixelSize(navBarIdentifier);
params.gravity = Gravity.TOP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// 取消弹窗遮罩效果后,异形屏的状态栏没法被自定义的遮罩试图挡住,需结合systemUiVisibility
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
params.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
params.format = PixelFormat.TRANSLUCENT;
params.token = activity.getWindow().getDecorView().getWindowToken();
params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN;
maskView = new View(activity);
maskView.setBackgroundColor(0x7F000000);
maskView.setFitsSystemWindows(false);
maskView.setOnKeyListener((view, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_BACK) {
dismiss();
return true;
}
return false;
});
activity.getWindowManager().addView(maskView, params);
DialogLog.print("dialog add mask view");
} catch (Throwable e) {
//...not attached to window manager
// Activity ...... has leaked window android.view.View
DialogLog.print(e);
}
}
@Override
public void onDismiss(DialogInterface dialog) {
removeMaskView();
super.onDismiss(dialog);
}
protected void removeMaskView() {
if (maskView == null) {
DialogLog.print("mask view is null");
return;
}
try {
activity.getWindowManager().removeViewImmediate(maskView);
DialogLog.print("dialog remove mask view");
} catch (Throwable e) {
//...not attached to window manager
// Activity ...... has leaked window android.view.View
DialogLog.print(e);
}
maskView = null;
}
}
================================================
FILE: Common/src/main/java/com/github/gzuliyujiang/dialog/CornerRound.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.dialog;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/9/17 9:58
*/
public @interface CornerRound {
int No = 0;
int Top = 1;
int All = 2;
}
================================================
FILE: Common/src/main/java/com/github/gzuliyujiang/dialog/DialogColor.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.dialog;
import android.graphics.Color;
import androidx.annotation.ColorInt;
import java.io.Serializable;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/9/17 17:04
*/
public class DialogColor implements Serializable {
private int contentBackgroundColor = Color.WHITE;
private int topLineColor = 0xFFDDDDDD;
private int titleTextColor = 0xFF666666;
private int cancelTextColor = 0xFF333333;
private int okTextColor = 0xFF333333;
private int cancelEllipseColor = 0xFFF4F4F4;
private int okEllipseColor = 0xFF0081FF;
public DialogColor contentBackgroundColor(@ColorInt int color) {
this.contentBackgroundColor = color;
return this;
}
@ColorInt
public int contentBackgroundColor() {
return contentBackgroundColor;
}
public DialogColor topLineColor(@ColorInt int color) {
this.topLineColor = color;
return this;
}
@ColorInt
public int topLineColor() {
return topLineColor;
}
public DialogColor titleTextColor(@ColorInt int color) {
this.titleTextColor = color;
return this;
}
@ColorInt
public int titleTextColor() {
return titleTextColor;
}
public DialogColor cancelTextColor(@ColorInt int color) {
this.cancelTextColor = color;
return this;
}
@ColorInt
public int cancelTextColor() {
return cancelTextColor;
}
public DialogColor okTextColor(@ColorInt int color) {
this.okTextColor = color;
return this;
}
@ColorInt
public int okTextColor() {
return okTextColor;
}
public DialogColor cancelEllipseColor(@ColorInt int color) {
this.cancelEllipseColor = color;
return this;
}
@ColorInt
public int cancelEllipseColor() {
return cancelEllipseColor;
}
public DialogColor okEllipseColor(@ColorInt int color) {
this.okEllipseColor = color;
return this;
}
@ColorInt
public int okEllipseColor() {
return okEllipseColor;
}
}
================================================
FILE: Common/src/main/java/com/github/gzuliyujiang/dialog/DialogConfig.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.dialog;
/**
* @author 贵州山魈羡民 (1032694760@qq.com)
* @since 2021/9/16 15:55
*/
public final class DialogConfig {
private static int dialogStyle = DialogStyle.Default;
private static DialogColor dialogColor = new DialogColor();
private DialogConfig() {
super();
}
public static void setDialogStyle(@DialogStyle int style) {
dialogStyle = style;
}
@DialogStyle
public static int getDialogStyle() {
return dialogStyle;
}
public static void setDialogColor(DialogColor color) {
dialogColor = color;
}
public static DialogColor getDialogColor() {
if (dialogColor == null) {
dialogColor = new DialogColor();
}
return dialogColor;
}
}
================================================
FILE: Common/src/main/java/com/github/gzuliyujiang/dialog/DialogLog.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.dialog;
import android.util.Log;
import androidx.annotation.NonNull;
/**
* 调试日志工具类
*
* @author 贵州山魈羡民 (1032694760@qq.com)
* @since 2021/3/26 21:34
*/
public final class DialogLog {
private static final String TAG = "AndroidPicker";
private static boolean enable = false;
private DialogLog() {
super();
}
/**
* 启用调试日志
*/
public static void enable() {
enable = true;
}
/**
* 打印调试日志
*
* @param log 日志信息
*/
public static void print(@NonNull Object log) {
if (!enable) {
return;
}
Log.d(TAG, log.toString());
}
}
================================================
FILE: Common/src/main/java/com/github/gzuliyujiang/dialog/DialogStyle.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.dialog;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* 弹窗样式枚举
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/9/16 15:31
*/
@Retention(RetentionPolicy.SOURCE)
public @interface DialogStyle {
int Default = 0;
int One = 1;
int Two = 2;
int Three = 3;
}
================================================
FILE: Common/src/main/java/com/github/gzuliyujiang/dialog/ModalDialog.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.dialog;
import android.app.Activity;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.CallSuper;
import androidx.annotation.Dimension;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.core.graphics.ColorUtils;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/3 15:23
*/
@SuppressWarnings("unused")
public abstract class ModalDialog extends BottomDialog implements View.OnClickListener {
protected View headerView;
protected TextView cancelView;
protected TextView titleView;
protected TextView okView;
protected View topLineView;
protected View bodyView;
protected View footerView;
public ModalDialog(@NonNull Activity activity) {
super(activity, DialogConfig.getDialogStyle() == DialogStyle.Three
? R.style.DialogTheme_Fade : R.style.DialogTheme_Sheet);
}
public ModalDialog(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@Override
public void onInit(@Nullable Bundle savedInstanceState) {
super.onInit(savedInstanceState);
if (DialogConfig.getDialogStyle() == DialogStyle.Three) {
setWidth((int) (activity.getResources().getDisplayMetrics().widthPixels * 0.8f));
setGravity(Gravity.CENTER);
}
}
@Override
protected boolean enableMaskView() {
return DialogConfig.getDialogStyle() != DialogStyle.Three;
}
@NonNull
@Override
protected View createContentView() {
LinearLayout rootLayout = new LinearLayout(activity);
rootLayout.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
rootLayout.setOrientation(LinearLayout.VERTICAL);
rootLayout.setGravity(Gravity.CENTER);
rootLayout.setPadding(0, 0, 0, 0);
headerView = createHeaderView();
if (headerView == null) {
headerView = new View(activity);
headerView.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
}
rootLayout.addView(headerView);
topLineView = createTopLineView();
if (topLineView == null) {
topLineView = new View(activity);
topLineView.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
}
rootLayout.addView(topLineView);
bodyView = createBodyView();
rootLayout.addView(bodyView, new LinearLayout.LayoutParams(MATCH_PARENT, 0, 1.0f));
footerView = createFooterView();
if (footerView == null) {
footerView = new View(activity);
footerView.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
}
rootLayout.addView(footerView);
return rootLayout;
}
@Nullable
protected View createHeaderView() {
switch (DialogConfig.getDialogStyle()) {
case DialogStyle.One:
return View.inflate(activity, R.layout.dialog_header_style_1, null);
case DialogStyle.Two:
return View.inflate(activity, R.layout.dialog_header_style_2, null);
case DialogStyle.Three:
return View.inflate(activity, R.layout.dialog_header_style_3, null);
default:
return View.inflate(activity, R.layout.dialog_header_style_default, null);
}
}
@Nullable
protected View createTopLineView() {
if (DialogConfig.getDialogStyle() == DialogStyle.Default) {
View view = new View(activity);
view.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, (int) (1 * activity.getResources().getDisplayMetrics().density)));
view.setBackgroundColor(DialogConfig.getDialogColor().topLineColor());
return view;
}
return null;
}
@NonNull
protected abstract View createBodyView();
@Nullable
protected View createFooterView() {
switch (DialogConfig.getDialogStyle()) {
case DialogStyle.One:
return View.inflate(activity, R.layout.dialog_footer_style_1, null);
case DialogStyle.Two:
return View.inflate(activity, R.layout.dialog_footer_style_2, null);
case DialogStyle.Three:
return View.inflate(activity, R.layout.dialog_footer_style_3, null);
default:
return null;
}
}
@CallSuper
@Override
protected void initView() {
super.initView();
int color = DialogConfig.getDialogColor().contentBackgroundColor();
switch (DialogConfig.getDialogStyle()) {
case DialogStyle.One:
case DialogStyle.Two:
setBackgroundColor(CornerRound.Top, color);
break;
case DialogStyle.Three:
setBackgroundColor(CornerRound.All, color);
break;
default:
setBackgroundColor(CornerRound.No, color);
break;
}
cancelView = contentView.findViewById(R.id.dialog_modal_cancel);
if (cancelView == null) {
throw new IllegalArgumentException("Cancel view id not found");
}
titleView = contentView.findViewById(R.id.dialog_modal_title);
if (titleView == null) {
throw new IllegalArgumentException("Title view id not found");
}
okView = contentView.findViewById(R.id.dialog_modal_ok);
if (okView == null) {
throw new IllegalArgumentException("Ok view id not found");
}
titleView.setTextColor(DialogConfig.getDialogColor().titleTextColor());
cancelView.setTextColor(DialogConfig.getDialogColor().cancelTextColor());
okView.setTextColor(DialogConfig.getDialogColor().okTextColor());
cancelView.setOnClickListener(this);
okView.setOnClickListener(this);
maybeBuildEllipseButton();
}
private void maybeBuildEllipseButton() {
if (DialogConfig.getDialogStyle() != DialogStyle.One && DialogConfig.getDialogStyle() != DialogStyle.Two) {
return;
}
if (DialogConfig.getDialogStyle() == DialogStyle.Two) {
Drawable background = cancelView.getBackground();
if (background != null) {
background.setColorFilter(new PorterDuffColorFilter(DialogConfig.getDialogColor().cancelEllipseColor(), PorterDuff.Mode.SRC_IN));
cancelView.setBackground(background);
} else {
cancelView.setBackgroundResource(R.mipmap.dialog_close_icon);
}
} else {
GradientDrawable cancelDrawable = new GradientDrawable();
cancelDrawable.setCornerRadius(okView.getResources().getDisplayMetrics().density * 999);
cancelDrawable.setColor(DialogConfig.getDialogColor().cancelEllipseColor());
cancelView.setBackground(cancelDrawable);
if (ColorUtils.calculateLuminance(DialogConfig.getDialogColor().cancelEllipseColor()) < 0.5f) {
cancelView.setTextColor(0xFFFFFFFF);
} else {
cancelView.setTextColor(0xFF666666);
}
}
GradientDrawable okDrawable = new GradientDrawable();
okDrawable.setCornerRadius(okView.getResources().getDisplayMetrics().density * 999);
okDrawable.setColor(DialogConfig.getDialogColor().okEllipseColor());
okView.setBackground(okDrawable);
if (ColorUtils.calculateLuminance(DialogConfig.getDialogColor().okEllipseColor()) < 0.5f) {
okView.setTextColor(0xFFFFFFFF);
} else {
okView.setTextColor(0xFF333333);
}
}
@Override
public void setTitle(final @Nullable CharSequence title) {
if (titleView != null) {
titleView.post(new Runnable() {
@Override
public void run() {
titleView.setText(title);
}
});
} else {
super.setTitle(title);
}
}
@Override
public void setTitle(final int titleId) {
if (titleView != null) {
titleView.post(new Runnable() {
@Override
public void run() {
titleView.setText(titleId);
}
});
} else {
super.setTitle(titleId);
}
}
@CallSuper
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.dialog_modal_cancel) {
DialogLog.print("cancel clicked");
onCancel();
dismiss();
} else if (id == R.id.dialog_modal_ok) {
DialogLog.print("ok clicked");
onOk();
dismiss();
}
}
protected abstract void onCancel();
protected abstract void onOk();
public final void setBodyWidth(@Dimension(unit = Dimension.DP) @IntRange(from = 50) int bodyWidth) {
ViewGroup.LayoutParams layoutParams = bodyView.getLayoutParams();
int width = WRAP_CONTENT;
if (bodyWidth != WRAP_CONTENT && bodyWidth != MATCH_PARENT) {
width = (int) (bodyView.getResources().getDisplayMetrics().density * bodyWidth);
}
layoutParams.width = width;
bodyView.setLayoutParams(layoutParams);
}
public final void setBodyHeight(@Dimension(unit = Dimension.DP) @IntRange(from = 50) int bodyHeight) {
ViewGroup.LayoutParams layoutParams = bodyView.getLayoutParams();
int height = WRAP_CONTENT;
if (bodyHeight != WRAP_CONTENT && bodyHeight != MATCH_PARENT) {
height = (int) (bodyView.getResources().getDisplayMetrics().density * bodyHeight);
}
layoutParams.height = height;
bodyView.setLayoutParams(layoutParams);
}
public final View getHeaderView() {
if (headerView == null) {
headerView = new View(activity);
}
return headerView;
}
public final View getTopLineView() {
return topLineView;
}
public final View getBodyView() {
return bodyView;
}
public final View getFooterView() {
return footerView;
}
public final TextView getCancelView() {
return cancelView;
}
public final TextView getTitleView() {
return titleView;
}
public final TextView getOkView() {
return okView;
}
}
================================================
FILE: Common/src/main/res/anim/dialog_sheet_enter.xml
================================================
================================================
FILE: Common/src/main/res/anim/dialog_sheet_exit.xml
================================================
================================================
FILE: Common/src/main/res/layout/dialog_footer_style_1.xml
================================================
================================================
FILE: Common/src/main/res/layout/dialog_footer_style_2.xml
================================================
================================================
FILE: Common/src/main/res/layout/dialog_footer_style_3.xml
================================================
================================================
FILE: Common/src/main/res/layout/dialog_header_style_1.xml
================================================
================================================
FILE: Common/src/main/res/layout/dialog_header_style_2.xml
================================================
================================================
FILE: Common/src/main/res/layout/dialog_header_style_3.xml
================================================
================================================
FILE: Common/src/main/res/layout/dialog_header_style_default.xml
================================================
================================================
FILE: Common/src/main/res/values/dialog_anims.xml
================================================
================================================
FILE: Common/src/main/res/values/dialog_themes.xml
================================================
================================================
FILE: FilePicker/README.md
================================================
# 文件/目录选择器
注意合规性,允许APP读取外置存储器,调用之前要确保已获得用户授予`READ_EXTERNAL_STORAGE`权限。
================================================
FILE: FilePicker/build.gradle
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
apply from: "${rootDir}/gradle/library.gradle"
apply from: "${rootDir}/gradle/publish.gradle"
dependencies {
implementation androidxLibrary.annotation
implementation androidxLibrary.recyclerview
api project(':Common')
}
================================================
FILE: FilePicker/consumer-rules.pro
================================================
# 本库模块专用的混淆规则
================================================
FILE: FilePicker/src/main/AndroidManifest.xml
================================================
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/ExplorerConfig.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import androidx.annotation.Dimension;
import androidx.core.content.ContextCompat;
import com.github.gzuliyujiang.filepicker.annotation.ExplorerMode;
import com.github.gzuliyujiang.filepicker.annotation.FileSort;
import com.github.gzuliyujiang.filepicker.contract.OnFileClickedListener;
import com.github.gzuliyujiang.filepicker.contract.OnFileLoadedListener;
import com.github.gzuliyujiang.filepicker.contract.OnFilePickedListener;
import com.github.gzuliyujiang.filepicker.contract.OnPathClickedListener;
import java.io.File;
import java.io.Serializable;
import java.util.Arrays;
/**
* @author liyujiang
* @since 2022/3/16 19:57
*/
@SuppressWarnings({"UnusedReturnValue", "unused"})
public class ExplorerConfig implements Serializable {
private final Context context;
private File rootDir;
private boolean loadAsync;
private String[] allowExtensions = null;
private int explorerMode = ExplorerMode.FILE;
private boolean showHomeDir = true;
private boolean showUpDir = true;
private boolean showHideDir = true;
private int fileSort = FileSort.BY_NAME_ASC;
private int itemHeight = 40;
private Drawable homeIcon;
private Drawable upIcon;
private Drawable folderIcon;
private Drawable fileIcon;
private OnFileLoadedListener onFileLoadedListener;
private OnPathClickedListener onPathClickedListener;
private OnFileClickedListener onFileClickedListener;
private OnFilePickedListener onFilePickedListener;
public ExplorerConfig(Context context) {
this.context = context;
homeIcon = ContextCompat.getDrawable(context, R.mipmap.file_picker_home);
upIcon = ContextCompat.getDrawable(context, R.mipmap.file_picker_up);
folderIcon = ContextCompat.getDrawable(context, R.mipmap.file_picker_folder);
fileIcon = ContextCompat.getDrawable(context, R.mipmap.file_picker_file);
}
/**
* 设置根目录
*/
public void setRootDir(File rootDir) {
this.rootDir = rootDir;
}
public File getRootDir() {
if (rootDir == null) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
rootDir = Environment.getExternalStorageDirectory();
} else {
rootDir = context.getFilesDir();
}
}
return rootDir;
}
/**
* 设置是否异步加载文件列表,默认是同步列出文件
*/
public void setLoadAsync(boolean loadAsync) {
this.loadAsync = loadAsync;
}
public boolean isLoadAsync() {
return loadAsync;
}
/**
* 设置列表条目的显示高度
*/
public ExplorerConfig setItemHeight(@Dimension(unit = Dimension.DP) int itemHeight) {
if (itemHeight < 10 || itemHeight > 100) {
return this;
}
this.itemHeight = itemHeight;
return this;
}
public int getItemHeight() {
return itemHeight;
}
/**
* 设置文件的图标
*/
public ExplorerConfig setFileIcon(Drawable fileIcon) {
if (fileIcon == null) {
return this;
}
this.fileIcon = fileIcon;
return this;
}
public Drawable getFileIcon() {
return fileIcon;
}
/**
* 设置文件夹的图标
*/
public ExplorerConfig setFolderIcon(Drawable folderIcon) {
if (folderIcon == null) {
return this;
}
this.folderIcon = folderIcon;
return this;
}
public Drawable getFolderIcon() {
return folderIcon;
}
/**
* 设置“返回主页”的图标
*/
public ExplorerConfig setHomeIcon(Drawable homeIcon) {
if (homeIcon == null) {
return this;
}
this.homeIcon = homeIcon;
return this;
}
public Drawable getHomeIcon() {
return homeIcon;
}
/**
* 设置“返回上级”的图标
*/
public ExplorerConfig setUpIcon(Drawable upIcon) {
if (upIcon == null) {
return this;
}
this.upIcon = upIcon;
return this;
}
public Drawable getUpIcon() {
return upIcon;
}
/**
* 设置允许的扩展名,如[".kml",".kmz"]
*/
public ExplorerConfig setAllowExtensions(String[] allowExtensions) {
if (this.allowExtensions != null && Arrays.equals(this.allowExtensions, allowExtensions)) {
return this;
}
this.allowExtensions = allowExtensions;
return this;
}
public String[] getAllowExtensions() {
return allowExtensions;
}
@ExplorerMode
public int getExplorerMode() {
return explorerMode;
}
/**
* 设置是目录模式还是文件模式
*/
public void setExplorerMode(@ExplorerMode int explorerMode) {
this.explorerMode = explorerMode;
}
/**
* 设置是否显示返回主目录
*/
public ExplorerConfig setShowHomeDir(boolean showHomeDir) {
if (this.showHomeDir == showHomeDir) {
return this;
}
this.showHomeDir = showHomeDir;
return this;
}
public boolean isShowHomeDir() {
return showHomeDir;
}
/**
* 设置是否显示返回上一级
*/
public ExplorerConfig setShowUpDir(boolean showUpDir) {
if (this.showUpDir == showUpDir) {
return this;
}
this.showUpDir = showUpDir;
return this;
}
public boolean isShowUpDir() {
return showUpDir;
}
/**
* 设置是否显示隐藏的目录(以“.”开头)
*/
public ExplorerConfig setShowHideDir(boolean showHideDir) {
if (this.showHideDir == showHideDir) {
return this;
}
this.showHideDir = showHideDir;
return this;
}
public boolean isShowHideDir() {
return showHideDir;
}
/**
* 设置文件排序方式,支持的排序方式已枚举为{@link FileSort}
*/
public ExplorerConfig setFileSort(@FileSort int fileSort) {
if (this.fileSort == fileSort) {
return this;
}
this.fileSort = fileSort;
return this;
}
@FileSort
public int getFileSort() {
return fileSort;
}
/**
* 设置文件加载监听器
*/
public ExplorerConfig setOnFileLoadedListener(OnFileLoadedListener listener) {
onFileLoadedListener = listener;
return this;
}
public OnFileLoadedListener getOnFileLoadedListener() {
return onFileLoadedListener;
}
/**
* 设置各层级路径点击监听器
*/
public ExplorerConfig setOnPathClickedListener(OnPathClickedListener listener) {
onPathClickedListener = listener;
return this;
}
public OnPathClickedListener getOnPathClickedListener() {
return onPathClickedListener;
}
public OnFileClickedListener getOnFileClickedListener() {
return onFileClickedListener;
}
/**
* 设置文件或目录点击监听器
*/
public void setOnFileClickedListener(OnFileClickedListener onFileClickedListener) {
this.onFileClickedListener = onFileClickedListener;
}
public OnFilePickedListener getOnFilePickedListener() {
return onFilePickedListener;
}
/**
* 设置文件或目录选择监听器
*/
public void setOnFilePickedListener(OnFilePickedListener onFilePickedListener) {
this.onFilePickedListener = onFilePickedListener;
}
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/FileExplorer.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;
import com.github.gzuliyujiang.dialog.DialogLog;
import com.github.gzuliyujiang.filepicker.adapter.FileAdapter;
import com.github.gzuliyujiang.filepicker.adapter.FileEntity;
import com.github.gzuliyujiang.filepicker.adapter.PathAdapter;
import com.github.gzuliyujiang.filepicker.adapter.ViewHolder;
import com.github.gzuliyujiang.filepicker.contract.OnFileLoadedListener;
import com.github.gzuliyujiang.filepicker.contract.OnPathClickedListener;
import java.io.File;
import java.util.Locale;
/**
* 文件浏览器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/10 18:50
*/
@SuppressWarnings("unused")
public class FileExplorer extends FrameLayout implements OnFileLoadedListener, OnPathClickedListener {
private CharSequence emptyHint;
private FileAdapter fileAdapter;
private PathAdapter pathAdapter;
private ProgressBar loadingView;
private RecyclerView fileListView;
private TextView emptyHintView;
private RecyclerView pathListView;
private ExplorerConfig explorerConfig;
public FileExplorer(Context context) {
super(context);
init(context);
}
public FileExplorer(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public FileExplorer(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public FileExplorer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
private void init(Context context) {
explorerConfig = new ExplorerConfig(context);
View contentView = inflate(context, R.layout.file_picker_content, this);
pathListView = contentView.findViewById(R.id.file_picker_path_list);
loadingView = contentView.findViewById(R.id.file_picker_loading);
fileListView = contentView.findViewById(R.id.file_picker_file_list);
emptyHintView = contentView.findViewById(R.id.file_picker_empty_hint);
pathAdapter = new PathAdapter(context);
pathAdapter.setOnPathClickedListener(this);
pathListView.setAdapter(pathAdapter);
fileAdapter = new FileAdapter(explorerConfig);
fileListView.setAdapter(fileAdapter);
emptyHint = Locale.getDefault().getDisplayLanguage().contains("中文") ? "<空>" : "";
emptyHintView.setText(emptyHint);
}
/**
* 以默认配置加载文件列表
*/
public void load() {
load(null);
}
/**
* 以自定义配置加载文件列表
*/
public void load(@Nullable ExplorerConfig config) {
if (config != null) {
explorerConfig = config;
fileAdapter = new FileAdapter(config);
fileListView.setAdapter(fileAdapter);
}
explorerConfig.setOnFileLoadedListener(this);
explorerConfig.setOnPathClickedListener(this);
refreshCurrent(explorerConfig.getRootDir());
}
@Override
public void onFileLoaded(@NonNull File file) {
loadingView.setVisibility(INVISIBLE);
fileListView.setVisibility(View.VISIBLE);
int itemCount = fileAdapter.getItemCount();
if (explorerConfig.isShowHomeDir()) {
itemCount--;
}
if (explorerConfig.isShowUpDir()) {
itemCount--;
}
if (itemCount < 1) {
DialogLog.print("no files, or dir is empty");
emptyHintView.setVisibility(View.VISIBLE);
emptyHintView.setText(emptyHint);
} else {
DialogLog.print("files or dirs count: " + itemCount);
emptyHintView.setVisibility(View.INVISIBLE);
}
pathListView.post(new Runnable() {
@Override
public void run() {
pathListView.scrollToPosition(pathAdapter.getItemCount() - 1);
}
});
}
@Override
public void onPathClicked(RecyclerView.Adapter adapter, int position, @NonNull String path) {
DialogLog.print("clicked path name: " + path);
if (adapter instanceof PathAdapter) {
refreshCurrent(new File(path));
} else if (adapter instanceof FileAdapter) {
FileEntity entity = fileAdapter.getItem(position);
DialogLog.print("clicked file item: " + entity);
File file = entity.getFile();
if (file.isDirectory()) {
refreshCurrent(file);
return;
}
if (explorerConfig.getOnFileClickedListener() != null) {
explorerConfig.getOnFileClickedListener().onFileClicked(file);
}
}
}
/**
* 设置指定路径下没有文件或目录时的提示语
*/
public void setEmptyHint(@NonNull CharSequence emptyHint) {
if (TextUtils.equals(this.emptyHint, emptyHint)) {
return;
}
this.emptyHint = emptyHint;
emptyHintView.setText(emptyHint);
}
/**
* 重新加载指定路径下的文件或目录到当前列表
*/
public final void refreshCurrent(File current) {
if (current == null) {
DialogLog.print("current dir is null");
return;
}
loadingView.setVisibility(VISIBLE);
fileListView.setVisibility(View.INVISIBLE);
emptyHintView.setVisibility(View.INVISIBLE);
long millis = System.currentTimeMillis();
pathAdapter.updatePath(current);
fileAdapter.loadData(current);
long spent = System.currentTimeMillis() - millis;
DialogLog.print("spent: " + spent + " ms" + ", async=" + explorerConfig.isLoadAsync() + ", thread=" + Thread.currentThread());
}
public final FileAdapter getFileAdapter() {
return fileAdapter;
}
public final PathAdapter getPathAdapter() {
return pathAdapter;
}
public ExplorerConfig getExplorerConfig() {
return explorerConfig;
}
public final File getRootDir() {
return explorerConfig.getRootDir();
}
/**
* 获取当前加载的文件夹
*/
public final File getCurrentFile() {
return fileAdapter.getCurrentFile();
}
/**
* 获取各级路径所在的视图
*/
public final RecyclerView getPathListView() {
return pathListView;
}
/**
* 获取文件或目录列表所在的视图
*/
public final RecyclerView getFileListView() {
return fileListView;
}
public final TextView getEmptyHintView() {
return emptyHintView;
}
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/FilePicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.recyclerview.widget.RecyclerView;
import com.github.gzuliyujiang.dialog.DialogLog;
import com.github.gzuliyujiang.dialog.ModalDialog;
import com.github.gzuliyujiang.filepicker.annotation.ExplorerMode;
import com.github.gzuliyujiang.filepicker.contract.OnFileClickedListener;
import java.io.File;
/**
* 文件目录选择器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2015/9/29
*/
@SuppressWarnings("unused")
public class FilePicker extends ModalDialog {
private FileExplorer fileExplorer;
private boolean initialized = false;
private ExplorerConfig explorerConfig;
public FilePicker(Activity activity) {
super(activity);
}
public FilePicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@NonNull
@Override
protected View createBodyView() {
fileExplorer = new FileExplorer(activity);
return fileExplorer;
}
@Override
protected void initView() {
super.initView();
setHeight((int) (activity.getResources().getDisplayMetrics().heightPixels * 0.6f));
}
@Override
protected void initData() {
super.initData();
initialized = true;
setExplorerConfig(explorerConfig);
final ExplorerConfig config = fileExplorer.getExplorerConfig();
config.setOnFileClickedListener(new OnFileClickedListener() {
@Override
public void onFileClicked(@NonNull File file) {
if (config.getExplorerMode() == ExplorerMode.FILE) {
dismiss();
config.getOnFilePickedListener().onFilePicked(file);
}
}
});
if (config.getExplorerMode() == ExplorerMode.FILE) {
okView.setVisibility(View.GONE);
}
}
@Override
protected void onCancel() {
}
@Override
protected void onOk() {
File currentFile = fileExplorer.getCurrentFile();
DialogLog.print("picked directory: " + currentFile);
if (fileExplorer.getExplorerConfig().getOnFilePickedListener() != null) {
fileExplorer.getExplorerConfig().getOnFilePickedListener().onFilePicked(currentFile);
}
}
/**
* 设置文件管理器配置
*/
public void setExplorerConfig(@Nullable ExplorerConfig config) {
explorerConfig = config;
if (initialized) {
fileExplorer.load(config);
}
}
public final File getCurrentFile() {
return fileExplorer.getCurrentFile();
}
/**
* 获取文件管理器对象
*/
public final FileExplorer getFileExplorer() {
return fileExplorer;
}
public final RecyclerView getFileListView() {
return fileExplorer.getFileListView();
}
public final TextView getEmptyHintView() {
return fileExplorer.getEmptyHintView();
}
public final RecyclerView getPathListView() {
return fileExplorer.getPathListView();
}
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/adapter/FileAdapter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.github.gzuliyujiang.dialog.DialogLog;
import com.github.gzuliyujiang.filepicker.ExplorerConfig;
import com.github.gzuliyujiang.filepicker.annotation.ExplorerMode;
import com.github.gzuliyujiang.filepicker.annotation.FileSort;
import com.github.gzuliyujiang.filepicker.filter.SimpleFilter;
import com.github.gzuliyujiang.filepicker.sort.SortByExtension;
import com.github.gzuliyujiang.filepicker.sort.SortByName;
import com.github.gzuliyujiang.filepicker.sort.SortBySize;
import com.github.gzuliyujiang.filepicker.sort.SortByTime;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
/**
* 文件目录数据适配
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2014/05/23 18:02
*/
@SuppressWarnings("unused")
public class FileAdapter extends RecyclerView.Adapter {
private static final ExecutorService THREAD_POOL = Executors.newCachedThreadPool();
private static final Handler UI_HANDLER = new Handler(Looper.getMainLooper());
public static final String DIR_ROOT = ".";
public static final String DIR_PARENT = "..";
private final List data = new ArrayList<>();
private File currentFile = null;
private final ConcurrentLinkedQueue> futureTasks = new ConcurrentLinkedQueue<>();
private ExplorerConfig explorerConfig;
public FileAdapter(ExplorerConfig explorerConfig) {
this.explorerConfig = explorerConfig;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER_VERTICAL);
int height = (int) (explorerConfig.getItemHeight() * context.getResources().getDisplayMetrics().density);
int matchParent = ViewGroup.LayoutParams.MATCH_PARENT;
layout.setLayoutParams(new ViewGroup.LayoutParams(matchParent, height));
int padding = (int) (5 * context.getResources().getDisplayMetrics().density);
layout.setPadding(padding, padding, padding, padding);
ImageView imageView = new ImageView(context);
int size = (int) (20 * context.getResources().getDisplayMetrics().density);
imageView.setLayoutParams(new LinearLayout.LayoutParams(size, size));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
layout.addView(imageView);
TextView textView = new TextView(context);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(matchParent, matchParent);
tvParams.leftMargin = (int) (10 * context.getResources().getDisplayMetrics().density);
textView.setLayoutParams(tvParams);
textView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
textView.setSingleLine();
layout.addView(textView);
ViewHolder viewHolder = new ViewHolder(layout);
viewHolder.textView = textView;
viewHolder.imageView = imageView;
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final int adapterPosition = holder.getAdapterPosition();
final FileEntity item = getItem(adapterPosition);
holder.imageView.setImageDrawable(item.getIcon());
holder.textView.setText(item.getName());
if (explorerConfig.getOnPathClickedListener() == null) {
return;
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
explorerConfig.getOnPathClickedListener().onPathClicked(FileAdapter.this, adapterPosition, item.getFile().getAbsolutePath());
}
});
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return data.size();
}
public void setExplorerConfig(ExplorerConfig explorerConfig) {
this.explorerConfig = explorerConfig;
loadData(currentFile);
}
public ExplorerConfig getExplorerConfig() {
return explorerConfig;
}
public final void recycleData() {
data.clear();
if (explorerConfig.getHomeIcon() instanceof BitmapDrawable) {
Bitmap homeBitmap = ((BitmapDrawable) explorerConfig.getHomeIcon()).getBitmap();
if (null != homeBitmap && !homeBitmap.isRecycled()) {
homeBitmap.recycle();
}
}
if (explorerConfig.getUpIcon() instanceof BitmapDrawable) {
Bitmap upBitmap = ((BitmapDrawable) explorerConfig.getUpIcon()).getBitmap();
if (null != upBitmap && !upBitmap.isRecycled()) {
upBitmap.recycle();
}
}
if (explorerConfig.getFolderIcon() instanceof BitmapDrawable) {
Bitmap folderBitmap = ((BitmapDrawable) explorerConfig.getFolderIcon()).getBitmap();
if (null != folderBitmap && !folderBitmap.isRecycled()) {
folderBitmap.recycle();
}
}
if (explorerConfig.getFileIcon() instanceof BitmapDrawable) {
Bitmap fileBitmap = ((BitmapDrawable) explorerConfig.getFileIcon()).getBitmap();
if (null != fileBitmap && !fileBitmap.isRecycled()) {
fileBitmap.recycle();
}
}
}
public File getCurrentFile() {
return currentFile;
}
public FileEntity getItem(int position) {
return data.get(position);
}
public void loadData(final File dir) {
if (!explorerConfig.isLoadAsync()) {
reallyRefresh(loadDataSync(dir));
return;
}
FutureTask> lastTask = futureTasks.peek();
if (lastTask != null && !lastTask.isDone()) {
lastTask.cancel(true);
}
FutureTask> newTask = new FutureTask<>(new Callable() {
@Override
public Void call() {
final List temp = loadDataSync(dir);
FutureTask> task = futureTasks.poll();
if (task != null && task.isCancelled()) {
DialogLog.print("data load is canceled: " + currentFile);
return null;
}
UI_HANDLER.post(new Runnable() {
@Override
public void run() {
reallyRefresh(temp);
}
});
return null;
}
});
futureTasks.add(newTask);
THREAD_POOL.execute(newTask);
}
@SuppressLint("NotifyDataSetChanged")
private void reallyRefresh(List temp) {
data.clear();
data.addAll(temp);
notifyDataSetChanged();
if (explorerConfig.getOnFileLoadedListener() != null) {
explorerConfig.getOnFileLoadedListener().onFileLoaded(currentFile);
}
DialogLog.print("notify changed when data loaded: " + currentFile);
}
private List loadDataSync(File dir) {
if (dir == null) {
DialogLog.print("current directory is null");
dir = explorerConfig.getRootDir();
}
long millis = System.currentTimeMillis();
List entities = new ArrayList<>();
DialogLog.print("will load directory: " + dir);
currentFile = dir;
if (explorerConfig.isShowHomeDir()) {
//添加“返回主目录”
FileEntity root = new FileEntity();
root.setIcon(explorerConfig.getHomeIcon());
root.setName(DIR_ROOT);
root.setFile(explorerConfig.getRootDir());
entities.add(root);
}
if (explorerConfig.isShowUpDir() && !File.separator.equals(dir.getAbsolutePath())) {
//添加“返回上一级目录”
FileEntity parent = new FileEntity();
parent.setIcon(explorerConfig.getUpIcon());
parent.setName(DIR_PARENT);
parent.setFile(dir.getParentFile());
entities.add(parent);
}
SimpleFilter filter = new SimpleFilter(explorerConfig.getExplorerMode() == ExplorerMode.DIRECTORY, explorerConfig.getAllowExtensions());
List files = listFiles(currentFile, filter);
sortFiles(files, explorerConfig.getFileSort());
for (File file : files) {
if (!explorerConfig.isShowHideDir() && file.getName().startsWith(".")) {
continue;
}
FileEntity FileEntity = new FileEntity();
if (file.isDirectory()) {
FileEntity.setIcon(explorerConfig.getFolderIcon());
} else {
FileEntity.setIcon(explorerConfig.getFileIcon());
}
FileEntity.setName(file.getName());
FileEntity.setFile(file);
entities.add(FileEntity);
}
long spent = System.currentTimeMillis() - millis;
DialogLog.print("spent: " + spent + " ms" + ", async=" + explorerConfig.isLoadAsync() + ", thread=" + Thread.currentThread());
return entities;
}
/**
* 列出指定目录下的所有子目录
*/
private List listFiles(File startDir, FileFilter fileFilter) {
DialogLog.print(String.format("list dir %s by filter %s", startDir, fileFilter.getClass().getName()));
if (!startDir.isDirectory()) {
return new ArrayList<>();
}
File[] dirs = startDir.listFiles(fileFilter);
if (dirs == null) {
return new ArrayList<>();
}
return Arrays.asList(dirs);
}
private void sortFiles(List files, @FileSort int sort) {
switch (sort) {
case FileSort.BY_NAME_ASC:
Collections.sort(files, new SortByName());
break;
case FileSort.BY_NAME_DESC:
Collections.sort(files, new SortByName());
Collections.reverse(files);
break;
case FileSort.BY_TIME_ASC:
Collections.sort(files, new SortByTime());
break;
case FileSort.BY_TIME_DESC:
Collections.sort(files, new SortByTime());
Collections.reverse(files);
break;
case FileSort.BY_SIZE_ASC:
Collections.sort(files, new SortBySize());
break;
case FileSort.BY_SIZE_DESC:
Collections.sort(files, new SortBySize());
Collections.reverse(files);
break;
case FileSort.BY_EXTENSION_ASC:
Collections.sort(files, new SortByExtension());
break;
case FileSort.BY_EXTENSION_DESC:
Collections.sort(files, new SortByExtension());
Collections.reverse(files);
break;
}
}
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/adapter/FileEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.adapter;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import java.io.File;
import java.io.Serializable;
/**
* 文件项信息
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2014/05/23 18:02
*/
public class FileEntity implements Serializable {
private Drawable icon;
private String name;
private File file;
public void setIcon(Drawable icon) {
this.icon = icon;
}
public Drawable getIcon() {
return icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
@NonNull
@Override
public String toString() {
return "FileEntity{" +
"name='" + name + '\'' +
", file='" + file + '\'' +
'}';
}
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/adapter/PathAdapter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.github.gzuliyujiang.filepicker.R;
import com.github.gzuliyujiang.filepicker.contract.OnPathClickedListener;
import java.io.File;
import java.util.Collections;
import java.util.LinkedList;
/**
* 文件路径数据适配
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2017/01/08 01:20
*/
@SuppressWarnings("unused")
public class PathAdapter extends RecyclerView.Adapter {
private static final String ROOT_HINT = "ROOT";
private final Context context;
private final LinkedList paths = new LinkedList<>();
private Drawable arrowIcon = null;
private OnPathClickedListener onPathClickedListener;
public PathAdapter(@NonNull Context context) {
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
int wrapContent = ViewGroup.LayoutParams.WRAP_CONTENT;
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setGravity(Gravity.CENTER_VERTICAL);
layout.setLayoutParams(new ViewGroup.LayoutParams(wrapContent, wrapContent));
TextView textView = new TextView(context);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(wrapContent, wrapContent);
textView.setLayoutParams(tvParams);
textView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
int padding = (int) (5 * context.getResources().getDisplayMetrics().density);
textView.setPadding(padding, 0, padding, 0);
layout.addView(textView);
ImageView imageView = new ImageView(context);
int size = (int) (15 * context.getResources().getDisplayMetrics().density);
imageView.setLayoutParams(new LinearLayout.LayoutParams(size, size));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
layout.addView(imageView);
ViewHolder viewHolder = new ViewHolder(layout);
viewHolder.textView = textView;
viewHolder.imageView = imageView;
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final int adapterPosition = holder.getAdapterPosition();
holder.textView.setText(paths.get(adapterPosition));
holder.imageView.setImageDrawable(arrowIcon);
if (adapterPosition == getItemCount() - 1) {
holder.textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
holder.imageView.setVisibility(View.GONE);
} else {
holder.textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
holder.imageView.setVisibility(View.VISIBLE);
}
if (onPathClickedListener == null) {
return;
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPathClickedListener.onPathClicked(PathAdapter.this, adapterPosition, getPath(adapterPosition));
}
});
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return paths.size();
}
public void setArrowIcon(Drawable arrowIcon) {
this.arrowIcon = arrowIcon;
}
@SuppressLint("NotifyDataSetChanged")
public void updatePath(File file) {
if (arrowIcon == null) {
arrowIcon = ContextCompat.getDrawable(context, R.mipmap.file_picker_arrow);
}
paths.clear();
String path = file.getAbsolutePath();
if (!File.separator.equals(path)) {
Collections.addAll(paths, path.substring(path.indexOf(File.separator) + 1)
.split(File.separator));
}
paths.addFirst(ROOT_HINT);
notifyDataSetChanged();
}
public String getPath(int position) {
StringBuilder sb = new StringBuilder(File.separator);
//忽略根目录
if (position == 0) {
return sb.toString();
}
for (int i = 1; i <= position; i++) {
sb.append(paths.get(i)).append(File.separator);
}
return sb.toString();
}
public final void recycleData() {
paths.clear();
if (arrowIcon instanceof BitmapDrawable) {
Bitmap homeBitmap = ((BitmapDrawable) arrowIcon).getBitmap();
if (null != homeBitmap && !homeBitmap.isRecycled()) {
homeBitmap.recycle();
}
}
}
public void setOnPathClickedListener(OnPathClickedListener listener) {
onPathClickedListener = listener;
}
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/adapter/ViewHolder.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.adapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/10 20:47
*/
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/annotation/ExplorerMode.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/10 15:28
*/
@Retention(RetentionPolicy.SOURCE)
public @interface ExplorerMode {
int DIRECTORY = 0;
int FILE = 1;
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/annotation/FileSort.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/10 14:35
*/
@Retention(RetentionPolicy.SOURCE)
public @interface FileSort {
int BY_NAME_ASC = 0;
int BY_NAME_DESC = 1;
int BY_TIME_ASC = 2;
int BY_TIME_DESC = 3;
int BY_SIZE_ASC = 4;
int BY_SIZE_DESC = 5;
int BY_EXTENSION_ASC = 6;
int BY_EXTENSION_DESC = 7;
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/contract/OnFileClickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.contract;
import androidx.annotation.NonNull;
import java.io.File;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/10 19:33
*/
public interface OnFileClickedListener {
void onFileClicked(@NonNull File file);
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/contract/OnFileLoadedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.contract;
import androidx.annotation.NonNull;
import java.io.File;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2022/3/16 14:29
*/
public interface OnFileLoadedListener {
void onFileLoaded(@NonNull File file);
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/contract/OnFilePickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.contract;
import androidx.annotation.NonNull;
import java.io.File;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2015/9/29
*/
public interface OnFilePickedListener {
void onFilePicked(@NonNull File file);
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/contract/OnPathClickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.contract;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.github.gzuliyujiang.filepicker.adapter.ViewHolder;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/10 20:15
*/
public interface OnPathClickedListener {
void onPathClicked(RecyclerView.Adapter adapter, int position, @NonNull String path);
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/filter/PatternFilter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.filter;
import androidx.annotation.NonNull;
import java.io.File;
import java.io.FileFilter;
import java.util.regex.Pattern;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/10 15:07
*/
public class PatternFilter implements FileFilter {
private final Pattern pattern;
public PatternFilter(@NonNull Pattern pattern) {
this.pattern = pattern;
}
@Override
public boolean accept(File pathname) {
if (pathname == null) {
return false;
}
return pattern.matcher(pathname.getName()).find();
}
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/filter/SimpleFilter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.filter;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.github.gzuliyujiang.dialog.DialogLog;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/10 15:11
*/
public class SimpleFilter implements FileFilter {
private final boolean isOnlyDir;
private final String[] allowExtensions;
public SimpleFilter(boolean isOnlyDir, @Nullable String[] allowExtensions) {
this.isOnlyDir = isOnlyDir;
this.allowExtensions = allowExtensions;
}
@Override
public boolean accept(File pathname) {
if (pathname == null) {
DialogLog.print("Filter>>>pathname is null");
return false;
}
if (pathname.isDirectory()) {
DialogLog.print("Filter>>>pathname is directory: " + pathname);
return true;
}
if (isOnlyDir && pathname.isFile()) {
DialogLog.print("Filter>>>except directory but is file: " + pathname);
return false;
}
if (allowExtensions == null || allowExtensions.length == 0) {
DialogLog.print("Filter>>>allow extensions is empty: " + pathname);
return true;
}
//返回当前目录所有以某些扩展名结尾的文件
String extension = getExtension(pathname.getPath());
DialogLog.print("Filter>>>extension of " + pathname + ": " + extension);
boolean contains = false;
for (String allowExtension : allowExtensions) {
if (TextUtils.isEmpty(allowExtension) || allowExtension.contains(extension)) {
contains = true;
break;
}
}
DialogLog.print("Filter>>>allow extensions is " + Arrays.toString(allowExtensions) + ", contains: " + contains);
return contains;
}
private String getExtension(String path) {
if (path == null) {
return "";
}
String ext = "";
int slashPos = path.lastIndexOf(File.separator);
if (slashPos != -1) {
path = path.substring(slashPos);
}
int dotPos = path.lastIndexOf('.');
if (dotPos != -1) {
ext = path.substring(dotPos + 1);
} else {
ext = path;
}
return ext;
}
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/sort/SortByExtension.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.sort;
import java.io.File;
import java.util.Comparator;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2014/4/18
*/
public class SortByExtension implements Comparator {
@Override
public int compare(File f1, File f2) {
if (f1 == null || f2 == null) {
if (f1 == null) {
return -1;
} else {
return 1;
}
} else {
if (f1.isDirectory() && f2.isFile()) {
return -1;
} else if (f1.isFile() && f2.isDirectory()) {
return 1;
} else {
return f1.getName().compareToIgnoreCase(f2.getName());
}
}
}
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/sort/SortByName.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.sort;
import java.io.File;
import java.util.Comparator;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2014/4/18
*/
public class SortByName implements Comparator {
private static final boolean CASE_SENSITIVE = false;
@Override
public int compare(File f1, File f2) {
if (f1 == null || f2 == null) {
if (f1 == null) {
return -1;
} else {
return 1;
}
} else {
if (f1.isDirectory() && f2.isFile()) {
return -1;
} else if (f1.isFile() && f2.isDirectory()) {
return 1;
} else {
String s1 = f1.getName();
String s2 = f2.getName();
if (CASE_SENSITIVE) {
return s1.compareTo(s2);
} else {
return s1.compareToIgnoreCase(s2);
}
}
}
}
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/sort/SortBySize.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.sort;
import java.io.File;
import java.util.Comparator;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2014/4/18
*/
public class SortBySize implements Comparator {
@Override
public int compare(File f1, File f2) {
if (f1 == null || f2 == null) {
if (f1 == null) {
return -1;
} else {
return 1;
}
} else {
if (f1.isDirectory() && f2.isFile()) {
return -1;
} else if (f1.isFile() && f2.isDirectory()) {
return 1;
} else {
if (f1.length() < f2.length()) {
return -1;
} else if (f1.length() > f2.length()) {
return 1;
} else {
return 0;
}
}
}
}
}
================================================
FILE: FilePicker/src/main/java/com/github/gzuliyujiang/filepicker/sort/SortByTime.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.filepicker.sort;
import java.io.File;
import java.util.Comparator;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2014/4/18
*/
public class SortByTime implements Comparator {
@Override
public int compare(File f1, File f2) {
if (f1 == null || f2 == null) {
if (f1 == null) {
return -1;
} else {
return 1;
}
} else {
if (f1.isDirectory() && f2.isFile()) {
return -1;
} else if (f1.isFile() && f2.isDirectory()) {
return 1;
} else {
if (f1.lastModified() > f2.lastModified()) {
return -1;
} else if (f1.lastModified() < f2.lastModified()) {
return -1;
} else {
return 0;
}
}
}
}
}
================================================
FILE: FilePicker/src/main/res/layout/file_picker_content.xml
================================================
================================================
FILE: ImagePicker/README.md
================================================
# 图片选择(相机+相册+裁剪)
图片选择(相机+相册+裁剪),改自 [ImagePicker](https://github.com/linchaolong/ImagePicker)
及 [Android-Image-Cropper](https://github.com/ArthurHub/Android-Image-Cropper) 。
注意合规性,允许APP使用摄像头,允许APP读取/写入外置存储器,调用之前要确保已获得用户授予`CAMERA`、`READ_EXTERNAL_STORAGE`、`WRITE_EXTERNAL_STORAGE`权限。
## 简单用法
```java
public class ImagePickerActivity extends FragmentActivity {
private final PickCallback cropCallback = new PickCallback() {
@Override
public void onPermissionDenied(String[] permissions, String message) {
Toast.makeText(ImagePickerActivity.this, message, Toast.LENGTH_SHORT).show();
}
@Override
public void cropConfig(ActivityBuilder builder) {
builder.setMultiTouchEnabled(true)
.setGuidelines(CropImageView.Guidelines.ON_TOUCH)
.setCropShape(CropImageView.CropShape.OVAL)
.setRequestedSize(400, 400)
.setFixAspectRatio(true)
.setAspectRatio(1, 1);
}
@Override
public void onCropImage(@Nullable Uri imageUri) {
Toast.makeText(ImagePickerActivity.this, String.valueOf(imageUri), Toast.LENGTH_SHORT).show();
}
};
public void onCamera(View view) {
ImagePicker.getInstance().startCamera(this, true, cropCallback);
}
public void onGallery(View view) {
ImagePicker.getInstance().startGallery(this, true, cropCallback);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ImagePicker.getInstance().onActivityResult(this, requestCode, resultCode, data);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
ImagePicker.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
}
}
```
================================================
FILE: ImagePicker/build.gradle
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
apply from: "${rootDir}/gradle/library.gradle"
apply from: "${rootDir}/gradle/publish.gradle"
dependencies {
implementation androidxLibrary.annotation
implementation androidxLibrary.core
implementation androidxLibrary.fragment
implementation androidxLibrary.exifinterface
}
================================================
FILE: ImagePicker/consumer-rules.pro
================================================
# 本库模块专用的混淆规则
================================================
FILE: ImagePicker/src/main/AndroidManifest.xml
================================================
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/ActivityBuilder.java
================================================
package com.github.gzuliyujiang.imagepicker;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
/**
* Builder used for creating Image Crop Activity by user request.
*/
@SuppressWarnings({"unused", "UnusedReturnValue"})
public class ActivityBuilder {
/**
* The image to crop source Android uri.
*/
private final Uri mSource;
/**
* Options for image crop UX
*/
private final CropImageOptions mOptions;
public ActivityBuilder(Uri source) {
mSource = source;
mOptions = new CropImageOptions();
}
/**
* Get {@link CropImageActivity} intent to start the activity.
*/
public Intent getCropIntent(@NonNull Context context) {
mOptions.validate();
Intent intent = new Intent(context, CropImageActivity.class);
intent.putExtra(CropImageConsts.CROP_IMAGE_EXTRA_SOURCE, mSource);
intent.putExtra(CropImageConsts.CROP_IMAGE_EXTRA_OPTIONS, mOptions);
return intent;
}
/**
* Start {@link CropImageActivity}.
*
* @param activity activity to receive result
*/
public void start(@NonNull Activity activity) {
mOptions.validate();
activity.startActivityForResult(getCropIntent(activity), CropImageConsts.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
}
/**
* Start {@link CropImageActivity}.
*
* @param fragment fragment to receive result
*/
public void start(@NonNull Fragment fragment) {
fragment.startActivityForResult(getCropIntent(fragment.requireActivity()), CropImageConsts.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
}
/**
* The shape of the cropping window.
* To set square/circle crop shape set aspect ratio to 1:1.
* Default: RECTANGLE
*/
public ActivityBuilder setCropShape(@NonNull CropImageView.CropShape cropShape) {
mOptions.cropShape = cropShape;
return this;
}
/**
* An edge of the crop window will snap to the corresponding edge of a specified bounding box
* when the crop window edge is less than or equal to this distance (in pixels) away from the bounding box
* edge (in pixels).
* Default: 3dp
*/
public ActivityBuilder setSnapRadius(float snapRadius) {
mOptions.snapRadius = snapRadius;
return this;
}
/**
* The radius of the touchable area around the handle (in pixels).
* We are basing this value off of the recommended 48dp Rhythm.
* See: http://developer.android.com/design/style/metrics-grids.html#48dp-rhythm
* Default: 48dp
*/
public ActivityBuilder setTouchRadius(float touchRadius) {
mOptions.touchRadius = touchRadius;
return this;
}
/**
* whether the guidelines should be on, off, or only showing when resizing.
* Default: ON_TOUCH
*/
public ActivityBuilder setGuidelines(@NonNull CropImageView.Guidelines guidelines) {
mOptions.guidelines = guidelines;
return this;
}
/**
* The initial scale type of the image in the crop image view
* Default: FIT_CENTER
*/
public ActivityBuilder setScaleType(@NonNull CropImageView.ScaleType scaleType) {
mOptions.scaleType = scaleType;
return this;
}
/**
* if to show crop overlay UI what contains the crop window UI surrounded by background over the cropping
* image.
* default: true, may disable for animation or frame transition.
*/
public ActivityBuilder setShowCropOverlay(boolean showCropOverlay) {
mOptions.showCropOverlay = showCropOverlay;
return this;
}
/**
* if auto-zoom functionality is enabled.
* default: true.
*/
public ActivityBuilder setAutoZoomEnabled(boolean autoZoomEnabled) {
mOptions.autoZoomEnabled = autoZoomEnabled;
return this;
}
/**
* if multi touch functionality is enabled.
* default: true.
*/
public ActivityBuilder setMultiTouchEnabled(boolean multiTouchEnabled) {
mOptions.multiTouchEnabled = multiTouchEnabled;
return this;
}
/**
* The max zoom allowed during cropping.
* Default: 4
*/
public ActivityBuilder setMaxZoom(int maxZoom) {
mOptions.maxZoom = maxZoom;
return this;
}
/**
* The initial crop window padding from image borders in percentage of the cropping image dimensions.
* Default: 0.1
*/
public ActivityBuilder setInitialCropWindowPaddingRatio(float initialCropWindowPaddingRatio) {
mOptions.initialCropWindowPaddingRatio = initialCropWindowPaddingRatio;
return this;
}
/**
* whether the width to height aspect ratio should be maintained or free to change.
* Default: false
*/
public ActivityBuilder setFixAspectRatio(boolean fixAspectRatio) {
mOptions.fixAspectRatio = fixAspectRatio;
return this;
}
/**
* the X,Y value of the aspect ratio.
* Also sets fixes aspect ratio to TRUE.
* Default: 1/1
*
* @param aspectRatioX the width
* @param aspectRatioY the height
*/
public ActivityBuilder setAspectRatio(int aspectRatioX, int aspectRatioY) {
mOptions.aspectRatioX = aspectRatioX;
mOptions.aspectRatioY = aspectRatioY;
mOptions.fixAspectRatio = true;
return this;
}
/**
* the thickness of the guidelines lines (in pixels).
* Default: 3dp
*/
public ActivityBuilder setBorderLineThickness(float borderLineThickness) {
mOptions.borderLineThickness = borderLineThickness;
return this;
}
/**
* the color of the guidelines lines.
* Default: Color.argb(170, 255, 255, 255)
*/
public ActivityBuilder setBorderLineColor(int borderLineColor) {
mOptions.borderLineColor = borderLineColor;
return this;
}
/**
* thickness of the corner line (in pixels).
* Default: 2dp
*/
public ActivityBuilder setBorderCornerThickness(float borderCornerThickness) {
mOptions.borderCornerThickness = borderCornerThickness;
return this;
}
/**
* the offset of corner line from crop window border (in pixels).
* Default: 5dp
*/
public ActivityBuilder setBorderCornerOffset(float borderCornerOffset) {
mOptions.borderCornerOffset = borderCornerOffset;
return this;
}
/**
* the length of the corner line away from the corner (in pixels).
* Default: 14dp
*/
public ActivityBuilder setBorderCornerLength(float borderCornerLength) {
mOptions.borderCornerLength = borderCornerLength;
return this;
}
/**
* the color of the corner line.
* Default: WHITE
*/
public ActivityBuilder setBorderCornerColor(int borderCornerColor) {
mOptions.borderCornerColor = borderCornerColor;
return this;
}
/**
* the thickness of the guidelines lines (in pixels).
* Default: 1dp
*/
public ActivityBuilder setGuidelinesThickness(float guidelinesThickness) {
mOptions.guidelinesThickness = guidelinesThickness;
return this;
}
/**
* the color of the guidelines lines.
* Default: Color.argb(170, 255, 255, 255)
*/
public ActivityBuilder setGuidelinesColor(int guidelinesColor) {
mOptions.guidelinesColor = guidelinesColor;
return this;
}
/**
* the color of the overlay background around the crop window cover the image parts not in the crop window.
* Default: Color.argb(119, 0, 0, 0)
*/
public ActivityBuilder setBackgroundColor(int backgroundColor) {
mOptions.backgroundColor = backgroundColor;
return this;
}
/**
* the min size the crop window is allowed to be (in pixels).
* Default: 42dp, 42dp
*/
public ActivityBuilder setMinCropWindowSize(int minCropWindowWidth, int minCropWindowHeight) {
mOptions.minCropWindowWidth = minCropWindowWidth;
mOptions.minCropWindowHeight = minCropWindowHeight;
return this;
}
/**
* the min size the resulting cropping image is allowed to be, affects the cropping window limits
* (in pixels).
* Default: 40px, 40px
*/
public ActivityBuilder setMinCropResultSize(int minCropResultWidth, int minCropResultHeight) {
mOptions.minCropResultWidth = minCropResultWidth;
mOptions.minCropResultHeight = minCropResultHeight;
return this;
}
/**
* the max size the resulting cropping image is allowed to be, affects the cropping window limits
* (in pixels).
* Default: 99999, 99999
*/
public ActivityBuilder setMaxCropResultSize(int maxCropResultWidth, int maxCropResultHeight) {
mOptions.maxCropResultWidth = maxCropResultWidth;
mOptions.maxCropResultHeight = maxCropResultHeight;
return this;
}
/**
* the Android Uri to save the cropped image to.
* Default: NONE, will create a temp file
*/
public ActivityBuilder setOutputUri(Uri outputUri) {
mOptions.outputUri = outputUri;
return this;
}
/**
* the compression format to use when writting the image.
* Default: JPEG
*/
public ActivityBuilder setOutputCompressFormat(Bitmap.CompressFormat outputCompressFormat) {
mOptions.outputCompressFormat = outputCompressFormat;
return this;
}
/**
* the quility (if applicable) to use when writting the image (0 - 100).
* Default: 90
*/
public ActivityBuilder setOutputCompressQuality(int outputCompressQuality) {
mOptions.outputCompressQuality = outputCompressQuality;
return this;
}
/**
* the size to resize the cropped image to.
* Uses {@link CropImageView.RequestSizeOptions#RESIZE_INSIDE} option.
* Default: 0, 0 - not set, will not resize
*/
public ActivityBuilder setRequestedSize(int reqWidth, int reqHeight) {
return setRequestedSize(reqWidth, reqHeight, CropImageView.RequestSizeOptions.RESIZE_INSIDE);
}
/**
* the size to resize the cropped image to.
* Default: 0, 0 - not set, will not resize
*/
public ActivityBuilder setRequestedSize(int reqWidth, int reqHeight, CropImageView.RequestSizeOptions options) {
mOptions.outputRequestWidth = reqWidth;
mOptions.outputRequestHeight = reqHeight;
mOptions.outputRequestSizeOptions = options;
return this;
}
/**
* if the result of crop image activity should not save the cropped image bitmap.
* Used if you want to crop the image manually and need only the crop rectangle and rotation data.
* Default: false
*/
public ActivityBuilder setNoOutputImage(boolean noOutputImage) {
mOptions.noOutputImage = noOutputImage;
return this;
}
/**
* the initial rectangle to set on the cropping image after loading.
* Default: NONE - will initialize using initial crop window padding ratio
*/
public ActivityBuilder setInitialCropWindowRectangle(Rect initialCropWindowRectangle) {
mOptions.initialCropWindowRectangle = initialCropWindowRectangle;
return this;
}
/**
* the initial rotation to set on the cropping image after loading (0-360 degrees clockwise).
* Default: NONE - will read image exif data
*/
public ActivityBuilder setInitialRotation(int initialRotation) {
mOptions.initialRotation = initialRotation;
return this;
}
/**
* if to allow rotation during cropping.
* Default: true
*/
public ActivityBuilder setAllowRotation(boolean allowRotation) {
mOptions.allowRotation = allowRotation;
return this;
}
/**
* The amount of degreees to rotate clockwise or counter-clockwise (0-360).
* Default: 90
*/
public ActivityBuilder setRotationDegrees(int rotationDegrees) {
mOptions.rotationDegrees = rotationDegrees;
return this;
}
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/ActivityResult.java
================================================
package com.github.gzuliyujiang.imagepicker;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Result data of Crop Image Activity.
*/
public class ActivityResult extends CropImageView.CropResult implements Parcelable {
public static final Creator CREATOR = new Creator() {
@Override
public ActivityResult createFromParcel(Parcel in) {
return new ActivityResult(in);
}
@Override
public ActivityResult[] newArray(int size) {
return new ActivityResult[size];
}
};
public ActivityResult(Bitmap bitmap, Uri uri, Exception error, float[] cropPoints, Rect cropRect, int rotation, int sampleSize) {
super(bitmap, uri, error, cropPoints, cropRect, rotation, sampleSize);
}
protected ActivityResult(Parcel in) {
super(null,
in.readParcelable(Uri.class.getClassLoader()),
(Exception) in.readSerializable(),
in.createFloatArray(),
in.readParcelable(Rect.class.getClassLoader()),
in.readInt(), in.readInt());
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(getUri(), flags);
dest.writeSerializable(getError());
dest.writeFloatArray(getCropPoints());
dest.writeParcelable(getCropRect(), flags);
dest.writeInt(getRotation());
dest.writeInt(getSampleSize());
}
@Override
public int describeContents() {
return 0;
}
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/BitmapCroppingTask.java
================================================
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
package com.github.gzuliyujiang.imagepicker;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import java.lang.ref.WeakReference;
/**
* Task to crop bitmap asynchronously from the UI thread.
*/
@SuppressLint("StaticFieldLeak")
final class BitmapCroppingTask extends AsyncTask {
//region: Fields and Consts
/**
* Use a WeakReference to ensure the ImageView can be garbage collected
*/
private final WeakReference mCropImageViewReference;
/**
* the bitmap to crop
*/
private final Bitmap mBitmap;
/**
* The Android URI of the image to load
*/
private final Uri mUri;
/**
* Required cropping 4 points (x0,y0,x1,y1,x2,y2,x3,y3)
*/
private final float[] mCropPoints;
/**
* Degrees the image was rotated after loading
*/
private final int mDegreesRotated;
/**
* the original width of the image to be cropped (for image loaded from URI)
*/
private final int mOrgWidth;
/**
* the original height of the image to be cropped (for image loaded from URI)
*/
private final int mOrgHeight;
/**
* is there is fixed aspect ratio for the crop rectangle
*/
private final boolean mFixAspectRatio;
/**
* the X aspect ration of the crop rectangle
*/
private final int mAspectRatioX;
/**
* the Y aspect ration of the crop rectangle
*/
private final int mAspectRatioY;
/**
* required width of the cropping image
*/
private final int mReqWidth;
/**
* required height of the cropping image
*/
private final int mReqHeight;
/**
* The option to handle requested width/height
*/
private final CropImageView.RequestSizeOptions mReqSizeOptions;
/**
* the Android Uri to save the cropped image to
*/
private final Uri mSaveUri;
/**
* the compression format to use when writing the image
*/
private final Bitmap.CompressFormat mSaveCompressFormat;
/**
* the quality (if applicable) to use when writing the image (0 - 100)
*/
private final int mSaveCompressQuality;
//endregion
BitmapCroppingTask(CropImageView cropImageView, Bitmap bitmap, float[] cropPoints,
int degreesRotated, boolean fixAspectRatio, int aspectRatioX, int aspectRatioY,
int reqWidth, int reqHeight, CropImageView.RequestSizeOptions options,
Uri saveUri, Bitmap.CompressFormat saveCompressFormat, int saveCompressQuality) {
mCropImageViewReference = new WeakReference<>(cropImageView);
mBitmap = bitmap;
mCropPoints = cropPoints;
mUri = null;
mDegreesRotated = degreesRotated;
mFixAspectRatio = fixAspectRatio;
mAspectRatioX = aspectRatioX;
mAspectRatioY = aspectRatioY;
mReqWidth = reqWidth;
mReqHeight = reqHeight;
mReqSizeOptions = options;
mSaveUri = saveUri;
mSaveCompressFormat = saveCompressFormat;
mSaveCompressQuality = saveCompressQuality;
mOrgWidth = 0;
mOrgHeight = 0;
}
BitmapCroppingTask(CropImageView cropImageView, Uri uri, float[] cropPoints,
int degreesRotated, int orgWidth, int orgHeight,
boolean fixAspectRatio, int aspectRatioX, int aspectRatioY,
int reqWidth, int reqHeight, CropImageView.RequestSizeOptions options,
Uri saveUri, Bitmap.CompressFormat saveCompressFormat, int saveCompressQuality) {
mCropImageViewReference = new WeakReference<>(cropImageView);
mUri = uri;
mCropPoints = cropPoints;
mDegreesRotated = degreesRotated;
mFixAspectRatio = fixAspectRatio;
mAspectRatioX = aspectRatioX;
mAspectRatioY = aspectRatioY;
mOrgWidth = orgWidth;
mOrgHeight = orgHeight;
mReqWidth = reqWidth;
mReqHeight = reqHeight;
mReqSizeOptions = options;
mSaveUri = saveUri;
mSaveCompressFormat = saveCompressFormat;
mSaveCompressQuality = saveCompressQuality;
mBitmap = null;
}
/**
* The Android URI that this task is currently loading.
*/
public Uri getUri() {
return mUri;
}
/**
* Crop image in background.
*
* @param params ignored
* @return the decoded bitmap data
*/
@Override
protected BitmapCroppingTask.Result doInBackground(Void... params) {
try {
if (!isCancelled()) {
CropImageView cropImageView = mCropImageViewReference.get();
Context mContext = cropImageView.getContext();
BitmapUtils.BitmapSampled bitmapSampled;
if (mUri != null) {
bitmapSampled = BitmapUtils.cropBitmap(mContext, mUri, mCropPoints, mDegreesRotated, mOrgWidth, mOrgHeight,
mFixAspectRatio, mAspectRatioX, mAspectRatioY, mReqWidth, mReqHeight);
} else if (mBitmap != null) {
bitmapSampled = BitmapUtils.cropBitmapObjectHandleOOM(mBitmap, mCropPoints, mDegreesRotated, mFixAspectRatio, mAspectRatioX, mAspectRatioY);
} else {
return new Result((Bitmap) null, 1);
}
Bitmap bitmap = BitmapUtils.resizeBitmap(bitmapSampled.bitmap, mReqWidth, mReqHeight, mReqSizeOptions);
if (mSaveUri == null) {
return new Result(bitmap, bitmapSampled.sampleSize);
} else {
BitmapUtils.writeBitmapToUri(mContext, bitmap, mSaveUri, mSaveCompressFormat, mSaveCompressQuality);
bitmap.recycle();
return new Result(mSaveUri, bitmapSampled.sampleSize);
}
}
return null;
} catch (Exception e) {
return new Result(e, mSaveUri != null);
}
}
/**
* Once complete, see if ImageView is still around and set bitmap.
*
* @param result the result of bitmap cropping
*/
@Override
protected void onPostExecute(Result result) {
if (result != null) {
boolean completeCalled = false;
if (!isCancelled()) {
CropImageView cropImageView = mCropImageViewReference.get();
if (cropImageView != null) {
completeCalled = true;
cropImageView.onImageCroppingAsyncComplete(result);
}
}
if (!completeCalled && result.bitmap != null) {
// fast release of unused bitmap
result.bitmap.recycle();
}
}
}
//region: Inner class: Result
/**
* The result of BitmapCroppingWorkerTask async loading.
*/
static final class Result {
/**
* The cropped bitmap
*/
public final Bitmap bitmap;
/**
* The saved cropped bitmap uri
*/
public final Uri uri;
/**
* The error that occurred during async bitmap cropping.
*/
final Exception error;
/**
* is the cropping request was to get a bitmap or to save it to uri
*/
final boolean isSave;
/**
* sample size used creating the crop bitmap to lower its size
*/
final int sampleSize;
Result(Bitmap bitmap, int sampleSize) {
this.bitmap = bitmap;
this.uri = null;
this.error = null;
this.isSave = false;
this.sampleSize = sampleSize;
}
Result(Uri uri, int sampleSize) {
this.bitmap = null;
this.uri = uri;
this.error = null;
this.isSave = true;
this.sampleSize = sampleSize;
}
Result(Exception error, boolean isSave) {
this.bitmap = null;
this.uri = null;
this.error = error;
this.isSave = isSave;
this.sampleSize = 1;
}
}
//endregion
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/BitmapLoadingTask.java
================================================
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
package com.github.gzuliyujiang.imagepicker;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.DisplayMetrics;
import java.lang.ref.WeakReference;
/**
* Task to load bitmap asynchronously from the UI thread.
*/
final class BitmapLoadingTask extends AsyncTask {
//region: Fields and Consts
/**
* Use a WeakReference to ensure the ImageView can be garbage collected
*/
private final WeakReference mCropImageViewReference;
/**
* The Android URI of the image to load
*/
private final Uri mUri;
/**
* required width of the cropping image after density adjustment
*/
private final int mWidth;
/**
* required height of the cropping image after density adjustment
*/
private final int mHeight;
//endregion
public BitmapLoadingTask(CropImageView cropImageView, Uri uri) {
mUri = uri;
mCropImageViewReference = new WeakReference<>(cropImageView);
DisplayMetrics metrics = cropImageView.getResources().getDisplayMetrics();
double densityAdj = metrics.density > 1 ? 1 / metrics.density : 1;
mWidth = (int) (metrics.widthPixels * densityAdj);
mHeight = (int) (metrics.heightPixels * densityAdj);
}
/**
* The Android URI that this task is currently loading.
*/
public Uri getUri() {
return mUri;
}
/**
* Decode image in background.
*
* @param params ignored
* @return the decoded bitmap data
*/
@Override
protected Result doInBackground(Void... params) {
try {
CropImageView cropImageView = mCropImageViewReference.get();
Context mContext = cropImageView.getContext();
if (!isCancelled()) {
BitmapUtils.BitmapSampled decodeResult =
BitmapUtils.decodeSampledBitmap(mContext, mUri, mWidth, mHeight);
if (!isCancelled()) {
BitmapUtils.RotateBitmapResult rotateResult =
BitmapUtils.rotateBitmapByExif(decodeResult.bitmap, mContext, mUri);
return new Result(mUri, rotateResult.bitmap, decodeResult.sampleSize, rotateResult.degrees);
}
}
return null;
} catch (Exception e) {
return new Result(mUri, e);
}
}
/**
* Once complete, see if ImageView is still around and set bitmap.
*
* @param result the result of bitmap loading
*/
@Override
protected void onPostExecute(Result result) {
if (result != null) {
boolean completeCalled = false;
if (!isCancelled()) {
CropImageView cropImageView = mCropImageViewReference.get();
if (cropImageView != null) {
completeCalled = true;
cropImageView.onSetImageUriAsyncComplete(result);
}
}
if (!completeCalled && result.bitmap != null) {
// fast release of unused bitmap
result.bitmap.recycle();
}
}
}
//region: Inner class: Result
/**
* The result of BitmapLoadingWorkerTask async loading.
*/
public static final class Result {
/**
* The Android URI of the image to load
*/
public final Uri uri;
/**
* The loaded bitmap
*/
public final Bitmap bitmap;
/**
* The sample size used to load the given bitmap
*/
public final int loadSampleSize;
/**
* The degrees the image was rotated
*/
public final int degreesRotated;
/**
* The error that occurred during async bitmap loading.
*/
public final Exception error;
Result(Uri uri, Bitmap bitmap, int loadSampleSize, int degreesRotated) {
this.uri = uri;
this.bitmap = bitmap;
this.loadSampleSize = loadSampleSize;
this.degreesRotated = degreesRotated;
this.error = null;
}
Result(Uri uri, Exception error) {
this.uri = uri;
this.bitmap = null;
this.loadSampleSize = 0;
this.degreesRotated = 0;
this.error = error;
}
}
//endregion
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/BitmapUtils.java
================================================
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
package com.github.gzuliyujiang.imagepicker;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.util.Pair;
import androidx.exifinterface.media.ExifInterface;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
/**
* Utility class that deals with operations with an ImageView.
*/
final class BitmapUtils {
static final Rect EMPTY_RECT = new Rect();
static final RectF EMPTY_RECT_F = new RectF();
/**
* Reusable rectangle for general internal usage
*/
static final RectF RECT = new RectF();
/**
* Reusable point for general internal usage
*/
static final float[] POINTS = new float[6];
/**
* Reusable point for general internal usage
*/
static final float[] POINTS2 = new float[6];
/**
* Used to know the max texture size allowed to be rendered
*/
private static int mMaxTextureSize;
/**
* used to save bitmaps during state save and restore so not to reload them.
*/
static Pair> mStateBitmap;
/**
* Rotate the given image by reading the Exif value of the image (uri).
* If no rotation is required the image will not be rotated.
* New bitmap is created and the old one is recycled.
*/
static RotateBitmapResult rotateBitmapByExif(Bitmap bitmap, Context context, Uri uri) {
try {
File file = getFileFromUri(context, uri);
if (file.exists()) {
ExifInterface ei = new ExifInterface(file.getAbsolutePath());
return rotateBitmapByExif(bitmap, ei);
}
} catch (Exception ignored) {
}
return new RotateBitmapResult(bitmap, 0);
}
/**
* Rotate the given image by given Exif value.
* If no rotation is required the image will not be rotated.
* New bitmap is created and the old one is recycled.
*/
static RotateBitmapResult rotateBitmapByExif(Bitmap bitmap, ExifInterface exif) {
int degrees;
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degrees = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degrees = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degrees = 270;
break;
default:
degrees = 0;
break;
}
return new RotateBitmapResult(bitmap, degrees);
}
/**
* Decode bitmap from stream using sampling to get bitmap with the requested limit.
*/
static BitmapSampled decodeSampledBitmap(Context context, Uri uri, int reqWidth, int reqHeight) {
try {
ContentResolver resolver = context.getContentResolver();
// First decode with inJustDecodeBounds=true to check dimensions
BitmapFactory.Options options = decodeImageForOption(resolver, uri);
// Calculate inSampleSize
options.inSampleSize = Math.max(
calculateInSampleSizeByReqestedSize(options.outWidth, options.outHeight, reqWidth, reqHeight),
calculateInSampleSizeByMaxTextureSize(options.outWidth, options.outHeight));
// Decode bitmap with inSampleSize set
Bitmap bitmap = decodeImage(resolver, uri, options);
return new BitmapSampled(bitmap, options.inSampleSize);
} catch (Exception e) {
throw new RuntimeException("Failed to load sampled bitmap: " + uri + "\r\n" + e.getMessage(), e);
}
}
/**
* Crop image bitmap from given bitmap using the given points in the original bitmap and the given rotation.
* if the rotation is not 0,90,180 or 270 degrees then we must first crop a larger area of the image that
* contains the requires rectangle, rotate and then crop again a sub rectangle.
* If crop fails due to OOM we scale the cropping image by 0.5 every time it fails until it is small enough.
*/
static BitmapSampled cropBitmapObjectHandleOOM(Bitmap bitmap, float[] points, int degreesRotated,
boolean fixAspectRatio, int aspectRatioX, int aspectRatioY) {
int scale = 1;
while (true) {
try {
Bitmap cropBitmap = cropBitmapObjectWithScale(bitmap, points, degreesRotated, fixAspectRatio, aspectRatioX, aspectRatioY, 1 / (float) scale);
return new BitmapSampled(cropBitmap, scale);
} catch (OutOfMemoryError e) {
scale *= 2;
if (scale > 8) {
throw e;
}
}
}
}
/**
* Crop image bitmap from given bitmap using the given points in the original bitmap and the given rotation.
* if the rotation is not 0,90,180 or 270 degrees then we must first crop a larger area of the image that
* contains the requires rectangle, rotate and then crop again a sub rectangle.
*
* @param scale how much to scale the cropped image part, use 0.5 to lower the image by half (OOM handling)
*/
private static Bitmap cropBitmapObjectWithScale(Bitmap bitmap, float[] points, int degreesRotated,
boolean fixAspectRatio, int aspectRatioX, int aspectRatioY, float scale) {
// get the rectangle in original image that contains the required cropped area (larger for non rectangular crop)
Rect rect = getRectFromPoints(points, bitmap.getWidth(), bitmap.getHeight(), fixAspectRatio, aspectRatioX, aspectRatioY);
// crop and rotate the cropped image in one operation
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
matrix.postRotate(degreesRotated, bitmap.getWidth() / 2f, bitmap.getHeight() / 2f);
Bitmap result = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height(), matrix, true);
if (result == bitmap) {
// corner case when all bitmap is selected, no worth optimizing for it
result = bitmap.copy(bitmap.getConfig(), false);
}
// rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping
if (degreesRotated % 90 != 0) {
// extra crop because non rectangular crop cannot be done directly on the image without rotating first
result = cropForRotatedImage(result, points, rect, degreesRotated, fixAspectRatio, aspectRatioX, aspectRatioY);
}
return result;
}
/**
* Crop image bitmap from URI by decoding it with specific width and height to down-sample if required.
* Additionally if OOM is thrown try to increase the sampling (2,4,8).
*/
static BitmapSampled cropBitmap(Context context, Uri loadedImageUri, float[] points,
int degreesRotated, int orgWidth, int orgHeight, boolean fixAspectRatio,
int aspectRatioX, int aspectRatioY, int reqWidth, int reqHeight) {
int sampleMulti = 1;
while (true) {
try {
// if successful, just return the resulting bitmap
return cropBitmap(context, loadedImageUri, points,
degreesRotated, orgWidth, orgHeight, fixAspectRatio,
aspectRatioX, aspectRatioY, reqWidth, reqHeight,
sampleMulti);
} catch (OutOfMemoryError e) {
// if OOM try to increase the sampling to lower the memory usage
sampleMulti *= 2;
if (sampleMulti > 16) {
throw new RuntimeException("Failed to handle OOM by sampling (" + sampleMulti + "): " + loadedImageUri + "\r\n" + e.getMessage(), e);
}
}
}
}
/**
* Get left value of the bounding rectangle of the given points.
*/
static float getRectLeft(float[] points) {
return Math.min(Math.min(Math.min(points[0], points[2]), points[4]), points[6]);
}
/**
* Get top value of the bounding rectangle of the given points.
*/
static float getRectTop(float[] points) {
return Math.min(Math.min(Math.min(points[1], points[3]), points[5]), points[7]);
}
/**
* Get right value of the bounding rectangle of the given points.
*/
static float getRectRight(float[] points) {
return Math.max(Math.max(Math.max(points[0], points[2]), points[4]), points[6]);
}
/**
* Get bottom value of the bounding rectangle of the given points.
*/
static float getRectBottom(float[] points) {
return Math.max(Math.max(Math.max(points[1], points[3]), points[5]), points[7]);
}
/**
* Get width of the bounding rectangle of the given points.
*/
static float getRectWidth(float[] points) {
return getRectRight(points) - getRectLeft(points);
}
/**
* Get heightof the bounding rectangle of the given points.
*/
static float getRectHeight(float[] points) {
return getRectBottom(points) - getRectTop(points);
}
/**
* Get horizontal center value of the bounding rectangle of the given points.
*/
static float getRectCenterX(float[] points) {
return (getRectRight(points) + getRectLeft(points)) / 2f;
}
/**
* Get verical center value of the bounding rectangle of the given points.
*/
static float getRectCenterY(float[] points) {
return (getRectBottom(points) + getRectTop(points)) / 2f;
}
/**
* Get a rectangle for the given 4 points (x0,y0,x1,y1,x2,y2,x3,y3) by finding the min/max 2 points that
* contains the given 4 points and is a stright rectangle.
*/
static Rect getRectFromPoints(float[] points, int imageWidth, int imageHeight, boolean fixAspectRatio, int aspectRatioX, int aspectRatioY) {
int left = Math.round(Math.max(0, getRectLeft(points)));
int top = Math.round(Math.max(0, getRectTop(points)));
int right = Math.round(Math.min(imageWidth, getRectRight(points)));
int bottom = Math.round(Math.min(imageHeight, getRectBottom(points)));
Rect rect = new Rect(left, top, right, bottom);
if (fixAspectRatio) {
fixRectForAspectRatio(rect, aspectRatioX, aspectRatioY);
}
return rect;
}
/**
* Fix the given rectangle if it doesn't confirm to aspect ration rule.
* Make sure that width and height are equal if 1:1 fixed aspect ratio is requested.
*/
private static void fixRectForAspectRatio(Rect rect, int aspectRatioX, int aspectRatioY) {
if (aspectRatioX == aspectRatioY && rect.width() != rect.height()) {
if (rect.height() > rect.width()) {
rect.bottom -= rect.height() - rect.width();
} else {
rect.right -= rect.width() - rect.height();
}
}
}
/**
* Write the given bitmap to the given uri using the given compression.
*/
static void writeBitmapToUri(Context context, Bitmap bitmap, Uri uri, Bitmap.CompressFormat compressFormat, int compressQuality) throws
FileNotFoundException {
OutputStream outputStream = null;
try {
outputStream = context.getContentResolver().openOutputStream(uri);
bitmap.compress(compressFormat, compressQuality, outputStream);
} finally {
closeSafe(outputStream);
}
}
/**
* Resize the given bitmap to the given width/height by the given option.
*/
static Bitmap resizeBitmap(Bitmap bitmap, int reqWidth, int reqHeight, CropImageView.RequestSizeOptions options) {
try {
if (reqWidth > 0 && reqHeight > 0 && (options == CropImageView.RequestSizeOptions.RESIZE_FIT ||
options == CropImageView.RequestSizeOptions.RESIZE_INSIDE ||
options == CropImageView.RequestSizeOptions.RESIZE_EXACT)) {
Bitmap resized = null;
if (options == CropImageView.RequestSizeOptions.RESIZE_EXACT) {
resized = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, false);
} else {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scale = Math.max(width / (float) reqWidth, height / (float) reqHeight);
if (scale > 1 || options == CropImageView.RequestSizeOptions.RESIZE_FIT) {
resized = Bitmap.createScaledBitmap(bitmap, (int) (width / scale), (int) (height / scale), false);
}
}
if (resized != null) {
if (resized != bitmap) {
bitmap.recycle();
}
return resized;
}
}
} catch (Exception e) {
Log.w("AIC", "Failed to resize cropped image, return bitmap before resize", e);
}
return bitmap;
}
//region: Private methods
/**
* Crop image bitmap from URI by decoding it with specific width and height to down-sample if required.
*
* @param orgWidth used to get rectangle from points (handle edge cases to limit rectangle)
* @param orgHeight used to get rectangle from points (handle edge cases to limit rectangle)
* @param sampleMulti used to increase the sampling of the image to handle memory issues.
*/
private static BitmapSampled cropBitmap(Context context, Uri loadedImageUri, float[] points,
int degreesRotated, int orgWidth, int orgHeight, boolean fixAspectRatio,
int aspectRatioX, int aspectRatioY, int reqWidth, int reqHeight, int sampleMulti) {
// get the rectangle in original image that contains the required cropped area (larger for non rectangular crop)
Rect rect = getRectFromPoints(points, orgWidth, orgHeight, fixAspectRatio, aspectRatioX, aspectRatioY);
int width = reqWidth > 0 ? reqWidth : rect.width();
int height = reqHeight > 0 ? reqHeight : rect.height();
Bitmap result = null;
int sampleSize = 1;
try {
// decode only the required image from URI, optionally sub-sampling if reqWidth/reqHeight is given.
BitmapSampled bitmapSampled = decodeSampledBitmapRegion(context, loadedImageUri, rect, width, height, sampleMulti);
result = bitmapSampled.bitmap;
sampleSize = bitmapSampled.sampleSize;
} catch (Exception ignored) {
}
if (result != null) {
try {
// rotate the decoded region by the required amount
result = rotateBitmapInt(result, degreesRotated);
// rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping
if (degreesRotated % 90 != 0) {
// extra crop because non rectangular crop cannot be done directly on the image without rotating first
result = cropForRotatedImage(result, points, rect, degreesRotated, fixAspectRatio, aspectRatioX, aspectRatioY);
}
} catch (OutOfMemoryError e) {
if (result != null) {
result.recycle();
}
throw e;
}
return new BitmapSampled(result, sampleSize);
} else {
// failed to decode region, may be skia issue, try full decode and then crop
return cropBitmap(context, loadedImageUri, points, degreesRotated, fixAspectRatio, aspectRatioX, aspectRatioY, sampleMulti, rect, width, height);
}
}
/**
* Crop bitmap by fully loading the original and then cropping it, fallback in case cropping region failed.
*/
private static BitmapSampled cropBitmap(Context context, Uri loadedImageUri, float[] points,
int degreesRotated, boolean fixAspectRatio, int aspectRatioX, int aspectRatioY,
int sampleMulti, Rect rect, int width, int height) {
Bitmap result = null;
int sampleSize;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = sampleSize = sampleMulti * calculateInSampleSizeByReqestedSize(rect.width(), rect.height(), width, height);
Bitmap fullBitmap = decodeImage(context.getContentResolver(), loadedImageUri, options);
if (fullBitmap != null) {
try {
// adjust crop points by the sampling because the image is smaller
float[] points2 = new float[points.length];
System.arraycopy(points, 0, points2, 0, points.length);
for (int i = 0; i < points2.length; i++) {
points2[i] = points2[i] / options.inSampleSize;
}
result = cropBitmapObjectWithScale(fullBitmap, points2, degreesRotated, fixAspectRatio, aspectRatioX, aspectRatioY, 1);
} finally {
if (result != fullBitmap) {
fullBitmap.recycle();
}
}
}
} catch (OutOfMemoryError e) {
if (result != null) {
result.recycle();
}
throw e;
} catch (Exception e) {
throw new RuntimeException("Failed to load sampled bitmap: " + loadedImageUri + "\r\n" + e.getMessage(), e);
}
return new BitmapSampled(result, sampleSize);
}
/**
* Decode image from uri using "inJustDecodeBounds" to get the image dimensions.
*/
private static BitmapFactory.Options decodeImageForOption(ContentResolver resolver, Uri uri) throws
FileNotFoundException {
InputStream stream = null;
try {
stream = resolver.openInputStream(uri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(stream, EMPTY_RECT, options);
options.inJustDecodeBounds = false;
return options;
} finally {
closeSafe(stream);
}
}
/**
* Decode image from uri using given "inSampleSize", but if failed due to out-of-memory then raise
* the inSampleSize until success.
*/
private static Bitmap decodeImage(ContentResolver resolver, Uri uri, BitmapFactory.Options options) throws
FileNotFoundException {
do {
InputStream stream = null;
try {
stream = resolver.openInputStream(uri);
return BitmapFactory.decodeStream(stream, EMPTY_RECT, options);
} catch (OutOfMemoryError e) {
options.inSampleSize *= 2;
} finally {
closeSafe(stream);
}
} while (options.inSampleSize <= 512);
throw new RuntimeException("Failed to decode image: " + uri);
}
/**
* Decode specific rectangle bitmap from stream using sampling to get bitmap with the requested limit.
*
* @param sampleMulti used to increase the sampling of the image to handle memory issues.
*/
private static BitmapSampled decodeSampledBitmapRegion(Context context, Uri uri, Rect rect, int reqWidth, int reqHeight, int sampleMulti) {
InputStream stream = null;
BitmapRegionDecoder decoder = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = sampleMulti * calculateInSampleSizeByReqestedSize(rect.width(), rect.height(), reqWidth, reqHeight);
stream = context.getContentResolver().openInputStream(uri);
decoder = BitmapRegionDecoder.newInstance(stream, false);
do {
try {
return new BitmapSampled(decoder.decodeRegion(rect, options), options.inSampleSize);
} catch (OutOfMemoryError e) {
options.inSampleSize *= 2;
}
} while (options.inSampleSize <= 512);
} catch (Exception e) {
throw new RuntimeException("Failed to load sampled bitmap: " + uri + "\r\n" + e.getMessage(), e);
} finally {
closeSafe(stream);
if (decoder != null) {
decoder.recycle();
}
}
return new BitmapSampled(null, 1);
}
/**
* Special crop of bitmap rotated by not stright angle, in this case the original crop bitmap contains parts
* beyond the required crop area, this method crops the already cropped and rotated bitmap to the final
* rectangle.
* Note: rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping.
*/
private static Bitmap cropForRotatedImage(Bitmap bitmap, float[] points, Rect rect, int degreesRotated,
boolean fixAspectRatio, int aspectRatioX, int aspectRatioY) {
if (degreesRotated % 90 != 0) {
int adjLeft = 0, adjTop = 0, width = 0, height = 0;
double rads = Math.toRadians(degreesRotated);
int compareTo = degreesRotated < 90 || (degreesRotated > 180 && degreesRotated < 270) ? rect.left : rect.right;
for (int i = 0; i < points.length; i += 2) {
if (points[i] >= compareTo - 1 && points[i] <= compareTo + 1) {
adjLeft = (int) Math.abs(Math.sin(rads) * (rect.bottom - points[i + 1]));
adjTop = (int) Math.abs(Math.cos(rads) * (points[i + 1] - rect.top));
width = (int) Math.abs((points[i + 1] - rect.top) / Math.sin(rads));
height = (int) Math.abs((rect.bottom - points[i + 1]) / Math.cos(rads));
break;
}
}
rect.set(adjLeft, adjTop, adjLeft + width, adjTop + height);
if (fixAspectRatio) {
fixRectForAspectRatio(rect, aspectRatioX, aspectRatioY);
}
Bitmap bitmapTmp = bitmap;
bitmap = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height());
if (bitmapTmp != bitmap) {
bitmapTmp.recycle();
}
}
return bitmap;
}
/**
* Calculate the largest inSampleSize value that is a power of 2 and keeps both
* height and width larger than the requested height and width.
*/
private static int calculateInSampleSizeByReqestedSize(int width, int height, int reqWidth, int reqHeight) {
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
while ((height / 2 / inSampleSize) > reqHeight && (width / 2 / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
/**
* Calculate the largest inSampleSize value that is a power of 2 and keeps both
* height and width smaller than max texture size allowed for the device.
*/
private static int calculateInSampleSizeByMaxTextureSize(int width, int height) {
int inSampleSize = 1;
if (mMaxTextureSize == 0) {
mMaxTextureSize = getMaxTextureSize();
}
if (mMaxTextureSize > 0) {
while ((height / inSampleSize) > mMaxTextureSize || (width / inSampleSize) > mMaxTextureSize) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
/**
* Get {@link File} object for the given Android URI.
* Use content resolver to get real path if direct path doesn't return valid file.
*/
private static File getFileFromUri(Context context, Uri uri) {
// first try by direct path
File file = new File(uri.getPath());
if (file.exists()) {
return file;
}
// try reading real path from content resolver (gallery images)
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(uri, proj, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String realPath = cursor.getString(column_index);
file = new File(realPath);
}
} catch (Exception ignored) {
} finally {
if (cursor != null) {
cursor.close();
}
}
return file;
}
/**
* Rotate the given bitmap by the given degrees.
* New bitmap is created and the old one is recycled.
*/
private static Bitmap rotateBitmapInt(Bitmap bitmap, int degrees) {
if (degrees > 0) {
Matrix matrix = new Matrix();
matrix.setRotate(degrees);
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
if (newBitmap != bitmap) {
bitmap.recycle();
}
return newBitmap;
} else {
return bitmap;
}
}
/**
* Get the max size of bitmap allowed to be rendered on the device.
* http://stackoverflow.com/questions/7428996/hw-accelerated-activity-how-to-get-opengl-texture-size-limit.
*/
private static int getMaxTextureSize() {
// Safe minimum default size
final int IMAGE_MAX_BITMAP_DIMENSION = 2048;
try {
// Get EGL Display
EGL10 egl = (EGL10) EGLContext.getEGL();
EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
// Initialise
int[] version = new int[2];
egl.eglInitialize(display, version);
// Query total number of configurations
int[] totalConfigurations = new int[1];
egl.eglGetConfigs(display, null, 0, totalConfigurations);
// Query actual list configurations
EGLConfig[] configurationsList = new EGLConfig[totalConfigurations[0]];
egl.eglGetConfigs(display, configurationsList, totalConfigurations[0], totalConfigurations);
int[] textureSize = new int[1];
int maximumTextureSize = 0;
// Iterate through all the configurations to located the maximum texture size
for (int i = 0; i < totalConfigurations[0]; i++) {
// Only need to check for width since opengl textures are always squared
egl.eglGetConfigAttrib(display, configurationsList[i], EGL10.EGL_MAX_PBUFFER_WIDTH, textureSize);
// Keep track of the maximum texture size
if (maximumTextureSize < textureSize[0]) {
maximumTextureSize = textureSize[0];
}
}
// Release
egl.eglTerminate(display);
// Return largest texture size found, or default
return Math.max(maximumTextureSize, IMAGE_MAX_BITMAP_DIMENSION);
} catch (Exception e) {
return IMAGE_MAX_BITMAP_DIMENSION;
}
}
/**
* Close the given closeable object (Stream) in a safe way: check if it is null and catch-log
* exception thrown.
*
* @param closeable the closable object to close
*/
private static void closeSafe(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ignored) {
}
}
}
//endregion
//region: Inner class: BitmapSampled
/**
* Holds bitmap instance and the sample size that the bitmap was loaded/cropped with.
*/
static final class BitmapSampled {
/**
* The bitmap instance
*/
public final Bitmap bitmap;
/**
* The sample size used to lower the size of the bitmap (1,2,4,8,...)
*/
final int sampleSize;
BitmapSampled(Bitmap bitmap, int sampleSize) {
this.bitmap = bitmap;
this.sampleSize = sampleSize;
}
}
//endregion
//region: Inner class: RotateBitmapResult
/**
* The result of {@link #rotateBitmapByExif(Bitmap, ExifInterface)}.
*/
static final class RotateBitmapResult {
/**
* The loaded bitmap
*/
public final Bitmap bitmap;
/**
* The degrees the image was rotated
*/
final int degrees;
RotateBitmapResult(Bitmap bitmap, int degrees) {
this.bitmap = bitmap;
this.degrees = degrees;
}
}
//endregion
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/CropImageActivity.java
================================================
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
package com.github.gzuliyujiang.imagepicker;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import java.io.File;
import java.io.IOException;
/**
* Built-in activity for image cropping.
*/
public class CropImageActivity extends FragmentActivity implements View.OnClickListener,
CropImageView.OnSetImageUriCompleteListener, CropImageView.OnCropImageCompleteListener {
/**
* The crop image view library widget used in the activity
*/
private CropImageView mCropImageView;
/**
* Persist URI image to crop URI if specific permissions are required
*/
private Uri mCropImageUri;
/**
* the options that were set for the crop image
*/
private CropImageOptions mOptions;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setStatusBarColor(0xFF000000);
getWindow().setNavigationBarColor(0xFF000000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().setNavigationBarDividerColor(0xFF000000);
}
setContentView(R.layout.crop_image_activity);
findViewById(R.id.crop_image_back).setOnClickListener(this);
View rotateView = findViewById(R.id.crop_image_rotate);
rotateView.setOnClickListener(this);
findViewById(R.id.crop_image_done).setOnClickListener(this);
mCropImageView = findViewById(R.id.crop_image_content);
Intent intent = getIntent();
mCropImageUri = intent.getParcelableExtra(CropImageConsts.CROP_IMAGE_EXTRA_SOURCE);
mOptions = intent.getParcelableExtra(CropImageConsts.CROP_IMAGE_EXTRA_OPTIONS);
mCropImageView.setCropImageOptions(mOptions);
if (!mOptions.allowRotation) {
rotateView.setVisibility(View.GONE);
}
if (savedInstanceState == null) {
if (mCropImageUri == null || mCropImageUri.equals(Uri.EMPTY)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && HybridityUtils.isExplicitCameraPermissionRequired(this)) {
// request permissions and handle the result in onRequestPermissionsResult()
requestPermissions(new String[]{Manifest.permission.CAMERA}, CropImageConsts.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE);
} else {
startCamera();
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && HybridityUtils.isReadExternalStoragePermissionsRequired(this, mCropImageUri)) {
// request permissions and handle the result in onRequestPermissionsResult()
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CropImageConsts.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
} else {
// no permissions required or already grunted, can start crop image activity
mCropImageView.setImageUriAsync(mCropImageUri);
}
}
}
private void startCamera() {
startActivityForResult(HybridityUtils.getCameraIntent(this), CropImageConsts.PICK_IMAGE_CHOOSER_REQUEST_CODE);
}
@Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.crop_image_back) {
setResultCancel();
} else if (id == R.id.crop_image_rotate) {
rotateImage(mOptions.rotationDegrees);
} else if (id == R.id.crop_image_done) {
cropImage();
}
}
@Override
protected void onStart() {
super.onStart();
mCropImageView.setOnSetImageUriCompleteListener(this);
mCropImageView.setOnCropImageCompleteListener(this);
}
@Override
protected void onStop() {
super.onStop();
mCropImageView.setOnSetImageUriCompleteListener(null);
mCropImageView.setOnCropImageCompleteListener(null);
}
@Override
public void onBackPressed() {
super.onBackPressed();
setResultCancel();
}
@Override
@SuppressLint("NewApi")
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// handle result of pick image chooser
if (requestCode == CropImageConsts.PICK_IMAGE_CHOOSER_REQUEST_CODE) {
if (resultCode == Activity.RESULT_CANCELED) {
//User cancelled the picker. We don't have anything to crop
setResultCancel();
}
if (resultCode == Activity.RESULT_OK) {
mCropImageUri = HybridityUtils.getPickImageResultUri(this, data);
// For API >= 23 we need to check specifically that we have permissions to read external storage.
if (HybridityUtils.isReadExternalStoragePermissionsRequired(this, mCropImageUri)) {
// request permissions and handle the result in onRequestPermissionsResult()
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, CropImageConsts.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
} else {
// no permissions required or already grunted, can start crop image activity
mCropImageView.setImageUriAsync(mCropImageUri);
}
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull
int[] grantResults) {
if (requestCode == CropImageConsts.PICK_IMAGE_PERMISSIONS_REQUEST_CODE) {
if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// required permissions granted, start crop image activity
mCropImageView.setImageUriAsync(mCropImageUri);
} else {
// Cancelling, required permissions are not granted
setResultCancel();
}
} else if (requestCode == CropImageConsts.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE) {
//Irrespective of whether camera permission was given or not, we show the picker
//The picker will not add the camera intent if permission is not available
startCamera();
}
}
@Override
public void onSetImageUriComplete(CropImageView view, Uri uri, Exception error) {
if (error == null) {
if (mOptions.initialCropWindowRectangle != null) {
mCropImageView.setCropRect(mOptions.initialCropWindowRectangle);
}
if (mOptions.initialRotation > -1) {
mCropImageView.setRotatedDegrees(mOptions.initialRotation);
}
} else {
setResult(null, error, 1);
}
}
@Override
public void onCropImageComplete(CropImageView view, CropImageView.CropResult result) {
setResult(result.getUri(), result.getError(), result.getSampleSize());
}
/**
* Execute crop image and save the result tou output uri.
*/
protected void cropImage() {
if (mOptions.noOutputImage) {
setResult(null, null, 1);
} else {
Uri outputUri = getOutputUri();
mCropImageView.saveCroppedImageAsync(outputUri,
mOptions.outputCompressFormat,
mOptions.outputCompressQuality,
mOptions.outputRequestWidth,
mOptions.outputRequestHeight,
mOptions.outputRequestSizeOptions);
}
}
/**
* Rotate the image in the crop image view.
*/
protected void rotateImage(int degrees) {
mCropImageView.rotateImage(degrees);
}
/**
* Get Android uri to save the cropped image into.
* Use the given in options or create a temp file.
*/
protected Uri getOutputUri() {
Uri outputUri = mOptions.outputUri;
if (outputUri.equals(Uri.EMPTY)) {
try {
String ext = mOptions.outputCompressFormat == Bitmap.CompressFormat.JPEG ? ".jpg" :
mOptions.outputCompressFormat == Bitmap.CompressFormat.PNG ? ".png" : ".webp";
outputUri = Uri.fromFile(File.createTempFile("cropped", ext, getCacheDir()));
} catch (IOException e) {
throw new RuntimeException("Failed to create temp file for output image", e);
}
}
return outputUri;
}
/**
* Result with cropped image data or error if failed.
*/
protected void setResult(Uri uri, Exception error, int sampleSize) {
int resultCode = error == null ? RESULT_OK : CropImageConsts.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE;
setResult(resultCode, getResultIntent(uri, error, sampleSize));
finish();
}
/**
* Cancel of cropping activity.
*/
protected void setResultCancel() {
setResult(RESULT_CANCELED);
finish();
}
/**
* Get intent instance to be used for the result of this activity.
*/
protected Intent getResultIntent(Uri uri, Exception error, int sampleSize) {
ActivityResult result = new ActivityResult(null,
uri,
error,
mCropImageView.getCropPoints(),
mCropImageView.getCropRect(),
mCropImageView.getRotatedDegrees(),
sampleSize);
Intent intent = new Intent();
intent.putExtra(CropImageConsts.CROP_IMAGE_EXTRA_RESULT, result);
return intent;
}
/**
* Update the color of a specific menu item to the given color.
*/
private void updateMenuItemIconColor(Menu menu, int itemId, int color) {
MenuItem menuItem = menu.findItem(itemId);
if (menuItem != null) {
Drawable menuItemIcon = menuItem.getIcon();
if (menuItemIcon != null) {
try {
menuItemIcon.mutate();
menuItemIcon.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
menuItem.setIcon(menuItemIcon);
} catch (Exception ignore) {
}
}
}
}
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/CropImageAnimation.java
================================================
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
package com.github.gzuliyujiang.imagepicker;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ImageView;
/**
* Animation to handle smooth cropping image matrix transformation change, specifically for zoom-in/out.
*/
final class CropImageAnimation extends Animation implements Animation.AnimationListener {
//region: Fields and Consts
private final ImageView mImageView;
private final CropOverlayView mCropOverlayView;
private final float[] mStartBoundPoints = new float[8];
private final float[] mEndBoundPoints = new float[8];
private final RectF mStartCropWindowRect = new RectF();
private final RectF mEndCropWindowRect = new RectF();
private final float[] mStartImageMatrix = new float[9];
private final float[] mEndImageMatrix = new float[9];
private final RectF mAnimRect = new RectF();
private final float[] mAnimPoints = new float[8];
private final float[] mAnimMatrix = new float[9];
//endregion
public CropImageAnimation(ImageView cropImageView, CropOverlayView cropOverlayView) {
mImageView = cropImageView;
mCropOverlayView = cropOverlayView;
setDuration(300);
setFillAfter(true);
setInterpolator(new AccelerateDecelerateInterpolator());
setAnimationListener(this);
}
public void setStartState(float[] boundPoints, Matrix imageMatrix) {
reset();
System.arraycopy(boundPoints, 0, mStartBoundPoints, 0, 8);
mStartCropWindowRect.set(mCropOverlayView.getCropWindowRect());
imageMatrix.getValues(mStartImageMatrix);
}
public void setEndState(float[] boundPoints, Matrix imageMatrix) {
System.arraycopy(boundPoints, 0, mEndBoundPoints, 0, 8);
mEndCropWindowRect.set(mCropOverlayView.getCropWindowRect());
imageMatrix.getValues(mEndImageMatrix);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
mAnimRect.left = mStartCropWindowRect.left + (mEndCropWindowRect.left - mStartCropWindowRect.left) * interpolatedTime;
mAnimRect.top = mStartCropWindowRect.top + (mEndCropWindowRect.top - mStartCropWindowRect.top) * interpolatedTime;
mAnimRect.right = mStartCropWindowRect.right + (mEndCropWindowRect.right - mStartCropWindowRect.right) * interpolatedTime;
mAnimRect.bottom = mStartCropWindowRect.bottom + (mEndCropWindowRect.bottom - mStartCropWindowRect.bottom) * interpolatedTime;
mCropOverlayView.setCropWindowRect(mAnimRect);
for (int i = 0; i < mAnimPoints.length; i++) {
mAnimPoints[i] = mStartBoundPoints[i] + (mEndBoundPoints[i] - mStartBoundPoints[i]) * interpolatedTime;
}
mCropOverlayView.setBounds(mAnimPoints, mImageView.getWidth(), mImageView.getHeight());
for (int i = 0; i < mAnimMatrix.length; i++) {
mAnimMatrix[i] = mStartImageMatrix[i] + (mEndImageMatrix[i] - mStartImageMatrix[i]) * interpolatedTime;
}
Matrix m = mImageView.getImageMatrix();
m.setValues(mAnimMatrix);
mImageView.setImageMatrix(m);
mImageView.invalidate();
mCropOverlayView.invalidate();
}
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mImageView.clearAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/CropImageConsts.java
================================================
package com.github.gzuliyujiang.imagepicker;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/7/29 17:49
*/
public final class CropImageConsts {
/**
* The key used to pass crop image source URI to {@link CropImageActivity}.
*/
public static final String CROP_IMAGE_EXTRA_SOURCE = "CROP_IMAGE_EXTRA_SOURCE";
/**
* The key used to pass crop image options to {@link CropImageActivity}.
*/
public static final String CROP_IMAGE_EXTRA_OPTIONS = "CROP_IMAGE_EXTRA_OPTIONS";
/**
* The key used to pass crop image result data back from {@link CropImageActivity}.
*/
public static final String CROP_IMAGE_EXTRA_RESULT = "CROP_IMAGE_EXTRA_RESULT";
/**
* The request code used to start pick image activity to be used on result to identify the this specific request.
*/
public static final int PICK_IMAGE_CHOOSER_REQUEST_CODE = 200;
/**
* The request code used to request permission to pick image from external storage.
*/
public static final int PICK_IMAGE_PERMISSIONS_REQUEST_CODE = 201;
/**
* The request code used to request permission to capture image from camera.
*/
public static final int CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE = 2011;
/**
* The request code used to start {@link CropImageActivity} to be used on result to identify the this specific
* request.
*/
public static final int CROP_IMAGE_ACTIVITY_REQUEST_CODE = 203;
/**
* The result code used to return error from {@link CropImageActivity}.
*/
public static final int CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE = 204;
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/CropImageOptions.java
================================================
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth;
// inexhaustible as the great rivers.
// When they come to an end;
// they begin again;
// like the days and months;
// they die and are reborn;
// like the four seasons."
//
// - Sun Tsu;
// "The Art of War"
package com.github.gzuliyujiang.imagepicker;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
/**
* All the possible options that can be set to customize crop image.
* Initialized with default values.
*/
public class CropImageOptions implements Parcelable {
public static final Creator
CREATOR = new Creator() {
@Override
public CropImageOptions createFromParcel(Parcel in) {
return new CropImageOptions(in);
}
@Override
public CropImageOptions[] newArray(int size) {
return new CropImageOptions[size];
}
};
/**
* The shape of the cropping window.
*/
public CropImageView.CropShape cropShape;
/**
* An edge of the crop window will snap to the corresponding edge of a specified bounding box when the crop
* window edge is less than or equal to this distance (in pixels) away from the bounding box edge. (in pixels)
*/
public float snapRadius;
/**
* The radius of the touchable area around the handle. (in pixels)
* We are basing this value off of the recommended 48dp Rhythm.
* See: http://developer.android.com/design/style/metrics-grids.html#48dp-rhythm
*/
public float touchRadius;
/**
* whether the guidelines should be on, off, or only showing when resizing.
*/
public CropImageView.Guidelines guidelines;
/**
* The initial scale type of the image in the crop image view
*/
public CropImageView.ScaleType scaleType;
/**
* if to show crop overlay UI what contains the crop window UI surrounded by background over the cropping
* image.
* default: true, may disable for animation or frame transition.
*/
public boolean showCropOverlay;
/**
* if to show progress bar when image async loading/cropping is in progress.
* default: true, disable to provide custom progress bar UI.
*/
public boolean showProgressBar;
/**
* if auto-zoom functionality is enabled.
* default: true.
*/
public boolean autoZoomEnabled;
/**
* if multi-touch should be enabled on the crop box
* default: false
*/
public boolean multiTouchEnabled;
/**
* The max zoom allowed during cropping.
*/
public int maxZoom;
/**
* The initial crop window padding from image borders in percentage of the cropping image dimensions.
*/
public float initialCropWindowPaddingRatio;
/**
* whether the width to height aspect ratio should be maintained or free to change.
*/
public boolean fixAspectRatio;
/**
* the X value of the aspect ratio.
*/
public int aspectRatioX;
/**
* the Y value of the aspect ratio.
*/
public int aspectRatioY;
/**
* the thickness of the guidelines lines in pixels. (in pixels)
*/
public float borderLineThickness;
/**
* the color of the guidelines lines
*/
public int borderLineColor;
/**
* thickness of the corner line. (in pixels)
*/
public float borderCornerThickness;
/**
* the offset of corner line from crop window border. (in pixels)
*/
public float borderCornerOffset;
/**
* the length of the corner line away from the corner. (in pixels)
*/
public float borderCornerLength;
/**
* the color of the corner line
*/
public int borderCornerColor;
/**
* the thickness of the guidelines lines. (in pixels)
*/
public float guidelinesThickness;
/**
* the color of the guidelines lines
*/
public int guidelinesColor;
/**
* the color of the overlay background around the crop window cover the image parts not in the crop window.
*/
public int backgroundColor;
/**
* the min width the crop window is allowed to be. (in pixels)
*/
public int minCropWindowWidth;
/**
* the min height the crop window is allowed to be. (in pixels)
*/
public int minCropWindowHeight;
/**
* the min width the resulting cropping image is allowed to be, affects the cropping window limits. (in pixels)
*/
public int minCropResultWidth;
/**
* the min height the resulting cropping image is allowed to be, affects the cropping window limits. (in pixels)
*/
public int minCropResultHeight;
/**
* the max width the resulting cropping image is allowed to be, affects the cropping window limits. (in pixels)
*/
public int maxCropResultWidth;
/**
* the max height the resulting cropping image is allowed to be, affects the cropping window limits. (in pixels)
*/
public int maxCropResultHeight;
/**
* the Android Uri to save the cropped image to
*/
public Uri outputUri;
/**
* the compression format to use when writing the image
*/
public Bitmap.CompressFormat outputCompressFormat;
/**
* the quality (if applicable) to use when writing the image (0 - 100)
*/
public int outputCompressQuality;
/**
* the width to resize the cropped image to (see options)
*/
public int outputRequestWidth;
/**
* the height to resize the cropped image to (see options)
*/
public int outputRequestHeight;
/**
* the resize method to use on the cropped bitmap (see options documentation)
*/
public CropImageView.RequestSizeOptions outputRequestSizeOptions;
/**
* if the result of crop image activity should not save the cropped image bitmap
*/
public boolean noOutputImage;
/**
* the initial rectangle to set on the cropping image after loading
*/
public Rect initialCropWindowRectangle;
/**
* the initial rotation to set on the cropping image after loading (0-360 degrees clockwise)
*/
public int initialRotation;
/**
* if to allow (all) rotation during cropping (activity)
*/
public boolean allowRotation;
/**
* the amount of degreees to rotate clockwise or counter-clockwise
*/
public int rotationDegrees;
/**
* Init options with defaults.
*/
public CropImageOptions() {
DisplayMetrics dm = Resources.getSystem().getDisplayMetrics();
cropShape = CropImageView.CropShape.RECTANGLE;
snapRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, dm);
touchRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, dm);
guidelines = CropImageView.Guidelines.ON_TOUCH;
scaleType = CropImageView.ScaleType.FIT_CENTER;
showCropOverlay = true;
showProgressBar = true;
autoZoomEnabled = true;
multiTouchEnabled = false;
maxZoom = 4;
initialCropWindowPaddingRatio = 0.1f;
fixAspectRatio = false;
aspectRatioX = 1;
aspectRatioY = 1;
borderLineThickness = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, dm);
borderLineColor = Color.argb(170, 255, 255, 255);
borderCornerThickness = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, dm);
borderCornerOffset = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, dm);
borderCornerLength = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, dm);
borderCornerColor = Color.WHITE;
guidelinesThickness = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, dm);
guidelinesColor = Color.argb(170, 255, 255, 255);
backgroundColor = Color.argb(119, 0, 0, 0);
minCropWindowWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42, dm);
minCropWindowHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42, dm);
minCropResultWidth = 40;
minCropResultHeight = 40;
maxCropResultWidth = 99999;
maxCropResultHeight = 99999;
outputUri = Uri.EMPTY;
outputCompressFormat = Bitmap.CompressFormat.JPEG;
outputCompressQuality = 90;
outputRequestWidth = 0;
outputRequestHeight = 0;
outputRequestSizeOptions = CropImageView.RequestSizeOptions.NONE;
noOutputImage = false;
initialCropWindowRectangle = null;
initialRotation = -1;
allowRotation = true;
rotationDegrees = 90;
}
/**
* Create object from parcel.
*/
protected CropImageOptions(Parcel in) {
cropShape = CropImageView.CropShape.values()[in.readInt()];
snapRadius = in.readFloat();
touchRadius = in.readFloat();
guidelines = CropImageView.Guidelines.values()[in.readInt()];
scaleType = CropImageView.ScaleType.values()[in.readInt()];
showCropOverlay = in.readByte() != 0;
showProgressBar = in.readByte() != 0;
autoZoomEnabled = in.readByte() != 0;
multiTouchEnabled = in.readByte() != 0;
maxZoom = in.readInt();
initialCropWindowPaddingRatio = in.readFloat();
fixAspectRatio = in.readByte() != 0;
aspectRatioX = in.readInt();
aspectRatioY = in.readInt();
borderLineThickness = in.readFloat();
borderLineColor = in.readInt();
borderCornerThickness = in.readFloat();
borderCornerOffset = in.readFloat();
borderCornerLength = in.readFloat();
borderCornerColor = in.readInt();
guidelinesThickness = in.readFloat();
guidelinesColor = in.readInt();
backgroundColor = in.readInt();
minCropWindowWidth = in.readInt();
minCropWindowHeight = in.readInt();
minCropResultWidth = in.readInt();
minCropResultHeight = in.readInt();
maxCropResultWidth = in.readInt();
maxCropResultHeight = in.readInt();
outputUri = in.readParcelable(Uri.class.getClassLoader());
outputCompressFormat = Bitmap.CompressFormat.valueOf(in.readString());
outputCompressQuality = in.readInt();
outputRequestWidth = in.readInt();
outputRequestHeight = in.readInt();
outputRequestSizeOptions = CropImageView.RequestSizeOptions.values()[in.readInt()];
noOutputImage = in.readByte() != 0;
initialCropWindowRectangle = in.readParcelable(Rect.class.getClassLoader());
initialRotation = in.readInt();
allowRotation = in.readByte() != 0;
rotationDegrees = in.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(cropShape.ordinal());
dest.writeFloat(snapRadius);
dest.writeFloat(touchRadius);
dest.writeInt(guidelines.ordinal());
dest.writeInt(scaleType.ordinal());
dest.writeByte((byte) (showCropOverlay ? 1 : 0));
dest.writeByte((byte) (showProgressBar ? 1 : 0));
dest.writeByte((byte) (autoZoomEnabled ? 1 : 0));
dest.writeByte((byte) (multiTouchEnabled ? 1 : 0));
dest.writeInt(maxZoom);
dest.writeFloat(initialCropWindowPaddingRatio);
dest.writeByte((byte) (fixAspectRatio ? 1 : 0));
dest.writeInt(aspectRatioX);
dest.writeInt(aspectRatioY);
dest.writeFloat(borderLineThickness);
dest.writeInt(borderLineColor);
dest.writeFloat(borderCornerThickness);
dest.writeFloat(borderCornerOffset);
dest.writeFloat(borderCornerLength);
dest.writeInt(borderCornerColor);
dest.writeFloat(guidelinesThickness);
dest.writeInt(guidelinesColor);
dest.writeInt(backgroundColor);
dest.writeInt(minCropWindowWidth);
dest.writeInt(minCropWindowHeight);
dest.writeInt(minCropResultWidth);
dest.writeInt(minCropResultHeight);
dest.writeInt(maxCropResultWidth);
dest.writeInt(maxCropResultHeight);
dest.writeParcelable(outputUri, flags);
dest.writeString(outputCompressFormat.name());
dest.writeInt(outputCompressQuality);
dest.writeInt(outputRequestWidth);
dest.writeInt(outputRequestHeight);
dest.writeInt(outputRequestSizeOptions.ordinal());
dest.writeInt(noOutputImage ? 1 : 0);
dest.writeParcelable(initialCropWindowRectangle, flags);
dest.writeInt(initialRotation);
dest.writeByte((byte) (allowRotation ? 1 : 0));
dest.writeInt(rotationDegrees);
}
@Override
public int describeContents() {
return 0;
}
/**
* Validate all the options are withing valid range.
*
* @throws IllegalArgumentException if any of the options is not valid
*/
public void validate() {
if (maxZoom < 0) {
throw new IllegalArgumentException("Cannot set max zoom to a number < 1");
}
if (touchRadius < 0) {
throw new IllegalArgumentException("Cannot set touch radius value to a number <= 0 ");
}
if (initialCropWindowPaddingRatio < 0 || initialCropWindowPaddingRatio >= 0.5) {
throw new IllegalArgumentException("Cannot set initial crop window padding value to a number < 0 or >= 0.5");
}
if (aspectRatioX <= 0) {
throw new IllegalArgumentException("Cannot set aspect ratio value to a number less than or equal to 0.");
}
if (aspectRatioY <= 0) {
throw new IllegalArgumentException("Cannot set aspect ratio value to a number less than or equal to 0.");
}
if (borderLineThickness < 0) {
throw new IllegalArgumentException("Cannot set line thickness value to a number less than 0.");
}
if (borderCornerThickness < 0) {
throw new IllegalArgumentException("Cannot set corner thickness value to a number less than 0.");
}
if (guidelinesThickness < 0) {
throw new IllegalArgumentException("Cannot set guidelines thickness value to a number less than 0.");
}
if (minCropWindowHeight < 0) {
throw new IllegalArgumentException("Cannot set min crop window height value to a number < 0 ");
}
if (minCropResultWidth < 0) {
throw new IllegalArgumentException("Cannot set min crop result width value to a number < 0 ");
}
if (minCropResultHeight < 0) {
throw new IllegalArgumentException("Cannot set min crop result height value to a number < 0 ");
}
if (maxCropResultWidth < minCropResultWidth) {
throw new IllegalArgumentException("Cannot set max crop result width to smaller value than min crop result width");
}
if (maxCropResultHeight < minCropResultHeight) {
throw new IllegalArgumentException("Cannot set max crop result height to smaller value than min crop result height");
}
if (outputRequestWidth < 0) {
throw new IllegalArgumentException("Cannot set request width value to a number < 0 ");
}
if (outputRequestHeight < 0) {
throw new IllegalArgumentException("Cannot set request height value to a number < 0 ");
}
if (rotationDegrees < 0 || rotationDegrees > 360) {
throw new IllegalArgumentException("Cannot set rotation degrees value to a number < 0 or > 360");
}
}
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/CropImageView.java
================================================
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
package com.github.gzuliyujiang.imagepicker;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import androidx.exifinterface.media.ExifInterface;
import java.lang.ref.WeakReference;
import java.util.UUID;
/**
* Custom view that provides cropping capabilities to an image.
*/
public class CropImageView extends FrameLayout {
//region: Fields and Consts
/**
* Image view widget used to show the image for cropping.
*/
private final ImageView mImageView;
/**
* Overlay over the image view to show cropping UI.
*/
private final CropOverlayView mCropOverlayView;
/**
* The matrix used to transform the cropping image in the image view
*/
private final Matrix mImageMatrix = new Matrix();
/**
* Reusing matrix instance for reverse matrix calculations.
*/
private final Matrix mImageInverseMatrix = new Matrix();
/**
* Progress bar widget to show progress bar on async image loading and cropping.
*/
private final ProgressBar mProgressBar;
/**
* Rectengale used in image matrix transformation calculation (reusing rect instance)
*/
private final float[] mImagePoints = new float[8];
/**
* Animation class to smooth animate zoom-in/out
*/
private CropImageAnimation mAnimation;
private Bitmap mBitmap;
private int mDegreesRotated;
private int mLayoutWidth;
private int mLayoutHeight;
private int mImageResource;
/**
* The initial scale type of the image in the crop image view
*/
private ScaleType mScaleType;
/**
* if to show crop overlay UI what contains the crop window UI surrounded by background over the cropping
* image.
* default: true, may disable for animation or frame transition.
*/
private boolean mShowCropOverlay = true;
/**
* if to show progress bar when image async loading/cropping is in progress.
* default: true, disable to provide custom progress bar UI.
*/
private boolean mShowProgressBar = true;
/**
* if auto-zoom functionality is enabled.
* default: true.
*/
private boolean mAutoZoomEnabled;
/**
* The max zoom allowed during cropping
*/
private int mMaxZoom;
/**
* callback to be invoked when image async loading is complete.
*/
private OnSetImageUriCompleteListener mOnSetImageUriCompleteListener;
/**
* callback to be invoked when image async cropping is complete.
*/
private OnCropImageCompleteListener mOnCropImageCompleteListener;
/**
* The URI that the image was loaded from (if loaded from URI)
*/
private Uri mLoadedImageUri;
/**
* The sample size the image was loaded by if was loaded by URI
*/
private int mLoadedSampleSize = 1;
/**
* The current zoom level to to scale the cropping image
*/
private float mZoom = 1;
/**
* The X offset that the cropping image was translated after zooming
*/
private float mZoomOffsetX;
/**
* The Y offset that the cropping image was translated after zooming
*/
private float mZoomOffsetY;
/**
* Used to restore the cropping windows rectangle after state restore
*/
private RectF mRestoreCropWindowRect;
/**
* Used to detect size change to handle auto-zoom using {@link #handleCropWindowChanged(boolean, boolean)} in
* {@link #layout(int, int, int, int)}.
*/
private boolean mSizeChanged;
/**
* Task used to load bitmap async from UI thread
*/
private WeakReference mBitmapLoadingWorkerTask;
/**
* Task used to crop bitmap async from UI thread
*/
private WeakReference mBitmapCroppingWorkerTask;
//endregion
public CropImageView(Context context) {
this(context, null);
}
public CropImageView(Context context, AttributeSet attrs) {
super(context, attrs);
CropImageOptions options = new CropImageOptions();
if (attrs != null) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CropImageView, 0, 0);
try {
options.fixAspectRatio = ta.getBoolean(R.styleable.CropImageView_cropFixAspectRatio, options.fixAspectRatio);
options.aspectRatioX = ta.getInteger(R.styleable.CropImageView_cropAspectRatioX, options.aspectRatioX);
options.aspectRatioY = ta.getInteger(R.styleable.CropImageView_cropAspectRatioY, options.aspectRatioY);
options.scaleType = ScaleType.values()[ta.getInt(
R.styleable.CropImageView_cropScaleType, options.scaleType.ordinal())];
options.autoZoomEnabled = ta.getBoolean(R.styleable.CropImageView_cropAutoZoomEnabled, options.autoZoomEnabled);
options.multiTouchEnabled = ta.getBoolean(R.styleable.CropImageView_cropMultiTouchEnabled, options.multiTouchEnabled);
options.maxZoom = ta.getInteger(R.styleable.CropImageView_cropMaxZoom, options.maxZoom);
options.cropShape = CropShape.values()[ta.getInt(
R.styleable.CropImageView_cropShape, options.cropShape.ordinal())];
options.guidelines = Guidelines.values()[ta.getInt(
R.styleable.CropImageView_cropGuidelines, options.guidelines.ordinal())];
options.snapRadius = ta.getDimension(R.styleable.CropImageView_cropSnapRadius, options.snapRadius);
options.touchRadius = ta.getDimension(R.styleable.CropImageView_cropTouchRadius, options.touchRadius);
options.initialCropWindowPaddingRatio = ta.getFloat(
R.styleable.CropImageView_cropInitialCropWindowPaddingRatio, options.initialCropWindowPaddingRatio);
options.borderLineThickness = ta.getDimension(R.styleable.CropImageView_cropBorderLineThickness, options.borderLineThickness);
options.borderLineColor = ta.getInteger(R.styleable.CropImageView_cropBorderLineColor, options.borderLineColor);
options.borderCornerThickness = ta.getDimension(
R.styleable.CropImageView_cropBorderCornerThickness, options.borderCornerThickness);
options.borderCornerOffset = ta.getDimension(R.styleable.CropImageView_cropBorderCornerOffset, options.borderCornerOffset);
options.borderCornerLength = ta.getDimension(R.styleable.CropImageView_cropBorderCornerLength, options.borderCornerLength);
options.borderCornerColor = ta.getInteger(R.styleable.CropImageView_cropBorderCornerColor, options.borderCornerColor);
options.guidelinesThickness = ta.getDimension(R.styleable.CropImageView_cropGuidelinesThickness, options.guidelinesThickness);
options.guidelinesColor = ta.getInteger(R.styleable.CropImageView_cropGuidelinesColor, options.guidelinesColor);
options.backgroundColor = ta.getInteger(R.styleable.CropImageView_cropBackgroundColor, options.backgroundColor);
options.showCropOverlay = ta.getBoolean(R.styleable.CropImageView_cropShowCropOverlay, mShowCropOverlay);
options.showProgressBar = ta.getBoolean(R.styleable.CropImageView_cropShowProgressBar, mShowProgressBar);
options.borderCornerThickness = ta.getDimension(
R.styleable.CropImageView_cropBorderCornerThickness, options.borderCornerThickness);
options.minCropWindowWidth = (int) ta.getDimension(
R.styleable.CropImageView_cropMinCropWindowWidth, options.minCropWindowWidth);
options.minCropWindowHeight = (int) ta.getDimension(
R.styleable.CropImageView_cropMinCropWindowHeight, options.minCropWindowHeight);
options.minCropResultWidth = (int) ta.getFloat(R.styleable.CropImageView_cropMinCropResultWidthPX, options.minCropResultWidth);
options.minCropResultHeight = (int) ta.getFloat(
R.styleable.CropImageView_cropMinCropResultHeightPX, options.minCropResultHeight);
options.maxCropResultWidth = (int) ta.getFloat(R.styleable.CropImageView_cropMaxCropResultWidthPX, options.maxCropResultWidth);
options.maxCropResultHeight = (int) ta.getFloat(
R.styleable.CropImageView_cropMaxCropResultHeightPX, options.maxCropResultHeight);
// if aspect ratio is set then set fixed to true
if (ta.hasValue(R.styleable.CropImageView_cropAspectRatioX) &&
ta.hasValue(R.styleable.CropImageView_cropAspectRatioX) &&
!ta.hasValue(R.styleable.CropImageView_cropFixAspectRatio)) {
options.fixAspectRatio = true;
}
} finally {
ta.recycle();
}
}
options.validate();
mScaleType = options.scaleType;
mAutoZoomEnabled = options.autoZoomEnabled;
mMaxZoom = options.maxZoom;
mShowCropOverlay = options.showCropOverlay;
mShowProgressBar = options.showProgressBar;
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.crop_image_view, this, true);
mImageView = v.findViewById(R.id.ImageView_image);
mImageView.setScaleType(ImageView.ScaleType.MATRIX);
mCropOverlayView = v.findViewById(R.id.CropOverlayView);
mCropOverlayView.setCropWindowChangeListener(new CropOverlayView.CropWindowChangeListener() {
@Override
public void onCropWindowChanged(boolean inProgress) {
handleCropWindowChanged(inProgress, true);
}
});
mCropOverlayView.setInitialAttributeValues(options);
mProgressBar = v.findViewById(R.id.CropProgressBar);
setProgressBarVisibility();
}
public void setCropImageOptions(CropImageOptions options) {
options.validate();
mScaleType = options.scaleType;
mAutoZoomEnabled = options.autoZoomEnabled;
mMaxZoom = options.maxZoom;
mShowCropOverlay = options.showCropOverlay;
mShowProgressBar = options.showProgressBar;
mCropOverlayView.setInitialAttributeValues(options);
}
/**
* Get the scale type of the image in the crop view.
*/
public ScaleType getScaleType() {
return mScaleType;
}
/**
* Set the scale type of the image in the crop view
*/
public void setScaleType(ScaleType scaleType) {
if (scaleType != mScaleType) {
mScaleType = scaleType;
mZoom = 1;
mZoomOffsetX = mZoomOffsetY = 0;
mCropOverlayView.resetCropOverlayView();
requestLayout();
}
}
/**
* The shape of the cropping area - rectangle/circular.
*/
public CropShape getCropShape() {
return mCropOverlayView.getCropShape();
}
/**
* The shape of the cropping area - rectangle/circular.
* To set square/circle crop shape set aspect ratio to 1:1.
*/
public void setCropShape(CropShape cropShape) {
mCropOverlayView.setCropShape(cropShape);
}
/**
* if auto-zoom functionality is enabled. default: true.
*/
public boolean isAutoZoomEnabled() {
return mAutoZoomEnabled;
}
/**
* Set auto-zoom functionality to enabled/disabled.
*/
public void setAutoZoomEnabled(boolean autoZoomEnabled) {
if (mAutoZoomEnabled != autoZoomEnabled) {
mAutoZoomEnabled = autoZoomEnabled;
handleCropWindowChanged(false, false);
mCropOverlayView.invalidate();
}
}
/**
* Set multi touch functionality to enabled/disabled.
*/
public void setMultiTouchEnabled(boolean multiTouchEnabled) {
if (mCropOverlayView.setMultiTouchEnabled(multiTouchEnabled)) {
handleCropWindowChanged(false, false);
mCropOverlayView.invalidate();
}
}
/**
* The max zoom allowed during cropping.
*/
public int getMaxZoom() {
return mMaxZoom;
}
/**
* The max zoom allowed during cropping.
*/
public void setMaxZoom(int maxZoom) {
if (mMaxZoom != maxZoom && maxZoom > 0) {
mMaxZoom = maxZoom;
handleCropWindowChanged(false, false);
mCropOverlayView.invalidate();
}
}
/**
* the min size the resulting cropping image is allowed to be, affects the cropping window limits
* (in pixels).
*/
public void setMinCropResultSize(int minCropResultWidth, int minCropResultHeight) {
mCropOverlayView.setMinCropResultSize(minCropResultWidth, minCropResultHeight);
}
/**
* the max size the resulting cropping image is allowed to be, affects the cropping window limits
* (in pixels).
*/
public void setMaxCropResultSize(int maxCropResultWidth, int maxCropResultHeight) {
mCropOverlayView.setMaxCropResultSize(maxCropResultWidth, maxCropResultHeight);
}
/**
* Get the amount of degrees the cropping image is rotated cloackwise.
*
* @return 0-360
*/
public int getRotatedDegrees() {
return mDegreesRotated;
}
/**
* Set the amount of degrees the cropping image is rotated cloackwise.
*
* @param degrees 0-360
*/
public void setRotatedDegrees(int degrees) {
if (mDegreesRotated != degrees) {
rotateImage(degrees - mDegreesRotated);
}
}
/**
* whether the aspect ratio is fixed or not; true fixes the aspect ratio, while false allows it to be changed.
*/
public boolean isFixAspectRatio() {
return mCropOverlayView.isFixAspectRatio();
}
/**
* Sets whether the aspect ratio is fixed or not; true fixes the aspect ratio, while false allows it to be changed.
*/
public void setFixedAspectRatio(boolean fixAspectRatio) {
mCropOverlayView.setFixedAspectRatio(fixAspectRatio);
}
/**
* Get the current guidelines option set.
*/
public Guidelines getGuidelines() {
return mCropOverlayView.getGuidelines();
}
/**
* Sets the guidelines for the CropOverlayView to be either on, off, or to show when resizing the application.
*/
public void setGuidelines(Guidelines guidelines) {
mCropOverlayView.setGuidelines(guidelines);
}
/**
* both the X and Y values of the aspectRatio.
*/
public Pair getAspectRatio() {
return new Pair<>(mCropOverlayView.getAspectRatioX(), mCropOverlayView.getAspectRatioY());
}
/**
* Sets the both the X and Y values of the aspectRatio.
* Sets fixed aspect ratio to TRUE.
*
* @param aspectRatioX int that specifies the new X value of the aspect ratio
* @param aspectRatioY int that specifies the new Y value of the aspect ratio
*/
public void setAspectRatio(int aspectRatioX, int aspectRatioY) {
mCropOverlayView.setAspectRatioX(aspectRatioX);
mCropOverlayView.setAspectRatioY(aspectRatioY);
setFixedAspectRatio(true);
}
/**
* Clears set aspect ratio values and sets fixed aspect ratio to FALSE.
*/
public void clearAspectRatio() {
mCropOverlayView.setAspectRatioX(1);
mCropOverlayView.setAspectRatioY(1);
setFixedAspectRatio(false);
}
/**
* An edge of the crop window will snap to the corresponding edge of a
* specified bounding box when the crop window edge is less than or equal to
* this distance (in pixels) away from the bounding box edge. (default: 3dp)
*/
public void setSnapRadius(float snapRadius) {
if (snapRadius >= 0) {
mCropOverlayView.setSnapRadius(snapRadius);
}
}
/**
* if to show progress bar when image async loading/cropping is in progress.
* default: true, disable to provide custom progress bar UI.
*/
public boolean isShowProgressBar() {
return mShowProgressBar;
}
/**
* if to show progress bar when image async loading/cropping is in progress.
* default: true, disable to provide custom progress bar UI.
*/
public void setShowProgressBar(boolean showProgressBar) {
if (mShowProgressBar != showProgressBar) {
mShowProgressBar = showProgressBar;
setProgressBarVisibility();
}
}
/**
* if to show crop overlay UI what contains the crop window UI surrounded by background over the cropping
* image.
* default: true, may disable for animation or frame transition.
*/
public boolean isShowCropOverlay() {
return mShowCropOverlay;
}
/**
* if to show crop overlay UI what contains the crop window UI surrounded by background over the cropping
* image.
* default: true, may disable for animation or frame transition.
*/
public void setShowCropOverlay(boolean showCropOverlay) {
if (mShowCropOverlay != showCropOverlay) {
mShowCropOverlay = showCropOverlay;
setCropOverlayVisibility();
}
}
/**
* Returns the integer of the imageResource
*/
public int getImageResource() {
return mImageResource;
}
/**
* Get the URI of an image that was set by URI, null otherwise.
*/
public Uri getImageUri() {
return mLoadedImageUri;
}
/**
* Gets the crop window's position relative to the source Bitmap (not the image
* displayed in the CropImageView) using the original image rotation.
*
* @return a Rect instance containing cropped area boundaries of the source Bitmap
*/
public Rect getCropRect() {
if (mBitmap != null) {
// get the points of the crop rectangle adjusted to source bitmap
float[] points = getCropPoints();
int orgWidth = mBitmap.getWidth() * mLoadedSampleSize;
int orgHeight = mBitmap.getHeight() * mLoadedSampleSize;
// get the rectangle for the points (it may be larger than original if rotation is not stright)
return BitmapUtils.getRectFromPoints(points, orgWidth, orgHeight,
mCropOverlayView.isFixAspectRatio(), mCropOverlayView.getAspectRatioX(), mCropOverlayView.getAspectRatioY());
} else {
return null;
}
}
/**
* Gets the 4 points of crop window's position relative to the source Bitmap (not the image
* displayed in the CropImageView) using the original image rotation.
* Note: the 4 points may not be a rectangle if the image was rotates to NOT stright angle (!= 90/180/270).
*
* @return 4 points (x0,y0,x1,y1,x2,y2,x3,y3) of cropped area boundaries
*/
public float[] getCropPoints() {
// Get crop window position relative to the displayed image.
RectF cropWindowRect = mCropOverlayView.getCropWindowRect();
float[] points = new float[]{
cropWindowRect.left,
cropWindowRect.top,
cropWindowRect.right,
cropWindowRect.top,
cropWindowRect.right,
cropWindowRect.bottom,
cropWindowRect.left,
cropWindowRect.bottom
};
mImageMatrix.invert(mImageInverseMatrix);
mImageInverseMatrix.mapPoints(points);
for (int i = 0; i < points.length; i++) {
points[i] *= mLoadedSampleSize;
}
return points;
}
/**
* Set the crop window position and size to the given rectangle.
* Image to crop must be first set before invoking this, for async - after complete callback.
*
* @param rect window rectangle (position and size) relative to source bitmap
*/
public void setCropRect(Rect rect) {
mCropOverlayView.setInitialCropWindowRect(rect);
}
/**
* Reset crop window to initial rectangle.
*/
public void resetCropRect() {
mZoom = 1;
mZoomOffsetX = 0;
mZoomOffsetY = 0;
mDegreesRotated = 0;
applyImageMatrix(getWidth(), getHeight(), false, false);
mCropOverlayView.resetCropWindowRect();
}
/**
* Gets the cropped image based on the current crop window.
*
* @return a new Bitmap representing the cropped image
*/
public Bitmap getCroppedImage() {
return getCroppedImage(0, 0, RequestSizeOptions.NONE);
}
/**
* Gets the cropped image based on the current crop window.
* Uses {@link RequestSizeOptions#RESIZE_INSIDE} option.
*
* @param reqWidth the width to resize the cropped image to
* @param reqHeight the height to resize the cropped image to
* @return a new Bitmap representing the cropped image
*/
public Bitmap getCroppedImage(int reqWidth, int reqHeight) {
return getCroppedImage(reqWidth, reqHeight, RequestSizeOptions.RESIZE_INSIDE);
}
/**
* Gets the cropped image based on the current crop window.
*
* @param reqWidth the width to resize the cropped image to (see options)
* @param reqHeight the height to resize the cropped image to (see options)
* @param options the resize method to use, see its documentation
* @return a new Bitmap representing the cropped image
*/
public Bitmap getCroppedImage(int reqWidth, int reqHeight, RequestSizeOptions options) {
Bitmap croppedBitmap = null;
if (mBitmap != null) {
mImageView.clearAnimation();
reqWidth = options != RequestSizeOptions.NONE ? reqWidth : 0;
reqHeight = options != RequestSizeOptions.NONE ? reqHeight : 0;
if (mLoadedImageUri != null && (mLoadedSampleSize > 1 || options == RequestSizeOptions.SAMPLING)) {
int orgWidth = mBitmap.getWidth() * mLoadedSampleSize;
int orgHeight = mBitmap.getHeight() * mLoadedSampleSize;
BitmapUtils.BitmapSampled bitmapSampled =
BitmapUtils.cropBitmap(getContext(), mLoadedImageUri, getCropPoints(),
mDegreesRotated, orgWidth, orgHeight,
mCropOverlayView.isFixAspectRatio(), mCropOverlayView.getAspectRatioX(), mCropOverlayView.getAspectRatioY(),
reqWidth, reqHeight);
croppedBitmap = bitmapSampled.bitmap;
} else {
croppedBitmap = BitmapUtils.cropBitmapObjectHandleOOM(mBitmap, getCropPoints(), mDegreesRotated,
mCropOverlayView.isFixAspectRatio(), mCropOverlayView.getAspectRatioX(), mCropOverlayView.getAspectRatioY()).bitmap;
}
croppedBitmap = BitmapUtils.resizeBitmap(croppedBitmap, reqWidth, reqHeight, options);
}
return croppedBitmap;
}
/**
* Gets the cropped image based on the current crop window.
*/
public void getCroppedImageAsync() {
getCroppedImageAsync(0, 0, RequestSizeOptions.NONE);
}
/**
* Gets the cropped image based on the current crop window.
* Uses {@link RequestSizeOptions#RESIZE_INSIDE} option.
* The result will be invoked to listener set by {@link #setOnCropImageCompleteListener(OnCropImageCompleteListener)}.
*
* @param reqWidth the width to resize the cropped image to
* @param reqHeight the height to resize the cropped image to
*/
public void getCroppedImageAsync(int reqWidth, int reqHeight) {
getCroppedImageAsync(reqWidth, reqHeight, RequestSizeOptions.RESIZE_INSIDE);
}
/**
* Gets the cropped image based on the current crop window.
* The result will be invoked to listener set by {@link #setOnCropImageCompleteListener(OnCropImageCompleteListener)}.
*
* @param reqWidth the width to resize the cropped image to (see options)
* @param reqHeight the height to resize the cropped image to (see options)
* @param options the resize method to use, see its documentation
*/
public void getCroppedImageAsync(int reqWidth, int reqHeight, RequestSizeOptions options) {
if (mOnCropImageCompleteListener == null) {
throw new IllegalArgumentException("mOnCropImageCompleteListener is not set");
}
startCropWorkerTask(reqWidth, reqHeight, options, null, null, 0);
}
/**
* Save the cropped image based on the current crop window to the given uri.
* Uses JPEG image compression with 90 compression quality.
*
* @param saveUri the Android Uri to save the cropped image to
*/
public void saveCroppedImageAsync(Uri saveUri) {
saveCroppedImageAsync(saveUri, Bitmap.CompressFormat.JPEG, 90, 0, 0, RequestSizeOptions.NONE);
}
/**
* Save the cropped image based on the current crop window to the given uri.
*
* @param saveUri the Android Uri to save the cropped image to
* @param saveCompressFormat the compression format to use when writing the image
* @param saveCompressQuality the quality (if applicable) to use when writing the image (0 - 100)
*/
public void saveCroppedImageAsync(Uri saveUri, Bitmap.CompressFormat saveCompressFormat, int saveCompressQuality) {
saveCroppedImageAsync(saveUri, saveCompressFormat, saveCompressQuality, 0, 0, RequestSizeOptions.NONE);
}
/**
* Save the cropped image based on the current crop window to the given uri.
* Uses {@link RequestSizeOptions#RESIZE_INSIDE} option.
*
* @param saveUri the Android Uri to save the cropped image to
* @param saveCompressFormat the compression format to use when writing the image
* @param saveCompressQuality the quality (if applicable) to use when writing the image (0 - 100)
* @param reqWidth the width to resize the cropped image to
* @param reqHeight the height to resize the cropped image to
*/
public void saveCroppedImageAsync(Uri saveUri, Bitmap.CompressFormat saveCompressFormat, int saveCompressQuality, int reqWidth, int reqHeight) {
saveCroppedImageAsync(saveUri, saveCompressFormat, saveCompressQuality, reqWidth, reqHeight, RequestSizeOptions.RESIZE_INSIDE);
}
/**
* Save the cropped image based on the current crop window to the given uri.
*
* @param saveUri the Android Uri to save the cropped image to
* @param saveCompressFormat the compression format to use when writing the image
* @param saveCompressQuality the quality (if applicable) to use when writing the image (0 - 100)
* @param reqWidth the width to resize the cropped image to (see options)
* @param reqHeight the height to resize the cropped image to (see options)
* @param options the resize method to use, see its documentation
*/
public void saveCroppedImageAsync(Uri saveUri, Bitmap.CompressFormat saveCompressFormat, int saveCompressQuality, int reqWidth, int reqHeight, RequestSizeOptions options) {
if (mOnCropImageCompleteListener == null) {
throw new IllegalArgumentException("mOnCropImageCompleteListener is not set");
}
startCropWorkerTask(reqWidth, reqHeight, options, saveUri, saveCompressFormat, saveCompressQuality);
}
/**
* Set the callback to be invoked when image async loading ({@link #setImageUriAsync(Uri)})
* is complete (successful or failed).
*/
public void setOnSetImageUriCompleteListener(OnSetImageUriCompleteListener listener) {
mOnSetImageUriCompleteListener = listener;
}
/**
* Set the callback to be invoked when image async cropping image ({@link #getCroppedImageAsync()} or
* {@link #saveCroppedImageAsync(Uri)}) is complete (successful or failed).
*/
public void setOnCropImageCompleteListener(OnCropImageCompleteListener listener) {
mOnCropImageCompleteListener = listener;
}
/**
* Sets a Bitmap as the content of the CropImageView.
*
* @param bitmap the Bitmap to set
*/
public void setImageBitmap(Bitmap bitmap) {
mCropOverlayView.setInitialCropWindowRect(null);
setBitmap(bitmap);
}
/**
* Sets a Bitmap and initializes the image rotation according to the EXIT data.
*
* The EXIF can be retrieved by doing the following:
* ExifInterface exif = new ExifInterface(path);
*
* @param bitmap the original bitmap to set; if null, this
* @param exif the EXIF information about this bitmap; may be null
*/
public void setImageBitmap(Bitmap bitmap, ExifInterface exif) {
Bitmap setBitmap;
if (bitmap != null && exif != null) {
BitmapUtils.RotateBitmapResult result = BitmapUtils.rotateBitmapByExif(bitmap, exif);
setBitmap = result.bitmap;
mDegreesRotated = result.degrees;
} else {
setBitmap = bitmap;
}
mCropOverlayView.setInitialCropWindowRect(null);
setBitmap(setBitmap);
}
/**
* Sets a Drawable as the content of the CropImageView.
*
* @param resId the drawable resource ID to set
*/
public void setImageResource(int resId) {
if (resId != 0) {
mCropOverlayView.setInitialCropWindowRect(null);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
setBitmap(bitmap, resId);
}
}
/**
* Sets a bitmap loaded from the given Android URI as the content of the CropImageView.
* Can be used with URI from gallery or camera source.
* Will rotate the image by exif data.
*
* @param uri the URI to load the image from
*/
public void setImageUriAsync(Uri uri) {
if (uri != null) {
BitmapLoadingTask task = mBitmapLoadingWorkerTask != null ? mBitmapLoadingWorkerTask.get() : null;
if (task != null) {
// cancel previous loading (no check if the same URI because camera URI can be the same for different images)
task.cancel(true);
}
// either no existing task is working or we canceled it, need to load new URI
clearImageInt();
mCropOverlayView.setInitialCropWindowRect(null);
mBitmapLoadingWorkerTask = new WeakReference<>(new BitmapLoadingTask(this, uri));
mBitmapLoadingWorkerTask.get().execute();
setProgressBarVisibility();
}
}
/**
* Clear the current image set for cropping.
*/
public void clearImage() {
clearImageInt();
mCropOverlayView.setInitialCropWindowRect(null);
}
/**
* Rotates image by the specified number of degrees clockwise.
* Negative values represent counter-clockwise rotations.
*
* @param degrees Integer specifying the number of degrees to rotate.
*/
public void rotateImage(int degrees) {
if (mBitmap != null) {
// Force degrees to be a non-zero value between 0 and 360 (inclusive)
if (degrees < 0) {
degrees = (degrees % 360) + 360;
} else {
degrees = degrees % 360;
}
boolean flipAxes = !mCropOverlayView.isFixAspectRatio() && ((degrees > 45 && degrees < 135) || (degrees > 215 && degrees < 305));
BitmapUtils.RECT.set(mCropOverlayView.getCropWindowRect());
float halfWidth = (flipAxes ? BitmapUtils.RECT.height() : BitmapUtils.RECT.width()) / 2f;
float halfHeight = (flipAxes ? BitmapUtils.RECT.width() : BitmapUtils.RECT.height()) / 2f;
mImageMatrix.invert(mImageInverseMatrix);
BitmapUtils.POINTS[0] = BitmapUtils.RECT.centerX();
BitmapUtils.POINTS[1] = BitmapUtils.RECT.centerY();
BitmapUtils.POINTS[2] = 0;
BitmapUtils.POINTS[3] = 0;
BitmapUtils.POINTS[4] = 1;
BitmapUtils.POINTS[5] = 0;
mImageInverseMatrix.mapPoints(BitmapUtils.POINTS);
// This is valid because degrees is not negative.
mDegreesRotated = (mDegreesRotated + degrees) % 360;
applyImageMatrix(getWidth(), getHeight(), true, false);
// adjust the zoom so the crop window size remains the same even after image scale change
mImageMatrix.mapPoints(BitmapUtils.POINTS2, BitmapUtils.POINTS);
mZoom /= Math.sqrt(Math.pow(BitmapUtils.POINTS2[4] - BitmapUtils.POINTS2[2], 2) + Math.pow(
BitmapUtils.POINTS2[5] - BitmapUtils.POINTS2[3], 2));
mZoom = Math.max(mZoom, 1);
applyImageMatrix(getWidth(), getHeight(), true, false);
mImageMatrix.mapPoints(BitmapUtils.POINTS2, BitmapUtils.POINTS);
// adjust the width/height by the changes in scaling to the image
double change = Math.sqrt(
Math.pow(BitmapUtils.POINTS2[4] - BitmapUtils.POINTS2[2], 2) + Math.pow(
BitmapUtils.POINTS2[5] - BitmapUtils.POINTS2[3], 2));
halfWidth *= change;
halfHeight *= change;
// calculate the new crop window rectangle to center in the same location and have proper width/height
BitmapUtils.RECT.set(BitmapUtils.POINTS2[0] - halfWidth, BitmapUtils.POINTS2[1] - halfHeight,
BitmapUtils.POINTS2[0] + halfWidth, BitmapUtils.POINTS2[1] + halfHeight);
mCropOverlayView.resetCropOverlayView();
mCropOverlayView.setCropWindowRect(BitmapUtils.RECT);
applyImageMatrix(getWidth(), getHeight(), true, false);
handleCropWindowChanged(false, false);
// make sure the crop window rectangle is within the cropping image bounds after all the changes
mCropOverlayView.fixCurrentCropWindowRect();
}
}
//region: Private methods
/**
* On complete of the async bitmap loading by {@link #setImageUriAsync(Uri)} set the result
* to the widget if still relevant and call listener if set.
*
* @param result the result of bitmap loading
*/
void onSetImageUriAsyncComplete(BitmapLoadingTask.Result result) {
mBitmapLoadingWorkerTask = null;
setProgressBarVisibility();
if (result.error == null) {
setBitmap(result.bitmap, result.uri, result.loadSampleSize, result.degreesRotated);
}
OnSetImageUriCompleteListener listener = mOnSetImageUriCompleteListener;
if (listener != null) {
listener.onSetImageUriComplete(this, result.uri, result.error);
}
}
/**
* On complete of the async bitmap cropping by {@link #getCroppedImageAsync()} call listener if set.
*
* @param result the result of bitmap cropping
*/
void onImageCroppingAsyncComplete(BitmapCroppingTask.Result result) {
mBitmapCroppingWorkerTask = null;
setProgressBarVisibility();
OnCropImageCompleteListener listener = mOnCropImageCompleteListener;
if (listener != null) {
CropResult cropResult = new CropResult(result.bitmap, result.uri, result.error, getCropPoints(), getCropRect(), getRotatedDegrees(), result.sampleSize);
listener.onCropImageComplete(this, cropResult);
}
}
/**
* {@link #setBitmap(Bitmap, int, Uri, int, int)}}
*/
private void setBitmap(Bitmap bitmap) {
setBitmap(bitmap, 0, null, 1, 0);
}
/**
* {@link #setBitmap(Bitmap, int, Uri, int, int)}}
*/
private void setBitmap(Bitmap bitmap, int imageResource) {
setBitmap(bitmap, imageResource, null, 1, 0);
}
/**
* {@link #setBitmap(Bitmap, int, Uri, int, int)}}
*/
private void setBitmap(Bitmap bitmap, Uri imageUri, int loadSampleSize, int degreesRotated) {
setBitmap(bitmap, 0, imageUri, loadSampleSize, degreesRotated);
}
/**
* Set the given bitmap to be used in for cropping
* Optionally clear full if the bitmap is new, or partial clear if the bitmap has been manipulated.
*/
private void setBitmap(Bitmap bitmap, int imageResource, Uri imageUri, int loadSampleSize, int degreesRotated) {
if (mBitmap == null || !mBitmap.equals(bitmap)) {
mImageView.clearAnimation();
clearImageInt();
mBitmap = bitmap;
mImageView.setImageBitmap(mBitmap);
mLoadedImageUri = imageUri;
mImageResource = imageResource;
mLoadedSampleSize = loadSampleSize;
mDegreesRotated = degreesRotated;
applyImageMatrix(getWidth(), getHeight(), true, false);
if (mCropOverlayView != null) {
mCropOverlayView.resetCropOverlayView();
setCropOverlayVisibility();
}
}
}
/**
* Clear the current image set for cropping.
* Full clear will also clear the data of the set image like Uri or Resource id while partial clear
* will only clear the bitmap and recycle if required.
*/
private void clearImageInt() {
// if we allocated the bitmap, release it as fast as possible
if (mBitmap != null && (mImageResource > 0 || mLoadedImageUri != null)) {
mBitmap.recycle();
}
mBitmap = null;
// clean the loaded image flags for new image
mImageResource = 0;
mLoadedImageUri = null;
mLoadedSampleSize = 1;
mDegreesRotated = 0;
mZoom = 1;
mZoomOffsetX = 0;
mZoomOffsetY = 0;
mImageMatrix.reset();
mImageView.setImageBitmap(null);
setCropOverlayVisibility();
}
/**
* Gets the cropped image based on the current crop window.
* If (reqWidth,reqHeight) is given AND image is loaded from URI cropping will try to use sample size to fit in
* the requested width and height down-sampling if possible - optimization to get best size to quality.
*
* @param reqWidth the width to resize the cropped image to (see options)
* @param reqHeight the height to resize the cropped image to (see options)
* @param options the resize method to use on the cropped bitmap
* @param saveUri optional: to save the cropped image to
* @param saveCompressFormat if saveUri is given, the given compression will be used for saving the image
* @param saveCompressQuality if saveUri is given, the given quality will be used for the compression.
*/
public void startCropWorkerTask(int reqWidth, int reqHeight, RequestSizeOptions options, Uri saveUri, Bitmap.CompressFormat saveCompressFormat, int saveCompressQuality) {
if (mBitmap != null) {
mImageView.clearAnimation();
BitmapCroppingTask
currentTask = mBitmapCroppingWorkerTask != null ? mBitmapCroppingWorkerTask.get() : null;
if (currentTask != null) {
// cancel previous cropping
currentTask.cancel(true);
}
reqWidth = options != RequestSizeOptions.NONE ? reqWidth : 0;
reqHeight = options != RequestSizeOptions.NONE ? reqHeight : 0;
int orgWidth = mBitmap.getWidth() * mLoadedSampleSize;
int orgHeight = mBitmap.getHeight() * mLoadedSampleSize;
if (mLoadedImageUri != null && (mLoadedSampleSize > 1 || options == RequestSizeOptions.SAMPLING)) {
mBitmapCroppingWorkerTask = new WeakReference<>(new BitmapCroppingTask(this, mLoadedImageUri, getCropPoints(),
mDegreesRotated, orgWidth, orgHeight,
mCropOverlayView.isFixAspectRatio(), mCropOverlayView.getAspectRatioX(), mCropOverlayView.getAspectRatioY(),
reqWidth, reqHeight, options,
saveUri, saveCompressFormat, saveCompressQuality));
} else {
mBitmapCroppingWorkerTask = new WeakReference<>(new BitmapCroppingTask(this, mBitmap, getCropPoints(), mDegreesRotated,
mCropOverlayView.isFixAspectRatio(), mCropOverlayView.getAspectRatioX(), mCropOverlayView.getAspectRatioY(),
reqWidth, reqHeight, options,
saveUri, saveCompressFormat, saveCompressQuality));
}
mBitmapCroppingWorkerTask.get().execute();
setProgressBarVisibility();
}
}
@Override
public Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable("instanceState", super.onSaveInstanceState());
bundle.putParcelable("LOADED_IMAGE_URI", mLoadedImageUri);
bundle.putInt("LOADED_IMAGE_RESOURCE", mImageResource);
if (mLoadedImageUri == null && mImageResource < 1) {
bundle.putParcelable("SET_BITMAP", mBitmap);
}
if (mLoadedImageUri != null && mBitmap != null) {
String key = UUID.randomUUID().toString();
BitmapUtils.mStateBitmap = new Pair<>(key, new WeakReference<>(mBitmap));
bundle.putString("LOADED_IMAGE_STATE_BITMAP_KEY", key);
}
if (mBitmapLoadingWorkerTask != null) {
BitmapLoadingTask task = mBitmapLoadingWorkerTask.get();
if (task != null) {
bundle.putParcelable("LOADING_IMAGE_URI", task.getUri());
}
}
bundle.putInt("LOADED_SAMPLE_SIZE", mLoadedSampleSize);
bundle.putInt("DEGREES_ROTATED", mDegreesRotated);
bundle.putParcelable("INITIAL_CROP_RECT", mCropOverlayView.getInitialCropWindowRect());
BitmapUtils.RECT.set(mCropOverlayView.getCropWindowRect());
mImageMatrix.invert(mImageInverseMatrix);
mImageInverseMatrix.mapRect(BitmapUtils.RECT);
bundle.putParcelable("CROP_WINDOW_RECT", BitmapUtils.RECT);
bundle.putString("CROP_SHAPE", mCropOverlayView.getCropShape().name());
bundle.putBoolean("CROP_AUTO_ZOOM_ENABLED", mAutoZoomEnabled);
bundle.putInt("CROP_MAX_ZOOM", mMaxZoom);
return bundle;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
// prevent restoring state if already set by outside code
if (mBitmapLoadingWorkerTask == null && mLoadedImageUri == null && mBitmap == null && mImageResource == 0) {
Uri uri = bundle.getParcelable("LOADED_IMAGE_URI");
if (uri != null) {
String key = bundle.getString("LOADED_IMAGE_STATE_BITMAP_KEY");
if (key != null) {
Bitmap stateBitmap = BitmapUtils.mStateBitmap != null && BitmapUtils.mStateBitmap.first.equals(key)
? BitmapUtils.mStateBitmap.second.get() : null;
if (stateBitmap != null && !stateBitmap.isRecycled()) {
BitmapUtils.mStateBitmap = null;
setBitmap(stateBitmap, uri, bundle.getInt("LOADED_SAMPLE_SIZE"), 0);
}
}
if (mLoadedImageUri == null) {
setImageUriAsync(uri);
}
} else {
int resId = bundle.getInt("LOADED_IMAGE_RESOURCE");
if (resId > 0) {
setImageResource(resId);
} else {
Bitmap bitmap = bundle.getParcelable("SET_BITMAP");
if (bitmap != null) {
setBitmap(bitmap);
} else {
uri = bundle.getParcelable("LOADING_IMAGE_URI");
if (uri != null) {
setImageUriAsync(uri);
}
}
}
}
mDegreesRotated = bundle.getInt("DEGREES_ROTATED");
mCropOverlayView.setInitialCropWindowRect(bundle.getParcelable("INITIAL_CROP_RECT"));
mRestoreCropWindowRect = bundle.getParcelable("CROP_WINDOW_RECT");
mCropOverlayView.setCropShape(CropShape.valueOf(bundle.getString("CROP_SHAPE")));
mAutoZoomEnabled = bundle.getBoolean("CROP_AUTO_ZOOM_ENABLED");
mMaxZoom = bundle.getInt("CROP_MAX_ZOOM");
}
super.onRestoreInstanceState(bundle.getParcelable("instanceState"));
} else {
super.onRestoreInstanceState(state);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (mBitmap != null) {
// Bypasses a baffling bug when used within a ScrollView, where heightSize is set to 0.
if (heightSize == 0) {
heightSize = mBitmap.getHeight();
}
int desiredWidth;
int desiredHeight;
double viewToBitmapWidthRatio = Double.POSITIVE_INFINITY;
double viewToBitmapHeightRatio = Double.POSITIVE_INFINITY;
// Checks if either width or height needs to be fixed
if (widthSize < mBitmap.getWidth()) {
viewToBitmapWidthRatio = (double) widthSize / (double) mBitmap.getWidth();
}
if (heightSize < mBitmap.getHeight()) {
viewToBitmapHeightRatio = (double) heightSize / (double) mBitmap.getHeight();
}
// If either needs to be fixed, choose smallest ratio and calculate from there
if (viewToBitmapWidthRatio != Double.POSITIVE_INFINITY || viewToBitmapHeightRatio != Double.POSITIVE_INFINITY) {
if (viewToBitmapWidthRatio <= viewToBitmapHeightRatio) {
desiredWidth = widthSize;
desiredHeight = (int) (mBitmap.getHeight() * viewToBitmapWidthRatio);
} else {
desiredHeight = heightSize;
desiredWidth = (int) (mBitmap.getWidth() * viewToBitmapHeightRatio);
}
} else {
// Otherwise, the picture is within frame layout bounds. Desired width is simply picture size
desiredWidth = mBitmap.getWidth();
desiredHeight = mBitmap.getHeight();
}
int width = getOnMeasureSpec(widthMode, widthSize, desiredWidth);
int height = getOnMeasureSpec(heightMode, heightSize, desiredHeight);
mLayoutWidth = width;
mLayoutHeight = height;
setMeasuredDimension(mLayoutWidth, mLayoutHeight);
} else {
setMeasuredDimension(widthSize, heightSize);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (mLayoutWidth > 0 && mLayoutHeight > 0) {
// Gets original parameters, and creates the new parameters
ViewGroup.LayoutParams origParams = this.getLayoutParams();
origParams.width = mLayoutWidth;
origParams.height = mLayoutHeight;
setLayoutParams(origParams);
if (mBitmap != null) {
applyImageMatrix(r - l, b - t, true, false);
// after state restore we want to restore the window crop, possible only after widget size is known
if (mRestoreCropWindowRect != null) {
mImageMatrix.mapRect(mRestoreCropWindowRect);
mCropOverlayView.setCropWindowRect(mRestoreCropWindowRect);
handleCropWindowChanged(false, false);
mCropOverlayView.fixCurrentCropWindowRect();
mRestoreCropWindowRect = null;
} else if (mSizeChanged) {
mSizeChanged = false;
handleCropWindowChanged(false, false);
}
} else {
updateImageBounds(true);
}
} else {
updateImageBounds(true);
}
}
/**
* Detect size change to handle auto-zoom using {@link #handleCropWindowChanged(boolean, boolean)} in
* {@link #layout(int, int, int, int)}.
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mSizeChanged = oldw > 0 && oldh > 0;
}
/**
* Handle crop window change to:
* 1. Execute auto-zoom-in/out depending on the area covered of cropping window relative to the
* available view area.
* 2. Slide the zoomed sub-area if the cropping window is outside of the visible view sub-area.
*
* @param inProgress is the crop window change is still in progress by the user
* @param animate if to animate the change to the image matrix, or set it directly
*/
private void handleCropWindowChanged(boolean inProgress, boolean animate) {
int width = getWidth();
int height = getHeight();
if (mBitmap != null && width > 0 && height > 0) {
RectF cropRect = mCropOverlayView.getCropWindowRect();
if (inProgress) {
if (cropRect.left < 0 || cropRect.top < 0 || cropRect.right > width || cropRect.bottom > height) {
applyImageMatrix(width, height, false, false);
}
} else if (mAutoZoomEnabled || mZoom > 1) {
float newZoom = 0;
// keep the cropping window covered area to 50%-65% of zoomed sub-area
if (mZoom < mMaxZoom && cropRect.width() < width * 0.5f && cropRect.height() < height * 0.5f) {
newZoom = Math.min(mMaxZoom, Math.min(width / (cropRect.width() / mZoom / 0.64f), height / (cropRect.height() / mZoom / 0.64f)));
}
if (mZoom > 1 && (cropRect.width() > width * 0.65f || cropRect.height() > height * 0.65f)) {
newZoom = Math.max(1, Math.min(width / (cropRect.width() / mZoom / 0.51f), height / (cropRect.height() / mZoom / 0.51f)));
}
if (!mAutoZoomEnabled) {
newZoom = 1;
}
if (newZoom > 0 && newZoom != mZoom) {
if (animate) {
if (mAnimation == null) {
// lazy create animation single instance
mAnimation = new CropImageAnimation(mImageView, mCropOverlayView);
}
// set the state for animation to start from
mAnimation.setStartState(mImagePoints, mImageMatrix);
}
mZoom = newZoom;
applyImageMatrix(width, height, true, animate);
}
}
}
}
/**
* Apply matrix to handle the image inside the image view.
*
* @param width the width of the image view
* @param height the height of the image view
*/
private void applyImageMatrix(float width, float height, boolean center, boolean animate) {
if (mBitmap != null && width > 0 && height > 0) {
mImageMatrix.invert(mImageInverseMatrix);
RectF cropRect = mCropOverlayView.getCropWindowRect();
mImageInverseMatrix.mapRect(cropRect);
mImageMatrix.reset();
// move the image to the center of the image view first so we can manipulate it from there
mImageMatrix.postTranslate((width - mBitmap.getWidth()) / 2, (height - mBitmap.getHeight()) / 2);
mapImagePointsByImageMatrix();
// rotate the image the required degrees from center of image
if (mDegreesRotated > 0) {
mImageMatrix.postRotate(mDegreesRotated, BitmapUtils.getRectCenterX(mImagePoints), BitmapUtils
.getRectCenterY(mImagePoints));
mapImagePointsByImageMatrix();
}
// scale the image to the image view, image rect transformed to know new width/height
float scale = Math.min(width / BitmapUtils.getRectWidth(mImagePoints), height / BitmapUtils
.getRectHeight(mImagePoints));
if (mScaleType == ScaleType.FIT_CENTER || (mScaleType == ScaleType.CENTER_INSIDE && scale < 1) || (scale > 1 && mAutoZoomEnabled)) {
mImageMatrix.postScale(scale, scale, BitmapUtils.getRectCenterX(mImagePoints), BitmapUtils
.getRectCenterY(mImagePoints));
mapImagePointsByImageMatrix();
}
// scale by the current zoom level
mImageMatrix.postScale(mZoom, mZoom, BitmapUtils.getRectCenterX(mImagePoints), BitmapUtils
.getRectCenterY(mImagePoints));
mapImagePointsByImageMatrix();
mImageMatrix.mapRect(cropRect);
if (center) {
// set the zoomed area to be as to the center of cropping window as possible
mZoomOffsetX = width > BitmapUtils.getRectWidth(mImagePoints) ? 0
: Math.max(Math.min(width / 2 - cropRect.centerX(), -BitmapUtils.getRectLeft(mImagePoints)), getWidth() - BitmapUtils
.getRectRight(mImagePoints)) / mZoom;
mZoomOffsetY = height > BitmapUtils.getRectHeight(mImagePoints) ? 0
: Math.max(Math.min(height / 2 - cropRect.centerY(), -BitmapUtils.getRectTop(mImagePoints)), getHeight() - BitmapUtils
.getRectBottom(mImagePoints)) / mZoom;
} else {
// adjust the zoomed area so the crop window rectangle will be inside the area in case it was moved outside
mZoomOffsetX = Math.min(
Math.max(mZoomOffsetX * mZoom, -cropRect.left), -cropRect.right + width) / mZoom;
mZoomOffsetY = Math.min(
Math.max(mZoomOffsetY * mZoom, -cropRect.top), -cropRect.bottom + height) / mZoom;
}
// apply to zoom offset translate and update the crop rectangle to offset correctly
mImageMatrix.postTranslate(mZoomOffsetX * mZoom, mZoomOffsetY * mZoom);
cropRect.offset(mZoomOffsetX * mZoom, mZoomOffsetY * mZoom);
mCropOverlayView.setCropWindowRect(cropRect);
mapImagePointsByImageMatrix();
// set matrix to apply
if (animate) {
// set the state for animation to end in, start animation now
mAnimation.setEndState(mImagePoints, mImageMatrix);
mImageView.startAnimation(mAnimation);
} else {
mImageView.setImageMatrix(mImageMatrix);
}
// update the image rectangle in the crop overlay
updateImageBounds(false);
}
}
/**
* Adjust the given image rectangle by image transformation matrix to know the final rectangle of the image.
* To get the proper rectangle it must be first reset to orginal image rectangle.
*/
private void mapImagePointsByImageMatrix() {
mImagePoints[0] = 0;
mImagePoints[1] = 0;
mImagePoints[2] = mBitmap.getWidth();
mImagePoints[3] = 0;
mImagePoints[4] = mBitmap.getWidth();
mImagePoints[5] = mBitmap.getHeight();
mImagePoints[6] = 0;
mImagePoints[7] = mBitmap.getHeight();
mImageMatrix.mapPoints(mImagePoints);
}
/**
* Determines the specs for the onMeasure function. Calculates the width or height
* depending on the mode.
*
* @param measureSpecMode The mode of the measured width or height.
* @param measureSpecSize The size of the measured width or height.
* @param desiredSize The desired size of the measured width or height.
* @return The final size of the width or height.
*/
private static int getOnMeasureSpec(int measureSpecMode, int measureSpecSize, int desiredSize) {
// Measure Width
int spec;
if (measureSpecMode == MeasureSpec.EXACTLY) {
// Must be this size
spec = measureSpecSize;
} else if (measureSpecMode == MeasureSpec.AT_MOST) {
// Can't be bigger than...; match_parent value
spec = Math.min(desiredSize, measureSpecSize);
} else {
// Be whatever you want; wrap_content
spec = desiredSize;
}
return spec;
}
/**
* Set visibility of crop overlay to hide it when there is no image or specificly set by client.
*/
private void setCropOverlayVisibility() {
if (mCropOverlayView != null) {
mCropOverlayView.setVisibility(mShowCropOverlay && mBitmap != null ? VISIBLE : INVISIBLE);
}
}
/**
* Set visibility of progress bar when async loading/cropping is in process and show is enabled.
*/
private void setProgressBarVisibility() {
boolean visible = mShowProgressBar &&
(mBitmap == null && mBitmapLoadingWorkerTask != null || mBitmapCroppingWorkerTask != null);
mProgressBar.setVisibility(visible ? VISIBLE : INVISIBLE);
}
/**
* Update the scale factor between the actual image bitmap and the shown image.
*/
private void updateImageBounds(boolean clear) {
if (mBitmap != null && !clear) {
// Get the scale factor between the actual Bitmap dimensions and the displayed dimensions for width/height.
float scaleFactorWidth = mBitmap.getWidth() * mLoadedSampleSize / BitmapUtils.getRectWidth(mImagePoints);
float scaleFactorHeight = mBitmap.getHeight() * mLoadedSampleSize / BitmapUtils.getRectHeight(mImagePoints);
mCropOverlayView.setCropWindowLimits(getWidth(), getHeight(), scaleFactorWidth, scaleFactorHeight);
}
// set the bitmap rectangle and update the crop window after scale factor is set
mCropOverlayView.setBounds(clear ? null : mImagePoints, getWidth(), getHeight());
}
//endregion
//region: Inner class: CropShape
/**
* The possible cropping area shape.
* To set square/circle crop shape set aspect ratio to 1:1.
*/
public enum CropShape {
RECTANGLE,
OVAL
}
//endregion
//region: Inner class: ScaleType
/**
* Options for scaling the bounds of cropping image to the bounds of Crop Image View.
* Note: Some options are affected by auto-zoom, if enabled.
*/
public enum ScaleType {
/**
* Scale the image uniformly (maintain the image's aspect ratio) to fit in crop image view.
* The largest dimension will be equals to crop image view and the second dimension will be smaller.
*/
FIT_CENTER,
/**
* Center the image in the view, but perform no scaling.
* Note: If auto-zoom is enabled and the source image is smaller than crop image view then it will be
* scaled uniformly to fit the crop image view.
*/
CENTER,
/**
* Scale the image uniformly (maintain the image's aspect ratio) so that both
* dimensions (width and height) of the image will be equal to or larger than the
* corresponding dimension of the view (minus padding).
* The image is then centered in the view.
*/
CENTER_CROP,
/**
* Scale the image uniformly (maintain the image's aspect ratio) so that both
* dimensions (width and height) of the image will be equal to or less than the
* corresponding dimension of the view (minus padding).
* The image is then centered in the view.
* Note: If auto-zoom is enabled and the source image is smaller than crop image view then it will be
* scaled uniformly to fit the crop image view.
*/
CENTER_INSIDE
}
//endregion
//region: Inner class: Guidelines
/**
* The possible guidelines showing types.
*/
public enum Guidelines {
/**
* Never show
*/
OFF,
/**
* Show when crop move action is live
*/
ON_TOUCH,
/**
* Always show
*/
ON
}
//endregion
//region: Inner class: RequestSizeOptions
/**
* Possible options for handling requested width/height for cropping.
*/
public enum RequestSizeOptions {
/**
* No resize/sampling is done unless required for memory management (OOM).
*/
NONE,
/**
* Only sample the image during loading (if image set using URI) so the smallest of the image
* dimensions will be between the requested size and x2 requested size.
* NOTE: resulting image will not be exactly requested width/height
* see: Loading Large
* Bitmaps Efficiently.
*/
SAMPLING,
/**
* Resize the image uniformly (maintain the image's aspect ratio) so that both
* dimensions (width and height) of the image will be equal to or less than the
* corresponding requested dimension.
* If the image is smaller than the requested size it will NOT change.
*/
RESIZE_INSIDE,
/**
* Resize the image uniformly (maintain the image's aspect ratio) to fit in the given width/height.
* The largest dimension will be equals to the requested and the second dimension will be smaller.
* If the image is smaller than the requested size it will enlarge it.
*/
RESIZE_FIT,
/**
* Resize the image to fit exactly in the given width/height.
* This resize method does NOT preserve aspect ratio.
* If the image is smaller than the requested size it will enlarge it.
*/
RESIZE_EXACT
}
//endregion
//region: Inner class: OnSetImageUriCompleteListener
/**
* Interface definition for a callback to be invoked when image async loading is complete.
*/
public interface OnSetImageUriCompleteListener {
/**
* Called when a crop image view has completed loading image for cropping.
* If loading failed error parameter will contain the error.
*
* @param view The crop image view that loading of image was complete.
* @param uri the URI of the image that was loading
* @param error if error occurred during loading will contain the error, otherwise null.
*/
void onSetImageUriComplete(CropImageView view, Uri uri, Exception error);
}
//endregion
//region: Inner class: OnGetCroppedImageCompleteListener
/**
* Interface definition for a callback to be invoked when image async crop is complete.
*/
public interface OnCropImageCompleteListener {
/**
* Called when a crop image view has completed cropping image.
* Result object contains the cropped bitmap, saved cropped image uri, crop points data or
* the error occured during cropping.
*
* @param view The crop image view that cropping of image was complete.
* @param result the crop image result data (with cropped image or error)
*/
void onCropImageComplete(CropImageView view, CropResult result);
}
//endregion
//region: Inner class: OnSaveCroppedImageCompleteListener
//region: Inner class: ActivityResult
/**
* Result data of crop image.
*/
public static class CropResult {
/**
* The cropped image bitmap result.
* Null if save cropped image was executed, no output requested or failure.
*/
private final Bitmap mBitmap;
/**
* The Android uri of the saved cropped image result.
* Null if get cropped image was executed, no output requested or failure.
*/
private final Uri mUri;
/**
* The error that failed the loading/cropping (null if successful)
*/
private final Exception mError;
/**
* The 4 points of the cropping window in the source image
*/
private final float[] mCropPoints;
/**
* The rectangle of the cropping window in the source image
*/
private final Rect mCropRect;
/**
* The final rotation of the cropped image relative to source
*/
private final int mRotation;
/**
* sample size used creating the crop bitmap to lower its size
*/
private final int mSampleSize;
public CropResult(Bitmap bitmap, Uri uri, Exception error, float[] cropPoints, Rect cropRect, int rotation, int sampleSize) {
mBitmap = bitmap;
mUri = uri;
mError = error;
mCropPoints = cropPoints;
mCropRect = cropRect;
mRotation = rotation;
mSampleSize = sampleSize;
}
/**
* Is the result is success or error.
*/
public boolean isSuccessful() {
return mError == null;
}
/**
* The cropped image bitmap result.
* Null if save cropped image was executed, no output requested or failure.
*/
public Bitmap getBitmap() {
return mBitmap;
}
/**
* The Android uri of the saved cropped image result
* Null if get cropped image was executed, no output requested or failure.
*/
public Uri getUri() {
return mUri;
}
/**
* The error that failed the loading/cropping (null if successful)
*/
public Exception getError() {
return mError;
}
/**
* The 4 points of the cropping window in the source image
*/
public float[] getCropPoints() {
return mCropPoints;
}
/**
* The rectangle of the cropping window in the source image
*/
public Rect getCropRect() {
return mCropRect;
}
/**
* The final rotation of the cropped image relative to source
*/
public int getRotation() {
return mRotation;
}
/**
* sample size used creating the crop bitmap to lower its size
*/
public int getSampleSize() {
return mSampleSize;
}
}
//endregion
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/CropOverlayView.java
================================================
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
package com.github.gzuliyujiang.imagepicker;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import java.util.Arrays;
/**
* A custom View representing the crop window and the shaded background outside the crop window.
*/
public class CropOverlayView extends View {
//region: Fields and Consts
/**
* Gesture detector used for multi touch box scaling
*/
private ScaleGestureDetector mScaleDetector;
/**
* Boolean to see if multi touch is enabled for the crop rectangle
*/
private boolean mMultiTouchEnabled;
/**
* Handler from crop window stuff, moving and knowing possition.
*/
private final CropWindowHandler mCropWindowHandler = new CropWindowHandler();
/**
* Listener to publicj crop window changes
*/
private CropWindowChangeListener mCropWindowChangeListener;
/**
* Rectangle used for drawing
*/
private final RectF mDrawRect = new RectF();
/**
* The Paint used to draw the white rectangle around the crop area.
*/
private Paint mBorderPaint;
/**
* The Paint used to draw the corners of the Border
*/
private Paint mBorderCornerPaint;
/**
* The Paint used to draw the guidelines within the crop area when pressed.
*/
private Paint mGuidelinePaint;
/**
* The Paint used to darken the surrounding areas outside the crop area.
*/
private Paint mBackgroundPaint;
/**
* Used for oval crop window shape or non-straight rotation drawing.
*/
private final Path mPath = new Path();
/**
* The bounding box around the Bitmap that we are cropping.
*/
private final float[] mBoundsPoints = new float[8];
/**
* The bounding box around the Bitmap that we are cropping.
*/
private final RectF mCalcBounds = new RectF();
/**
* The bounding image view width used to know the crop overlay is at view edges.
*/
private int mViewWidth;
/**
* The bounding image view height used to know the crop overlay is at view edges.
*/
private int mViewHeight;
/**
* The offset to draw the border corener from the border
*/
private float mBorderCornerOffset;
/**
* the length of the border corner to draw
*/
private float mBorderCornerLength;
/**
* The initial crop window padding from image borders
*/
private float mInitialCropWindowPaddingRatio;
/**
* The radius of the touch zone (in pixels) around a given Handle.
*/
private float mTouchRadius;
/**
* An edge of the crop window will snap to the corresponding edge of a specified bounding box
* when the crop window edge is less than or equal to this distance (in pixels) away from the bounding box edge.
*/
private float mSnapRadius;
/**
* The Handle that is currently pressed; null if no Handle is pressed.
*/
private CropWindowMoveHandler mMoveHandler;
/**
* Flag indicating if the crop area should always be a certain aspect ratio (indicated by mTargetAspectRatio).
*/
private boolean mFixAspectRatio;
/**
* save the current aspect ratio of the image
*/
private int mAspectRatioX;
/**
* save the current aspect ratio of the image
*/
private int mAspectRatioY;
/**
* The aspect ratio that the crop area should maintain;
* this variable is only used when mMaintainAspectRatio is true.
*/
private float mTargetAspectRatio = ((float) mAspectRatioX) / mAspectRatioY;
/**
* Instance variables for customizable attributes
*/
private CropImageView.Guidelines mGuidelines;
/**
* The shape of the cropping area - rectangle/circular.
*/
private CropImageView.CropShape mCropShape;
/**
* the initial crop window rectangle to set
*/
private final Rect mInitialCropWindowRect = new Rect();
/**
* Whether the Crop View has been initialized for the first time
*/
private boolean initializedCropWindow;
/**
* Used to set back LayerType after changing to software.
*/
private Integer mOriginalLayerType;
//endregion
public CropOverlayView(Context context) {
this(context, null);
}
public CropOverlayView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Set the crop window change listener.
*/
public void setCropWindowChangeListener(CropWindowChangeListener listener) {
mCropWindowChangeListener = listener;
}
/**
* Get the left/top/right/bottom coordinates of the crop window.
*/
public RectF getCropWindowRect() {
return mCropWindowHandler.getRect();
}
/**
* Set the left/top/right/bottom coordinates of the crop window.
*/
public void setCropWindowRect(RectF rect) {
mCropWindowHandler.setRect(rect);
}
/**
* Fix the current crop window rectangle if it is outside of cropping image or view bounds.
*/
public void fixCurrentCropWindowRect() {
RectF rect = getCropWindowRect();
fixCropWindowRectByRules(rect);
mCropWindowHandler.setRect(rect);
}
/**
* Informs the CropOverlayView of the image's position relative to the
* ImageView. This is necessary to call in order to draw the crop window.
*
* @param boundsPoints the image's bounding points
* @param viewWidth The bounding image view width.
* @param viewHeight The bounding image view height.
*/
public void setBounds(float[] boundsPoints, int viewWidth, int viewHeight) {
if (boundsPoints == null || !Arrays.equals(mBoundsPoints, boundsPoints)) {
if (boundsPoints == null) {
Arrays.fill(mBoundsPoints, 0);
} else {
System.arraycopy(boundsPoints, 0, mBoundsPoints, 0, boundsPoints.length);
}
mViewWidth = viewWidth;
mViewHeight = viewHeight;
RectF cropRect = mCropWindowHandler.getRect();
if (cropRect.width() == 0 || cropRect.height() == 0) {
initCropWindow();
}
}
}
/**
* Resets the crop overlay view.
*/
public void resetCropOverlayView() {
if (initializedCropWindow) {
setCropWindowRect(BitmapUtils.EMPTY_RECT_F);
initCropWindow();
invalidate();
}
}
/**
* The shape of the cropping area - rectangle/circular.
*/
public CropImageView.CropShape getCropShape() {
return mCropShape;
}
/**
* The shape of the cropping area - rectangle/circular.
*/
public void setCropShape(CropImageView.CropShape cropShape) {
if (mCropShape != cropShape) {
mCropShape = cropShape;
invalidate();
}
}
/**
* Get the current guidelines option set.
*/
public CropImageView.Guidelines getGuidelines() {
return mGuidelines;
}
/**
* Sets the guidelines for the CropOverlayView to be either on, off, or to show when resizing the application.
*/
public void setGuidelines(CropImageView.Guidelines guidelines) {
if (mGuidelines != guidelines) {
mGuidelines = guidelines;
if (initializedCropWindow) {
invalidate();
}
}
}
/**
* whether the aspect ratio is fixed or not; true fixes the aspect ratio, while false allows it to be changed.
*/
public boolean isFixAspectRatio() {
return mFixAspectRatio;
}
/**
* Sets whether the aspect ratio is fixed or not; true fixes the aspect ratio, while false allows it to be changed.
*/
public void setFixedAspectRatio(boolean fixAspectRatio) {
if (mFixAspectRatio != fixAspectRatio) {
mFixAspectRatio = fixAspectRatio;
if (initializedCropWindow) {
initCropWindow();
invalidate();
}
}
}
/**
* the X value of the aspect ratio;
*/
public int getAspectRatioX() {
return mAspectRatioX;
}
/**
* Sets the X value of the aspect ratio; is defaulted to 1.
*/
public void setAspectRatioX(int aspectRatioX) {
if (aspectRatioX <= 0) {
throw new IllegalArgumentException("Cannot set aspect ratio value to a number less than or equal to 0.");
} else if (mAspectRatioX != aspectRatioX) {
mAspectRatioX = aspectRatioX;
mTargetAspectRatio = ((float) mAspectRatioX) / mAspectRatioY;
if (initializedCropWindow) {
initCropWindow();
invalidate();
}
}
}
/**
* the Y value of the aspect ratio;
*/
public int getAspectRatioY() {
return mAspectRatioY;
}
/**
* Sets the Y value of the aspect ratio; is defaulted to 1.
*
* @param aspectRatioY int that specifies the new Y value of the aspect
* ratio
*/
public void setAspectRatioY(int aspectRatioY) {
if (aspectRatioY <= 0) {
throw new IllegalArgumentException("Cannot set aspect ratio value to a number less than or equal to 0.");
} else if (mAspectRatioY != aspectRatioY) {
mAspectRatioY = aspectRatioY;
mTargetAspectRatio = ((float) mAspectRatioX) / mAspectRatioY;
if (initializedCropWindow) {
initCropWindow();
invalidate();
}
}
}
/**
* An edge of the crop window will snap to the corresponding edge of a
* specified bounding box when the crop window edge is less than or equal to
* this distance (in pixels) away from the bounding box edge. (default: 3)
*/
public void setSnapRadius(float snapRadius) {
mSnapRadius = snapRadius;
}
/**
* Set multi touch functionality to enabled/disabled.
*/
public boolean setMultiTouchEnabled(boolean multiTouchEnabled) {
if (mMultiTouchEnabled != multiTouchEnabled) {
mMultiTouchEnabled = multiTouchEnabled;
if (mMultiTouchEnabled && mScaleDetector == null) {
mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleListener());
}
return true;
}
return false;
}
/**
* the min size the resulting cropping image is allowed to be, affects the cropping window limits
* (in pixels).
*/
public void setMinCropResultSize(int minCropResultWidth, int minCropResultHeight) {
mCropWindowHandler.setMinCropResultSize(minCropResultWidth, minCropResultHeight);
}
/**
* the max size the resulting cropping image is allowed to be, affects the cropping window limits
* (in pixels).
*/
public void setMaxCropResultSize(int maxCropResultWidth, int maxCropResultHeight) {
mCropWindowHandler.setMaxCropResultSize(maxCropResultWidth, maxCropResultHeight);
}
/**
* set the max width/height and scale factor of the shown image to original image to scale the limits
* appropriately.
*/
public void setCropWindowLimits(float maxWidth, float maxHeight, float scaleFactorWidth, float scaleFactorHeight) {
mCropWindowHandler.setCropWindowLimits(maxWidth, maxHeight, scaleFactorWidth, scaleFactorHeight);
}
/**
* Get crop window initial rectangle.
*/
public Rect getInitialCropWindowRect() {
return mInitialCropWindowRect;
}
/**
* Set crop window initial rectangle to be used instead of default.
*/
public void setInitialCropWindowRect(Rect rect) {
mInitialCropWindowRect.set(rect != null ? rect : BitmapUtils.EMPTY_RECT);
if (initializedCropWindow) {
initCropWindow();
invalidate();
callOnCropWindowChanged(false);
}
}
/**
* Reset crop window to initial rectangle.
*/
public void resetCropWindowRect() {
if (initializedCropWindow) {
initCropWindow();
invalidate();
callOnCropWindowChanged(false);
}
}
/**
* Sets all initial values, but does not call initCropWindow to reset the views.
* Used once at the very start to initialize the attributes.
*/
public void setInitialAttributeValues(CropImageOptions options) {
mCropWindowHandler.setInitialAttributeValues(options);
setCropShape(options.cropShape);
setSnapRadius(options.snapRadius);
setGuidelines(options.guidelines);
setFixedAspectRatio(options.fixAspectRatio);
setAspectRatioX(options.aspectRatioX);
setAspectRatioY(options.aspectRatioY);
setMultiTouchEnabled(options.multiTouchEnabled);
mTouchRadius = options.touchRadius;
mInitialCropWindowPaddingRatio = options.initialCropWindowPaddingRatio;
mBorderPaint = getNewPaintOrNull(options.borderLineThickness, options.borderLineColor);
mBorderCornerOffset = options.borderCornerOffset;
mBorderCornerLength = options.borderCornerLength;
mBorderCornerPaint = getNewPaintOrNull(options.borderCornerThickness, options.borderCornerColor);
mGuidelinePaint = getNewPaintOrNull(options.guidelinesThickness, options.guidelinesColor);
mBackgroundPaint = getNewPaint(options.backgroundColor);
}
//region: Private methods
/**
* Set the initial crop window size and position. This is dependent on the
* size and position of the image being cropped.
*/
private void initCropWindow() {
float leftLimit = Math.max(BitmapUtils.getRectLeft(mBoundsPoints), 0);
float topLimit = Math.max(BitmapUtils.getRectTop(mBoundsPoints), 0);
float rightLimit = Math.min(BitmapUtils.getRectRight(mBoundsPoints), getWidth());
float bottomLimit = Math.min(BitmapUtils.getRectBottom(mBoundsPoints), getHeight());
if (rightLimit <= leftLimit || bottomLimit <= topLimit) {
return;
}
RectF rect = new RectF();
// Tells the attribute functions the crop window has already been initialized
initializedCropWindow = true;
float horizontalPadding = mInitialCropWindowPaddingRatio * (rightLimit - leftLimit);
float verticalPadding = mInitialCropWindowPaddingRatio * (bottomLimit - topLimit);
if (mInitialCropWindowRect.width() > 0 && mInitialCropWindowRect.height() > 0) {
// Get crop window position relative to the displayed image.
rect.left = leftLimit + mInitialCropWindowRect.left / mCropWindowHandler.getScaleFactorWidth();
rect.top = topLimit + mInitialCropWindowRect.top / mCropWindowHandler.getScaleFactorHeight();
rect.right = rect.left + mInitialCropWindowRect.width() / mCropWindowHandler.getScaleFactorWidth();
rect.bottom = rect.top + mInitialCropWindowRect.height() / mCropWindowHandler.getScaleFactorHeight();
// Correct for floating point errors. Crop rect boundaries should not exceed the source Bitmap bounds.
rect.left = Math.max(leftLimit, rect.left);
rect.top = Math.max(topLimit, rect.top);
rect.right = Math.min(rightLimit, rect.right);
rect.bottom = Math.min(bottomLimit, rect.bottom);
} else if (mFixAspectRatio) {
// If the image aspect ratio is wider than the crop aspect ratio,
// then the image height is the determining initial length. Else, vice-versa.
float bitmapAspectRatio = (rightLimit - leftLimit) / (bottomLimit - topLimit);
if (bitmapAspectRatio > mTargetAspectRatio) {
rect.top = topLimit + verticalPadding;
rect.bottom = bottomLimit - verticalPadding;
float centerX = getWidth() / 2f;
//dirty fix for wrong crop overlay aspect ratio when using fixed aspect ratio
mTargetAspectRatio = (float) mAspectRatioX / mAspectRatioY;
// Limits the aspect ratio to no less than 40 wide or 40 tall
float cropWidth = Math.max(mCropWindowHandler.getMinCropWidth(), rect.height() * mTargetAspectRatio);
float halfCropWidth = cropWidth / 2f;
rect.left = centerX - halfCropWidth;
rect.right = centerX + halfCropWidth;
} else {
rect.left = leftLimit + horizontalPadding;
rect.right = rightLimit - horizontalPadding;
float centerY = getHeight() / 2f;
// Limits the aspect ratio to no less than 40 wide or 40 tall
float cropHeight = Math.max(mCropWindowHandler.getMinCropHeight(), rect.width() / mTargetAspectRatio);
float halfCropHeight = cropHeight / 2f;
rect.top = centerY - halfCropHeight;
rect.bottom = centerY + halfCropHeight;
}
} else {
// Initialize crop window to have 10% padding w/ respect to image.
rect.left = leftLimit + horizontalPadding;
rect.top = topLimit + verticalPadding;
rect.right = rightLimit - horizontalPadding;
rect.bottom = bottomLimit - verticalPadding;
}
fixCropWindowRectByRules(rect);
mCropWindowHandler.setRect(rect);
}
/**
* Fix the given rect to fit into bitmap rect and follow min, max and aspect ratio rules.
*/
private void fixCropWindowRectByRules(RectF rect) {
if (rect.width() < mCropWindowHandler.getMinCropWidth()) {
float adj = (mCropWindowHandler.getMinCropWidth() - rect.width()) / 2;
rect.left -= adj;
rect.right += adj;
}
if (rect.height() < mCropWindowHandler.getMinCropHeight()) {
float adj = (mCropWindowHandler.getMinCropHeight() - rect.height()) / 2;
rect.top -= adj;
rect.bottom += adj;
}
if (rect.width() > mCropWindowHandler.getMaxCropWidth()) {
float adj = (rect.width() - mCropWindowHandler.getMaxCropWidth()) / 2;
rect.left += adj;
rect.right -= adj;
}
if (rect.height() > mCropWindowHandler.getMaxCropHeight()) {
float adj = (rect.height() - mCropWindowHandler.getMaxCropHeight()) / 2;
rect.top += adj;
rect.bottom -= adj;
}
calculateBounds(rect);
if (mCalcBounds.width() > 0 && mCalcBounds.height() > 0) {
float leftLimit = Math.max(mCalcBounds.left, 0);
float topLimit = Math.max(mCalcBounds.top, 0);
float rightLimit = Math.min(mCalcBounds.right, getWidth());
float bottomLimit = Math.min(mCalcBounds.bottom, getHeight());
if (rect.left < leftLimit) {
rect.left = leftLimit;
}
if (rect.top < topLimit) {
rect.top = topLimit;
}
if (rect.right > rightLimit) {
rect.right = rightLimit;
}
if (rect.bottom > bottomLimit) {
rect.bottom = bottomLimit;
}
}
if (mFixAspectRatio && Math.abs(rect.width() - rect.height() * mTargetAspectRatio) > 0.1) {
if (rect.width() > rect.height() * mTargetAspectRatio) {
float adj = Math.abs(rect.height() * mTargetAspectRatio - rect.width()) / 2;
rect.left += adj;
rect.right -= adj;
} else {
float adj = Math.abs(rect.width() / mTargetAspectRatio - rect.height()) / 2;
rect.top += adj;
rect.bottom -= adj;
}
}
}
/**
* Draw crop overview by drawing background over image not in the cripping area, then borders and guidelines.
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Draw translucent background for the cropped area.
drawBackground(canvas);
if (mCropWindowHandler.showGuidelines()) {
// Determines whether guidelines should be drawn or not
if (mGuidelines == CropImageView.Guidelines.ON) {
drawGuidelines(canvas);
} else if (mGuidelines == CropImageView.Guidelines.ON_TOUCH && mMoveHandler != null) {
// Draw only when resizing
drawGuidelines(canvas);
}
}
drawBorders(canvas);
drawCorners(canvas);
}
/**
* Draw shadow background over the image not including the crop area.
*/
private void drawBackground(Canvas canvas) {
RectF rect = mCropWindowHandler.getRect();
float left = Math.max(BitmapUtils.getRectLeft(mBoundsPoints), 0);
float top = Math.max(BitmapUtils.getRectTop(mBoundsPoints), 0);
float right = Math.min(BitmapUtils.getRectRight(mBoundsPoints), getWidth());
float bottom = Math.min(BitmapUtils.getRectBottom(mBoundsPoints), getHeight());
if (mCropShape == CropImageView.CropShape.RECTANGLE) {
if (!isNonStraightAngleRotated()) {
canvas.drawRect(left, top, right, rect.top, mBackgroundPaint);
canvas.drawRect(left, rect.bottom, right, bottom, mBackgroundPaint);
canvas.drawRect(left, rect.top, rect.left, rect.bottom, mBackgroundPaint);
canvas.drawRect(rect.right, rect.top, right, rect.bottom, mBackgroundPaint);
} else {
mPath.reset();
mPath.moveTo(mBoundsPoints[0], mBoundsPoints[1]);
mPath.lineTo(mBoundsPoints[2], mBoundsPoints[3]);
mPath.lineTo(mBoundsPoints[4], mBoundsPoints[5]);
mPath.lineTo(mBoundsPoints[6], mBoundsPoints[7]);
mPath.close();
canvas.save();
canvas.clipPath(mPath, Region.Op.INTERSECT);
canvas.clipRect(rect, Region.Op.DIFFERENCE);
canvas.drawRect(left, top, right, bottom, mBackgroundPaint);
canvas.restore();
}
} else {
mPath.reset();
mDrawRect.set(rect.left, rect.top, rect.right, rect.bottom);
mPath.addOval(mDrawRect, Path.Direction.CW);
canvas.save();
canvas.clipPath(mPath, Region.Op.DIFFERENCE);
canvas.drawRect(left, top, right, bottom, mBackgroundPaint);
canvas.restore();
}
}
/**
* Draw 2 veritcal and 2 horizontal guidelines inside the cropping area to split it into 9 equal parts.
*/
private void drawGuidelines(Canvas canvas) {
if (mGuidelinePaint != null) {
float sw = mBorderPaint != null ? mBorderPaint.getStrokeWidth() : 0;
RectF rect = mCropWindowHandler.getRect();
rect.inset(sw, sw);
float oneThirdCropWidth = rect.width() / 3;
float oneThirdCropHeight = rect.height() / 3;
if (mCropShape == CropImageView.CropShape.OVAL) {
float w = rect.width() / 2 - sw;
float h = rect.height() / 2 - sw;
// Draw vertical guidelines.
float x1 = rect.left + oneThirdCropWidth;
float x2 = rect.right - oneThirdCropWidth;
float yv = (float) (h * Math.sin(Math.acos((w - oneThirdCropWidth) / w)));
canvas.drawLine(x1, rect.top + h - yv, x1, rect.bottom - h + yv, mGuidelinePaint);
canvas.drawLine(x2, rect.top + h - yv, x2, rect.bottom - h + yv, mGuidelinePaint);
// Draw horizontal guidelines.
float y1 = rect.top + oneThirdCropHeight;
float y2 = rect.bottom - oneThirdCropHeight;
float xv = (float) (w * Math.cos(Math.asin((h - oneThirdCropHeight) / h)));
canvas.drawLine(rect.left + w - xv, y1, rect.right - w + xv, y1, mGuidelinePaint);
canvas.drawLine(rect.left + w - xv, y2, rect.right - w + xv, y2, mGuidelinePaint);
} else {
// Draw vertical guidelines.
float x1 = rect.left + oneThirdCropWidth;
float x2 = rect.right - oneThirdCropWidth;
canvas.drawLine(x1, rect.top, x1, rect.bottom, mGuidelinePaint);
canvas.drawLine(x2, rect.top, x2, rect.bottom, mGuidelinePaint);
// Draw horizontal guidelines.
float y1 = rect.top + oneThirdCropHeight;
float y2 = rect.bottom - oneThirdCropHeight;
canvas.drawLine(rect.left, y1, rect.right, y1, mGuidelinePaint);
canvas.drawLine(rect.left, y2, rect.right, y2, mGuidelinePaint);
}
}
}
/**
* Draw borders of the crop area.
*/
private void drawBorders(Canvas canvas) {
if (mBorderPaint != null) {
float w = mBorderPaint.getStrokeWidth();
RectF rect = mCropWindowHandler.getRect();
rect.inset(w / 2, w / 2);
if (mCropShape == CropImageView.CropShape.RECTANGLE) {
// Draw rectangle crop window border.
canvas.drawRect(rect, mBorderPaint);
} else {
// Draw circular crop window border
canvas.drawOval(rect, mBorderPaint);
}
}
}
/**
* Draw the corner of crop overlay.
*/
private void drawCorners(Canvas canvas) {
if (mBorderCornerPaint != null) {
float lineWidth = mBorderPaint != null ? mBorderPaint.getStrokeWidth() : 0;
float cornerWidth = mBorderCornerPaint.getStrokeWidth();
float w = cornerWidth / 2 + mBorderCornerOffset;
RectF rect = mCropWindowHandler.getRect();
rect.inset(w, w);
float cornerOffset = (cornerWidth - lineWidth) / 2;
float cornerExtension = cornerWidth / 2 + cornerOffset;
// Top left
canvas.drawLine(rect.left - cornerOffset, rect.top - cornerExtension, rect.left - cornerOffset, rect.top + mBorderCornerLength, mBorderCornerPaint);
canvas.drawLine(rect.left - cornerExtension, rect.top - cornerOffset, rect.left + mBorderCornerLength, rect.top - cornerOffset, mBorderCornerPaint);
// Top right
canvas.drawLine(rect.right + cornerOffset, rect.top - cornerExtension, rect.right + cornerOffset, rect.top + mBorderCornerLength, mBorderCornerPaint);
canvas.drawLine(rect.right + cornerExtension, rect.top - cornerOffset, rect.right - mBorderCornerLength, rect.top - cornerOffset, mBorderCornerPaint);
// Bottom left
canvas.drawLine(rect.left - cornerOffset, rect.bottom + cornerExtension, rect.left - cornerOffset, rect.bottom - mBorderCornerLength, mBorderCornerPaint);
canvas.drawLine(rect.left - cornerExtension, rect.bottom + cornerOffset, rect.left + mBorderCornerLength, rect.bottom + cornerOffset, mBorderCornerPaint);
// Bottom left
canvas.drawLine(rect.right + cornerOffset, rect.bottom + cornerExtension, rect.right + cornerOffset, rect.bottom - mBorderCornerLength, mBorderCornerPaint);
canvas.drawLine(rect.right + cornerExtension, rect.bottom + cornerOffset, rect.right - mBorderCornerLength, rect.bottom + cornerOffset, mBorderCornerPaint);
}
}
/**
* Creates the Paint object for drawing.
*/
private static Paint getNewPaint(int color) {
Paint paint = new Paint();
paint.setColor(color);
return paint;
}
/**
* Creates the Paint object for given thickness and color, if thickness < 0 return null.
*/
private static Paint getNewPaintOrNull(float thickness, int color) {
if (thickness > 0) {
Paint borderPaint = new Paint();
borderPaint.setColor(color);
borderPaint.setStrokeWidth(thickness);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setAntiAlias(true);
return borderPaint;
} else {
return null;
}
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
// If this View is not enabled, don't allow for touch interactions.
if (isEnabled()) {
if (mMultiTouchEnabled) {
mScaleDetector.onTouchEvent(event);
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
onActionDown(event.getX(), event.getY());
return true;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
getParent().requestDisallowInterceptTouchEvent(false);
onActionUp();
return true;
case MotionEvent.ACTION_MOVE:
onActionMove(event.getX(), event.getY());
getParent().requestDisallowInterceptTouchEvent(true);
return true;
default:
return false;
}
} else {
return false;
}
}
/**
* On press down start crop window movment depending on the location of the press.
* if press is far from crop window then no move handler is returned (null).
*/
private void onActionDown(float x, float y) {
mMoveHandler = mCropWindowHandler.getMoveHandler(x, y, mTouchRadius, mCropShape);
if (mMoveHandler != null) {
invalidate();
}
}
/**
* Clear move handler starting in {@link #onActionDown(float, float)} if exists.
*/
private void onActionUp() {
if (mMoveHandler != null) {
mMoveHandler = null;
callOnCropWindowChanged(false);
invalidate();
}
}
/**
* Handle move of crop window using the move handler created in {@link #onActionDown(float, float)}.
* The move handler will do the proper move/resize of the crop window.
*/
private void onActionMove(float x, float y) {
if (mMoveHandler != null) {
float snapRadius = mSnapRadius;
RectF rect = mCropWindowHandler.getRect();
if (calculateBounds(rect)) {
snapRadius = 0;
}
mMoveHandler.move(rect, x, y, mCalcBounds, mViewWidth, mViewHeight, snapRadius, mFixAspectRatio, mTargetAspectRatio);
mCropWindowHandler.setRect(rect);
callOnCropWindowChanged(true);
invalidate();
}
}
/**
* Calculate the bounding rectangle for current crop window, handle non-straight rotation angles.
* If the rotation angle is straight then the bounds rectangle is the bitmap rectangle,
* otherwsie we find the max rectangle that is within the image bounds starting from the crop window rectangle.
*
* @param rect the crop window rectangle to start finsing bounded rectangle from
* @return true - non straight rotation in place, false - otherwise.
*/
private boolean calculateBounds(RectF rect) {
float left = BitmapUtils.getRectLeft(mBoundsPoints);
float top = BitmapUtils.getRectTop(mBoundsPoints);
float right = BitmapUtils.getRectRight(mBoundsPoints);
float bottom = BitmapUtils.getRectBottom(mBoundsPoints);
if (!isNonStraightAngleRotated()) {
mCalcBounds.set(left, top, right, bottom);
return false;
} else {
float x0 = mBoundsPoints[0];
float y0 = mBoundsPoints[1];
float x2 = mBoundsPoints[4];
float y2 = mBoundsPoints[5];
float x3 = mBoundsPoints[6];
float y3 = mBoundsPoints[7];
if (mBoundsPoints[7] < mBoundsPoints[1]) {
if (mBoundsPoints[1] < mBoundsPoints[3]) {
x0 = mBoundsPoints[6];
y0 = mBoundsPoints[7];
x2 = mBoundsPoints[2];
y2 = mBoundsPoints[3];
x3 = mBoundsPoints[4];
y3 = mBoundsPoints[5];
} else {
x0 = mBoundsPoints[4];
y0 = mBoundsPoints[5];
x2 = mBoundsPoints[0];
y2 = mBoundsPoints[1];
x3 = mBoundsPoints[2];
y3 = mBoundsPoints[3];
}
} else if (mBoundsPoints[1] > mBoundsPoints[3]) {
x0 = mBoundsPoints[2];
y0 = mBoundsPoints[3];
x2 = mBoundsPoints[6];
y2 = mBoundsPoints[7];
x3 = mBoundsPoints[0];
y3 = mBoundsPoints[1];
}
float a0 = (y3 - y0) / (x3 - x0);
float a1 = -1f / a0;
float b0 = y0 - a0 * x0;
float b1 = y0 - a1 * x0;
float b2 = y2 - a0 * x2;
float b3 = y2 - a1 * x2;
float c0 = (rect.centerY() - rect.top) / (rect.centerX() - rect.left);
float c1 = -c0;
float d0 = rect.top - c0 * rect.left;
float d1 = rect.top - c1 * rect.right;
left = Math.max(left, (d0 - b0) / (a0 - c0) < rect.right ? (d0 - b0) / (a0 - c0) : left);
left = Math.max(left, (d0 - b1) / (a1 - c0) < rect.right ? (d0 - b1) / (a1 - c0) : left);
left = Math.max(left, (d1 - b3) / (a1 - c1) < rect.right ? (d1 - b3) / (a1 - c1) : left);
right = Math.min(right, (d1 - b1) / (a1 - c1) > rect.left ? (d1 - b1) / (a1 - c1) : right);
right = Math.min(right, (d1 - b2) / (a0 - c1) > rect.left ? (d1 - b2) / (a0 - c1) : right);
right = Math.min(right, (d0 - b2) / (a0 - c0) > rect.left ? (d0 - b2) / (a0 - c0) : right);
top = Math.max(top, Math.max(a0 * left + b0, a1 * right + b1));
bottom = Math.min(bottom, Math.min(a1 * left + b3, a0 * right + b2));
mCalcBounds.left = left;
mCalcBounds.top = top;
mCalcBounds.right = right;
mCalcBounds.bottom = bottom;
return true;
}
}
/**
* Is the cropping image has been rotated by NOT 0,90,180 or 270 degrees.
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean isNonStraightAngleRotated() {
return mBoundsPoints[0] != mBoundsPoints[6] && mBoundsPoints[1] != mBoundsPoints[7];
}
/**
* Invoke on crop change listener safe, don't let the app crash on exception.
*/
private void callOnCropWindowChanged(boolean inProgress) {
try {
if (mCropWindowChangeListener != null) {
mCropWindowChangeListener.onCropWindowChanged(inProgress);
}
} catch (Exception e) {
Log.e("AIC", "Exception in crop window changed", e);
}
}
//endregion
//region: Inner class: CropWindowChangeListener
/**
* Interface definition for a callback to be invoked when crop window rectangle is changing.
*/
public interface CropWindowChangeListener {
/**
* Called after a change in crop window rectangle.
*
* @param inProgress is the crop window change operation is still in progress by user touch
*/
void onCropWindowChanged(boolean inProgress);
}
//endregion
//region: Inner class: ScaleListener
/**
* Handle scaling the rectangle based on two finger input
*/
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public boolean onScale(ScaleGestureDetector detector) {
RectF rect = mCropWindowHandler.getRect();
float x = detector.getFocusX();
float y = detector.getFocusY();
float dY = detector.getCurrentSpanY() / 2;
float dX = detector.getCurrentSpanX() / 2;
float newTop = y - dY;
float newLeft = x - dX;
float newRight = x + dX;
float newBottom = y + dY;
if (newLeft < newRight &&
newTop <= newBottom &&
newLeft >= 0 &&
newRight <= mCropWindowHandler.getMaxCropWidth() &&
newTop >= 0 &&
newBottom <= mCropWindowHandler.getMaxCropHeight()) {
rect.set(newLeft, newTop, newRight, newBottom);
mCropWindowHandler.setRect(rect);
invalidate();
}
return true;
}
}
//endregion
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/CropWindowHandler.java
================================================
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
package com.github.gzuliyujiang.imagepicker;
import android.graphics.RectF;
/**
* Handler from crop window stuff, moving and knowing possition.
*/
final class CropWindowHandler {
//region: Fields and Consts
/**
* The 4 edges of the crop window defining its coordinates and size
*/
private final RectF mEdges = new RectF();
/**
* Rectangle used to return the edges rectangle without ability to change it and without creating new all the time.
*/
private final RectF mGetEdges = new RectF();
/**
* Minimum width in pixels that the crop window can get.
*/
private float mMinCropWindowWidth;
/**
* Minimum height in pixels that the crop window can get.
*/
private float mMinCropWindowHeight;
/**
* Maximum width in pixels that the crop window can CURRENTLY get.
*/
private float mMaxCropWindowWidth;
/**
* Maximum height in pixels that the crop window can CURRENTLY get.
*/
private float mMaxCropWindowHeight;
/**
* Minimum width in pixels that the result of cropping an image can get,
* affects crop window width adjusted by width scale factor.
*/
private float mMinCropResultWidth;
/**
* Minimum height in pixels that the result of cropping an image can get,
* affects crop window height adjusted by height scale factor.
*/
private float mMinCropResultHeight;
/**
* Maximum width in pixels that the result of cropping an image can get,
* affects crop window width adjusted by width scale factor.
*/
private float mMaxCropResultWidth;
/**
* Maximum height in pixels that the result of cropping an image can get,
* affects crop window height adjusted by height scale factor.
*/
private float mMaxCropResultHeight;
/**
* The width scale factor of shown image and actual image
*/
private float mScaleFactorWidth = 1;
/**
* The height scale factor of shown image and actual image
*/
private float mScaleFactorHeight = 1;
//endregion
/**
* Get the left/top/right/bottom coordinates of the crop window.
*/
public RectF getRect() {
mGetEdges.set(mEdges);
return mGetEdges;
}
/**
* Minimum width in pixels that the crop window can get.
*/
public float getMinCropWidth() {
return Math.max(mMinCropWindowWidth, mMinCropResultWidth / mScaleFactorWidth);
}
/**
* Minimum height in pixels that the crop window can get.
*/
public float getMinCropHeight() {
return Math.max(mMinCropWindowHeight, mMinCropResultHeight / mScaleFactorHeight);
}
/**
* Maximum width in pixels that the crop window can get.
*/
public float getMaxCropWidth() {
return Math.min(mMaxCropWindowWidth, mMaxCropResultWidth / mScaleFactorWidth);
}
/**
* Maximum height in pixels that the crop window can get.
*/
public float getMaxCropHeight() {
return Math.min(mMaxCropWindowHeight, mMaxCropResultHeight / mScaleFactorHeight);
}
/**
* get the scale factor (on width) of the showen image to original image.
*/
public float getScaleFactorWidth() {
return mScaleFactorWidth;
}
/**
* get the scale factor (on height) of the showen image to original image.
*/
public float getScaleFactorHeight() {
return mScaleFactorHeight;
}
/**
* the min size the resulting cropping image is allowed to be, affects the cropping window limits
* (in pixels).
*/
public void setMinCropResultSize(int minCropResultWidth, int minCropResultHeight) {
mMinCropResultWidth = minCropResultWidth;
mMinCropResultHeight = minCropResultHeight;
}
/**
* the max size the resulting cropping image is allowed to be, affects the cropping window limits
* (in pixels).
*/
public void setMaxCropResultSize(int maxCropResultWidth, int maxCropResultHeight) {
mMaxCropResultWidth = maxCropResultWidth;
mMaxCropResultHeight = maxCropResultHeight;
}
/**
* set the max width/height and scale factor of the showen image to original image to scale the limits
* appropriately.
*/
public void setCropWindowLimits(float maxWidth, float maxHeight, float scaleFactorWidth, float scaleFactorHeight) {
mMaxCropWindowWidth = maxWidth;
mMaxCropWindowHeight = maxHeight;
mScaleFactorWidth = scaleFactorWidth;
mScaleFactorHeight = scaleFactorHeight;
}
/**
* Set the variables to be used during crop window handling.
*/
public void setInitialAttributeValues(CropImageOptions options) {
mMinCropWindowWidth = options.minCropWindowWidth;
mMinCropWindowHeight = options.minCropWindowHeight;
mMinCropResultWidth = options.minCropResultWidth;
mMinCropResultHeight = options.minCropResultHeight;
mMaxCropResultWidth = options.maxCropResultWidth;
mMaxCropResultHeight = options.maxCropResultHeight;
}
/**
* Set the left/top/right/bottom coordinates of the crop window.
*/
public void setRect(RectF rect) {
mEdges.set(rect);
}
/**
* Indicates whether the crop window is small enough that the guidelines
* should be shown. Public because this function is also used to determine
* if the center handle should be focused.
*
* @return boolean Whether the guidelines should be shown or not
*/
public boolean showGuidelines() {
return !(mEdges.width() < 100 || mEdges.height() < 100);
}
/**
* Determines which, if any, of the handles are pressed given the touch
* coordinates, the bounding box, and the touch radius.
*
* @param x the x-coordinate of the touch point
* @param y the y-coordinate of the touch point
* @param targetRadius the target radius in pixels
* @return the Handle that was pressed; null if no Handle was pressed
*/
public CropWindowMoveHandler getMoveHandler(float x, float y, float targetRadius, CropImageView.CropShape cropShape) {
CropWindowMoveHandler.Type type = cropShape == CropImageView.CropShape.OVAL
? getOvalPressedMoveType(x, y)
: getRectanglePressedMoveType(x, y, targetRadius);
return type != null ? new CropWindowMoveHandler(type, this, x, y) : null;
}
//region: Private methods
/**
* Determines which, if any, of the handles are pressed given the touch
* coordinates, the bounding box, and the touch radius.
*
* @param x the x-coordinate of the touch point
* @param y the y-coordinate of the touch point
* @param targetRadius the target radius in pixels
* @return the Handle that was pressed; null if no Handle was pressed
*/
private CropWindowMoveHandler.Type getRectanglePressedMoveType(float x, float y, float targetRadius) {
CropWindowMoveHandler.Type moveType = null;
// Note: corner-handles take precedence, then side-handles, then center.
if (CropWindowHandler.isInCornerTargetZone(x, y, mEdges.left, mEdges.top, targetRadius)) {
moveType = CropWindowMoveHandler.Type.TOP_LEFT;
} else if (CropWindowHandler.isInCornerTargetZone(x, y, mEdges.right, mEdges.top, targetRadius)) {
moveType = CropWindowMoveHandler.Type.TOP_RIGHT;
} else if (CropWindowHandler.isInCornerTargetZone(x, y, mEdges.left, mEdges.bottom, targetRadius)) {
moveType = CropWindowMoveHandler.Type.BOTTOM_LEFT;
} else if (CropWindowHandler.isInCornerTargetZone(x, y, mEdges.right, mEdges.bottom, targetRadius)) {
moveType = CropWindowMoveHandler.Type.BOTTOM_RIGHT;
} else if (CropWindowHandler.isInCenterTargetZone(x, y, mEdges.left, mEdges.top, mEdges.right, mEdges.bottom) && focusCenter()) {
moveType = CropWindowMoveHandler.Type.CENTER;
} else if (CropWindowHandler.isInHorizontalTargetZone(x, y, mEdges.left, mEdges.right, mEdges.top, targetRadius)) {
moveType = CropWindowMoveHandler.Type.TOP;
} else if (CropWindowHandler.isInHorizontalTargetZone(x, y, mEdges.left, mEdges.right, mEdges.bottom, targetRadius)) {
moveType = CropWindowMoveHandler.Type.BOTTOM;
} else if (CropWindowHandler.isInVerticalTargetZone(x, y, mEdges.left, mEdges.top, mEdges.bottom, targetRadius)) {
moveType = CropWindowMoveHandler.Type.LEFT;
} else if (CropWindowHandler.isInVerticalTargetZone(x, y, mEdges.right, mEdges.top, mEdges.bottom, targetRadius)) {
moveType = CropWindowMoveHandler.Type.RIGHT;
} else if (CropWindowHandler.isInCenterTargetZone(x, y, mEdges.left, mEdges.top, mEdges.right, mEdges.bottom) && !focusCenter()) {
moveType = CropWindowMoveHandler.Type.CENTER;
}
return moveType;
}
/**
* Determines which, if any, of the handles are pressed given the touch
* coordinates, the bounding box/oval, and the touch radius.
*
* @param x the x-coordinate of the touch point
* @param y the y-coordinate of the touch point
* @return the Handle that was pressed; null if no Handle was pressed
*/
private CropWindowMoveHandler.Type getOvalPressedMoveType(float x, float y) {
/*
Use a 6x6 grid system divided into 9 "handles", with the center the biggest region. While
this is not perfect, it's a good quick-to-ship approach.
TL T T T T TR
L C C C C R
L C C C C R
L C C C C R
L C C C C R
BL B B B B BR
*/
float cellLength = mEdges.width() / 6;
float leftCenter = mEdges.left + cellLength;
float rightCenter = mEdges.left + (5 * cellLength);
float cellHeight = mEdges.height() / 6;
float topCenter = mEdges.top + cellHeight;
float bottomCenter = mEdges.top + 5 * cellHeight;
CropWindowMoveHandler.Type moveType;
if (x < leftCenter) {
if (y < topCenter) {
moveType = CropWindowMoveHandler.Type.TOP_LEFT;
} else if (y < bottomCenter) {
moveType = CropWindowMoveHandler.Type.LEFT;
} else {
moveType = CropWindowMoveHandler.Type.BOTTOM_LEFT;
}
} else if (x < rightCenter) {
if (y < topCenter) {
moveType = CropWindowMoveHandler.Type.TOP;
} else if (y < bottomCenter) {
moveType = CropWindowMoveHandler.Type.CENTER;
} else {
moveType = CropWindowMoveHandler.Type.BOTTOM;
}
} else {
if (y < topCenter) {
moveType = CropWindowMoveHandler.Type.TOP_RIGHT;
} else if (y < bottomCenter) {
moveType = CropWindowMoveHandler.Type.RIGHT;
} else {
moveType = CropWindowMoveHandler.Type.BOTTOM_RIGHT;
}
}
return moveType;
}
/**
* Determines if the specified coordinate is in the target touch zone for a
* corner handle.
*
* @param x the x-coordinate of the touch point
* @param y the y-coordinate of the touch point
* @param handleX the x-coordinate of the corner handle
* @param handleY the y-coordinate of the corner handle
* @param targetRadius the target radius in pixels
* @return true if the touch point is in the target touch zone; false
* otherwise
*/
private static boolean isInCornerTargetZone(float x, float y, float handleX, float handleY, float targetRadius) {
return Math.abs(x - handleX) <= targetRadius && Math.abs(y - handleY) <= targetRadius;
}
/**
* Determines if the specified coordinate is in the target touch zone for a
* horizontal bar handle.
*
* @param x the x-coordinate of the touch point
* @param y the y-coordinate of the touch point
* @param handleXStart the left x-coordinate of the horizontal bar handle
* @param handleXEnd the right x-coordinate of the horizontal bar handle
* @param handleY the y-coordinate of the horizontal bar handle
* @param targetRadius the target radius in pixels
* @return true if the touch point is in the target touch zone; false
* otherwise
*/
private static boolean isInHorizontalTargetZone(float x, float y, float handleXStart, float handleXEnd, float handleY, float targetRadius) {
return x > handleXStart && x < handleXEnd && Math.abs(y - handleY) <= targetRadius;
}
/**
* Determines if the specified coordinate is in the target touch zone for a
* vertical bar handle.
*
* @param x the x-coordinate of the touch point
* @param y the y-coordinate of the touch point
* @param handleX the x-coordinate of the vertical bar handle
* @param handleYStart the top y-coordinate of the vertical bar handle
* @param handleYEnd the bottom y-coordinate of the vertical bar handle
* @param targetRadius the target radius in pixels
* @return true if the touch point is in the target touch zone; false
* otherwise
*/
private static boolean isInVerticalTargetZone(float x, float y, float handleX, float handleYStart, float handleYEnd, float targetRadius) {
return Math.abs(x - handleX) <= targetRadius && y > handleYStart && y < handleYEnd;
}
/**
* Determines if the specified coordinate falls anywhere inside the given
* bounds.
*
* @param x the x-coordinate of the touch point
* @param y the y-coordinate of the touch point
* @param left the x-coordinate of the left bound
* @param top the y-coordinate of the top bound
* @param right the x-coordinate of the right bound
* @param bottom the y-coordinate of the bottom bound
* @return true if the touch point is inside the bounding rectangle; false
* otherwise
*/
private static boolean isInCenterTargetZone(float x, float y, float left, float top, float right, float bottom) {
return x > left && x < right && y > top && y < bottom;
}
/**
* Determines if the cropper should focus on the center handle or the side
* handles. If it is a small image, focus on the center handle so the user
* can move it. If it is a large image, focus on the side handles so user
* can grab them. Corresponds to the appearance of the
* RuleOfThirdsGuidelines.
*
* @return true if it is small enough such that it should focus on the
* center; less than show_guidelines limit
*/
private boolean focusCenter() {
return !showGuidelines();
}
//endregion
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/CropWindowMoveHandler.java
================================================
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
package com.github.gzuliyujiang.imagepicker;
import android.graphics.PointF;
import android.graphics.RectF;
/**
* Handler to update crop window edges by the move type - Horizontal, Vertical, Corner or Center.
*/
final class CropWindowMoveHandler {
//region: Fields and Consts
/**
* Minimum width in pixels that the crop window can get.
*/
private final float mMinCropWidth;
/**
* Minimum width in pixels that the crop window can get.
*/
private final float mMinCropHeight;
/**
* Maximum height in pixels that the crop window can get.
*/
private final float mMaxCropWidth;
/**
* Maximum height in pixels that the crop window can get.
*/
private final float mMaxCropHeight;
/**
* The type of crop window move that is handled.
*/
private final Type mType;
/**
* Holds the x and y offset between the exact touch location and the exact handle location that is activated.
* There may be an offset because we allow for some leeway (specified by mHandleRadius) in activating a handle.
* However, we want to maintain these offset values while the handle is being dragged so that the handle
* doesn't jump.
*/
private final PointF mTouchOffset = new PointF();
//endregion
/**
* @param type the type of move this handler is executing
* @param cropWindowHandler main crop window handle to get and update the crop window edges
* @param touchX the location of the initial toch possition to measure move distance
* @param touchY the location of the initial toch possition to measure move distance
*/
public CropWindowMoveHandler(Type type, CropWindowHandler cropWindowHandler, float touchX, float touchY) {
mType = type;
mMinCropWidth = cropWindowHandler.getMinCropWidth();
mMinCropHeight = cropWindowHandler.getMinCropHeight();
mMaxCropWidth = cropWindowHandler.getMaxCropWidth();
mMaxCropHeight = cropWindowHandler.getMaxCropHeight();
calculateTouchOffset(cropWindowHandler.getRect(), touchX, touchY);
}
/**
* Updates the crop window by change in the toch location.
* Move type handled by this instance, as initialized in creation, affects how the change in toch location
* changes the crop window position and size.
* After the crop window position/size is changed by toch move it may result in values that vialate contraints:
* outside the bounds of the shown bitmap, smaller/larger than min/max size or missmatch in aspect ratio.
* So a series of fixes is executed on "secondary" edges to adjust it by the "primary" edge movement.
* Primary is the edge directly affected by move type, secondary is the other edge.
* The crop window is changed by directly setting the Edge coordinates.
*
* @param x the new x-coordinate of this handle
* @param y the new y-coordinate of this handle
* @param bounds the bounding rectangle of the image
* @param viewWidth The bounding image view width used to know the crop overlay is at view edges.
* @param viewHeight The bounding image view height used to know the crop overlay is at view edges.
* @param snapMargin the maximum distance (in pixels) at which the crop window should snap to the image
* @param fixedAspectRatio is the aspect ration fixed and 'targetAspectRatio' should be used
* @param aspectRatio the aspect ratio to maintain
*/
public void move(RectF rect, float x, float y, RectF bounds, int viewWidth, int viewHeight, float snapMargin, boolean fixedAspectRatio, float aspectRatio) {
// Adjust the coordinates for the finger position's offset (i.e. the
// distance from the initial touch to the precise handle location).
// We want to maintain the initial touch's distance to the pressed
// handle so that the crop window size does not "jump".
float adjX = x + mTouchOffset.x;
float adjY = y + mTouchOffset.y;
if (mType == Type.CENTER) {
moveCenter(rect, adjX, adjY, bounds, viewWidth, viewHeight, snapMargin);
} else {
if (fixedAspectRatio) {
moveSizeWithFixedAspectRatio(rect, adjX, adjY, bounds, viewWidth, viewHeight, snapMargin, aspectRatio);
} else {
moveSizeWithFreeAspectRatio(rect, adjX, adjY, bounds, viewWidth, viewHeight, snapMargin);
}
}
}
//region: Private methods
/**
* Calculates the offset of the touch point from the precise location of the specified handle.
* Save these values in a member variable since we want to maintain this offset as we drag the handle.
*/
private void calculateTouchOffset(RectF rect, float touchX, float touchY) {
float touchOffsetX = 0;
float touchOffsetY = 0;
// Calculate the offset from the appropriate handle.
switch (mType) {
case TOP_LEFT:
touchOffsetX = rect.left - touchX;
touchOffsetY = rect.top - touchY;
break;
case TOP_RIGHT:
touchOffsetX = rect.right - touchX;
touchOffsetY = rect.top - touchY;
break;
case BOTTOM_LEFT:
touchOffsetX = rect.left - touchX;
touchOffsetY = rect.bottom - touchY;
break;
case BOTTOM_RIGHT:
touchOffsetX = rect.right - touchX;
touchOffsetY = rect.bottom - touchY;
break;
case LEFT:
touchOffsetX = rect.left - touchX;
touchOffsetY = 0;
break;
case TOP:
touchOffsetX = 0;
touchOffsetY = rect.top - touchY;
break;
case RIGHT:
touchOffsetX = rect.right - touchX;
touchOffsetY = 0;
break;
case BOTTOM:
touchOffsetX = 0;
touchOffsetY = rect.bottom - touchY;
break;
case CENTER:
touchOffsetX = rect.centerX() - touchX;
touchOffsetY = rect.centerY() - touchY;
break;
default:
break;
}
mTouchOffset.x = touchOffsetX;
mTouchOffset.y = touchOffsetY;
}
/**
* Center move only changes the position of the crop window without changing the size.
*/
private void moveCenter(RectF rect, float x, float y, RectF bounds, int viewWidth, int viewHeight, float snapRadius) {
float dx = x - rect.centerX();
float dy = y - rect.centerY();
if (rect.left + dx < 0 || rect.right + dx > viewWidth || rect.left + dx < bounds.left || rect.right + dx > bounds.right) {
dx /= 1.05f;
mTouchOffset.x -= dx / 2;
}
if (rect.top + dy < 0 || rect.bottom + dy > viewHeight || rect.top + dy < bounds.top || rect.bottom + dy > bounds.bottom) {
dy /= 1.05f;
mTouchOffset.y -= dy / 2;
}
rect.offset(dx, dy);
snapEdgesToBounds(rect, bounds, snapRadius);
}
/**
* Change the size of the crop window on the required edge (or edges for corner size move) without
* affecting "secondary" edges.
* Only the primary edge(s) are fixed to stay within limits.
*/
private void moveSizeWithFreeAspectRatio(RectF rect, float x, float y, RectF bounds, int viewWidth, int viewHeight, float snapMargin) {
switch (mType) {
case TOP_LEFT:
adjustTop(rect, y, bounds, snapMargin, 0, false, false);
adjustLeft(rect, x, bounds, snapMargin, 0, false, false);
break;
case TOP_RIGHT:
adjustTop(rect, y, bounds, snapMargin, 0, false, false);
adjustRight(rect, x, bounds, viewWidth, snapMargin, 0, false, false);
break;
case BOTTOM_LEFT:
adjustBottom(rect, y, bounds, viewHeight, snapMargin, 0, false, false);
adjustLeft(rect, x, bounds, snapMargin, 0, false, false);
break;
case BOTTOM_RIGHT:
adjustBottom(rect, y, bounds, viewHeight, snapMargin, 0, false, false);
adjustRight(rect, x, bounds, viewWidth, snapMargin, 0, false, false);
break;
case LEFT:
adjustLeft(rect, x, bounds, snapMargin, 0, false, false);
break;
case TOP:
adjustTop(rect, y, bounds, snapMargin, 0, false, false);
break;
case RIGHT:
adjustRight(rect, x, bounds, viewWidth, snapMargin, 0, false, false);
break;
case BOTTOM:
adjustBottom(rect, y, bounds, viewHeight, snapMargin, 0, false, false);
break;
default:
break;
}
}
/**
* Change the size of the crop window on the required "primary" edge WITH affect to relevant "secondary"
* edge via aspect ratio.
* Example: change in the left edge (primary) will affect top and bottom edges (secondary) to preserve the
* given aspect ratio.
*/
private void moveSizeWithFixedAspectRatio(RectF rect, float x, float y, RectF bounds, int viewWidth, int viewHeight, float snapMargin, float aspectRatio) {
switch (mType) {
case TOP_LEFT:
if (calculateAspectRatio(x, y, rect.right, rect.bottom) < aspectRatio) {
adjustTop(rect, y, bounds, snapMargin, aspectRatio, true, false);
adjustLeftByAspectRatio(rect, aspectRatio);
} else {
adjustLeft(rect, x, bounds, snapMargin, aspectRatio, true, false);
adjustTopByAspectRatio(rect, aspectRatio);
}
break;
case TOP_RIGHT:
if (calculateAspectRatio(rect.left, y, x, rect.bottom) < aspectRatio) {
adjustTop(rect, y, bounds, snapMargin, aspectRatio, false, true);
adjustRightByAspectRatio(rect, aspectRatio);
} else {
adjustRight(rect, x, bounds, viewWidth, snapMargin, aspectRatio, true, false);
adjustTopByAspectRatio(rect, aspectRatio);
}
break;
case BOTTOM_LEFT:
if (calculateAspectRatio(x, rect.top, rect.right, y) < aspectRatio) {
adjustBottom(rect, y, bounds, viewHeight, snapMargin, aspectRatio, true, false);
adjustLeftByAspectRatio(rect, aspectRatio);
} else {
adjustLeft(rect, x, bounds, snapMargin, aspectRatio, false, true);
adjustBottomByAspectRatio(rect, aspectRatio);
}
break;
case BOTTOM_RIGHT:
if (calculateAspectRatio(rect.left, rect.top, x, y) < aspectRatio) {
adjustBottom(rect, y, bounds, viewHeight, snapMargin, aspectRatio, false, true);
adjustRightByAspectRatio(rect, aspectRatio);
} else {
adjustRight(rect, x, bounds, viewWidth, snapMargin, aspectRatio, false, true);
adjustBottomByAspectRatio(rect, aspectRatio);
}
break;
case LEFT:
adjustLeft(rect, x, bounds, snapMargin, aspectRatio, true, true);
adjustTopBottomByAspectRatio(rect, bounds, aspectRatio);
break;
case TOP:
adjustTop(rect, y, bounds, snapMargin, aspectRatio, true, true);
adjustLeftRightByAspectRatio(rect, bounds, aspectRatio);
break;
case RIGHT:
adjustRight(rect, x, bounds, viewWidth, snapMargin, aspectRatio, true, true);
adjustTopBottomByAspectRatio(rect, bounds, aspectRatio);
break;
case BOTTOM:
adjustBottom(rect, y, bounds, viewHeight, snapMargin, aspectRatio, true, true);
adjustLeftRightByAspectRatio(rect, bounds, aspectRatio);
break;
default:
break;
}
}
/**
* Check if edges have gone out of bounds (including snap margin), and fix if needed.
*/
private void snapEdgesToBounds(RectF edges, RectF bounds, float margin) {
if (edges.left < bounds.left + margin) {
edges.offset(bounds.left - edges.left, 0);
}
if (edges.top < bounds.top + margin) {
edges.offset(0, bounds.top - edges.top);
}
if (edges.right > bounds.right - margin) {
edges.offset(bounds.right - edges.right, 0);
}
if (edges.bottom > bounds.bottom - margin) {
edges.offset(0, bounds.bottom - edges.bottom);
}
}
/**
* Get the resulting x-position of the left edge of the crop window given
* the handle's position and the image's bounding box and snap radius.
*
* @param left the position that the left edge is dragged to
* @param bounds the bounding box of the image that is being cropped
* @param snapMargin the snap distance to the image edge (in pixels)
*/
private void adjustLeft(RectF rect, float left, RectF bounds, float snapMargin, float aspectRatio, boolean topMoves, boolean bottomMoves) {
float newLeft = left;
if (newLeft < 0) {
newLeft /= 1.05f;
mTouchOffset.x -= newLeft / 1.1f;
}
if (newLeft < bounds.left) {
mTouchOffset.x -= (newLeft - bounds.left) / 2f;
}
if (newLeft - bounds.left < snapMargin) {
newLeft = bounds.left;
}
// Checks if the window is too small horizontally
if (rect.right - newLeft < mMinCropWidth) {
newLeft = rect.right - mMinCropWidth;
}
// Checks if the window is too large horizontally
if (rect.right - newLeft > mMaxCropWidth) {
newLeft = rect.right - mMaxCropWidth;
}
if (newLeft - bounds.left < snapMargin) {
newLeft = bounds.left;
}
// check vertical bounds if aspect ratio is in play
if (aspectRatio > 0) {
float newHeight = (rect.right - newLeft) / aspectRatio;
// Checks if the window is too small vertically
if (newHeight < mMinCropHeight) {
newLeft = Math.max(bounds.left, rect.right - mMinCropHeight * aspectRatio);
newHeight = (rect.right - newLeft) / aspectRatio;
}
// Checks if the window is too large vertically
if (newHeight > mMaxCropHeight) {
newLeft = Math.max(bounds.left, rect.right - mMaxCropHeight * aspectRatio);
newHeight = (rect.right - newLeft) / aspectRatio;
}
// if top AND bottom edge moves by aspect ratio check that it is within full height bounds
if (topMoves && bottomMoves) {
newLeft = Math.max(newLeft, Math.max(bounds.left, rect.right - bounds.height() * aspectRatio));
} else {
// if top edge moves by aspect ratio check that it is within bounds
if (topMoves && rect.bottom - newHeight < bounds.top) {
newLeft = Math.max(bounds.left, rect.right - (rect.bottom - bounds.top) * aspectRatio);
newHeight = (rect.right - newLeft) / aspectRatio;
}
// if bottom edge moves by aspect ratio check that it is within bounds
if (bottomMoves && rect.top + newHeight > bounds.bottom) {
newLeft = Math.max(newLeft, Math.max(bounds.left, rect.right - (bounds.bottom - rect.top) * aspectRatio));
}
}
}
rect.left = newLeft;
}
/**
* Get the resulting x-position of the right edge of the crop window given
* the handle's position and the image's bounding box and snap radius.
*
* @param right the position that the right edge is dragged to
* @param bounds the bounding box of the image that is being cropped
* @param viewWidth view width
* @param snapMargin the snap distance to the image edge (in pixels)
*/
private void adjustRight(RectF rect, float right, RectF bounds, int viewWidth, float snapMargin, float aspectRatio, boolean topMoves, boolean bottomMoves) {
float newRight = right;
if (newRight > viewWidth) {
newRight = viewWidth + (newRight - viewWidth) / 1.05f;
mTouchOffset.x -= (newRight - viewWidth) / 1.1f;
}
if (newRight > bounds.right) {
mTouchOffset.x -= (newRight - bounds.right) / 2f;
}
// If close to the edge
if (bounds.right - newRight < snapMargin) {
newRight = bounds.right;
}
// Checks if the window is too small horizontally
if (newRight - rect.left < mMinCropWidth) {
newRight = rect.left + mMinCropWidth;
}
// Checks if the window is too large horizontally
if (newRight - rect.left > mMaxCropWidth) {
newRight = rect.left + mMaxCropWidth;
}
// If close to the edge
if (bounds.right - newRight < snapMargin) {
newRight = bounds.right;
}
// check vertical bounds if aspect ratio is in play
if (aspectRatio > 0) {
float newHeight = (newRight - rect.left) / aspectRatio;
// Checks if the window is too small vertically
if (newHeight < mMinCropHeight) {
newRight = Math.min(bounds.right, rect.left + mMinCropHeight * aspectRatio);
newHeight = (newRight - rect.left) / aspectRatio;
}
// Checks if the window is too large vertically
if (newHeight > mMaxCropHeight) {
newRight = Math.min(bounds.right, rect.left + mMaxCropHeight * aspectRatio);
newHeight = (newRight - rect.left) / aspectRatio;
}
// if top AND bottom edge moves by aspect ratio check that it is within full height bounds
if (topMoves && bottomMoves) {
newRight = Math.min(newRight, Math.min(bounds.right, rect.left + bounds.height() * aspectRatio));
} else {
// if top edge moves by aspect ratio check that it is within bounds
if (topMoves && rect.bottom - newHeight < bounds.top) {
newRight = Math.min(bounds.right, rect.left + (rect.bottom - bounds.top) * aspectRatio);
newHeight = (newRight - rect.left) / aspectRatio;
}
// if bottom edge moves by aspect ratio check that it is within bounds
if (bottomMoves && rect.top + newHeight > bounds.bottom) {
newRight = Math.min(newRight, Math.min(bounds.right, rect.left + (bounds.bottom - rect.top) * aspectRatio));
}
}
}
rect.right = newRight;
}
/**
* Get the resulting y-position of the top edge of the crop window given the
* handle's position and the image's bounding box and snap radius.
*
* @param top the x-position that the top edge is dragged to
* @param bounds the bounding box of the image that is being cropped
* @param snapMargin the snap distance to the image edge (in pixels)
*/
private void adjustTop(RectF rect, float top, RectF bounds, float snapMargin, float aspectRatio, boolean leftMoves, boolean rightMoves) {
float newTop = top;
if (newTop < 0) {
newTop /= 1.05f;
mTouchOffset.y -= newTop / 1.1f;
}
if (newTop < bounds.top) {
mTouchOffset.y -= (newTop - bounds.top) / 2f;
}
if (newTop - bounds.top < snapMargin) {
newTop = bounds.top;
}
// Checks if the window is too small vertically
if (rect.bottom - newTop < mMinCropHeight) {
newTop = rect.bottom - mMinCropHeight;
}
// Checks if the window is too large vertically
if (rect.bottom - newTop > mMaxCropHeight) {
newTop = rect.bottom - mMaxCropHeight;
}
if (newTop - bounds.top < snapMargin) {
newTop = bounds.top;
}
// check horizontal bounds if aspect ratio is in play
if (aspectRatio > 0) {
float newWidth = (rect.bottom - newTop) * aspectRatio;
// Checks if the crop window is too small horizontally due to aspect ratio adjustment
if (newWidth < mMinCropWidth) {
newTop = Math.max(bounds.top, rect.bottom - (mMinCropWidth / aspectRatio));
newWidth = (rect.bottom - newTop) * aspectRatio;
}
// Checks if the crop window is too large horizontally due to aspect ratio adjustment
if (newWidth > mMaxCropWidth) {
newTop = Math.max(bounds.top, rect.bottom - (mMaxCropWidth / aspectRatio));
newWidth = (rect.bottom - newTop) * aspectRatio;
}
// if left AND right edge moves by aspect ratio check that it is within full width bounds
if (leftMoves && rightMoves) {
newTop = Math.max(newTop, Math.max(bounds.top, rect.bottom - bounds.width() / aspectRatio));
} else {
// if left edge moves by aspect ratio check that it is within bounds
if (leftMoves && rect.right - newWidth < bounds.left) {
newTop = Math.max(bounds.top, rect.bottom - (rect.right - bounds.left) / aspectRatio);
newWidth = (rect.bottom - newTop) * aspectRatio;
}
// if right edge moves by aspect ratio check that it is within bounds
if (rightMoves && rect.left + newWidth > bounds.right) {
newTop = Math.max(newTop, Math.max(bounds.top, rect.bottom - (bounds.right - rect.left) / aspectRatio));
}
}
}
rect.top = newTop;
}
/**
* Get the resulting y-position of the bottom edge of the crop window given
* the handle's position and the image's bounding box and snap radius.
*
* @param bottom the position that the bottom edge is dragged to
* @param bounds the bounding box of the image that is being cropped
* @param viewHeight view height
* @param snapMargin the snap distance to the image edge (in pixels)
*/
private void adjustBottom(RectF rect, float bottom, RectF bounds, int viewHeight, float snapMargin, float aspectRatio, boolean leftMoves, boolean rightMoves) {
float newBottom = bottom;
if (newBottom > viewHeight) {
newBottom = viewHeight + (newBottom - viewHeight) / 1.05f;
mTouchOffset.y -= (newBottom - viewHeight) / 1.1f;
}
if (newBottom > bounds.bottom) {
mTouchOffset.y -= (newBottom - bounds.bottom) / 2f;
}
if (bounds.bottom - newBottom < snapMargin) {
newBottom = bounds.bottom;
}
// Checks if the window is too small vertically
if (newBottom - rect.top < mMinCropHeight) {
newBottom = rect.top + mMinCropHeight;
}
// Checks if the window is too small vertically
if (newBottom - rect.top > mMaxCropHeight) {
newBottom = rect.top + mMaxCropHeight;
}
if (bounds.bottom - newBottom < snapMargin) {
newBottom = bounds.bottom;
}
// check horizontal bounds if aspect ratio is in play
if (aspectRatio > 0) {
float newWidth = (newBottom - rect.top) * aspectRatio;
// Checks if the window is too small horizontally
if (newWidth < mMinCropWidth) {
newBottom = Math.min(bounds.bottom, rect.top + mMinCropWidth / aspectRatio);
newWidth = (newBottom - rect.top) * aspectRatio;
}
// Checks if the window is too large horizontally
if (newWidth > mMaxCropWidth) {
newBottom = Math.min(bounds.bottom, rect.top + mMaxCropWidth / aspectRatio);
newWidth = (newBottom - rect.top) * aspectRatio;
}
// if left AND right edge moves by aspect ratio check that it is within full width bounds
if (leftMoves && rightMoves) {
newBottom = Math.min(newBottom, Math.min(bounds.bottom, rect.top + bounds.width() / aspectRatio));
} else {
// if left edge moves by aspect ratio check that it is within bounds
if (leftMoves && rect.right - newWidth < bounds.left) {
newBottom = Math.min(bounds.bottom, rect.top + (rect.right - bounds.left) / aspectRatio);
newWidth = (newBottom - rect.top) * aspectRatio;
}
// if right edge moves by aspect ratio check that it is within bounds
if (rightMoves && rect.left + newWidth > bounds.right) {
newBottom = Math.min(newBottom, Math.min(bounds.bottom, rect.top + (bounds.right - rect.left) / aspectRatio));
}
}
}
rect.bottom = newBottom;
}
/**
* Adjust left edge by current crop window height and the given aspect ratio,
* the right edge remains in possition while the left adjusts to keep aspect ratio to the height.
*/
private void adjustLeftByAspectRatio(RectF rect, float aspectRatio) {
rect.left = rect.right - rect.height() * aspectRatio;
}
/**
* Adjust top edge by current crop window width and the given aspect ratio,
* the bottom edge remains in possition while the top adjusts to keep aspect ratio to the width.
*/
private void adjustTopByAspectRatio(RectF rect, float aspectRatio) {
rect.top = rect.bottom - rect.width() / aspectRatio;
}
/**
* Adjust right edge by current crop window height and the given aspect ratio,
* the left edge remains in possition while the left adjusts to keep aspect ratio to the height.
*/
private void adjustRightByAspectRatio(RectF rect, float aspectRatio) {
rect.right = rect.left + rect.height() * aspectRatio;
}
/**
* Adjust bottom edge by current crop window width and the given aspect ratio,
* the top edge remains in possition while the top adjusts to keep aspect ratio to the width.
*/
private void adjustBottomByAspectRatio(RectF rect, float aspectRatio) {
rect.bottom = rect.top + rect.width() / aspectRatio;
}
/**
* Adjust left and right edges by current crop window height and the given aspect ratio,
* both right and left edges adjusts equally relative to center to keep aspect ratio to the height.
*/
private void adjustLeftRightByAspectRatio(RectF rect, RectF bounds, float aspectRatio) {
rect.inset((rect.width() - rect.height() * aspectRatio) / 2, 0);
if (rect.left < bounds.left) {
rect.offset(bounds.left - rect.left, 0);
}
if (rect.right > bounds.right) {
rect.offset(bounds.right - rect.right, 0);
}
}
/**
* Adjust top and bottom edges by current crop window width and the given aspect ratio,
* both top and bottom edges adjusts equally relative to center to keep aspect ratio to the width.
*/
private void adjustTopBottomByAspectRatio(RectF rect, RectF bounds, float aspectRatio) {
rect.inset(0, (rect.height() - rect.width() / aspectRatio) / 2);
if (rect.top < bounds.top) {
rect.offset(0, bounds.top - rect.top);
}
if (rect.bottom > bounds.bottom) {
rect.offset(0, bounds.bottom - rect.bottom);
}
}
/**
* Calculates the aspect ratio given a rectangle.
*/
private static float calculateAspectRatio(float left, float top, float right, float bottom) {
return (right - left) / (bottom - top);
}
//endregion
//region: Inner class: Type
/**
* The type of crop window move that is handled.
*/
public enum Type {
TOP_LEFT,
TOP_RIGHT,
BOTTOM_LEFT,
BOTTOM_RIGHT,
LEFT,
TOP,
RIGHT,
BOTTOM,
CENTER
}
//endregion
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/HybridityUtils.java
================================================
package com.github.gzuliyujiang.imagepicker;
import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;
import java.io.File;
import java.io.InputStream;
/**
* Created by linchaolong on 2017/5/10.
*/
final class HybridityUtils {
public static Intent getCameraIntent(@NonNull Context context) {
Uri outputFileUri = HybridityUtils.getCaptureImageOutputUri(context);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, HybridityUtils.getIntentUri(context, outputFileUri));
return intent;
}
public static Intent getGalleryIntent() {
return getChooserIntent("image/*");
}
public static Intent getChooserIntent(String mime) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(mime);
return intent;
}
public static Uri getPickImageResultUri(@NonNull Context context, @Nullable Intent data) {
boolean isCamera = true;
if (data != null && data.getData() != null) {
String action = data.getAction();
isCamera = action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
}
return isCamera || data.getData() == null ? getCaptureImageOutputUri(context) : data.getData();
}
public static Uri getCaptureImageOutputUri(@NonNull Context context) {
Uri outputFileUri = null;
File getImage = context.getExternalCacheDir();
if (getImage != null) {
outputFileUri = Uri.fromFile(new File(getImage.getPath(), "pickImageResult.jpeg"));
}
return outputFileUri;
}
/**
* 获取文件的真实路径,比如:content://media/external/images/media/74275 的真实路径 file:///storage/sdcard0/Pictures/X.jpg
*
* http://stackoverflow.com/questions/20028319/how-to-convert-content-media-external-images-media-y-to-file-storage-sdc
*/
public static String getRealPathFromUri(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
if (cursor == null) {
return "";
}
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(columnIndex);
} catch (IllegalStateException e) {
//IllegalArgumentException: column '_data' does not exist. Available columns: []
return "";
} finally {
if (cursor != null) {
cursor.close();
}
}
}
/**
* Check if explicetly requesting camera permission is required.
* It is required in Android Marshmellow and above if "CAMERA" permission is requested in the manifest.
* See StackOverflow
* question.
*/
public static boolean isExplicitCameraPermissionRequired(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return hasPermissionInManifest(context, Manifest.permission.CAMERA) &&
context.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED;
}
return false;
}
/**
* Check if the app requests a specific permission in the manifest.
*
* @param permissionName the permission to check
* @return true - the permission in requested in manifest, false - not.
*/
public static boolean hasPermissionInManifest(@NonNull Context context, @NonNull
String permissionName) {
String packageName = context.getPackageName();
try {
PackageInfo
packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
final String[] declaredPermisisons = packageInfo.requestedPermissions;
if (declaredPermisisons != null && declaredPermisisons.length > 0) {
for (String p : declaredPermisisons) {
if (p.equalsIgnoreCase(permissionName)) {
return true;
}
}
}
} catch (PackageManager.NameNotFoundException ignore) {
}
return false;
}
/**
* Check if the given picked image URI requires READ_EXTERNAL_STORAGE permissions.
* Only relevant for API version 23 and above and not required for all URI's depends on the
* implementation of the app that was used for picking the image. So we just test if we can open the stream or
* do we get an exception when we try, Android is awesome.
*
* @param context used to access Android APIs, like content resolve, it is your activity/fragment/widget.
* @param uri the result URI of image pick.
* @return true - required permission are not granted, false - either no need for permissions or they are granted
*/
public static boolean isReadExternalStoragePermissionsRequired(@NonNull Context context, @NonNull
Uri uri) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return hasPermissionInManifest(context, Manifest.permission.READ_EXTERNAL_STORAGE) &&
context.checkSelfPermission(
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
isUriRequiresPermissions(context, uri);
}
return false;
}
/**
* Test if we can open the given Android URI to test if permission required error is thrown.
* Only relevant for API version 23 and above.
*
* @param context used to access Android APIs, like content resolve, it is your activity/fragment/widget.
* @param uri the result URI of image pick.
*/
public static boolean isUriRequiresPermissions(@NonNull Context context, @NonNull Uri uri) {
try {
ContentResolver resolver = context.getContentResolver();
InputStream stream = resolver.openInputStream(uri);
if (stream != null) {
stream.close();
}
return false;
} catch (Exception e) {
return true;
}
}
/**
* 兼容 Android N,Intent中不能使用 file:///*
*/
public static Uri getIntentUri(Context context, Uri uri) {
//support android N+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return getContentUri(context, uri);
} else {
return uri;
}
}
public static Uri getContentUri(Context context, Uri fileUri) {
return FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".imagepicker.provider", new File(fileUri.getPath()));
}
/**
* content uri to path
*/
public static String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index;
if (cursor != null) {
column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return "";
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/ImagePicker.java
================================================
package com.github.gzuliyujiang.imagepicker;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import java.io.File;
import java.io.FileNotFoundException;
/**
* 图片选择器(相机拍照+相册选取+图片裁剪)
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2020/4/3 17:18
*/
public class ImagePicker {
private Uri cropImageUri;
private boolean cropEnabled = true;
private PickCallback callback;
private static class Holder {
private static final ImagePicker INSTANCE = new ImagePicker();
}
public static ImagePicker getInstance() {
return Holder.INSTANCE;
}
private ImagePicker() {
super();
}
/**
* 启动照相机
*/
public void startCamera(Activity activity, boolean cropEnabled, @NonNull PickCallback callback) {
this.cropEnabled = cropEnabled;
this.callback = callback;
if (HybridityUtils.isExplicitCameraPermissionRequired(activity)) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA},
CropImageConsts.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE);
} else {
activity.startActivityForResult(HybridityUtils.getCameraIntent(activity), CropImageConsts.PICK_IMAGE_CHOOSER_REQUEST_CODE);
}
}
/**
* 启动照相机
*/
public void startCamera(Fragment fragment, boolean cropEnabled, @NonNull PickCallback callback) {
this.cropEnabled = cropEnabled;
this.callback = callback;
if (HybridityUtils.isExplicitCameraPermissionRequired(fragment.requireActivity())) {
//noinspection deprecation
fragment.requestPermissions(new String[]{Manifest.permission.CAMERA}, CropImageConsts.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE);
} else {
//noinspection deprecation
fragment.startActivityForResult(HybridityUtils.getCameraIntent(fragment.requireActivity()), CropImageConsts.PICK_IMAGE_CHOOSER_REQUEST_CODE);
}
}
/**
* 启动图库选择器
*/
public void startGallery(Activity activity, boolean cropEnabled, @NonNull PickCallback callback) {
this.cropEnabled = cropEnabled;
this.callback = callback;
activity.startActivityForResult(HybridityUtils.getGalleryIntent(), CropImageConsts.PICK_IMAGE_CHOOSER_REQUEST_CODE);
}
/**
* 启动图库选择器
*/
public void startGallery(Fragment fragment, boolean cropEnabled, @NonNull PickCallback callback) {
this.cropEnabled = cropEnabled;
this.callback = callback;
//noinspection deprecation
fragment.startActivityForResult(HybridityUtils.getGalleryIntent(), CropImageConsts.PICK_IMAGE_CHOOSER_REQUEST_CODE);
}
/**
* 启动文件选择器
*/
public void startChooser(Activity activity, String mime, @NonNull PickCallback callback) {
this.cropEnabled = false;
this.callback = callback;
activity.startActivityForResult(HybridityUtils.getChooserIntent(mime), CropImageConsts.PICK_IMAGE_CHOOSER_REQUEST_CODE);
}
/**
* 启动文件选择器
*/
public void startChooser(Fragment fragment, String mime, @NonNull PickCallback callback) {
this.cropEnabled = false;
this.callback = callback;
//noinspection deprecation
fragment.startActivityForResult(HybridityUtils.getChooserIntent(mime), CropImageConsts.PICK_IMAGE_CHOOSER_REQUEST_CODE);
}
/**
* 图片选择/裁剪结果回调,在 {@link Activity#onActivityResult(int, int, Intent)} 中调用
*/
@SuppressWarnings("JavadocReference")
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
onActivityResultInner(activity, null, requestCode, resultCode, data);
}
/**
* 图片选择/裁剪结果回调,在 {@link Fragment#onActivityResult(int, int, Intent)} 中调用
*/
public void onActivityResult(Fragment fragment, int requestCode, int resultCode, Intent data) {
onActivityResultInner(null, fragment, requestCode, resultCode, data);
}
private void onActivityResultInner(Activity activity, Fragment fragment, int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
if (callback != null) {
callback.onCanceled();
}
return;
}
Context context;
if (activity != null) {
context = activity;
} else {
context = fragment.getActivity();
}
if (context != null && requestCode == CropImageConsts.PICK_IMAGE_CHOOSER_REQUEST_CODE) {
Uri pickImageUri = HybridityUtils.getPickImageResultUri(context, data);
// 检查读取文件权限
if (HybridityUtils.isReadExternalStoragePermissionsRequired(context, pickImageUri)) {
if (activity != null) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
CropImageConsts.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
} else {
//noinspection deprecation
fragment.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
CropImageConsts.PICK_IMAGE_PERMISSIONS_REQUEST_CODE);
}
} else {
// 选择图片回调
if (activity != null) {
handlePickImage(activity, null, pickImageUri);
} else {
handlePickImage(null, fragment, pickImageUri);
}
}
} else if (requestCode == CropImageConsts.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
// 裁剪图片回调
handleCropResult(context, data);
}
}
/**
* 授权结果回调,在 {@link Activity#onRequestPermissionsResult(int, String[], int[])} 中调用
*/
public void onRequestPermissionsResult(Activity activity, int requestCode, String[] permissions,
int[] grantResults) {
onRequestPermissionsResultInner(activity, null, requestCode, permissions, grantResults);
}
/**
* 授权结果回调,在 {@link Fragment#onRequestPermissionsResult(int, String[], int[])} 中调用
*/
public void onRequestPermissionsResult(Fragment fragment, int requestCode, String[] permissions,
int[] grantResults) {
onRequestPermissionsResultInner(null, fragment, requestCode, permissions, grantResults);
}
private void onRequestPermissionsResultInner(Activity activity, Fragment fragment, int requestCode,
String[] permissions, int[] grantResults) {
if (requestCode == CropImageConsts.CAMERA_CAPTURE_PERMISSIONS_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (activity != null) {
startCamera(activity, cropEnabled, callback);
} else {
startCamera(fragment, cropEnabled, callback);
}
} else {
if (callback != null) {
callback.onPermissionDenied(permissions, "没有相机权限");
}
}
} else if (requestCode == CropImageConsts.PICK_IMAGE_PERMISSIONS_REQUEST_CODE) {
if (cropImageUri != null
&& grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (activity != null) {
handlePickImage(activity, null, cropImageUri);
} else {
handlePickImage(null, fragment, cropImageUri);
}
} else {
if (callback != null) {
callback.onPermissionDenied(permissions, "没有外部存储权限");
}
}
}
}
/**
* 裁剪图片结果回调
*/
private void handleCropResult(Context context, Intent data) {
ActivityResult result = null;
if (data != null) {
result = data.getParcelableExtra(CropImageConsts.CROP_IMAGE_EXTRA_RESULT);
}
if (result == null) {
return;
}
Exception error = result.getError();
if (error == null) {
cropImageUri = result.getUri();
if (callback != null) {
callback.onCropImage(handleUri(context, cropImageUri));
}
} else {
if (callback != null) {
callback.onCropError(error);
}
}
}
/**
* 选择图片结果回调
*/
private void handlePickImage(Activity activity, Fragment fragment, Uri imageUri) {
if (callback != null) {
Context context;
if (activity != null) {
context = activity;
} else {
context = fragment.getContext();
}
callback.onPickImage(handleUri(context, imageUri));
}
if (!cropEnabled) {
return;
}
if (imageUri == null || imageUri.equals(Uri.EMPTY)) {
if (callback != null) {
callback.onCropError(new IllegalArgumentException("Uri is null or empty"));
}
return;
}
ActivityBuilder builder = new ActivityBuilder(imageUri);
if (callback != null) {
callback.cropConfig(builder);
}
if (activity != null) {
builder.start(activity);
} else {
builder.start(fragment);
}
}
/**
* 处理返回图片的 uri,content 协议自动转换 file 协议,避免 {@link FileNotFoundException}
*/
private Uri handleUri(Context context, Uri imageUri) {
if ("content".equals(imageUri.getScheme())) {
String realPath = HybridityUtils.getRealPathFromUri(context, imageUri);
if (!TextUtils.isEmpty(realPath)) {
return Uri.fromFile(new File(realPath));
}
}
return imageUri;
}
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/ImageProvider.java
================================================
package com.github.gzuliyujiang.imagepicker;
import androidx.core.content.FileProvider;
/**
* @author 贵州山魈羡民 (1032694760@qq.com)
* @since 2020/12/1 14:58
*/
public class ImageProvider extends FileProvider {
}
================================================
FILE: ImagePicker/src/main/java/com/github/gzuliyujiang/imagepicker/PickCallback.java
================================================
package com.github.gzuliyujiang.imagepicker;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2020/4/3 17:18
*/
public abstract class PickCallback {
/**
* 用户取消回调
*/
public void onCanceled() {
}
/**
* 用户拒绝授权回调
*/
public void onPermissionDenied(String[] permissions, String message) {
}
/**
* 图片选择回调
*/
public void onPickImage(@Nullable Uri imageUri) {
}
/**
* 图片裁剪配置
*/
public void cropConfig(ActivityBuilder builder) {
builder.setMultiTouchEnabled(false)
.setCropShape(CropImageView.CropShape.OVAL)
.setRequestedSize(640, 640)
.setAspectRatio(5, 5);
}
/**
* 图片裁剪回调
*/
public void onCropImage(@Nullable Uri imageUri) {
}
/**
* 图片裁剪出错
*/
public void onCropError(@NonNull Exception error) {
}
}
================================================
FILE: ImagePicker/src/main/res/layout/crop_image_activity.xml
================================================
================================================
FILE: ImagePicker/src/main/res/layout/crop_image_view.xml
================================================
================================================
FILE: ImagePicker/src/main/res/values/crop_image_attrs.xml
================================================
================================================
FILE: ImagePicker/src/main/res/xml/crop_image_paths.xml
================================================
================================================
FILE: LICENSE
================================================
木兰宽松许可证, 第2版
木兰宽松许可证, 第2版
2020年1月 http://license.coscl.org.cn/MulanPSL2
您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束:
0. 定义
“软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。
“贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。
“贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。
“法人实体”是指提交贡献的机构及其“关联实体”。
“关联实体”是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。
1. 授予版权许可
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。
2. 授予专利许可
每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。
3. 无商标许可
“本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。
4. 分发限制
您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。
5. 免责声明与责任限制
“软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。
6. 语言
“本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文版为准。
条款结束
如何将木兰宽松许可证,第2版,应用到您的软件
如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步:
1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字;
2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中;
3, 请将如下声明文本放入每个源文件的头部注释中。
Copyright (c) [Year] [name of copyright holder]
[Software Name] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
Mulan Permissive Software License,Version 2
Mulan Permissive Software License,Version 2 (Mulan PSL v2)
January 2020 http://license.coscl.org.cn/MulanPSL2
Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions:
0. Definition
Software means the program and related documents which are licensed under this License and comprise all Contribution(s).
Contribution means the copyrightable work licensed by a particular Contributor under this License.
Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License.
Legal Entity means the entity making a Contribution and all its Affiliates.
Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity.
1. Grant of Copyright License
Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not.
2. Grant of Patent License
Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken.
3. No Trademark License
No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4.
4. Distribution Restriction
You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software.
5. Disclaimer of Warranty and Limitation of Liability
THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
6. Language
THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL.
END OF THE TERMS AND CONDITIONS
How to Apply the Mulan Permissive Software License,Version 2 (Mulan PSL v2) to Your Software
To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps:
i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner;
ii Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package;
iii Attach the statement to the appropriate annotated syntax at the beginning of each source file.
Copyright (c) [Year] [name of copyright holder]
[Software Name] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
================================================
FILE: NOTICE
================================================
Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
The software is licensed under the Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
PURPOSE.
See the Mulan PSL v2 for more details.
================================================
FILE: README.md
================================================
# AndroidPicker
[](https://github.com/gzu-liyujiang/AndroidPicker)

安卓选择器类库,包括日期及时间选择器(可用于出生日期、营业时间等)、单项选择器(可用于性别、民族、职业、学历、星座等)、二三级联动选择器(可用于车牌号、基金定投日期等)、城市地址选择器(分省级、地市级及区县级)、数字选择器(可用于年龄、身高、体重、温度等)、日历选日期择器(可用于酒店及机票预定日期)、颜色选择器、文件及目录选择器等……
【抱歉!各位小伙伴,从2022年开始我已经没做安卓开发了,项目虽然已经趋于稳定,不过需要大家参与维护,多提PullRequest,我目前已经没法贡献代码了】
欢迎大伙儿在[Issues](https://github.com/gzu-liyujiang/AndroidPicker/issues)提交你的意见或建议。欢迎 Fork & Pull
requests 贡献您的代码,大家共同学习【[AndroidPicker 交流群 604235437](https://jq.qq.com/?_wv=1027&k=42bKOeD)】。
- GitHub:https://github.com/gzu-liyujiang/AndroidPicker
- 码云(GitEE):https://gitee.com/li_yu_jiang/AndroidPicker
- Demo:[https://github.com/gzu-liyujiang/AndroidPicker/blob/master/demo.apk](/demo.apk)
## 接入指引
**最新版本** :[](https://jitpack.io/#gzu-liyujiang/AndroidPicker)
(具体**历史版本号**参见 [更新日志](/ChangeLog.md))
### 注意事项
- 3.0.0 开始完全重构了底层代码,改进了性能,对 XML 布局更友好, 3.x 版本 的 API 和 1.x 及 2.x 版本的不大一样,**请谨慎升级**。
- [1.x Support 版本封存分支](https://github.com/gzu-liyujiang/AndroidPicker/tree/1.x-support)
- [2.0 androidx 版本封存分支](https://github.com/gzu-liyujiang/AndroidPicker/tree/2.0-androidx)
### 依赖配置
#### 在项目根目录下的`build.gradle`中
如果你的项目 Gradle 配置是在 7.0 以下,需要在 build.gradle 文件中加入:
```groovy
allprojects {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
```
如果你的 Gradle 配置是 7.0 及以上,则需要在 settings.gradle 文件中加入:
```groovy
dependencyResolutionManagement {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
```
#### 在项目模块下的`build.gradle`中(以下依赖项不必全部引入,请按需来)
所有选择器的基础窗体(用于自定义弹窗):
```groovy
dependencies {
implementation 'com.github.gzu-liyujiang.AndroidPicker:Common:'
}
```
滚轮选择器的滚轮控件(用于自定义滚轮选择器):
```groovy
dependencies {
implementation 'com.github.gzu-liyujiang.AndroidPicker:WheelView:'
}
```
单项/数字、二三级联动、日期/时间等滚轮选择器:
```groovy
dependencies {
implementation 'com.github.gzu-liyujiang.AndroidPicker:WheelPicker:'
}
```
省市区地址选择器:
```groovy
dependencies {
implementation 'com.github.gzu-liyujiang.AndroidPicker:AddressPicker:'
}
```
文件/目录选择器:
```groovy
dependencies {
implementation 'com.github.gzu-liyujiang.AndroidPicker:FilePicker:'
}
```
颜色选择器:
```groovy
dependencies {
implementation 'com.github.gzu-liyujiang.AndroidPicker:ColorPicker:'
}
```
日历日期选择器([README.md](/CalendarPicker/README.md)):
```groovy
dependencies {
implementation 'com.github.gzu-liyujiang.AndroidPicker:CalendarPicker:'
}
```
图片选择器([README.md](/ImagePicker/README.md)):
```groovy
dependencies {
implementation 'com.github.gzu-liyujiang.AndroidPicker:ImagePicker:'
}
```
旧版本 **AndroidX 稳定版本** (不推荐):
```groovy
dependencies {
implementation 'com.github.gzu-liyujiang.AndroidPicker:Common:2.0.0'
implementation 'com.github.gzu-liyujiang.AndroidPicker:WheelPicker:2.0.0'
implementation 'com.github.gzu-liyujiang.AndroidPicker:FilePicker:2.0.0'
implementation 'com.github.gzu-liyujiang.AndroidPicker:ColorPicker:2.0.0'
}
```
旧版本 **Support 稳定版本** (不推荐):
```groovy
dependencies {
implementation 'com.github.gzu-liyujiang.AndroidPicker:Common:1.5.6.20181018'
implementation 'com.github.gzu-liyujiang.AndroidPicker:WheelPicker:1.5.6.20181018'
implementation 'com.github.gzu-liyujiang.AndroidPicker:FilePicker:1.5.6.20181018'
implementation 'com.github.gzu-liyujiang.AndroidPicker:ColorPicker:1.5.6.20181018'
}
```
## API 说明
[API 说明文档](/API.md) 。
## 混淆规则
项目库混淆无需额外配置。
## 用法示例
常见用法请参阅 [demo](/app),**高级用法**请细读[源码](/WheelPicker), 诸如可以重写同名的`assets/china_address.json`来自定义省市区数据,
重写同名的`DialogSheetAnimation`来自定义弹窗动画……。 代码是最好的老师,强烈建议拉取代码运行,尝试修改 demo 对比查看实际效果以便加深理解。
### 在 Java 中
```groovy
List data = new ArrayList<>();
data.add(new GoodsCategoryBean(1, "食品生鲜"));
data.add(new GoodsCategoryBean(2, "家用电器"));
data.add(new GoodsCategoryBean(3, "家居生活"));
data.add(new GoodsCategoryBean(4, "医疗保健"));
data.add(new GoodsCategoryBean(5, "酒水饮料"));
data.add(new GoodsCategoryBean(6, "图书音像"));
OptionPicker picker = new OptionPicker(this);
picker.setTitle("货物分类");
picker.setBodyWidth(140);
picker.setData(data);
picker.setDefaultPosition(2);
picker.setOnOptionPickedListener(this);
//OptionWheelLayout wheelLayout = picker.getWheelLayout();
//wheelLayout.setIndicatorEnabled(false);
//wheelLayout.setTextColor(0xFFFF00FF);
//wheelLayout.setSelectedTextColor(0xFFFF0000);
//wheelLayout.setTextSize(15 * view.getResources().getDisplayMetrics().scaledDensity);
//wheelLayout.setSelectedTextBold(true);
//wheelLayout.setCurtainEnabled(true);
//wheelLayout.setCurtainColor(0xEEFF0000);
//wheelLayout.setCurtainCorner(CurtainCorner.ALL);
//wheelLayout.setCurtainRadius(5 * view.getResources().getDisplayMetrics().density);
//wheelLayout.setOnOptionSelectedListener(new OnOptionSelectedListener() {
// @Override
// public void onOptionSelected(int position, Object item) {
// picker.getTitleView().setText(picker.getWheelView().formatItem(position));
// }
//});
picker.show();
```
```groovy
DatePicker picker = new DatePicker(this);
//picker.setBodyWidth(240);
//DateWheelLayout wheelLayout = picker.getWheelLayout();
//wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY);
//wheelLayout.setDateLabel("年", "月", "日");
//wheelLayout.setDateFormatter(new UnitDateFormatter());
//wheelLayout.setRange(DateEntity.target(2021, 1, 1), DateEntity.target(2050, 12, 31), DateEntity.today());
//wheelLayout.setCurtainEnabled(true);
//wheelLayout.setCurtainColor(0xFFCC0000);
//wheelLayout.setIndicatorEnabled(true);
//wheelLayout.setIndicatorColor(0xFFFF0000);
//wheelLayout.setIndicatorSize(view.getResources().getDisplayMetrics().density * 2);
//wheelLayout.setTextColor(0xCCCC0000);
//wheelLayout.setSelectedTextColor(0xFFFF0000);
//wheelLayout.getYearLabelView().setTextColor(0xFF999999);
//wheelLayout.getMonthLabelView().setTextColor(0xFF999999);
picker.getWheelLayout().setResetWhenLinkage(false);
picker.setOnDatePickedListener(this);
picker.show();
```
```groovy
AddressPicker picker = new AddressPicker(this);
picker.setAddressMode(AddressMode.PROVINCE_CITY);
//picker.setAddressMode("china_address_guizhou_city.json", AddressMode.PROVINCE_CITY,
// new AddressJsonParser.Builder()
// .provinceCodeField("code")
// .provinceNameField("name")
// .provinceChildField("city")
// .cityCodeField("code")
// .cityNameField("name")
// .cityChildField("area")
// .countyCodeField("code")
// .countyNameField("name")
// .build());
//picker.setTitle("贵州省地址选择");
//picker.setDefaultValue("贵州省", "毕节市", "纳雍县");
picker.setOnAddressPickedListener(this);
//LinkageWheelLayout wheelLayout = picker.getWheelLayout();
//wheelLayout.setTextSize(15 * view.getResources().getDisplayMetrics().scaledDensity);
//wheelLayout.setSelectedTextBold(true);
//wheelLayout.setIndicatorEnabled(false);
//wheelLayout.setCurtainEnabled(true);
//wheelLayout.setCurtainColor(0xEE0081FF);
//wheelLayout.setCurtainRadius(5 * view.getResources().getDisplayMetrics().density);
//int padding = (int) (10 * view.getResources().getDisplayMetrics().density);
//wheelLayout.setPadding(padding, 0, padding, 0);
//wheelLayout.setOnLinkageSelectedListener(new OnLinkageSelectedListener() {
// @Override
// public void onLinkageSelected(Object first, Object second, Object third) {
// picker.getTitleView().setText(String.format("%s%s%s",
// picker.getProvinceWheelView().formatItem(first),
// picker.getCityWheelView().formatItem(second),
// picker.getCountyWheelView().formatItem(third)));
// }
//});
//picker.getProvinceWheelView().setCurtainCorner(CurtainCorner.LEFT);
//picker.getCityWheelView().setCurtainCorner(CurtainCorner.RIGHT);
picker.show();
```
### 在 XML 中
```xml
```
```xml
...
```
### 自定义样式(可选)
#### 全局配置所有选择器样式及配色
```java
//4.0.0版本开始内置支持四种弹窗样式(Default、One、Two、Three),效果可运行Demo查看
public class DemoApp extends Application {
@Override
public void onCreate() {
super.onCreate();
DialogConfig.setDialogStyle(DialogStyle.Default);
DialogConfig.setDialogColor(new DialogColor()
.cancelTextColor(0xFF999999)
.okTextColor(0xFF0099CC));
}
}
```
#### 自定义 style
- **调用`setStyle`**(只作用于当前选择器,推荐)
在`app/.../res/values/styles.xml`中参考`WheelDefault`写个style,然后设置。
```groovy
picker.getWheelView().setStyle(R.style.WheelStyleDemo);
```
- **重写`WheelDefault`覆盖** (所有选择器都会生效,不推荐)
在`app/.../res/values/styles.xml`中**重写`WheelDefault`覆盖** 。
```xml
```
#### 在Java中集成重写某一选择器样式及配色
```java
//仿蚂蚁财富APP定投周期选择弹窗样式
public class AntFortuneLikePicker extends LinkagePicker {
private int lastDialogStyle;
public AntFortuneLikePicker(@NonNull Activity activity) {
super(activity);
}
@Override
protected void onInit(@NonNull Context context) {
super.onInit(context);
lastDialogStyle = DialogConfig.getDialogStyle();
DialogConfig.setDialogStyle(DialogStyle.Default);
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
DialogConfig.setDialogStyle(lastDialogStyle);
}
@Override
protected void initData() {
super.initData();
setBackgroundColor(0xFFFFFFFF);
cancelView.setText("取消");
cancelView.setTextSize(16);
cancelView.setTextColor(0xFF0081FF);
okView.setTextColor(0xFF0081FF);
okView.setText("确定");
okView.setTextSize(16);
titleView.setTextColor(0xFF333333);
titleView.setText("定投周期");
titleView.setTextSize(16);
wheelLayout.setData(new AntFortuneLikeProvider());
wheelLayout.setAtmosphericEnabled(true);
wheelLayout.setVisibleItemCount(7);
wheelLayout.setCyclicEnabled(false);
wheelLayout.setIndicatorEnabled(true);
wheelLayout.setIndicatorColor(0xFFDDDDDD);
wheelLayout.setIndicatorSize((int) (contentView.getResources().getDisplayMetrics().density * 1));
wheelLayout.setTextColor(0xFF999999);
wheelLayout.setSelectedTextColor(0xFF333333);
wheelLayout.setCurtainEnabled(false);
wheelLayout.setCurvedEnabled(false);
}
}
````
### 和`DialogFragment`结合
`XXXPicker` 都继承自 `android.app.Dialog` ,因此可以直接和`androidx.fragment.app.DialogFragment`结合使用。
```java
public class OptionPickerFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
OptionPicker picker = new OptionPicker(requireActivity());
picker.setData("土人", "里民子", "羡民", "穿青人", "不在56个民族之内", "未定民族");
picker.setOnOptionPickedListener(new OnOptionPickedListener() {
@Override
public void onOptionPicked(int position, Object item) {
Toast.makeText(requireContext(), item.toString(), Toast.LENGTH_SHORT).show();
}
});
picker.getWheelView().setStyle(R.style.WheelStyleDemo);
return picker;
}
}
```
## 效果预览
以下图片显示的效果可能已修改过,实际效果请运行 demo 查看。
- 
- 
- 
- 
- 
- 
- 
- 
- 
## 特别鸣谢
- [基于 View 的 WheelView](https://github.com/weidongjian/androidWheelView)
- [基于 ListView 的 WheelView](https://github.com/venshine/WheelView)
- [基于 ScrollView 的 WheelView](https://github.com/wangjiegulu/WheelView)
- [SingleDateAndTimePicker](https://github.com/florent37/SingleDateAndTimePicker)
- [China_Province_City](https://github.com/small-dream/China_Province_City)
- [Administrative-divisions-of-China](https://github.com/modood/Administrative-divisions-of-China)
- [AndroidColorPicker](https://github.com/jbruchanov/AndroidColorPicker)
- [calendar](https://github.com/oxsource/calendar)
- [Android-Image-Cropper](https://github.com/ArthurHub/Android-Image-Cropper)
## 许可协议
声明:不授权给 [@q88qaz](https://github.com/q88qaz) 这种“吃人家粮还骂人家娘”的厚颜无耻的伸手党及其关联企业使用 。
### 3.0.0 之后
```text
Copyright (c) 2020-2021 gzu-liyujiang <1032694760@qq.com>
The software is licensed under the Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
PURPOSE.
See the Mulan PSL v2 for more details.
```
### 3.0.0 之前
```text
MIT License
Copyright (c) 穿青山魈人马
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
================================================
FILE: WheelPicker/README.md
================================================
# 滚轮选择器
滚轮选择器,包括单项选择器、数字选择器、二三级联动选择器、日期时间选择器、生日选择器、民族选择器等。
================================================
FILE: WheelPicker/build.gradle
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
apply from: "${rootDir}/gradle/library.gradle"
apply from: "${rootDir}/gradle/publish.gradle"
dependencies {
implementation androidxLibrary.annotation
api project(':Common')
api project(':WheelView')
}
================================================
FILE: WheelPicker/consumer-rules.pro
================================================
# 本库模块专用的混淆规则
================================================
FILE: WheelPicker/src/main/AndroidManifest.xml
================================================
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/BirthdayPicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker;
import android.app.Activity;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode;
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity;
import com.github.gzuliyujiang.wheelpicker.impl.BirthdayFormatter;
import java.util.Calendar;
/**
* 出生日期选择器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 14:31
* @since 2.0
*/
@SuppressWarnings("unused")
public class BirthdayPicker extends DatePicker {
private static final int MAX_AGE = 100;
private DateEntity defaultValue;
private boolean initialized = false;
public BirthdayPicker(@NonNull Activity activity) {
super(activity);
}
public BirthdayPicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@Override
protected void initData() {
super.initData();
initialized = true;
Calendar calendar = Calendar.getInstance();
int currentYear = calendar.get(Calendar.YEAR);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
DateEntity startValue = DateEntity.target(currentYear - MAX_AGE, 1, 1);
DateEntity endValue = DateEntity.target(currentYear, currentMonth, currentDay);
wheelLayout.setRange(startValue, endValue, defaultValue);
wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY);
wheelLayout.setDateFormatter(new BirthdayFormatter());
}
public void setDefaultValue(int year, int month, int day) {
defaultValue = DateEntity.target(year, month, day);
if (initialized) {
wheelLayout.setDefaultValue(defaultValue);
}
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/CarPlatePicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker;
import android.app.Activity;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.wheelpicker.contract.LinkageProvider;
import com.github.gzuliyujiang.wheelpicker.contract.OnCarPlatePickedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnLinkagePickedListener;
import com.github.gzuliyujiang.wheelpicker.widget.CarPlateWheelLayout;
/**
* 中国大陆车牌号滚轮选择
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2016/12/18 10:47
*/
@SuppressWarnings({"unused"})
public class CarPlatePicker extends LinkagePicker {
private OnCarPlatePickedListener onCarPlatePickedListener;
public CarPlatePicker(@NonNull Activity activity) {
super(activity);
}
public CarPlatePicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@Deprecated
@Override
public void setData(@NonNull LinkageProvider data) {
throw new UnsupportedOperationException("Data already preset");
}
@Deprecated
@Override
public void setOnLinkagePickedListener(OnLinkagePickedListener onLinkagePickedListener) {
throw new UnsupportedOperationException("Use setOnCarPlatePickedListener instead");
}
@NonNull
@Override
protected View createBodyView() {
wheelLayout = new CarPlateWheelLayout(activity);
return wheelLayout;
}
@Override
protected void onOk() {
if (onCarPlatePickedListener != null) {
String province = wheelLayout.getFirstWheelView().getCurrentItem();
String letter = wheelLayout.getSecondWheelView().getCurrentItem();
onCarPlatePickedListener.onCarNumberPicked(province, letter);
}
}
public void setOnCarPlatePickedListener(OnCarPlatePickedListener onCarPlatePickedListener) {
this.onCarPlatePickedListener = onCarPlatePickedListener;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/ConstellationPicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker;
import android.app.Activity;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.dialog.DialogLog;
import com.github.gzuliyujiang.wheelpicker.entity.ConstellationEntity;
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* 星座选择器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/17 13:10
*/
@SuppressWarnings("WeakerAccess")
public class ConstellationPicker extends OptionPicker {
public static String JSON = "[{\"id\":0,\"name\":\"不限\",\"startDate\":\"\",\"endDate\":\"\",\"english\":\"Unlimited\"},\n" +
"{\"id\":1,\"name\":\"白羊座\",\"startDate\":\"3-21\",\"endDate\":\"4-19\",\"english\":\"Aries\"},\n" +
"{\"id\":2,\"name\":\"金牛座\",\"startDate\":\"4-20\",\"endDate\":\"5-20\",\"english\":\"Taurus\"},\n" +
"{\"id\":3,\"name\":\"双子座\",\"startDate\":\"5-21\",\"endDate\":\"6-21\",\"english\":\"Gemini\"},\n" +
"{\"id\":4,\"name\":\"巨蟹座\",\"startDate\":\"6-22\",\"endDate\":\"7-22\",\"english\":\"Cancer\"},\n" +
"{\"id\":5,\"name\":\"狮子座\",\"startDate\":\"7-23\",\"endDate\":\"8-22\",\"english\":\"Leo\"},\n" +
"{\"id\":6,\"name\":\"处女座\",\"startDate\":\"8-23\",\"endDate\":\"9-22\",\"english\":\"Virgo\"},\n" +
"{\"id\":7,\"name\":\"天秤座\",\"startDate\":\"9-23\",\"endDate\":\"10-23\",\"english\":\"Libra\"},\n" +
"{\"id\":8,\"name\":\"天蝎座\",\"startDate\":\"10-24\",\"endDate\":\"11-22\",\"english\":\"Scorpio\"},\n" +
"{\"id\":9,\"name\":\"射手座\",\"startDate\":\"11-23\",\"endDate\":\"12-21\",\"english\":\"Sagittarius\"},\n" +
"{\"id\":10,\"name\":\"摩羯座\",\"startDate\":\"12-22\",\"endDate\":\"1-19\",\"english\":\"Capricorn\"},\n" +
"{\"id\":11,\"name\":\"水瓶座\",\"startDate\":\"1-20\",\"endDate\":\"2-18\",\"english\":\"Aquarius\"},\n" +
"{\"id\":12,\"name\":\"双鱼座\",\"startDate\":\"2-19\",\"endDate\":\"3-20\",\"english\":\"Pisces\"}]";
private boolean includeUnlimited = false;
public ConstellationPicker(Activity activity) {
super(activity);
}
public ConstellationPicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
public void setIncludeUnlimited(boolean includeUnlimited) {
this.includeUnlimited = includeUnlimited;
setData(provideData());
}
@Override
public void setDefaultValue(Object item) {
if (item instanceof String) {
setDefaultValueByName(item.toString());
} else {
super.setDefaultValue(item);
}
}
public void setDefaultValueById(String id) {
ConstellationEntity entity = new ConstellationEntity();
entity.setId(id);
super.setDefaultValue(entity);
}
public void setDefaultValueByName(String name) {
ConstellationEntity entity = new ConstellationEntity();
entity.setName(name);
super.setDefaultValue(entity);
}
public void setDefaultValueByDate(DateEntity date) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(date.toTimeInMillis());
setDefaultValueByDate(calendar.getTime());
}
public void setDefaultValueByDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
String name;
switch (month) {
case 1:
name = day < 21 ? "摩羯座" : "水瓶座";
break;
case 2:
name = day < 20 ? "水瓶座" : "双鱼座";
break;
case 3:
name = day < 21 ? "双鱼座" : "白羊座";
break;
case 4:
name = day < 21 ? "白羊座" : "金牛座";
break;
case 5:
name = day < 22 ? "金牛座" : "双子座";
break;
case 6:
name = day < 22 ? "双子座" : "巨蟹座";
break;
case 7:
name = day < 23 ? "巨蟹座" : "狮子座";
break;
case 8:
name = day < 24 ? "狮子座" : "处女座";
break;
case 9:
name = day < 24 ? "处女座" : "天秤座";
break;
case 10:
name = day < 24 ? "天秤座" : "天蝎座";
break;
case 11:
name = day < 23 ? "天蝎座" : "射手座";
break;
case 12:
name = day < 22 ? "射手座" : "摩羯座";
break;
default:
name = "不限";
break;
}
setDefaultValueByName(name);
}
public void setDefaultValueByEnglish(String english) {
ConstellationEntity entity = new ConstellationEntity();
entity.setEnglish(english);
super.setDefaultValue(entity);
}
@Override
protected List> provideData() {
ArrayList data = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(JSON);
for (int i = 0, n = jsonArray.length(); i < n; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
ConstellationEntity entity = new ConstellationEntity();
entity.setId(jsonObject.getString("id"));
entity.setStartDate(jsonObject.getString("startDate"));
entity.setEndDate(jsonObject.getString("endDate"));
entity.setName(jsonObject.getString("name"));
entity.setEnglish(jsonObject.getString("english"));
if (!includeUnlimited && "0".equals(entity.getId())) {
continue;
}
data.add(entity);
}
} catch (JSONException e) {
DialogLog.print(e);
}
return data;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/DatePicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker;
import android.app.Activity;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.dialog.ModalDialog;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatePickedListener;
import com.github.gzuliyujiang.wheelpicker.widget.DateWheelLayout;
/**
* 日期选择器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/5 18:17
*/
@SuppressWarnings("unused")
public class DatePicker extends ModalDialog {
protected DateWheelLayout wheelLayout;
private OnDatePickedListener onDatePickedListener;
public DatePicker(@NonNull Activity activity) {
super(activity);
}
public DatePicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@NonNull
@Override
protected View createBodyView() {
wheelLayout = new DateWheelLayout(activity);
return wheelLayout;
}
@Override
protected void onCancel() {
}
@Override
protected void onOk() {
if (onDatePickedListener != null) {
int year = wheelLayout.getSelectedYear();
int month = wheelLayout.getSelectedMonth();
int day = wheelLayout.getSelectedDay();
onDatePickedListener.onDatePicked(year, month, day);
}
}
public void setOnDatePickedListener(OnDatePickedListener onDatePickedListener) {
this.onDatePickedListener = onDatePickedListener;
}
public final DateWheelLayout getWheelLayout() {
return wheelLayout;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/DatimePicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker;
import android.app.Activity;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.dialog.ModalDialog;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatimePickedListener;
import com.github.gzuliyujiang.wheelpicker.widget.DatimeWheelLayout;
/**
* 日期时间选择器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/5 18:21
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public class DatimePicker extends ModalDialog {
protected DatimeWheelLayout wheelLayout;
private OnDatimePickedListener onDatimePickedListener;
public DatimePicker(@NonNull Activity activity) {
super(activity);
}
public DatimePicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@NonNull
@Override
protected View createBodyView() {
wheelLayout = new DatimeWheelLayout(activity);
return wheelLayout;
}
@Override
protected void onCancel() {
}
@Override
protected void onOk() {
if (onDatimePickedListener != null) {
int year = wheelLayout.getSelectedYear();
int month = wheelLayout.getSelectedMonth();
int day = wheelLayout.getSelectedDay();
int hour = wheelLayout.getSelectedHour();
int minute = wheelLayout.getSelectedMinute();
int second = wheelLayout.getSelectedSecond();
onDatimePickedListener.onDatimePicked(year, month, day, hour, minute, second);
}
}
public void setOnDatimePickedListener(OnDatimePickedListener onDatimePickedListener) {
this.onDatimePickedListener = onDatimePickedListener;
}
public final DatimeWheelLayout getWheelLayout() {
return wheelLayout;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/EthnicPicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker;
import android.app.Activity;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.dialog.DialogLog;
import com.github.gzuliyujiang.wheelpicker.annotation.EthnicSpec;
import com.github.gzuliyujiang.wheelpicker.entity.EthnicEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* 民族选择器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/12 13:50
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public class EthnicPicker extends OptionPicker {
public static String JSON = "[{\"code\":\"01\",\"name\":\"汉族\",\"spelling\":\"Han\"}," +
"{\"code\":\"02\",\"name\":\"蒙古族\",\"spelling\":\"Mongol\"}," +
"{\"code\":\"03\",\"name\":\"回族\",\"spelling\":\"Hui\"}," +
"{\"code\":\"04\",\"name\":\"藏族\",\"spelling\":\"Zang\"}," +
"{\"code\":\"05\",\"name\":\"维吾尔族\",\"spelling\":\"Uygur\"}," +
"{\"code\":\"06\",\"name\":\"苗族\",\"spelling\":\"Miao\"}," +
"{\"code\":\"07\",\"name\":\"彝族\",\"spelling\":\"Yi\"}," +
"{\"code\":\"08\",\"name\":\"壮族\",\"spelling\":\"Zhuang\"}," +
"{\"code\":\"09\",\"name\":\"布依族\",\"spelling\":\"Buyei\"}," +
"{\"code\":\"10\",\"name\":\"朝鲜族\",\"spelling\":\"Chosen\"}," +
"{\"code\":\"11\",\"name\":\"满族\",\"spelling\":\"Man\"}," +
"{\"code\":\"12\",\"name\":\"侗族\",\"spelling\":\"Dong\"}," +
"{\"code\":\"13\",\"name\":\"瑶族\",\"spelling\":\"Yao\"}," +
"{\"code\":\"14\",\"name\":\"白族\",\"spelling\":\"Bai\"}," +
"{\"code\":\"15\",\"name\":\"土家族\",\"spelling\":\"Tujia\"}," +
"{\"code\":\"16\",\"name\":\"哈尼族\",\"spelling\":\"Hani\"}," +
"{\"code\":\"17\",\"name\":\"哈萨克族\",\"spelling\":\"Kazak\"}," +
"{\"code\":\"18\",\"name\":\"傣族\",\"spelling\":\"Dai\"}," +
"{\"code\":\"19\",\"name\":\"黎族\",\"spelling\":\"Li\"}," +
"{\"code\":\"20\",\"name\":\"傈僳族\",\"spelling\":\"Lisu\"}," +
"{\"code\":\"21\",\"name\":\"佤族\",\"spelling\":\"Va\"}," +
"{\"code\":\"22\",\"name\":\"畲族\",\"spelling\":\"She\"}," +
"{\"code\":\"23\",\"name\":\"高山族\",\"spelling\":\"Gaoshan\"}," +
"{\"code\":\"24\",\"name\":\"拉祜族\",\"spelling\":\"Lahu\"}," +
"{\"code\":\"25\",\"name\":\"水族\",\"spelling\":\"Sui\"}," +
"{\"code\":\"26\",\"name\":\"东乡族\",\"spelling\":\"Dongxiang\"}," +
"{\"code\":\"27\",\"name\":\"纳西族\",\"spelling\":\"Naxi\"}," +
"{\"code\":\"28\",\"name\":\"景颇族\",\"spelling\":\"Jingpo\"}," +
"{\"code\":\"29\",\"name\":\"柯尔克孜族\",\"spelling\":\"Kirgiz\"}," +
"{\"code\":\"30\",\"name\":\"土族\",\"spelling\":\"Tu\"}," +
"{\"code\":\"31\",\"name\":\"达斡尔族\",\"spelling\":\"Daur\"}," +
"{\"code\":\"32\",\"name\":\"仫佬族\",\"spelling\":\"Mulao\"}," +
"{\"code\":\"33\",\"name\":\"羌族\",\"spelling\":\"Qiang\"}," +
"{\"code\":\"34\",\"name\":\"布朗族\",\"spelling\":\"Blang\"}," +
"{\"code\":\"35\",\"name\":\"撒拉族\",\"spelling\":\"Salar\"}," +
"{\"code\":\"36\",\"name\":\"毛难族\",\"spelling\":\"Maonan\"}," +
"{\"code\":\"37\",\"name\":\"仡佬族\",\"spelling\":\"Gelao\"}," +
"{\"code\":\"38\",\"name\":\"锡伯族\",\"spelling\":\"Xibe\"}," +
"{\"code\":\"39\",\"name\":\"阿昌族\",\"spelling\":\"Achang\"}," +
"{\"code\":\"40\",\"name\":\"普米族\",\"spelling\":\"Pumi\"}," +
"{\"code\":\"41\",\"name\":\"塔吉克族\",\"spelling\":\"Tajik\"}," +
"{\"code\":\"42\",\"name\":\"怒族\",\"spelling\":\"Nu\"}," +
"{\"code\":\"43\",\"name\":\"乌孜别克族\",\"spelling\":\"Uzbek\"}," +
"{\"code\":\"44\",\"name\":\"俄罗斯族\",\"spelling\":\"Russ\"}," +
"{\"code\":\"45\",\"name\":\"鄂温克族\",\"spelling\":\"Ewenki\"}," +
"{\"code\":\"46\",\"name\":\"德昂族\",\"spelling\":\"Deang\"}," +
"{\"code\":\"47\",\"name\":\"保安族\",\"spelling\":\"Bonan\"}," +
"{\"code\":\"48\",\"name\":\"裕固族\",\"spelling\":\"Yugur\"}," +
"{\"code\":\"49\",\"name\":\"京族\",\"spelling\":\"Gin\"}," +
"{\"code\":\"50\",\"name\":\"塔塔尔族\",\"spelling\":\"Tatar\"}," +
"{\"code\":\"51\",\"name\":\"独龙族\",\"spelling\":\"Derung\"}," +
"{\"code\":\"52\",\"name\":\"鄂伦春族\",\"spelling\":\"Oroqen\"}," +
"{\"code\":\"53\",\"name\":\"赫哲族\",\"spelling\":\"Hezhen\"}," +
"{\"code\":\"54\",\"name\":\"门巴族\",\"spelling\":\"Monba\"}," +
"{\"code\":\"55\",\"name\":\"珞巴族\",\"spelling\":\"Lhoba\"}," +
"{\"code\":\"56\",\"name\":\"基诺族\",\"spelling\":\"Jino\"}]";
private int ethnicSpec = EthnicSpec.DEFAULT;
public EthnicPicker(@NonNull Activity activity) {
super(activity);
}
public EthnicPicker(@NonNull Activity activity, int themeResId) {
super(activity, themeResId);
}
public void setEthnicSpec(@EthnicSpec int ethnicSpec) {
this.ethnicSpec = ethnicSpec;
setData(provideData());
}
@Override
public void setDefaultValue(Object item) {
if (item instanceof String) {
setDefaultValueByName(item.toString());
} else {
super.setDefaultValue(item);
}
}
public void setDefaultValueByCode(String code) {
EthnicEntity entity = new EthnicEntity();
entity.setCode(code);
super.setDefaultValue(entity);
}
public void setDefaultValueByName(String name) {
EthnicEntity entity = new EthnicEntity();
entity.setName(name);
super.setDefaultValue(entity);
}
public void setDefaultValueBySpelling(String spelling) {
EthnicEntity entity = new EthnicEntity();
entity.setSpelling(spelling);
super.setDefaultValue(entity);
}
@Override
protected List provideData() {
ArrayList data = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(JSON);
for (int i = 0, n = jsonArray.length(); i < n; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
EthnicEntity entity = new EthnicEntity();
entity.setCode(jsonObject.getString("code"));
entity.setName(jsonObject.getString("name"));
entity.setSpelling(jsonObject.getString("spelling"));
data.add(entity);
}
} catch (JSONException e) {
DialogLog.print(e);
}
switch (ethnicSpec) {
case EthnicSpec.DEFAULT:
EthnicEntity other = new EthnicEntity();
other.setCode("97");
other.setName("其他");
other.setSpelling("Other");
data.add(other);
EthnicEntity foreign = new EthnicEntity();
foreign.setCode("98");
foreign.setName("外国血统");
foreign.setSpelling("Foreign");
data.add(foreign);
break;
case EthnicSpec.SEVENTH_NATIONAL_CENSUS:
EthnicEntity unrecognized = new EthnicEntity();
unrecognized.setCode("97");
unrecognized.setName("未定族称人口");
unrecognized.setSpelling("Unrecognized");
data.add(unrecognized);
EthnicEntity naturalization = new EthnicEntity();
naturalization.setCode("98");
naturalization.setName("入籍");
naturalization.setSpelling("Naturalization");
data.add(naturalization);
break;
default:
break;
}
return data;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/LinkagePicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker;
import android.app.Activity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.dialog.ModalDialog;
import com.github.gzuliyujiang.wheelpicker.contract.LinkageProvider;
import com.github.gzuliyujiang.wheelpicker.contract.OnLinkagePickedListener;
import com.github.gzuliyujiang.wheelpicker.widget.LinkageWheelLayout;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
/**
* 二三级联动选择器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @see com.github.gzuliyujiang.wheelview.contract.TextProvider
* @see LinkageProvider
* @see LinkageWheelLayout
* @see OnLinkagePickedListener
* @since 2019/6/17 11:21
*/
@SuppressWarnings({"WeakerAccess", "unused"})
public class LinkagePicker extends ModalDialog {
protected LinkageWheelLayout wheelLayout;
private OnLinkagePickedListener onLinkagePickedListener;
public LinkagePicker(@NonNull Activity activity) {
super(activity);
}
public LinkagePicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@NonNull
@Override
protected View createBodyView() {
wheelLayout = new LinkageWheelLayout(activity);
return wheelLayout;
}
@Override
protected void onCancel() {
}
@Override
protected void onOk() {
if (onLinkagePickedListener != null) {
Object first = wheelLayout.getFirstWheelView().getCurrentItem();
Object second = wheelLayout.getSecondWheelView().getCurrentItem();
Object third = wheelLayout.getThirdWheelView().getCurrentItem();
onLinkagePickedListener.onLinkagePicked(first, second, third);
}
}
public void setData(@NonNull LinkageProvider data) {
wheelLayout.setData(data);
}
public void setDefaultValue(Object first, Object second, Object third) {
wheelLayout.setDefaultValue(first, second, third);
}
public void setOnLinkagePickedListener(OnLinkagePickedListener onLinkagePickedListener) {
this.onLinkagePickedListener = onLinkagePickedListener;
}
public final LinkageWheelLayout getWheelLayout() {
return wheelLayout;
}
public final WheelView getFirstWheelView() {
return wheelLayout.getFirstWheelView();
}
public final WheelView getSecondWheelView() {
return wheelLayout.getSecondWheelView();
}
public final WheelView getThirdWheelView() {
return wheelLayout.getThirdWheelView();
}
public final TextView getFirstLabelView() {
return wheelLayout.getFirstLabelView();
}
public final TextView getSecondLabelView() {
return wheelLayout.getSecondLabelView();
}
public final TextView getThirdLabelView() {
return wheelLayout.getThirdLabelView();
}
public final ProgressBar getLoadingView() {
return wheelLayout.getLoadingView();
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/NumberPicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.dialog.ModalDialog;
import com.github.gzuliyujiang.wheelpicker.contract.OnNumberPickedListener;
import com.github.gzuliyujiang.wheelpicker.widget.NumberWheelLayout;
import com.github.gzuliyujiang.wheelview.contract.WheelFormatter;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
/**
* 数字选择器
*
* @author 李玉江[QQ:1032694760]
* @since 2015/10/24
*/
@SuppressWarnings("unused")
public class NumberPicker extends ModalDialog {
protected NumberWheelLayout wheelLayout;
private OnNumberPickedListener onNumberPickedListener;
public NumberPicker(@NonNull Activity activity) {
super(activity);
}
public NumberPicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@NonNull
@Override
protected View createBodyView() {
wheelLayout = new NumberWheelLayout(activity);
return wheelLayout;
}
@Override
protected void onCancel() {
}
@Override
protected void onOk() {
if (onNumberPickedListener != null) {
int position = wheelLayout.getWheelView().getCurrentPosition();
Number item = wheelLayout.getWheelView().getCurrentItem();
onNumberPickedListener.onNumberPicked(position, item);
}
}
public void setFormatter(WheelFormatter formatter) {
wheelLayout.getWheelView().setFormatter(formatter);
}
public void setRange(int min, int max, int step) {
wheelLayout.setRange(min, max, step);
}
public void setRange(float min, float max, float step) {
wheelLayout.setRange(min, max, step);
}
public void setDefaultValue(Object item) {
wheelLayout.setDefaultValue(item);
}
public void setDefaultPosition(int position) {
wheelLayout.setDefaultPosition(position);
}
public final void setOnNumberPickedListener(OnNumberPickedListener onNumberPickedListener) {
this.onNumberPickedListener = onNumberPickedListener;
}
public final NumberWheelLayout getWheelLayout() {
return wheelLayout;
}
public final WheelView getWheelView() {
return wheelLayout.getWheelView();
}
public final TextView getLabelView() {
return wheelLayout.getLabelView();
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/OptionPicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.dialog.ModalDialog;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionPickedListener;
import com.github.gzuliyujiang.wheelpicker.widget.OptionWheelLayout;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
import java.util.Arrays;
import java.util.List;
/**
* 单项选择器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @see com.github.gzuliyujiang.wheelview.contract.TextProvider
* @since 2019/5/8 10:04
*/
@SuppressWarnings({"unused"})
public class OptionPicker extends ModalDialog {
protected OptionWheelLayout wheelLayout;
private OnOptionPickedListener onOptionPickedListener;
private boolean initialized = false;
private List> data;
private Object defaultValue;
private int defaultPosition = -1;
public OptionPicker(@NonNull Activity activity) {
super(activity);
}
public OptionPicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@NonNull
@Override
protected View createBodyView() {
wheelLayout = new OptionWheelLayout(activity);
return wheelLayout;
}
@Override
protected void initData() {
super.initData();
initialized = true;
if (data == null || data.size() == 0) {
data = provideData();
}
wheelLayout.setData(data);
if (defaultValue != null) {
wheelLayout.setDefaultValue(defaultValue);
}
if (defaultPosition != -1) {
wheelLayout.setDefaultPosition(defaultPosition);
}
}
@Override
protected void onCancel() {
}
@Override
protected void onOk() {
if (onOptionPickedListener != null) {
int position = wheelLayout.getWheelView().getCurrentPosition();
Object item = wheelLayout.getWheelView().getCurrentItem();
onOptionPickedListener.onOptionPicked(position, item);
}
}
protected List> provideData() {
return null;
}
public final boolean isInitialized() {
return initialized;
}
public void setData(Object... data) {
setData(Arrays.asList(data));
}
public void setData(List> data) {
this.data = data;
if (initialized) {
wheelLayout.setData(data);
}
}
public void setDefaultValue(Object item) {
this.defaultValue = item;
if (initialized) {
wheelLayout.setDefaultValue(item);
}
}
public void setDefaultPosition(int position) {
this.defaultPosition = position;
if (initialized) {
wheelLayout.setDefaultPosition(position);
}
}
public void setOnOptionPickedListener(OnOptionPickedListener onOptionPickedListener) {
this.onOptionPickedListener = onOptionPickedListener;
}
public final OptionWheelLayout getWheelLayout() {
return wheelLayout;
}
public final WheelView getWheelView() {
return wheelLayout.getWheelView();
}
public final TextView getLabelView() {
return wheelLayout.getLabelView();
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/PhoneCodePicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker;
import android.app.Activity;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.dialog.DialogLog;
import com.github.gzuliyujiang.wheelpicker.entity.PhoneCodeEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* 手机号前缀选择器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/10 16:44
*/
@SuppressWarnings("unused")
public class PhoneCodePicker extends OptionPicker {
public static String JSON = "[{\"prefix\":\"1\",\"en\":\"USA\",\"cn\":\"美国\"},\n" +
"{\"prefix\":\"1\",\"en\":\"PuertoRico\",\"cn\":\"波多黎各\"},\n" +
"{\"prefix\":\"1\",\"en\":\"Canada\",\"cn\":\"加拿大\"},\n" +
"{\"prefix\":\"7\",\"en\":\"Russia\",\"cn\":\"俄罗斯\"},\n" +
"{\"prefix\":\"7\",\"en\":\"Kazeakhstan\",\"cn\":\"哈萨克斯坦\"},\n" +
"{\"prefix\":\"20\",\"en\":\"Egypt\",\"cn\":\"埃及\"},\n" +
"{\"prefix\":\"27\",\"en\":\"South Africa\",\"cn\":\"南非\"},\n" +
"{\"prefix\":\"30\",\"en\":\"Greece\",\"cn\":\"希腊\"},\n" +
"{\"prefix\":\"31\",\"en\":\"Netherlands\",\"cn\":\"荷兰\"},\n" +
"{\"prefix\":\"32\",\"en\":\"Belgium\",\"cn\":\"比利时\"},\n" +
"{\"prefix\":\"33\",\"en\":\"France\",\"cn\":\"法国\"},\n" +
"{\"prefix\":\"34\",\"en\":\"Spain\",\"cn\":\"西班牙\"},\n" +
"{\"prefix\":\"36\",\"en\":\"Hungary\",\"cn\":\"匈牙利\"},\n" +
"{\"prefix\":\"40\",\"en\":\"Romania\",\"cn\":\"罗马尼亚\"},\n" +
"{\"prefix\":\"41\",\"en\":\"Switzerland\",\"cn\":\"瑞士\"},\n" +
"{\"prefix\":\"43\",\"en\":\"Austria\",\"cn\":\"奥地利\"},\n" +
"{\"prefix\":\"44\",\"en\":\"United Kingdom\",\"cn\":\"英国\"},\n" +
"{\"prefix\":\"44\",\"en\":\"Jersey\",\"cn\":\"泽西岛\"},\n" +
"{\"prefix\":\"44\",\"en\":\"Isle of Man\",\"cn\":\"马恩岛\"},\n" +
"{\"prefix\":\"44\",\"en\":\"Guernsey\",\"cn\":\"根西\"},\n" +
"{\"prefix\":\"45\",\"en\":\"Denmark\",\"cn\":\"丹麦\"},\n" +
"{\"prefix\":\"46\",\"en\":\"Sweden\",\"cn\":\"瑞典\"},\n" +
"{\"prefix\":\"47\",\"en\":\"Norway\",\"cn\":\"挪威\"},\n" +
"{\"prefix\":\"48\",\"en\":\"Poland\",\"cn\":\"波兰\"},\n" +
"{\"prefix\":\"51\",\"en\":\"Peru\",\"cn\":\"秘鲁\"},\n" +
"{\"prefix\":\"52\",\"en\":\"Mexico\",\"cn\":\"墨西哥\"},\n" +
"{\"prefix\":\"53\",\"en\":\"Cuba\",\"cn\":\"古巴\"},\n" +
"{\"prefix\":\"54\",\"en\":\"Argentina\",\"cn\":\"阿根廷\"},\n" +
"{\"prefix\":\"55\",\"en\":\"Brazill\",\"cn\":\"巴西\"},\n" +
"{\"prefix\":\"56\",\"en\":\"Chile\",\"cn\":\"智利\"},\n" +
"{\"prefix\":\"57\",\"en\":\"Colombia\",\"cn\":\"哥伦比亚\"},\n" +
"{\"prefix\":\"58\",\"en\":\"Venezuela\",\"cn\":\"委内瑞拉\"},\n" +
"{\"prefix\":\"60\",\"en\":\"Malaysia\",\"cn\":\"马来西亚\"},\n" +
"{\"prefix\":\"61\",\"en\":\"Australia\",\"cn\":\"澳大利亚\"},\n" +
"{\"prefix\":\"62\",\"en\":\"Indonesia\",\"cn\":\"印度尼西亚\"},\n" +
"{\"prefix\":\"63\",\"en\":\"Philippines\",\"cn\":\"菲律宾\"},\n" +
"{\"prefix\":\"64\",\"en\":\"NewZealand\",\"cn\":\"新西兰\"},\n" +
"{\"prefix\":\"65\",\"en\":\"Singapore\",\"cn\":\"新加坡\"},\n" +
"{\"prefix\":\"66\",\"en\":\"Thailand\",\"cn\":\"泰国\"},\n" +
"{\"prefix\":\"81\",\"en\":\"Japan\",\"cn\":\"日本\"},\n" +
"{\"prefix\":\"82\",\"en\":\"Korea\",\"cn\":\"韩国\"},\n" +
"{\"prefix\":\"84\",\"en\":\"Vietnam\",\"cn\":\"越南\"},\n" +
"{\"prefix\":\"86\",\"en\":\"China\",\"cn\":\"中国\"},\n" +
"{\"prefix\":\"90\",\"en\":\"Turkey\",\"cn\":\"土耳其\"},\n" +
"{\"prefix\":\"91\",\"en\":\"Indea\",\"cn\":\"印度\"},\n" +
"{\"prefix\":\"92\",\"en\":\"Pakistan\",\"cn\":\"巴基斯坦\"},\n" +
"{\"prefix\":\"93\",\"en\":\"Italy\",\"cn\":\"意大利\"},\n" +
"{\"prefix\":\"93\",\"en\":\"Afghanistan\",\"cn\":\"阿富汗\"},\n" +
"{\"prefix\":\"94\",\"en\":\"SriLanka\",\"cn\":\"斯里兰卡\"},\n" +
"{\"prefix\":\"94\",\"en\":\"Germany\",\"cn\":\"德国\"},\n" +
"{\"prefix\":\"95\",\"en\":\"Myanmar\",\"cn\":\"缅甸\"},\n" +
"{\"prefix\":\"98\",\"en\":\"Iran\",\"cn\":\"伊朗\"},\n" +
"{\"prefix\":\"212\",\"en\":\"Morocco\",\"cn\":\"摩洛哥\"},\n" +
"{\"prefix\":\"213\",\"en\":\"Algera\",\"cn\":\"阿尔格拉\"},\n" +
"{\"prefix\":\"216\",\"en\":\"Tunisia\",\"cn\":\"突尼斯\"},\n" +
"{\"prefix\":\"218\",\"en\":\"Libya\",\"cn\":\"利比亚\"},\n" +
"{\"prefix\":\"220\",\"en\":\"Gambia\",\"cn\":\"冈比亚\"},\n" +
"{\"prefix\":\"221\",\"en\":\"Senegal\",\"cn\":\"塞内加尔\"},\n" +
"{\"prefix\":\"222\",\"en\":\"Mauritania\",\"cn\":\"毛里塔尼亚\"},\n" +
"{\"prefix\":\"223\",\"en\":\"Mali\",\"cn\":\"马里\"},\n" +
"{\"prefix\":\"224\",\"en\":\"Guinea\",\"cn\":\"几内亚\"},\n" +
"{\"prefix\":\"225\",\"en\":\"Cote divoire\",\"cn\":\"科特迪沃\"},\n" +
"{\"prefix\":\"226\",\"en\":\"Burkina Faso\",\"cn\":\"布基纳法索\"},\n" +
"{\"prefix\":\"227\",\"en\":\"Niger\",\"cn\":\"尼日尔\"},\n" +
"{\"prefix\":\"228\",\"en\":\"Togo\",\"cn\":\"多哥\"},\n" +
"{\"prefix\":\"229\",\"en\":\"Benin\",\"cn\":\"贝宁\"},\n" +
"{\"prefix\":\"230\",\"en\":\"Mauritius\",\"cn\":\"毛里求斯\"},\n" +
"{\"prefix\":\"231\",\"en\":\"Liberia\",\"cn\":\"利比里亚\"},\n" +
"{\"prefix\":\"232\",\"en\":\"Sierra Leone\",\"cn\":\"塞拉利昂\"},\n" +
"{\"prefix\":\"233\",\"en\":\"Ghana\",\"cn\":\"加纳\"},\n" +
"{\"prefix\":\"234\",\"en\":\"Nigeria\",\"cn\":\"尼日利亚\"},\n" +
"{\"prefix\":\"235\",\"en\":\"Chad\",\"cn\":\"乍得\"},\n" +
"{\"prefix\":\"236\",\"en\":\"Central African Republic\",\"cn\":\"中非共和国\"},\n" +
"{\"prefix\":\"237\",\"en\":\"Cameroon\",\"cn\":\"喀麦隆\"},\n" +
"{\"prefix\":\"238\",\"en\":\"Cape Verde\",\"cn\":\"佛得角\"},\n" +
"{\"prefix\":\"239\",\"en\":\"Sao Tome and Principe\",\"cn\":\"圣多美和普林西比\"},\n" +
"{\"prefix\":\"240\",\"en\":\"Guinea\",\"cn\":\"几内亚\"},\n" +
"{\"prefix\":\"241\",\"en\":\"Gabon\",\"cn\":\"加蓬\"},\n" +
"{\"prefix\":\"242\",\"en\":\"Republic of the Congo\",\"cn\":\"刚果共和国\"},\n" +
"{\"prefix\":\"243\",\"en\":\"Democratic Republic of the Congo\",\"cn\":\"刚果民主共和国\"},\n" +
"{\"prefix\":\"244\",\"en\":\"Angola\",\"cn\":\"安哥拉\"},\n" +
"{\"prefix\":\"247\",\"en\":\"Ascension\",\"cn\":\"阿森松岛\"},\n" +
"{\"prefix\":\"248\",\"en\":\"Seychelles\",\"cn\":\"塞舌尔\"},\n" +
"{\"prefix\":\"249\",\"en\":\"Sudan\",\"cn\":\"苏丹\"},\n" +
"{\"prefix\":\"250\",\"en\":\"Rwanda\",\"cn\":\"卢旺达\"},\n" +
"{\"prefix\":\"251\",\"en\":\"Ethiopia\",\"cn\":\"埃塞俄比亚\"},\n" +
"{\"prefix\":\"253\",\"en\":\"Djibouti\",\"cn\":\"吉布提\"},\n" +
"{\"prefix\":\"254\",\"en\":\"Kenya\",\"cn\":\"肯尼亚\"},\n" +
"{\"prefix\":\"255\",\"en\":\"Tanzania\",\"cn\":\"坦桑尼亚\"},\n" +
"{\"prefix\":\"256\",\"en\":\"Uganda\",\"cn\":\"乌干达\"},\n" +
"{\"prefix\":\"257\",\"en\":\"Burundi\",\"cn\":\"布隆迪\"},\n" +
"{\"prefix\":\"258\",\"en\":\"Mozambique\",\"cn\":\"莫桑比克\"},\n" +
"{\"prefix\":\"260\",\"en\":\"Zambia\",\"cn\":\"赞比亚\"},\n" +
"{\"prefix\":\"261\",\"en\":\"Madagascar\",\"cn\":\"马达加斯加\"},\n" +
"{\"prefix\":\"262\",\"en\":\"Reunion\",\"cn\":\"留尼汪\"},\n" +
"{\"prefix\":\"262\",\"en\":\"Mayotte\",\"cn\":\"马约特\"},\n" +
"{\"prefix\":\"263\",\"en\":\"Zimbabwe\",\"cn\":\"津巴布韦\"},\n" +
"{\"prefix\":\"264\",\"en\":\"Namibia\",\"cn\":\"纳米比亚\"},\n" +
"{\"prefix\":\"265\",\"en\":\"Malawi\",\"cn\":\"马拉维\"},\n" +
"{\"prefix\":\"266\",\"en\":\"Lesotho\",\"cn\":\"莱索托\"},\n" +
"{\"prefix\":\"267\",\"en\":\"Botwana\",\"cn\":\"博茨瓦纳\"},\n" +
"{\"prefix\":\"268\",\"en\":\"Swaziland\",\"cn\":\"斯威士兰\"},\n" +
"{\"prefix\":\"269\",\"en\":\"Comoros\",\"cn\":\"科摩罗\"},\n" +
"{\"prefix\":\"297\",\"en\":\"Aruba\",\"cn\":\"阿鲁巴\"},\n" +
"{\"prefix\":\"298\",\"en\":\"Faroe Islands\",\"cn\":\"法罗群岛\"},\n" +
"{\"prefix\":\"299\",\"en\":\"Greenland\",\"cn\":\"格陵兰\"},\n" +
"{\"prefix\":\"350\",\"en\":\"Gibraltar\",\"cn\":\"直布罗陀\"},\n" +
"{\"prefix\":\"351\",\"en\":\"Portugal\",\"cn\":\"葡萄牙\"},\n" +
"{\"prefix\":\"352\",\"en\":\"Luxembourg\",\"cn\":\"卢森堡\"},\n" +
"{\"prefix\":\"353\",\"en\":\"Ireland\",\"cn\":\"爱尔兰\"},\n" +
"{\"prefix\":\"354\",\"en\":\"Iceland\",\"cn\":\"冰岛\"},\n" +
"{\"prefix\":\"355\",\"en\":\"Albania\",\"cn\":\"阿尔巴尼亚\"},\n" +
"{\"prefix\":\"356\",\"en\":\"Malta\",\"cn\":\"马耳他\"},\n" +
"{\"prefix\":\"357\",\"en\":\"Cyprus\",\"cn\":\"塞浦路斯\"},\n" +
"{\"prefix\":\"358\",\"en\":\"Finland\",\"cn\":\"芬兰\"},\n" +
"{\"prefix\":\"359\",\"en\":\"Bulgaria\",\"cn\":\"保加利亚\"},\n" +
"{\"prefix\":\"370\",\"en\":\"Lithuania\",\"cn\":\"立陶宛\"},\n" +
"{\"prefix\":\"371\",\"en\":\"Latvia\",\"cn\":\"拉脱维亚\"},\n" +
"{\"prefix\":\"372\",\"en\":\"Estonia\",\"cn\":\"爱沙尼亚\"},\n" +
"{\"prefix\":\"373\",\"en\":\"Moldova\",\"cn\":\"摩尔多瓦\"},\n" +
"{\"prefix\":\"374\",\"en\":\"Armenia\",\"cn\":\"亚美尼亚\"},\n" +
"{\"prefix\":\"375\",\"en\":\"Belarus\",\"cn\":\"白俄罗斯\"},\n" +
"{\"prefix\":\"376\",\"en\":\"Andorra\",\"cn\":\"安道尔\"},\n" +
"{\"prefix\":\"377\",\"en\":\"Monaco\",\"cn\":\"摩纳哥\"},\n" +
"{\"prefix\":\"378\",\"en\":\"San Marino\",\"cn\":\"圣马力诺\"},\n" +
"{\"prefix\":\"380\",\"en\":\"Ukraine\",\"cn\":\"乌克兰\"},\n" +
"{\"prefix\":\"381\",\"en\":\"Serbia\",\"cn\":\"塞尔维亚\"},\n" +
"{\"prefix\":\"382\",\"en\":\"Montenegro\",\"cn\":\"黑山\"},\n" +
"{\"prefix\":\"383\",\"en\":\"Kosovo\",\"cn\":\"科索沃\"},\n" +
"{\"prefix\":\"385\",\"en\":\"Croatia\",\"cn\":\"克罗地亚\"},\n" +
"{\"prefix\":\"386\",\"en\":\"Slovenia\",\"cn\":\"斯洛文尼亚\"},\n" +
"{\"prefix\":\"387\",\"en\":\"Bosnia and Herzegovina\",\"cn\":\"波斯尼亚和黑塞哥维那\"},\n" +
"{\"prefix\":\"389\",\"en\":\"Macedonia\",\"cn\":\"马其顿\"},\n" +
"{\"prefix\":\"420\",\"en\":\"Czech Republic\",\"cn\":\"捷克共和国\"},\n" +
"{\"prefix\":\"421\",\"en\":\"Slovakia\",\"cn\":\"斯洛伐克\"},\n" +
"{\"prefix\":\"423\",\"en\":\"Liechtenstein\",\"cn\":\"列支敦士登\"},\n" +
"{\"prefix\":\"501\",\"en\":\"Belize\",\"cn\":\"伯利兹\"},\n" +
"{\"prefix\":\"502\",\"en\":\"Guatemala\",\"cn\":\"危地马拉\"},\n" +
"{\"prefix\":\"503\",\"en\":\"EISalvador\",\"cn\":\"艾萨尔瓦多\"},\n" +
"{\"prefix\":\"504\",\"en\":\"Honduras\",\"cn\":\"洪都拉斯\"},\n" +
"{\"prefix\":\"505\",\"en\":\"Nicaragua\",\"cn\":\"尼加拉瓜\"},\n" +
"{\"prefix\":\"506\",\"en\":\"Costa Rica\",\"cn\":\"哥斯达黎加\"},\n" +
"{\"prefix\":\"507\",\"en\":\"Panama\",\"cn\":\"巴拿马\"},\n" +
"{\"prefix\":\"509\",\"en\":\"Haiti\",\"cn\":\"海地\"},\n" +
"{\"prefix\":\"590\",\"en\":\"Guadeloupe\",\"cn\":\"瓜德罗普\"},\n" +
"{\"prefix\":\"591\",\"en\":\"Bolivia\",\"cn\":\"玻利维亚\"},\n" +
"{\"prefix\":\"592\",\"en\":\"Guyana\",\"cn\":\"圭亚那\"},\n" +
"{\"prefix\":\"593\",\"en\":\"Ecuador\",\"cn\":\"厄瓜多尔\"},\n" +
"{\"prefix\":\"594\",\"en\":\"French Guiana\",\"cn\":\"法属圭亚那\"},\n" +
"{\"prefix\":\"595\",\"en\":\"Paraguay\",\"cn\":\"巴拉圭\"},\n" +
"{\"prefix\":\"596\",\"en\":\"Martinique\",\"cn\":\"马提尼克\"},\n" +
"{\"prefix\":\"597\",\"en\":\"Suriname\",\"cn\":\"苏里南\"},\n" +
"{\"prefix\":\"598\",\"en\":\"Uruguay\",\"cn\":\"乌拉圭\"},\n" +
"{\"prefix\":\"599\",\"en\":\"Netherlands Antillse\",\"cn\":\"荷属安的列斯\"},\n" +
"{\"prefix\":\"670\",\"en\":\"Timor Leste\",\"cn\":\"东帝汶\"},\n" +
"{\"prefix\":\"673\",\"en\":\"Brunei\",\"cn\":\"文莱\"},\n" +
"{\"prefix\":\"675\",\"en\":\"Papua New Guinea\",\"cn\":\"巴布亚新几内亚\"},\n" +
"{\"prefix\":\"676\",\"en\":\"Tonga\",\"cn\":\"汤加\"},\n" +
"{\"prefix\":\"678\",\"en\":\"Vanuatu\",\"cn\":\"瓦努阿图\"},\n" +
"{\"prefix\":\"679\",\"en\":\"Fiji\",\"cn\":\"斐济\"},\n" +
"{\"prefix\":\"682\",\"en\":\"Cook Islands\",\"cn\":\"库克群岛\"},\n" +
"{\"prefix\":\"684\",\"en\":\"Samoa Eastern\",\"cn\":\"萨摩亚东部\"},\n" +
"{\"prefix\":\"685\",\"en\":\"Samoa Western\",\"cn\":\"萨摩亚西部\"},\n" +
"{\"prefix\":\"687\",\"en\":\"New Caledonia\",\"cn\":\"新喀里多尼亚\"},\n" +
"{\"prefix\":\"689\",\"en\":\"French Polynesia\",\"cn\":\"法属波利尼西亚\"},\n" +
"{\"prefix\":\"852\",\"en\":\"Hong Kong\",\"cn\":\"香港\"},\n" +
"{\"prefix\":\"853\",\"en\":\"Macao\",\"cn\":\"澳门\"},\n" +
"{\"prefix\":\"855\",\"en\":\"Cambodia\",\"cn\":\"柬埔寨\"},\n" +
"{\"prefix\":\"856\",\"en\":\"Laos\",\"cn\":\"老挝\"},\n" +
"{\"prefix\":\"880\",\"en\":\"Bangladesh\",\"cn\":\"孟加拉国\"},\n" +
"{\"prefix\":\"886\",\"en\":\"Taiwan\",\"cn\":\"台湾\"},\n" +
"{\"prefix\":\"960\",\"en\":\"Maldives\",\"cn\":\"马尔代夫\"},\n" +
"{\"prefix\":\"961\",\"en\":\"Lebanon\",\"cn\":\"黎巴嫩\"},\n" +
"{\"prefix\":\"962\",\"en\":\"Jordan\",\"cn\":\"约旦\"},\n" +
"{\"prefix\":\"963\",\"en\":\"Syria\",\"cn\":\"叙利亚\"},\n" +
"{\"prefix\":\"964\",\"en\":\"Iraq\",\"cn\":\"伊拉克\"},\n" +
"{\"prefix\":\"965\",\"en\":\"Kuwait\",\"cn\":\"科威特\"},\n" +
"{\"prefix\":\"966\",\"en\":\"Saudi Arabia\",\"cn\":\"沙特阿拉伯\"},\n" +
"{\"prefix\":\"967\",\"en\":\"Yemen\",\"cn\":\"也门\"},\n" +
"{\"prefix\":\"968\",\"en\":\"Oman\",\"cn\":\"阿曼\"},\n" +
"{\"prefix\":\"970\",\"en\":\"Palestinian\",\"cn\":\"巴勒斯坦\"},\n" +
"{\"prefix\":\"971\",\"en\":\"United Arab Emirates\",\"cn\":\"阿拉伯联合酋长国\"},\n" +
"{\"prefix\":\"972\",\"en\":\"Israel\",\"cn\":\"以色列\"},\n" +
"{\"prefix\":\"973\",\"en\":\"Bahrain\",\"cn\":\"巴林\"},\n" +
"{\"prefix\":\"974\",\"en\":\"Qotar\",\"cn\":\"库塔\"},\n" +
"{\"prefix\":\"975\",\"en\":\"Bhutan\",\"cn\":\"不丹\"},\n" +
"{\"prefix\":\"976\",\"en\":\"Mongolia\",\"cn\":\"蒙古\"},\n" +
"{\"prefix\":\"977\",\"en\":\"Nepal\",\"cn\":\"尼泊尔\"},\n" +
"{\"prefix\":\"992\",\"en\":\"Tajikistan\",\"cn\":\"塔吉克斯坦\"},\n" +
"{\"prefix\":\"993\",\"en\":\"Turkmenistan\",\"cn\":\"土库曼斯坦\"},\n" +
"{\"prefix\":\"994\",\"en\":\"Azerbaijan\",\"cn\":\"阿塞拜疆\"},\n" +
"{\"prefix\":\"995\",\"en\":\"Georgia\",\"cn\":\"格鲁吉亚\"},\n" +
"{\"prefix\":\"996\",\"en\":\"Kyrgyzstan\",\"cn\":\"吉尔吉斯斯坦\"},\n" +
"{\"prefix\":\"998\",\"en\":\"Uzbekistan\",\"cn\":\"乌兹别克斯坦\"},\n" +
"{\"prefix\":\"1242\",\"en\":\"Bahamas\",\"cn\":\"巴哈马\"},\n" +
"{\"prefix\":\"1246\",\"en\":\"Barbados\",\"cn\":\"巴巴多斯\"},\n" +
"{\"prefix\":\"1264\",\"en\":\"Anguilla\",\"cn\":\"安圭拉\"},\n" +
"{\"prefix\":\"1268\",\"en\":\"Antigua and Barbuda\",\"cn\":\"安提瓜和巴布达\"},\n" +
"{\"prefix\":\"1340\",\"en\":\"Virgin Islands\",\"cn\":\"维尔京群岛\"},\n" +
"{\"prefix\":\"1345\",\"en\":\"Cayman Islands\",\"cn\":\"开曼群岛\"},\n" +
"{\"prefix\":\"1441\",\"en\":\"Bermuda\",\"cn\":\"百慕大\"},\n" +
"{\"prefix\":\"1473\",\"en\":\"Grenada\",\"cn\":\"格林纳达\"},\n" +
"{\"prefix\":\"1649\",\"en\":\"Turks and Caicos Islands\",\"cn\":\"特克斯和凯科斯群岛\"},\n" +
"{\"prefix\":\"1664\",\"en\":\"Montserrat\",\"cn\":\"蒙特塞拉特\"},\n" +
"{\"prefix\":\"1671\",\"en\":\"Guam\",\"cn\":\"关岛\"},\n" +
"{\"prefix\":\"1758\",\"en\":\"St.Lucia\",\"cn\":\"圣卢西亚\"},\n" +
"{\"prefix\":\"1767\",\"en\":\"Dominica\",\"cn\":\"多米尼加\"},\n" +
"{\"prefix\":\"1784\",\"en\":\"St.Vincent\",\"cn\":\"圣文森特\"},\n" +
"{\"prefix\":\"1809\",\"en\":\"Dominican Republic\",\"cn\":\"多米尼加共和国\"},\n" +
"{\"prefix\":\"1868\",\"en\":\"Trinidad and Tobago\",\"cn\":\"特立尼达和多巴哥\"},\n" +
"{\"prefix\":\"1869\",\"en\":\"St Kitts and Nevis\",\"cn\":\"圣基茨和尼维斯\"},\n" +
"{\"prefix\":\"1876\",\"en\":\"Jamaica\",\"cn\":\"牙买加\"}]";
private boolean onlyChina = false;
public PhoneCodePicker(@NonNull Activity activity) {
super(activity);
}
public PhoneCodePicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
public void setOnlyChina(boolean onlyChina) {
this.onlyChina = onlyChina;
setData(provideData());
}
@Override
public void setDefaultValue(Object item) {
if (item instanceof String) {
setDefaultValueByName(item.toString());
} else {
super.setDefaultValue(item);
}
}
public void setDefaultValueByCode(String code) {
PhoneCodeEntity entity = new PhoneCodeEntity();
entity.setCode(code);
super.setDefaultValue(entity);
}
public void setDefaultValueByName(String name) {
PhoneCodeEntity entity = new PhoneCodeEntity();
entity.setName(name);
super.setDefaultValue(entity);
}
public void setDefaultValueByEnglish(String english) {
PhoneCodeEntity entity = new PhoneCodeEntity();
entity.setEnglish(english);
super.setDefaultValue(entity);
}
@Override
protected List> provideData() {
List data = new ArrayList<>();
if (onlyChina) {
PhoneCodeEntity china = new PhoneCodeEntity();
china.setCode("+86");
china.setName("中国大陆+86");
china.setEnglish("Chinese Mainland");
data.add(china);
PhoneCodeEntity hongKong = new PhoneCodeEntity();
hongKong.setCode("+852");
hongKong.setName("香港+852");
hongKong.setEnglish("Hong Kong");
data.add(hongKong);
PhoneCodeEntity macao = new PhoneCodeEntity();
macao.setCode("+853");
macao.setName("澳门+853");
macao.setEnglish("Macao");
data.add(macao);
PhoneCodeEntity taiwan = new PhoneCodeEntity();
taiwan.setCode("+886");
taiwan.setName("台湾+886");
taiwan.setEnglish("Taiwan");
data.add(taiwan);
} else {
try {
JSONArray jsonArray = new JSONArray(JSON);
for (int i = 0, n = jsonArray.length(); i < n; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
PhoneCodeEntity entity = new PhoneCodeEntity();
entity.setCode("+" + jsonObject.getString("prefix"));
entity.setName(jsonObject.getString("cn"));
entity.setEnglish(jsonObject.getString("en"));
data.add(entity);
}
} catch (JSONException e) {
DialogLog.print(e);
}
}
return data;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/SexPicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker;
import android.app.Activity;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.dialog.DialogLog;
import com.github.gzuliyujiang.wheelpicker.entity.SexEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* 性别选择器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/23 11:48
*/
@SuppressWarnings("WeakerAccess")
public class SexPicker extends OptionPicker {
public static String JSON = "[{\"id\":0,\"name\":\"保密\",\"english\":\"Secrecy\"},\n" +
"{\"id\":1,\"name\":\"男\",\"english\":\"Male\"},\n" +
"{\"id\":2,\"name\":\"女\",\"english\":\"Female\"}]";
private boolean includeSecrecy;
public SexPicker(Activity activity) {
super(activity);
}
public SexPicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
public void setIncludeSecrecy(boolean includeSecrecy) {
this.includeSecrecy = includeSecrecy;
setData(provideData());
}
@Override
public void setDefaultValue(Object item) {
if (item instanceof String) {
setDefaultValueByName(item.toString());
} else {
super.setDefaultValue(item);
}
}
public void setDefaultValueByName(String name) {
SexEntity entity = new SexEntity();
entity.setName(name);
super.setDefaultValue(entity);
}
public void setDefaultValueByEnglish(String english) {
SexEntity entity = new SexEntity();
entity.setEnglish(english);
super.setDefaultValue(entity);
}
@Override
protected List> provideData() {
ArrayList data = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(JSON);
for (int i = 0, n = jsonArray.length(); i < n; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
SexEntity entity = new SexEntity();
entity.setId(jsonObject.getString("id"));
entity.setName(jsonObject.getString("name"));
entity.setEnglish(jsonObject.getString("english"));
if (!includeSecrecy && "0".equals(entity.getId())) {
continue;
}
data.add(entity);
}
} catch (JSONException e) {
DialogLog.print(e);
}
return data;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/TimePicker.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker;
import android.app.Activity;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.dialog.ModalDialog;
import com.github.gzuliyujiang.wheelpicker.contract.OnTimeMeridiemPickedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnTimePickedListener;
import com.github.gzuliyujiang.wheelpicker.widget.TimeWheelLayout;
/**
* 时间选择器
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/5 18:19
*/
@SuppressWarnings("unused")
public class TimePicker extends ModalDialog {
protected TimeWheelLayout wheelLayout;
private OnTimePickedListener onTimePickedListener;
private OnTimeMeridiemPickedListener onTimeMeridiemPickedListener;
public TimePicker(@NonNull Activity activity) {
super(activity);
}
public TimePicker(@NonNull Activity activity, @StyleRes int themeResId) {
super(activity, themeResId);
}
@NonNull
@Override
protected View createBodyView() {
wheelLayout = new TimeWheelLayout(activity);
return wheelLayout;
}
@Override
protected void onCancel() {
}
@Override
protected void onOk() {
int hour = wheelLayout.getSelectedHour();
int minute = wheelLayout.getSelectedMinute();
int second = wheelLayout.getSelectedSecond();
if (onTimePickedListener != null) {
onTimePickedListener.onTimePicked(hour, minute, second);
}
if (onTimeMeridiemPickedListener != null) {
onTimeMeridiemPickedListener.onTimePicked(hour, minute, second, wheelLayout.isAnteMeridiem());
}
}
public void setOnTimePickedListener(OnTimePickedListener onTimePickedListener) {
this.onTimePickedListener = onTimePickedListener;
}
public void setOnTimeMeridiemPickedListener(OnTimeMeridiemPickedListener onTimeMeridiemPickedListener) {
this.onTimeMeridiemPickedListener = onTimeMeridiemPickedListener;
}
public final TimeWheelLayout getWheelLayout() {
return wheelLayout;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/annotation/DateMode.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* 日期模式
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 17:10
*/
@Retention(RetentionPolicy.SOURCE)
public @interface DateMode {
/**
* 不显示
*/
int NONE = -1;
/**
* 年月日
*/
int YEAR_MONTH_DAY = 0;
/**
* 年月
*/
int YEAR_MONTH = 1;
/**
* 月日
*/
int MONTH_DAY = 2;
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/annotation/EthnicSpec.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.annotation;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/12 14:22
*/
public @interface EthnicSpec {
int DEFAULT = 1;
int GB3304_91 = 2;
int SEVENTH_NATIONAL_CENSUS = 3;
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/annotation/TimeMode.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* 时间模式
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 17:09
*/
@Retention(RetentionPolicy.SOURCE)
public @interface TimeMode {
/**
* 不显示
*/
int NONE = -1;
/**
* 24小时制(不含秒)
*/
int HOUR_24_NO_SECOND = 0;
/**
* 24小时制(包括秒)
*/
int HOUR_24_HAS_SECOND = 1;
/**
* 12小时制(不含秒)
*/
int HOUR_12_NO_SECOND = 2;
/**
* 12小时制(包括秒)
*/
int HOUR_12_HAS_SECOND = 3;
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/DateFormatter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 日期显示文本格式化接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 19:55
*/
public interface DateFormatter {
/**
* 格式化年份
*
* @param year 年份数字
* @return 格式化后最终显示的年份字符串
*/
String formatYear(int year);
/**
* 格式化月份
*
* @param month 月份数字
* @return 格式化后最终显示的月份字符串
*/
String formatMonth(int month);
/**
* 格式化日子
*
* @param day 日子数字
* @return 格式化后最终显示的日子字符串
*/
String formatDay(int day);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/LinkageProvider.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
import androidx.annotation.NonNull;
import java.util.List;
/**
* 提供二级或三级联动数据
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 11:27
*/
public interface LinkageProvider {
int INDEX_NO_FOUND = -1;
/**
* 是否展示第一级
*
* @return 返回true表示展示第一级
*/
boolean firstLevelVisible();
/**
* 是否展示第三级
*
* @return 返回true表示展示第三级
*/
boolean thirdLevelVisible();
/**
* 提供第一级数据
*
* @return 第一级数据
*/
@NonNull
List> provideFirstData();
/**
* 根据第一级数据联动第二级数据
*
* @param firstIndex 第一级数据索引
* @return 第二级数据
*/
@NonNull
List> linkageSecondData(int firstIndex);
/**
* 根据第一二级数据联动第三级数据
*
* @param firstIndex 第一级数据索引
* @param secondIndex 第二级数据索引
* @return 第三级数据
*/
@NonNull
List> linkageThirdData(int firstIndex, int secondIndex);
/**
* 根据第一数据值查找其索引
*
* @param firstValue 第一级数据值
* @return 第一级数据索引
*/
int findFirstIndex(Object firstValue);
/**
* 根据第二数据值查找其索引
*
* @param firstIndex 第一级数据索引
* @param secondValue 第二级数据值
* @return 第二级数据索引
*/
int findSecondIndex(int firstIndex, Object secondValue);
/**
* 根据第三数据值查找其索引
*
* @param firstIndex 第一级数据索引
* @param secondIndex 第二级数据索引
* @param thirdValue 第三级数据值
* @return 第三级数据索引
*/
int findThirdIndex(int firstIndex, int secondIndex, Object thirdValue);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnCarPlatePickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/9 11:53
*/
public interface OnCarPlatePickedListener {
void onCarNumberPicked(String province, String letter);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnDatePickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 日期选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 19:57
*/
public interface OnDatePickedListener {
/**
* 已选择的日期
*
* @param year 年
* @param month 月
* @param day 日
*/
void onDatePicked(int year, int month, int day);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnDateSelectedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 日期选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 19:57
*/
public interface OnDateSelectedListener {
/**
* 已选择的日期
*
* @param year 年
* @param month 月
* @param day 日
*/
void onDateSelected(int year, int month, int day);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnDatimePickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 日期时间选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/5 17:29
*/
public interface OnDatimePickedListener {
/**
* 日期时间选择回调
*
* @param year 年
* @param month 月
* @param day 日
* @param hour 时
* @param minute 分
* @param second 秒
*/
void onDatimePicked(int year, int month, int day, int hour, int minute, int second);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnDatimeSelectedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 日期时间选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/5 17:29
*/
public interface OnDatimeSelectedListener {
/**
* 日期时间选择回调
*
* @param year 年
* @param month 月
* @param day 日
* @param hour 时
* @param minute 分
* @param second 秒
*/
void onDatimeSelected(int year, int month, int day, int hour, int minute, int second);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnLinkagePickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 联动选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 18:23
*/
public interface OnLinkagePickedListener {
/**
* 联动选择回调
*
* @param first 选中项的第一级条目内容
* @param second 选中项的第二级条目内容
* @param third 选中项的第三级条目内容
*/
void onLinkagePicked(Object first, Object second, Object third);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnLinkageSelectedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 联动选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 18:23
*/
public interface OnLinkageSelectedListener {
/**
* 联动选择回调
*
* @param first 选中项的第一级条目内容
* @param second 选中项的第二级条目内容
* @param third 选中项的第三级条目内容
*/
void onLinkageSelected(Object first, Object second, Object third);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnNumberPickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/5 15:17
*/
public interface OnNumberPickedListener {
void onNumberPicked(int position, Number item);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnNumberSelectedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/5 15:17
*/
public interface OnNumberSelectedListener {
void onNumberSelected(int position, Number item);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnOptionPickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 单项条目选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 20:00
*/
public interface OnOptionPickedListener {
/**
* 单项条目选择回调
*
* @param position 选中项的索引
* @param item 选中项的内容
*/
void onOptionPicked(int position, Object item);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnOptionSelectedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 单项条目选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 20:00
*/
public interface OnOptionSelectedListener {
/**
* 单项条目选择回调
*
* @param position 选中项的索引
* @param item 选中项的内容
*/
void onOptionSelected(int position, Object item);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnTimeMeridiemPickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 时间选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 19:58
*/
public interface OnTimeMeridiemPickedListener {
/**
* 时间选择回调
*
* @param hour 时
* @param minute 分
* @param second 秒
* @param isAnteMeridiem 是否上午
*/
void onTimePicked(int hour, int minute, int second, boolean isAnteMeridiem);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnTimeMeridiemSelectedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 时间选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 19:58
*/
public interface OnTimeMeridiemSelectedListener {
/**
* 时间选择回调
*
* @param hour 时
* @param minute 分
* @param second 秒
* @param isAnteMeridiem 是否上午
*/
void onTimeSelected(int hour, int minute, int second, boolean isAnteMeridiem);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnTimePickedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 时间选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 19:58
*/
public interface OnTimePickedListener {
/**
* 时间选择回调
*
* @param hour 时
* @param minute 分
* @param second 秒
*/
void onTimePicked(int hour, int minute, int second);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/OnTimeSelectedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 时间选择接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 19:58
*/
public interface OnTimeSelectedListener {
/**
* 时间选择回调
*
* @param hour 时
* @param minute 分
* @param second 秒
*/
void onTimeSelected(int hour, int minute, int second);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/contract/TimeFormatter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.contract;
/**
* 时间显示文本格式化接口
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 19:55
*/
public interface TimeFormatter {
/**
* 格式化小时数
*
* @param hour 小时数
* @return 格式化后最终显示的小时数字符串
*/
String formatHour(int hour);
/**
* 格式化分钟数
*
* @param minute 分钟数
* @return 格式化后最终显示的分钟数字符串
*/
String formatMinute(int minute);
/**
* 格式化秒数
*
* @param second 秒数
* @return 格式化后最终显示的秒数字符串
*/
String formatSecond(int second);
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/entity/ConstellationEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.entity;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.wheelview.contract.TextProvider;
import java.io.Serializable;
import java.util.Locale;
import java.util.Objects;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/10/28 8:37
*/
public class ConstellationEntity implements TextProvider, Serializable {
private static final boolean IS_CHINESE;
private String id;
private String startDate;
private String endDate;
private String name;
private String english;
static {
IS_CHINESE = Locale.getDefault().getDisplayLanguage().contains("中文");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEnglish() {
return english;
}
public void setEnglish(String english) {
this.english = english;
}
@Override
public String provideText() {
if (IS_CHINESE) {
return name;
}
return english;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ConstellationEntity that = (ConstellationEntity) o;
return Objects.equals(id, that.id) ||
Objects.equals(startDate, that.startDate) ||
Objects.equals(endDate, that.endDate) ||
Objects.equals(name, that.name) ||
Objects.equals(english, that.english);
}
@Override
public int hashCode() {
return Objects.hash(id, startDate, endDate, name, english);
}
@NonNull
@Override
public String toString() {
return "ConstellationEntity{" +
"id='" + id + '\'' +
", startDate='" + startDate + '\'' +
", endDate='" + endDate + '\'' +
", name='" + name + '\'' +
", english" + english + '\'' +
'}';
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/entity/DateEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.entity;
import androidx.annotation.NonNull;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
/**
* 日期数据实体
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 15:29
*/
@SuppressWarnings({"unused"})
public class DateEntity implements Serializable {
private int year;
private int month;
private int day;
public static DateEntity target(int year, int month, int dayOfMonth) {
DateEntity entity = new DateEntity();
entity.setYear(year);
entity.setMonth(month);
entity.setDay(dayOfMonth);
return entity;
}
public static DateEntity target(Calendar calendar) {
int year = calendar.get(Calendar.YEAR);
// 月份的值是从0开始的,转为从1开始
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
return target(year, month, day);
}
public static DateEntity target(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return target(calendar);
}
public static DateEntity today() {
return target(Calendar.getInstance());
}
public static DateEntity dayOnFuture(int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, dayOfMonth);
return target(calendar);
}
public static DateEntity monthOnFuture(int month) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, month);
return target(calendar);
}
public static DateEntity yearOnFuture(int year) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, year);
return target(calendar);
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public long toTimeInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DateEntity that = (DateEntity) o;
return year == that.year &&
month == that.month &&
day == that.day;
}
@Override
public int hashCode() {
return Objects.hash(year, month, day);
}
@NonNull
@Override
public String toString() {
return year + "-" + month + "-" + day;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/entity/DatimeEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.entity;
import androidx.annotation.NonNull;
import java.io.Serializable;
import java.util.Calendar;
/**
* 日期时间数据实体。
*
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 17:30
*/
@SuppressWarnings({"unused"})
public class DatimeEntity implements Serializable {
private DateEntity date;
private TimeEntity time;
public static DatimeEntity now() {
DatimeEntity entity = new DatimeEntity();
entity.setDate(DateEntity.today());
entity.setTime(TimeEntity.now());
return entity;
}
public static DatimeEntity minuteOnFuture(int minute) {
DatimeEntity entity = now();
entity.setTime(TimeEntity.minuteOnFuture(minute));
return entity;
}
public static DatimeEntity hourOnFuture(int hour) {
DatimeEntity entity = now();
entity.setTime(TimeEntity.hourOnFuture(hour));
return entity;
}
public static DatimeEntity dayOnFuture(int day) {
DatimeEntity entity = now();
entity.setDate(DateEntity.dayOnFuture(day));
return entity;
}
public static DatimeEntity monthOnFuture(int month) {
DatimeEntity entity = now();
entity.setDate(DateEntity.monthOnFuture(month));
return entity;
}
public static DatimeEntity yearOnFuture(int year) {
DatimeEntity entity = now();
entity.setDate(DateEntity.yearOnFuture(year));
return entity;
}
public DateEntity getDate() {
return date;
}
public void setDate(DateEntity date) {
this.date = date;
}
public TimeEntity getTime() {
return time;
}
public void setTime(TimeEntity time) {
this.time = time;
}
public long toTimeInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, date.getYear());
calendar.set(Calendar.MONTH, date.getMonth() - 1);
calendar.set(Calendar.DAY_OF_MONTH, date.getDay());
calendar.set(Calendar.HOUR_OF_DAY, time.getHour());
calendar.set(Calendar.MINUTE, time.getMinute());
calendar.set(Calendar.SECOND, time.getSecond());
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
@NonNull
@Override
public String toString() {
return date.toString() + " " + time.toString();
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/entity/EthnicEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.entity;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.wheelview.contract.TextProvider;
import java.io.Serializable;
import java.util.Locale;
import java.util.Objects;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/12 15:05
*/
public class EthnicEntity implements TextProvider, Serializable {
private static final boolean IS_CHINESE;
private String code;
private String name;
private String spelling;
static {
IS_CHINESE = Locale.getDefault().getDisplayLanguage().contains("中文");
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSpelling() {
return spelling;
}
public void setSpelling(String spelling) {
this.spelling = spelling;
}
@Override
public String provideText() {
if (IS_CHINESE) {
return name;
}
return spelling;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
EthnicEntity that = (EthnicEntity) o;
return Objects.equals(code, that.code) ||
Objects.equals(name, that.name) ||
Objects.equals(spelling, that.spelling);
}
@Override
public int hashCode() {
return Objects.hash(code, name, spelling);
}
@NonNull
@Override
public String toString() {
return "EthnicEntity{" +
"code='" + code + '\'' +
", name='" + name + '\'' +
", spelling='" + spelling + '\'' +
'}';
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/entity/PhoneCodeEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.entity;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.wheelview.contract.TextProvider;
import java.io.Serializable;
import java.util.Locale;
import java.util.Objects;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/3 16:27
*/
public class PhoneCodeEntity implements TextProvider, Serializable {
private static final boolean IS_CHINESE;
private String code;
private String name;
private String english;
static {
IS_CHINESE = Locale.getDefault().getDisplayLanguage().contains("中文");
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEnglish() {
return english;
}
public void setEnglish(String english) {
this.english = english;
}
@Override
public String provideText() {
if (IS_CHINESE) {
return name;
}
return english;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PhoneCodeEntity that = (PhoneCodeEntity) o;
return Objects.equals(code, that.code) ||
Objects.equals(name, that.name) ||
Objects.equals(english, that.english);
}
@Override
public int hashCode() {
return Objects.hash(code, name, english);
}
@NonNull
@Override
public String toString() {
return "PhoneCodeEntity{" +
"code='" + code + '\'' +
", name='" + name + '\'' +
", english" + english + '\'' +
'}';
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/entity/SexEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.entity;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.wheelview.contract.TextProvider;
import java.io.Serializable;
import java.util.Locale;
import java.util.Objects;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/10/28 9:17
*/
public class SexEntity implements TextProvider, Serializable {
private static final boolean IS_CHINESE;
private String id;
private String name;
private String english;
static {
IS_CHINESE = Locale.getDefault().getDisplayLanguage().contains("中文");
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEnglish() {
return english;
}
public void setEnglish(String english) {
this.english = english;
}
@Override
public String provideText() {
if (IS_CHINESE) {
return name;
}
return english;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SexEntity that = (SexEntity) o;
return Objects.equals(id, that.id) ||
Objects.equals(name, that.name) ||
Objects.equals(english, that.english);
}
@Override
public int hashCode() {
return Objects.hash(id, name, english);
}
@NonNull
@Override
public String toString() {
return "SexEntity{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", english" + english + '\'' +
'}';
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/entity/TimeEntity.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.entity;
import androidx.annotation.NonNull;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
/**
* 时间数据实体
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/17 15:29
*/
@SuppressWarnings({"unused"})
public class TimeEntity implements Serializable {
private int hour;
private int minute;
private int second;
public static TimeEntity target(int hourOfDay, int minute, int second) {
TimeEntity entity = new TimeEntity();
entity.setHour(hourOfDay);
entity.setMinute(minute);
entity.setSecond(second);
return entity;
}
public static TimeEntity target(Calendar calendar) {
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
return target(hour, minute, second);
}
public static TimeEntity target(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return target(calendar);
}
public static TimeEntity now() {
return target(Calendar.getInstance());
}
public static TimeEntity minuteOnFuture(int minute) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, minute);
return target(calendar);
}
public static TimeEntity hourOnFuture(int hourOfDay) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, hourOfDay);
return target(calendar);
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
public long toTimeInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
@NonNull
@Override
public String toString() {
return hour + ":" + minute + ":" + second;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/impl/BirthdayFormatter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.impl;
/**
* 生日格式化
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 14:31
*/
public class BirthdayFormatter extends SimpleDateFormatter {
@Override
public String formatYear(int year) {
return super.formatYear(year) + "年";
}
@Override
public String formatMonth(int month) {
return super.formatMonth(month) + "月";
}
@Override
public String formatDay(int day) {
return super.formatDay(day) + "日";
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/impl/CarPlateProvider.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.impl;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.wheelpicker.contract.LinkageProvider;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 数据参见 http://www.360doc.com/content/12/0602/07/3899427_215339300.shtml
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/9 11:31
*/
public class CarPlateProvider implements LinkageProvider {
private static final String[] ABBREVIATIONS = {
"京", "津", "冀", "晋", "蒙", "辽", "吉", "黑", "沪",
"苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘",
"粤", "桂", "琼", "渝", "川", "贵", "云", "藏", "陕",
"甘", "青", "宁", "新"};
@Override
public boolean firstLevelVisible() {
return true;
}
@Override
public boolean thirdLevelVisible() {
return false;
}
@NonNull
@Override
public List provideFirstData() {
List provinces = new ArrayList<>();
Collections.addAll(provinces, ABBREVIATIONS);
return provinces;
}
@NonNull
@Override
public List linkageSecondData(int firstIndex) {
List letters = new ArrayList<>();
if (firstIndex == INDEX_NO_FOUND) {
firstIndex = 0;
}
String province = provideFirstData().get(firstIndex);
switch (province) {
case "京":
for (char i = 'A'; i <= 'M'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
letters.add("Y");
break;
case "津":
case "青":
for (char i = 'A'; i <= 'H'; i++) {
letters.add(String.valueOf(i));
}
break;
case "冀":
for (char i = 'A'; i <= 'H'; i++) {
letters.add(String.valueOf(i));
}
letters.add("J");
letters.add("R");
letters.add("S");
letters.add("T");
break;
case "晋":
for (char i = 'A'; i <= 'M'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("G");
letters.remove("I");
break;
case "蒙":
case "赣":
for (char i = 'A'; i <= 'M'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
break;
case "辽":
case "甘":
for (char i = 'A'; i <= 'P'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
letters.remove("O");
break;
case "吉":
case "闽":
for (char i = 'A'; i <= 'K'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
break;
case "黑":
case "新":
for (char i = 'A'; i <= 'R'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
letters.remove("O");
break;
case "沪":
for (char i = 'A'; i <= 'D'; i++) {
letters.add(String.valueOf(i));
}
letters.add("R");
break;
case "苏":
for (char i = 'A'; i <= 'N'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
break;
case "浙":
for (char i = 'A'; i <= 'L'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
break;
case "皖":
case "鄂":
for (char i = 'A'; i <= 'S'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
letters.remove("O");
break;
case "鲁":
for (char i = 'A'; i <= 'V'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
letters.remove("O");
letters.add("Y");
break;
case "豫":
for (char i = 'A'; i <= 'U'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
letters.remove("O");
break;
case "湘":
for (char i = 'A'; i <= 'N'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
letters.remove("O");
letters.add("U");
break;
case "粤":
for (char i = 'A'; i <= 'Z'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
letters.remove("O");
break;
case "桂":
for (char i = 'A'; i <= 'P'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
letters.remove("O");
letters.add("R");
break;
case "琼":
case "宁":
for (char i = 'A'; i <= 'E'; i++) {
letters.add(String.valueOf(i));
}
break;
case "渝":
for (char i = 'A'; i <= 'D'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("D");
letters.remove("E");
break;
case "川":
for (char i = 'A'; i <= 'Z'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("G");
letters.remove("I");
letters.remove("O");
break;
case "贵":
case "藏":
for (char i = 'A'; i <= 'J'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
break;
case "云":
// “A-V”为昆明市东川区(原东川市)
letters.add("A-V");
for (char i = 'A'; i <= 'S'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("B");
letters.remove("I");
letters.remove("O");
break;
case "陕":
for (char i = 'A'; i <= 'K'; i++) {
letters.add(String.valueOf(i));
}
letters.remove("I");
letters.add("V");
break;
}
return letters;
}
@NonNull
@Override
public List> linkageThirdData(int firstIndex, int secondIndex) {
return new ArrayList<>();
}
@Override
public int findFirstIndex(Object firstValue) {
if (firstValue == null) {
return INDEX_NO_FOUND;
}
for (int i = 0, n = ABBREVIATIONS.length; i < n; i++) {
String abbreviation = ABBREVIATIONS[i];
if (abbreviation.equals(firstValue.toString())) {
return i;
}
}
return INDEX_NO_FOUND;
}
@Override
public int findSecondIndex(int firstIndex, Object secondValue) {
if (secondValue == null) {
return INDEX_NO_FOUND;
}
List letters = linkageSecondData(firstIndex);
for (int i = 0, n = letters.size(); i < n; i++) {
String letter = letters.get(i);
if (letter.equals(secondValue.toString())) {
return i;
}
}
return INDEX_NO_FOUND;
}
@Override
public int findThirdIndex(int firstIndex, int secondIndex, Object thirdValue) {
return 0;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/impl/SimpleDateFormatter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.impl;
import com.github.gzuliyujiang.wheelpicker.contract.DateFormatter;
/**
* 简单的日期格式化
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/15 18:11
*/
public class SimpleDateFormatter implements DateFormatter {
@Override
public String formatYear(int year) {
if (year < 1000) {
year += 1000;
}
return "" + year;
}
@Override
public String formatMonth(int month) {
return month < 10 ? "0" + month : "" + month;
}
@Override
public String formatDay(int day) {
return day < 10 ? "0" + day : "" + day;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/impl/SimpleTimeFormatter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.impl;
import com.github.gzuliyujiang.wheelpicker.contract.TimeFormatter;
import com.github.gzuliyujiang.wheelpicker.widget.TimeWheelLayout;
/**
* 简单的时间格式化
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/15 18:13
*/
public class SimpleTimeFormatter implements TimeFormatter {
private final TimeWheelLayout wheelLayout;
public SimpleTimeFormatter(TimeWheelLayout wheelLayout) {
this.wheelLayout = wheelLayout;
}
@Override
public String formatHour(int hour) {
if (wheelLayout.isHour12Mode()) {
if (hour == 0) {
hour = 24;
}
if (hour > 12) {
hour = hour - 12;
}
}
return hour < 10 ? "0" + hour : "" + hour;
}
@Override
public String formatMinute(int minute) {
return minute < 10 ? "0" + minute : "" + minute;
}
@Override
public String formatSecond(int second) {
return second < 10 ? "0" + second : "" + second;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/impl/SimpleWheelFormatter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.impl;
import androidx.annotation.NonNull;
import com.github.gzuliyujiang.wheelview.contract.WheelFormatter;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/7 00:06
*/
public class SimpleWheelFormatter implements WheelFormatter {
@Override
public String formatItem(@NonNull Object item) {
return item.toString();
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/impl/UnitDateFormatter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.impl;
import com.github.gzuliyujiang.wheelpicker.contract.DateFormatter;
/**
* 带单位的日期格式化
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/4 11:33
*/
public class UnitDateFormatter implements DateFormatter {
@Override
public String formatYear(int year) {
return year + "年";
}
@Override
public String formatMonth(int month) {
return month + "月";
}
@Override
public String formatDay(int day) {
return day + "日";
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/impl/UnitTimeFormatter.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.impl;
import com.github.gzuliyujiang.wheelpicker.contract.TimeFormatter;
/**
* 带单位的时间格式化
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/4 11:33
*/
public class UnitTimeFormatter implements TimeFormatter {
@Override
public String formatHour(int hour) {
return hour + "点";
}
@Override
public String formatMinute(int minute) {
return minute + "分";
}
@Override
public String formatSecond(int second) {
return second + "秒";
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/widget/BaseWheelLayout.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import androidx.annotation.ColorInt;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.StyleRes;
import com.github.gzuliyujiang.wheelpicker.R;
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.OnWheelChangedListener;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
import java.util.ArrayList;
import java.util.List;
/**
* 抽象的滚轮控件
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/5 16:18
*/
@SuppressWarnings("unused")
public abstract class BaseWheelLayout extends LinearLayout implements OnWheelChangedListener {
private final List wheelViews = new ArrayList<>();
public BaseWheelLayout(Context context) {
super(context);
init(context, null, R.attr.WheelStyle, R.style.WheelDefault);
}
public BaseWheelLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
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, defStyleAttr, R.style.WheelDefault);
}
public BaseWheelLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, defStyleRes);
}
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);
}
}
protected void onInit(@NonNull Context context) {
}
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();
protected abstract List provideWheelViews();
public void setStyle(@StyleRes int style) {
initAttrs(getContext(), null, R.attr.WheelStyle, style);
requestLayout();
invalidate();
}
@Override
public void onWheelScrolled(WheelView view, int offset) {
}
@Override
public void onWheelScrollStateChanged(WheelView view, @ScrollState int state) {
}
@Override
public void onWheelLoopFinished(WheelView view) {
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
for (WheelView wheelView : wheelViews) {
wheelView.setEnabled(enabled);
}
}
public void setVisibleItemCount(int visibleItemCount) {
for (WheelView wheelView : wheelViews) {
wheelView.setVisibleItemCount(visibleItemCount);
}
}
public void setItemSpace(@Px int space) {
for (WheelView wheelView : wheelViews) {
wheelView.setItemSpace(space);
}
}
public void setSameWidthEnabled(boolean sameWidthEnabled) {
for (WheelView wheelView : wheelViews) {
wheelView.setSameWidthEnabled(sameWidthEnabled);
}
}
public void setDefaultItemPosition(int position) {
for (WheelView wheelView : wheelViews) {
wheelView.setDefaultPosition(position);
}
}
public void setCurtainEnabled(boolean hasCurtain) {
for (WheelView wheelView : wheelViews) {
wheelView.setCurtainEnabled(hasCurtain);
}
}
public void setCurtainColor(@ColorInt int color) {
for (WheelView wheelView : wheelViews) {
wheelView.setCurtainColor(color);
}
}
public void setCurtainCorner(@CurtainCorner int corner) {
for (WheelView wheelView : wheelViews) {
wheelView.setCurtainCorner(corner);
}
}
public void setCurtainRadius(@Px float radius) {
for (WheelView wheelView : wheelViews) {
wheelView.setCurtainRadius(radius);
}
}
public void setAtmosphericEnabled(boolean hasAtmospheric) {
for (WheelView wheelView : wheelViews) {
wheelView.setAtmosphericEnabled(hasAtmospheric);
}
}
public void setCurvedEnabled(boolean curved) {
for (WheelView wheelView : wheelViews) {
wheelView.setCurvedEnabled(curved);
}
}
public void setCurvedMaxAngle(int curvedMaxAngle) {
for (WheelView wheelView : wheelViews) {
wheelView.setCurvedMaxAngle(curvedMaxAngle);
}
}
public void setCurvedIndicatorSpace(@Px int space) {
for (WheelView wheelView : wheelViews) {
wheelView.setCurvedIndicatorSpace(space);
}
}
public void setCyclicEnabled(boolean cyclic) {
for (WheelView wheelView : wheelViews) {
wheelView.setCyclicEnabled(cyclic);
}
}
public void setIndicatorEnabled(boolean hasIndicator) {
for (WheelView wheelView : wheelViews) {
wheelView.setIndicatorEnabled(hasIndicator);
}
}
public void setIndicatorSize(@Px float size) {
for (WheelView wheelView : wheelViews) {
wheelView.setIndicatorSize(size);
}
}
public void setIndicatorColor(@ColorInt int color) {
for (WheelView wheelView : wheelViews) {
wheelView.setIndicatorColor(color);
}
}
public void setMaxWidthText(String text) {
if (TextUtils.isEmpty(text)) {
return;
}
for (WheelView wheelView : wheelViews) {
wheelView.setMaxWidthText(text);
}
}
public void setTextSize(@Px float textSize) {
for (WheelView wheelView : wheelViews) {
wheelView.setTextSize(textSize);
}
}
public void setSelectedTextSize(@Px float textSize) {
for (WheelView wheelView : wheelViews) {
wheelView.setSelectedTextSize(textSize);
}
}
public void setTextColor(@ColorInt int color) {
for (WheelView wheelView : wheelViews) {
wheelView.setTextColor(color);
}
}
public void setSelectedTextColor(@ColorInt int color) {
for (WheelView wheelView : wheelViews) {
wheelView.setSelectedTextColor(color);
}
}
public void setSelectedTextBold(boolean bold) {
for (WheelView wheelView : wheelViews) {
wheelView.setSelectedTextBold(bold);
}
}
public void setTextAlign(@ItemTextAlign int align) {
for (WheelView wheelView : wheelViews) {
wheelView.setTextAlign(align);
}
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/widget/CarPlateWheelLayout.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.widget;
import android.content.Context;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.github.gzuliyujiang.wheelpicker.impl.CarPlateProvider;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/9 11:57
*/
public class CarPlateWheelLayout extends LinkageWheelLayout {
private CarPlateProvider provider;
public CarPlateWheelLayout(Context context) {
super(context);
}
public CarPlateWheelLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CarPlateWheelLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CarPlateWheelLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onInit(@NonNull Context context) {
super.onInit(context);
provider = new CarPlateProvider();
setData(provider);
}
@Override
protected void onAttributeSet(@NonNull Context context, @Nullable AttributeSet attrs) {
super.onAttributeSet(context, attrs);
setFirstVisible(provider.firstLevelVisible());
setThirdVisible(provider.thirdLevelVisible());
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/widget/DateWheelLayout.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
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.ScrollState;
import com.github.gzuliyujiang.wheelview.contract.WheelFormatter;
import com.github.gzuliyujiang.wheelview.widget.NumberWheelView;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
import java.util.Arrays;
import java.util.List;
/**
* 日期滚轮控件
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/5 16:12
*/
@SuppressWarnings("unused")
public class DateWheelLayout extends BaseWheelLayout {
private NumberWheelView yearWheelView;
private NumberWheelView monthWheelView;
private NumberWheelView dayWheelView;
private TextView yearLabelView;
private TextView monthLabelView;
private TextView dayLabelView;
private DateEntity startValue;
private DateEntity endValue;
private Integer selectedYear;
private Integer selectedMonth;
private Integer selectedDay;
private OnDateSelectedListener onDateSelectedListener;
private boolean resetWhenLinkage = true;
public DateWheelLayout(Context context) {
super(context);
}
public DateWheelLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DateWheelLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public DateWheelLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected int provideLayoutRes() {
return R.layout.wheel_picker_date;
}
@Override
protected List provideWheelViews() {
return Arrays.asList(yearWheelView, monthWheelView, dayWheelView);
}
@Override
protected void onInit(@NonNull Context context) {
yearWheelView = findViewById(R.id.wheel_picker_date_year_wheel);
monthWheelView = findViewById(R.id.wheel_picker_date_month_wheel);
dayWheelView = findViewById(R.id.wheel_picker_date_day_wheel);
yearLabelView = findViewById(R.id.wheel_picker_date_year_label);
monthLabelView = findViewById(R.id.wheel_picker_date_month_label);
dayLabelView = findViewById(R.id.wheel_picker_date_day_label);
}
@Override
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());
}
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility == VISIBLE && startValue == null && endValue == null) {
setRange(DateEntity.today(), DateEntity.yearOnFuture(30), DateEntity.today());
}
}
@Override
public void onWheelSelected(WheelView view, int position) {
int id = view.getId();
if (id == R.id.wheel_picker_date_year_wheel) {
selectedYear = yearWheelView.getItem(position);
if (resetWhenLinkage) {
selectedMonth = null;
selectedDay = null;
}
changeMonth(selectedYear);
dateSelectedCallback();
return;
}
if (id == R.id.wheel_picker_date_month_wheel) {
selectedMonth = monthWheelView.getItem(position);
if (resetWhenLinkage) {
selectedDay = null;
}
changeDay(selectedYear, selectedMonth);
dateSelectedCallback();
return;
}
if (id == R.id.wheel_picker_date_day_wheel) {
selectedDay = dayWheelView.getItem(position);
dateSelectedCallback();
}
}
@Override
public void onWheelScrollStateChanged(WheelView view, @ScrollState int state) {
int id = view.getId();
if (id == R.id.wheel_picker_date_year_wheel) {
monthWheelView.setEnabled(state == ScrollState.IDLE);
dayWheelView.setEnabled(state == ScrollState.IDLE);
return;
}
if (id == R.id.wheel_picker_date_month_wheel) {
yearWheelView.setEnabled(state == ScrollState.IDLE);
dayWheelView.setEnabled(state == ScrollState.IDLE);
return;
}
if (id == R.id.wheel_picker_date_day_wheel) {
yearWheelView.setEnabled(state == ScrollState.IDLE);
monthWheelView.setEnabled(state == ScrollState.IDLE);
}
}
private void dateSelectedCallback() {
if (onDateSelectedListener == null) {
return;
}
dayWheelView.post(new Runnable() {
@Override
public void run() {
onDateSelectedListener.onDateSelected(selectedYear, selectedMonth, selectedDay);
}
});
}
public void setDateMode(@DateMode int dateMode) {
yearWheelView.setVisibility(View.VISIBLE);
yearLabelView.setVisibility(View.VISIBLE);
monthWheelView.setVisibility(View.VISIBLE);
monthLabelView.setVisibility(View.VISIBLE);
dayWheelView.setVisibility(View.VISIBLE);
dayLabelView.setVisibility(View.VISIBLE);
if (dateMode == DateMode.NONE) {
yearWheelView.setVisibility(View.GONE);
yearLabelView.setVisibility(View.GONE);
monthWheelView.setVisibility(View.GONE);
monthLabelView.setVisibility(View.GONE);
dayWheelView.setVisibility(View.GONE);
dayLabelView.setVisibility(View.GONE);
return;
}
if (dateMode == DateMode.MONTH_DAY) {
yearWheelView.setVisibility(View.GONE);
yearLabelView.setVisibility(View.GONE);
return;
}
if (dateMode == DateMode.YEAR_MONTH) {
dayWheelView.setVisibility(View.GONE);
dayLabelView.setVisibility(View.GONE);
}
}
/**
* 设置日期时间范围
*/
public void setRange(DateEntity startValue, DateEntity endValue) {
setRange(startValue, endValue, null);
}
/**
* 设置日期时间范围
*/
public void setRange(DateEntity startValue, DateEntity endValue, DateEntity defaultValue) {
if (startValue == null) {
startValue = DateEntity.today();
}
if (endValue == null) {
endValue = DateEntity.yearOnFuture(30);
}
if (endValue.toTimeInMillis() < startValue.toTimeInMillis()) {
throw new IllegalArgumentException("Ensure the start date is less than the end date");
}
this.startValue = startValue;
this.endValue = endValue;
if (defaultValue != null) {
selectedYear = defaultValue.getYear();
selectedMonth = defaultValue.getMonth();
selectedDay = defaultValue.getDay();
} else {
selectedYear = null;
selectedMonth = null;
selectedDay = null;
}
changeYear();
}
public void setDefaultValue(DateEntity defaultValue) {
setRange(startValue, endValue, defaultValue);
}
public void setDateFormatter(final DateFormatter dateFormatter) {
if (dateFormatter == null) {
return;
}
yearWheelView.setFormatter(new WheelFormatter() {
@Override
public String formatItem(@NonNull Object value) {
return dateFormatter.formatYear((Integer) value);
}
});
monthWheelView.setFormatter(new WheelFormatter() {
@Override
public String formatItem(@NonNull Object value) {
return dateFormatter.formatMonth((Integer) value);
}
});
dayWheelView.setFormatter(new WheelFormatter() {
@Override
public String formatItem(@NonNull Object value) {
return dateFormatter.formatDay((Integer) value);
}
});
}
public void setDateLabel(CharSequence year, CharSequence month, CharSequence day) {
yearLabelView.setText(year);
monthLabelView.setText(month);
dayLabelView.setText(day);
}
public void setOnDateSelectedListener(OnDateSelectedListener onDateSelectedListener) {
this.onDateSelectedListener = onDateSelectedListener;
}
public void setResetWhenLinkage(boolean resetWhenLinkage) {
this.resetWhenLinkage = resetWhenLinkage;
}
public final DateEntity getStartValue() {
return startValue;
}
public final DateEntity getEndValue() {
return endValue;
}
public final NumberWheelView getYearWheelView() {
return yearWheelView;
}
public final NumberWheelView getMonthWheelView() {
return monthWheelView;
}
public final NumberWheelView getDayWheelView() {
return dayWheelView;
}
public final TextView getYearLabelView() {
return yearLabelView;
}
public final TextView getMonthLabelView() {
return monthLabelView;
}
public final TextView getDayLabelView() {
return dayLabelView;
}
public final int getSelectedYear() {
return yearWheelView.getCurrentItem();
}
public final int getSelectedMonth() {
return monthWheelView.getCurrentItem();
}
public final int getSelectedDay() {
return dayWheelView.getCurrentItem();
}
private void changeYear() {
final int min = Math.min(startValue.getYear(), endValue.getYear());
final int max = Math.max(startValue.getYear(), endValue.getYear());
if (selectedYear == null) {
selectedYear = min;
} else {
selectedYear = Math.max(selectedYear, min);
selectedYear = Math.min(selectedYear, max);
}
yearWheelView.setRange(min, max, 1);
yearWheelView.setDefaultValue(selectedYear);
changeMonth(selectedYear);
}
private void changeMonth(int year) {
final int min, max;
//开始年份和结束年份相同(即只有一个年份,这种情况建议使用月日模式)
if (startValue.getYear() == endValue.getYear()) {
min = Math.min(startValue.getMonth(), endValue.getMonth());
max = Math.max(startValue.getMonth(), endValue.getMonth());
}
//当前所选年份和开始年份相同
else if (year == startValue.getYear()) {
min = startValue.getMonth();
max = 12;
}
//当前所选年份和结束年份相同
else if (year == endValue.getYear()) {
min = 1;
max = endValue.getMonth();
}
//当前所选年份在开始年份和结束年份之间
else {
min = 1;
max = 12;
}
if (selectedMonth == null) {
selectedMonth = min;
} else {
selectedMonth = Math.max(selectedMonth, min);
selectedMonth = Math.min(selectedMonth, max);
}
monthWheelView.setRange(min, max, 1);
monthWheelView.setDefaultValue(selectedMonth);
changeDay(year, selectedMonth);
}
private void changeDay(int year, int month) {
final int min, max;
//开始年月及结束年月相同情况
if (year == startValue.getYear() && month == startValue.getMonth()
&& year == endValue.getYear() && month == endValue.getMonth()) {
min = startValue.getDay();
max = endValue.getDay();
}
//开始年月相同情况
else if (year == startValue.getYear() && month == startValue.getMonth()) {
min = startValue.getDay();
max = getTotalDaysInMonth(year, month);
}
//结束年月相同情况
else if (year == endValue.getYear() && month == endValue.getMonth()) {
min = 1;
max = endValue.getDay();
} else {
min = 1;
max = getTotalDaysInMonth(year, month);
}
if (selectedDay == null) {
selectedDay = min;
} else {
selectedDay = Math.max(selectedDay, min);
selectedDay = Math.min(selectedDay, max);
}
dayWheelView.setRange(min, max, 1);
dayWheelView.setDefaultValue(selectedDay);
}
/**
* 根据年份及月份获取每月的天数,类似于{@link java.util.Calendar#getActualMaximum(int)}
*/
private int getTotalDaysInMonth(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
// 大月月份为31天
return 31;
case 4:
case 6:
case 9:
case 11:
// 小月月份为30天
return 30;
case 2:
// 二月需要判断是否闰年
if (year <= 0) {
return 29;
}
// 是否闰年:能被4整除但不能被100整除;能被400整除;
boolean isLeap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
if (isLeap) {
return 29;
} else {
return 28;
}
default:
return 30;
}
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/widget/DatimeWheelLayout.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
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.annotation.TimeMode;
import com.github.gzuliyujiang.wheelpicker.contract.DateFormatter;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatimeSelectedListener;
import com.github.gzuliyujiang.wheelpicker.contract.TimeFormatter;
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.ScrollState;
import com.github.gzuliyujiang.wheelview.widget.NumberWheelView;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
import java.util.ArrayList;
import java.util.List;
/**
* 日期时间滚轮控件
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 15:26
*/
@SuppressWarnings("unused")
public class DatimeWheelLayout extends BaseWheelLayout {
private DateWheelLayout dateWheelLayout;
private TimeWheelLayout timeWheelLayout;
private DatimeEntity startValue;
private DatimeEntity endValue;
private OnDatimeSelectedListener onDatimeSelectedListener;
public DatimeWheelLayout(Context context) {
super(context);
}
public DatimeWheelLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DatimeWheelLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public DatimeWheelLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected int provideLayoutRes() {
return R.layout.wheel_picker_datime;
}
@Override
protected List provideWheelViews() {
List list = new ArrayList<>();
list.addAll(dateWheelLayout.provideWheelViews());
list.addAll(timeWheelLayout.provideWheelViews());
return list;
}
@Override
protected void onInit(@NonNull Context context) {
dateWheelLayout = findViewById(R.id.wheel_picker_date_wheel);
timeWheelLayout = findViewById(R.id.wheel_picker_time_wheel);
}
@Override
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);
String monthLabel = typedArray.getString(R.styleable.DatimeWheelLayout_wheel_monthLabel);
String dayLabel = typedArray.getString(R.styleable.DatimeWheelLayout_wheel_dayLabel);
setDateLabel(yearLabel, monthLabel, dayLabel);
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));
}
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility == VISIBLE && startValue == null && endValue == null) {
setRange(DatimeEntity.now(), DatimeEntity.yearOnFuture(30), DatimeEntity.now());
}
}
@Override
public void onWheelSelected(WheelView view, int position) {
dateWheelLayout.onWheelSelected(view, position);
timeWheelLayout.onWheelSelected(view, position);
if (onDatimeSelectedListener == null) {
return;
}
timeWheelLayout.post(new Runnable() {
@Override
public void run() {
onDatimeSelectedListener.onDatimeSelected(dateWheelLayout.getSelectedYear(),
dateWheelLayout.getSelectedMonth(), dateWheelLayout.getSelectedDay(),
timeWheelLayout.getSelectedHour(), timeWheelLayout.getSelectedMinute(),
timeWheelLayout.getSelectedSecond());
}
});
}
@Override
public void onWheelScrolled(WheelView view, int offset) {
dateWheelLayout.onWheelScrolled(view, offset);
timeWheelLayout.onWheelScrolled(view, offset);
}
@Override
public void onWheelScrollStateChanged(WheelView view, @ScrollState int state) {
dateWheelLayout.onWheelScrollStateChanged(view, state);
timeWheelLayout.onWheelScrollStateChanged(view, state);
}
@Override
public void onWheelLoopFinished(WheelView view) {
dateWheelLayout.onWheelLoopFinished(view);
timeWheelLayout.onWheelLoopFinished(view);
}
public void setDateMode(@DateMode int dateMode) {
dateWheelLayout.setDateMode(dateMode);
}
public void setTimeMode(@TimeMode int timeMode) {
timeWheelLayout.setTimeMode(timeMode);
}
/**
* 设置日期时间范围
*/
public void setRange(DatimeEntity startValue, DatimeEntity endValue) {
setRange(startValue, endValue, null);
}
/**
* 设置日期时间范围
*/
public void setRange(DatimeEntity startValue, DatimeEntity endValue, DatimeEntity defaultValue) {
if (startValue == null) {
startValue = DatimeEntity.now();
}
if (endValue == null) {
endValue = DatimeEntity.yearOnFuture(10);
}
if (defaultValue == null) {
defaultValue = startValue;
}
dateWheelLayout.setRange(startValue.getDate(), endValue.getDate(), defaultValue.getDate());
timeWheelLayout.setRange(null, null, defaultValue.getTime());
this.startValue = startValue;
this.endValue = endValue;
}
public void setDefaultValue(DatimeEntity defaultValue) {
if (defaultValue == null) {
defaultValue = DatimeEntity.now();
}
dateWheelLayout.setDefaultValue(defaultValue.getDate());
timeWheelLayout.setDefaultValue(defaultValue.getTime());
}
public void setDateFormatter(DateFormatter dateFormatter) {
dateWheelLayout.setDateFormatter(dateFormatter);
}
public void setTimeFormatter(TimeFormatter timeFormatter) {
timeWheelLayout.setTimeFormatter(timeFormatter);
}
public void setDateLabel(CharSequence year, CharSequence month, CharSequence day) {
dateWheelLayout.setDateLabel(year, month, day);
}
public void setTimeLabel(CharSequence hour, CharSequence minute, CharSequence second) {
timeWheelLayout.setTimeLabel(hour, minute, second);
}
public void setOnDatimeSelectedListener(OnDatimeSelectedListener onDatimeSelectedListener) {
this.onDatimeSelectedListener = onDatimeSelectedListener;
}
public void setResetWhenLinkage(boolean dateResetWhenLinkage, boolean timeResetWhenLinkage) {
dateWheelLayout.setResetWhenLinkage(dateResetWhenLinkage);
timeWheelLayout.setResetWhenLinkage(timeResetWhenLinkage);
}
public final DatimeEntity getStartValue() {
return startValue;
}
public final DatimeEntity getEndValue() {
return endValue;
}
public final DateWheelLayout getDateWheelLayout() {
return dateWheelLayout;
}
public final TimeWheelLayout getTimeWheelLayout() {
return timeWheelLayout;
}
public final NumberWheelView getYearWheelView() {
return dateWheelLayout.getYearWheelView();
}
public final NumberWheelView getMonthWheelView() {
return dateWheelLayout.getMonthWheelView();
}
public final NumberWheelView getDayWheelView() {
return dateWheelLayout.getDayWheelView();
}
public final NumberWheelView getHourWheelView() {
return timeWheelLayout.getHourWheelView();
}
public final NumberWheelView getMinuteWheelView() {
return timeWheelLayout.getMinuteWheelView();
}
public final NumberWheelView getSecondWheelView() {
return timeWheelLayout.getSecondWheelView();
}
public final WheelView getMeridiemWheelView() {
return timeWheelLayout.getMeridiemWheelView();
}
public final TextView getYearLabelView() {
return dateWheelLayout.getYearLabelView();
}
public final TextView getMonthLabelView() {
return dateWheelLayout.getMonthLabelView();
}
public final TextView getDayLabelView() {
return dateWheelLayout.getDayLabelView();
}
public final TextView getHourLabelView() {
return timeWheelLayout.getHourLabelView();
}
public final TextView getMinuteLabelView() {
return timeWheelLayout.getMinuteLabelView();
}
public final TextView getSecondLabelView() {
return timeWheelLayout.getSecondLabelView();
}
public final int getSelectedYear() {
return dateWheelLayout.getSelectedYear();
}
public final int getSelectedMonth() {
return dateWheelLayout.getSelectedMonth();
}
public final int getSelectedDay() {
return dateWheelLayout.getSelectedDay();
}
public final int getSelectedHour() {
return timeWheelLayout.getSelectedHour();
}
public final int getSelectedMinute() {
return timeWheelLayout.getSelectedMinute();
}
public final int getSelectedSecond() {
return timeWheelLayout.getSelectedSecond();
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/widget/LinkageWheelLayout.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.github.gzuliyujiang.wheelpicker.R;
import com.github.gzuliyujiang.wheelpicker.contract.LinkageProvider;
import com.github.gzuliyujiang.wheelpicker.contract.OnLinkageSelectedListener;
import com.github.gzuliyujiang.wheelview.annotation.ScrollState;
import com.github.gzuliyujiang.wheelview.contract.WheelFormatter;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
import java.util.Arrays;
import java.util.List;
/**
* 二三级联动滚轮控件
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/15 11:55
*/
@SuppressWarnings("unused")
public class LinkageWheelLayout extends BaseWheelLayout {
private WheelView firstWheelView, secondWheelView, thirdWheelView;
private TextView firstLabelView, secondLabelView, thirdLabelView;
private ProgressBar loadingView;
private Object firstValue, secondValue, thirdValue;
private int firstIndex, secondIndex, thirdIndex;
private LinkageProvider dataProvider;
private OnLinkageSelectedListener onLinkageSelectedListener;
public LinkageWheelLayout(Context context) {
super(context);
}
public LinkageWheelLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LinkageWheelLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public LinkageWheelLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected int provideLayoutRes() {
return R.layout.wheel_picker_linkage;
}
@CallSuper
@Override
protected List provideWheelViews() {
return Arrays.asList(firstWheelView, secondWheelView, thirdWheelView);
}
@CallSuper
@Override
protected void onInit(@NonNull Context context) {
firstWheelView = findViewById(R.id.wheel_picker_linkage_first_wheel);
secondWheelView = findViewById(R.id.wheel_picker_linkage_second_wheel);
thirdWheelView = findViewById(R.id.wheel_picker_linkage_third_wheel);
firstLabelView = findViewById(R.id.wheel_picker_linkage_first_label);
secondLabelView = findViewById(R.id.wheel_picker_linkage_second_label);
thirdLabelView = findViewById(R.id.wheel_picker_linkage_third_label);
loadingView = findViewById(R.id.wheel_picker_linkage_loading);
}
@CallSuper
@Override
protected void onAttributeSet(@NonNull Context context, @Nullable AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LinkageWheelLayout);
setFirstVisible(typedArray.getBoolean(R.styleable.LinkageWheelLayout_wheel_firstVisible, true));
setThirdVisible(typedArray.getBoolean(R.styleable.LinkageWheelLayout_wheel_thirdVisible, true));
String firstLabel = typedArray.getString(R.styleable.LinkageWheelLayout_wheel_firstLabel);
String secondLabel = typedArray.getString(R.styleable.LinkageWheelLayout_wheel_secondLabel);
String thirdLabel = typedArray.getString(R.styleable.LinkageWheelLayout_wheel_thirdLabel);
typedArray.recycle();
setLabel(firstLabel, secondLabel, thirdLabel);
}
@CallSuper
@Override
public void onWheelSelected(WheelView view, int position) {
int id = view.getId();
if (id == R.id.wheel_picker_linkage_first_wheel) {
firstIndex = position;
secondIndex = 0;
thirdIndex = 0;
changeSecondData();
changeThirdData();
selectedCallback();
return;
}
if (id == R.id.wheel_picker_linkage_second_wheel) {
secondIndex = position;
thirdIndex = 0;
changeThirdData();
selectedCallback();
return;
}
if (id == R.id.wheel_picker_linkage_third_wheel) {
thirdIndex = position;
selectedCallback();
}
}
@CallSuper
@Override
public void onWheelScrollStateChanged(WheelView view, @ScrollState int state) {
int id = view.getId();
if (id == R.id.wheel_picker_linkage_first_wheel) {
secondWheelView.setEnabled(state == ScrollState.IDLE);
thirdWheelView.setEnabled(state == ScrollState.IDLE);
return;
}
if (id == R.id.wheel_picker_linkage_second_wheel) {
firstWheelView.setEnabled(state == ScrollState.IDLE);
thirdWheelView.setEnabled(state == ScrollState.IDLE);
return;
}
if (id == R.id.wheel_picker_linkage_third_wheel) {
firstWheelView.setEnabled(state == ScrollState.IDLE);
secondWheelView.setEnabled(state == ScrollState.IDLE);
}
}
public void setData(@NonNull LinkageProvider provider) {
setFirstVisible(provider.firstLevelVisible());
setThirdVisible(provider.thirdLevelVisible());
if (firstValue != null) {
firstIndex = provider.findFirstIndex(firstValue);
}
if (secondValue != null) {
secondIndex = provider.findSecondIndex(firstIndex, secondValue);
}
if (thirdValue != null) {
thirdIndex = provider.findThirdIndex(firstIndex, secondIndex, thirdValue);
}
dataProvider = provider;
changeFirstData();
changeSecondData();
changeThirdData();
}
public void setDefaultValue(Object first, Object second, Object third) {
if (dataProvider != null) {
firstIndex = dataProvider.findFirstIndex(first);
secondIndex = dataProvider.findSecondIndex(firstIndex, second);
thirdIndex = dataProvider.findThirdIndex(firstIndex, secondIndex, third);
changeFirstData();
changeSecondData();
changeThirdData();
} else {
this.firstValue = first;
this.secondValue = second;
this.thirdValue = third;
}
}
public void setFormatter(WheelFormatter first, WheelFormatter second, WheelFormatter third) {
firstWheelView.setFormatter(first);
secondWheelView.setFormatter(second);
thirdWheelView.setFormatter(third);
}
public void setLabel(CharSequence first, CharSequence second, CharSequence third) {
firstLabelView.setText(first);
secondLabelView.setText(second);
thirdLabelView.setText(third);
}
public void showLoading() {
loadingView.setVisibility(VISIBLE);
}
public void hideLoading() {
loadingView.setVisibility(GONE);
}
public void setOnLinkageSelectedListener(OnLinkageSelectedListener onLinkageSelectedListener) {
this.onLinkageSelectedListener = onLinkageSelectedListener;
}
public void setFirstVisible(boolean visible) {
if (visible) {
firstWheelView.setVisibility(VISIBLE);
firstLabelView.setVisibility(VISIBLE);
} else {
firstWheelView.setVisibility(GONE);
firstLabelView.setVisibility(GONE);
}
}
public void setThirdVisible(boolean visible) {
if (visible) {
thirdWheelView.setVisibility(VISIBLE);
thirdLabelView.setVisibility(VISIBLE);
} else {
thirdWheelView.setVisibility(GONE);
thirdLabelView.setVisibility(GONE);
}
}
private void selectedCallback() {
if (onLinkageSelectedListener == null) {
return;
}
thirdWheelView.post(new Runnable() {
@Override
public void run() {
Object first = firstWheelView.getCurrentItem();
Object second = secondWheelView.getCurrentItem();
Object third = thirdWheelView.getCurrentItem();
onLinkageSelectedListener.onLinkageSelected(first, second, third);
}
});
}
private void changeFirstData() {
firstWheelView.setData(dataProvider.provideFirstData());
firstWheelView.setDefaultPosition(firstIndex);
}
private void changeSecondData() {
secondWheelView.setData(dataProvider.linkageSecondData(firstIndex));
secondWheelView.setDefaultPosition(secondIndex);
}
private void changeThirdData() {
if (!dataProvider.thirdLevelVisible()) {
return;
}
thirdWheelView.setData(dataProvider.linkageThirdData(firstIndex, secondIndex));
thirdWheelView.setDefaultPosition(thirdIndex);
}
public final WheelView getFirstWheelView() {
return firstWheelView;
}
public final WheelView getSecondWheelView() {
return secondWheelView;
}
public final WheelView getThirdWheelView() {
return thirdWheelView;
}
public final TextView getFirstLabelView() {
return firstLabelView;
}
public final TextView getSecondLabelView() {
return secondLabelView;
}
public final TextView getThirdLabelView() {
return thirdLabelView;
}
public final ProgressBar getLoadingView() {
return loadingView;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/widget/NumberWheelLayout.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
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.R;
import com.github.gzuliyujiang.wheelpicker.contract.OnNumberSelectedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionSelectedListener;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
import java.util.ArrayList;
import java.util.List;
/**
* 数字滚轮控件
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/5 18:35
*/
public class NumberWheelLayout extends OptionWheelLayout {
private OnNumberSelectedListener onNumberSelectedListener;
public NumberWheelLayout(Context context) {
super(context);
}
public NumberWheelLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public NumberWheelLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public NumberWheelLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onAttributeSet(@NonNull Context context, @Nullable AttributeSet attrs) {
super.onAttributeSet(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NumberWheelLayout);
float minNumber = typedArray.getFloat(R.styleable.NumberWheelLayout_wheel_minNumber, 0);
float maxNumber = typedArray.getFloat(R.styleable.NumberWheelLayout_wheel_maxNumber, 10);
float stepNumber = typedArray.getFloat(R.styleable.NumberWheelLayout_wheel_stepNumber, 1);
boolean isDecimal = typedArray.getBoolean(R.styleable.NumberWheelLayout_wheel_isDecimal, false);
typedArray.recycle();
if (isDecimal) {
setRange(minNumber, maxNumber, stepNumber);
} else {
setRange((int) minNumber, (int) maxNumber, (int) stepNumber);
}
}
@Override
public void onWheelSelected(WheelView view, int position) {
super.onWheelSelected(view, position);
if (onNumberSelectedListener != null) {
Object item = getWheelView().getItem(position);
onNumberSelectedListener.onNumberSelected(position, (Number) item);
}
}
/**
* @deprecated 使用 {@link #setRange} 代替
*/
@Deprecated
@Override
public void setData(List> data) {
throw new UnsupportedOperationException("Use setRange instead");
}
/**
* @deprecated 使用 {@link #setOnNumberSelectedListener} 代替
*/
@Deprecated
@Override
public void setOnOptionSelectedListener(OnOptionSelectedListener onOptionSelectedListener) {
throw new UnsupportedOperationException("Use setOnNumberSelectedListener instead");
}
public void setOnNumberSelectedListener(OnNumberSelectedListener onNumberSelectedListener) {
this.onNumberSelectedListener = onNumberSelectedListener;
}
public void setRange(int min, int max, int step) {
int minValue = Math.min(min, max);
int maxValue = Math.max(min, max);
// 指定初始容量,避免OutOfMemory
int capacity = (maxValue - minValue) / step;
List data = new ArrayList<>(capacity);
for (int i = minValue; i <= maxValue; i = i + step) {
data.add(i);
}
super.setData(data);
}
public void setRange(float min, float max, float step) {
float minValue = Math.min(min, max);
float maxValue = Math.max(min, max);
// 指定初始容量,避免OutOfMemory
int capacity = (int) ((maxValue - minValue) / step);
List data = new ArrayList<>(capacity);
for (float i = minValue; i <= maxValue; i = i + step) {
data.add(i);
}
super.setData(data);
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/widget/OptionWheelLayout.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.github.gzuliyujiang.wheelpicker.R;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionSelectedListener;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
import java.util.Collections;
import java.util.List;
/**
* 单项滚轮控件
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/6 23:13
*/
@SuppressWarnings("unused")
public class OptionWheelLayout extends BaseWheelLayout {
private WheelView wheelView;
private TextView labelView;
private OnOptionSelectedListener onOptionSelectedListener;
public OptionWheelLayout(Context context) {
super(context);
}
public OptionWheelLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public OptionWheelLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public OptionWheelLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected int provideLayoutRes() {
return R.layout.wheel_picker_option;
}
@CallSuper
@Override
protected List provideWheelViews() {
return Collections.singletonList(wheelView);
}
@CallSuper
@Override
protected void onInit(@NonNull Context context) {
wheelView = findViewById(R.id.wheel_picker_option_wheel);
labelView = findViewById(R.id.wheel_picker_option_label);
}
@CallSuper
@Override
protected void onAttributeSet(@NonNull Context context, @Nullable AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.OptionWheelLayout);
labelView.setText(typedArray.getString(R.styleable.OptionWheelLayout_wheel_label));
typedArray.recycle();
}
@CallSuper
@Override
public void onWheelSelected(WheelView view, int position) {
if (onOptionSelectedListener != null) {
onOptionSelectedListener.onOptionSelected(position, wheelView.getItem(position));
}
}
public void setData(List> data) {
wheelView.setData(data);
}
public void setDefaultValue(Object value) {
wheelView.setDefaultValue(value);
}
public void setDefaultPosition(int position) {
wheelView.setDefaultPosition(position);
}
public void setOnOptionSelectedListener(OnOptionSelectedListener onOptionSelectedListener) {
this.onOptionSelectedListener = onOptionSelectedListener;
}
public final WheelView getWheelView() {
return wheelView;
}
public final TextView getLabelView() {
return labelView;
}
}
================================================
FILE: WheelPicker/src/main/java/com/github/gzuliyujiang/wheelpicker/widget/TimeWheelLayout.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelpicker.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.github.gzuliyujiang.wheelpicker.R;
import com.github.gzuliyujiang.wheelpicker.annotation.TimeMode;
import com.github.gzuliyujiang.wheelpicker.contract.OnTimeMeridiemSelectedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnTimeSelectedListener;
import com.github.gzuliyujiang.wheelpicker.contract.TimeFormatter;
import com.github.gzuliyujiang.wheelpicker.entity.TimeEntity;
import com.github.gzuliyujiang.wheelpicker.impl.SimpleTimeFormatter;
import com.github.gzuliyujiang.wheelview.annotation.ScrollState;
import com.github.gzuliyujiang.wheelview.contract.WheelFormatter;
import com.github.gzuliyujiang.wheelview.widget.NumberWheelView;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
import java.util.Arrays;
import java.util.List;
/**
* 时间滚轮控件
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/6/5 16:20
*/
@SuppressWarnings("unused")
public class TimeWheelLayout extends BaseWheelLayout {
private NumberWheelView hourWheelView;
private NumberWheelView minuteWheelView;
private NumberWheelView secondWheelView;
private TextView hourLabelView;
private TextView minuteLabelView;
private TextView secondLabelView;
private WheelView meridiemWheelView;
private TimeEntity startValue;
private TimeEntity endValue;
private TimeEntity defaultValue;
private Integer selectedHour;
private Integer selectedMinute;
private Integer selectedSecond;
private boolean isAnteMeridiem;
private int timeMode;
private int hourStep = 1;
private int minuteStep = 1;
private int secondStep = 1;
private OnTimeSelectedListener onTimeSelectedListener;
private OnTimeMeridiemSelectedListener onTimeMeridiemSelectedListener;
private boolean resetWhenLinkage = true;
public TimeWheelLayout(Context context) {
super(context);
}
public TimeWheelLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TimeWheelLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public TimeWheelLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected int provideLayoutRes() {
return R.layout.wheel_picker_time;
}
@Override
protected List provideWheelViews() {
return Arrays.asList(hourWheelView, minuteWheelView, secondWheelView, meridiemWheelView);
}
@Override
protected void onInit(@NonNull Context context) {
hourWheelView = findViewById(R.id.wheel_picker_time_hour_wheel);
minuteWheelView = findViewById(R.id.wheel_picker_time_minute_wheel);
secondWheelView = findViewById(R.id.wheel_picker_time_second_wheel);
hourLabelView = findViewById(R.id.wheel_picker_time_hour_label);
minuteLabelView = findViewById(R.id.wheel_picker_time_minute_label);
secondLabelView = findViewById(R.id.wheel_picker_time_second_label);
meridiemWheelView = findViewById(R.id.wheel_picker_time_meridiem_wheel);
}
@Override
protected void onAttributeSet(@NonNull Context context, @Nullable AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TimeWheelLayout);
setTimeMode(typedArray.getInt(R.styleable.TimeWheelLayout_wheel_timeMode, TimeMode.HOUR_24_NO_SECOND));
String hourLabel = typedArray.getString(R.styleable.TimeWheelLayout_wheel_hourLabel);
String minuteLabel = typedArray.getString(R.styleable.TimeWheelLayout_wheel_minuteLabel);
String secondLabel = typedArray.getString(R.styleable.TimeWheelLayout_wheel_secondLabel);
typedArray.recycle();
setTimeLabel(hourLabel, minuteLabel, secondLabel);
setTimeFormatter(new SimpleTimeFormatter(this));
}
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility == VISIBLE && startValue == null && endValue == null) {
setRange(TimeEntity.target(0, 0, 0),
TimeEntity.target(23, 59, 59), TimeEntity.now());
}
}
@Override
public void onWheelSelected(WheelView view, int position) {
int id = view.getId();
if (id == R.id.wheel_picker_time_hour_wheel) {
selectedHour = hourWheelView.getItem(position);
if (resetWhenLinkage) {
selectedMinute = null;
selectedSecond = null;
}
changeMinute(selectedHour);
timeSelectedCallback();
return;
}
if (id == R.id.wheel_picker_time_minute_wheel) {
selectedMinute = minuteWheelView.getItem(position);
if (resetWhenLinkage) {
selectedSecond = null;
}
changeSecond();
timeSelectedCallback();
return;
}
if (id == R.id.wheel_picker_time_second_wheel) {
selectedSecond = secondWheelView.getItem(position);
timeSelectedCallback();
return;
}
if (id == R.id.wheel_picker_time_meridiem_wheel) {
isAnteMeridiem = "AM".equalsIgnoreCase(meridiemWheelView.getItem(position));
timeSelectedCallback();
}
}
@Override
public void onWheelScrollStateChanged(WheelView view, @ScrollState int state) {
int id = view.getId();
if (id == R.id.wheel_picker_time_hour_wheel) {
minuteWheelView.setEnabled(state == ScrollState.IDLE);
secondWheelView.setEnabled(state == ScrollState.IDLE);
return;
}
if (id == R.id.wheel_picker_time_minute_wheel) {
hourWheelView.setEnabled(state == ScrollState.IDLE);
secondWheelView.setEnabled(state == ScrollState.IDLE);
return;
}
if (id == R.id.wheel_picker_time_second_wheel) {
hourWheelView.setEnabled(state == ScrollState.IDLE);
minuteWheelView.setEnabled(state == ScrollState.IDLE);
}
}
private void timeSelectedCallback() {
if (onTimeSelectedListener != null) {
secondWheelView.post(new Runnable() {
@Override
public void run() {
onTimeSelectedListener.onTimeSelected(selectedHour, selectedMinute, selectedSecond);
}
});
}
if (onTimeMeridiemSelectedListener != null) {
secondWheelView.post(new Runnable() {
@Override
public void run() {
onTimeMeridiemSelectedListener.onTimeSelected(selectedHour, selectedMinute, selectedSecond, isAnteMeridiem());
}
});
}
}
public void setTimeMode(@TimeMode int timeMode) {
this.timeMode = timeMode;
hourWheelView.setVisibility(View.VISIBLE);
hourLabelView.setVisibility(View.VISIBLE);
minuteWheelView.setVisibility(View.VISIBLE);
minuteLabelView.setVisibility(View.VISIBLE);
secondWheelView.setVisibility(View.VISIBLE);
secondLabelView.setVisibility(View.VISIBLE);
meridiemWheelView.setVisibility(View.GONE);
if (timeMode == TimeMode.NONE) {
hourWheelView.setVisibility(View.GONE);
hourLabelView.setVisibility(View.GONE);
minuteWheelView.setVisibility(View.GONE);
minuteLabelView.setVisibility(View.GONE);
secondWheelView.setVisibility(View.GONE);
secondLabelView.setVisibility(View.GONE);
this.timeMode = timeMode;
return;
}
if (timeMode == TimeMode.HOUR_12_NO_SECOND
|| timeMode == TimeMode.HOUR_24_NO_SECOND) {
secondWheelView.setVisibility(View.GONE);
secondLabelView.setVisibility(View.GONE);
}
if (isHour12Mode()) {
meridiemWheelView.setVisibility(View.VISIBLE);
meridiemWheelView.setData(Arrays.asList("AM", "PM"));
}
}
public boolean isHour12Mode() {
return timeMode == TimeMode.HOUR_12_NO_SECOND
|| timeMode == TimeMode.HOUR_12_HAS_SECOND;
}
/**
* 设置日期时间范围
*/
public void setRange(TimeEntity startValue, TimeEntity endValue) {
setRange(startValue, endValue, null);
}
/**
* 设置日期时间范围
*/
public void setRange(TimeEntity startValue, TimeEntity endValue, TimeEntity defaultValue) {
if (startValue == null) {
startValue = TimeEntity.target(isHour12Mode() ? 1 : 0, 0, 0);
}
if (endValue == null) {
endValue = TimeEntity.target(isHour12Mode() ? 12 : 23, 59, 59);
}
if (endValue.toTimeInMillis() < startValue.toTimeInMillis()) {
throw new IllegalArgumentException("Ensure the start time is less than the time date");
}
this.startValue = startValue;
this.endValue = endValue;
if (defaultValue == null) {
defaultValue = startValue;
}
this.defaultValue = defaultValue;
isAnteMeridiem = defaultValue.getHour() < 12 || defaultValue.getHour() == 24;
selectedHour = fixHour(defaultValue.getHour());
selectedMinute = defaultValue.getMinute();
selectedSecond = defaultValue.getSecond();
changeHour();
changeAnteMeridiem();
}
public void setDefaultValue(@NonNull final TimeEntity defaultValue) {
setRange(startValue, endValue, defaultValue);
}
public void setTimeFormatter(final TimeFormatter timeFormatter) {
if (timeFormatter == null) {
return;
}
hourWheelView.setFormatter(new WheelFormatter() {
@Override
public String formatItem(@NonNull Object value) {
return timeFormatter.formatHour((Integer) value);
}
});
minuteWheelView.setFormatter(new WheelFormatter() {
@Override
public String formatItem(@NonNull Object value) {
return timeFormatter.formatMinute((Integer) value);
}
});
secondWheelView.setFormatter(new WheelFormatter() {
@Override
public String formatItem(@NonNull Object value) {
return timeFormatter.formatSecond((Integer) value);
}
});
}
public void setTimeLabel(CharSequence hour, CharSequence minute, CharSequence second) {
hourLabelView.setText(hour);
minuteLabelView.setText(minute);
secondLabelView.setText(second);
}
public void setOnTimeSelectedListener(OnTimeSelectedListener onTimeSelectedListener) {
this.onTimeSelectedListener = onTimeSelectedListener;
}
public void setOnTimeMeridiemSelectedListener(OnTimeMeridiemSelectedListener onTimeMeridiemSelectedListener) {
this.onTimeMeridiemSelectedListener = onTimeMeridiemSelectedListener;
}
public void setResetWhenLinkage(boolean resetWhenLinkage) {
this.resetWhenLinkage = resetWhenLinkage;
}
public void setTimeStep(int hourStep, int minuteStep, int secondStep) {
this.hourStep = hourStep;
this.minuteStep = minuteStep;
this.secondStep = secondStep;
if (isDataAlready()) {
setRange(startValue, endValue, defaultValue);
}
}
protected boolean isDataAlready() {
return startValue != null && endValue != null;
}
public final TimeEntity getStartValue() {
return startValue;
}
public final TimeEntity getEndValue() {
return endValue;
}
public final NumberWheelView getHourWheelView() {
return hourWheelView;
}
public final NumberWheelView getMinuteWheelView() {
return minuteWheelView;
}
public final NumberWheelView getSecondWheelView() {
return secondWheelView;
}
public final TextView getHourLabelView() {
return hourLabelView;
}
public final TextView getMinuteLabelView() {
return minuteLabelView;
}
public final TextView getSecondLabelView() {
return secondLabelView;
}
public final WheelView getMeridiemWheelView() {
return meridiemWheelView;
}
@Deprecated
public final TextView getMeridiemLabelView() {
throw new UnsupportedOperationException("Use getMeridiemWheelView instead");
}
public final int getSelectedHour() {
int hour = hourWheelView.getCurrentItem();
return fixHour(hour);
}
private int fixHour(int hour) {
if (isHour12Mode()) {
if (hour == 0) {
hour = 24;
}
if (hour > 12) {
hour = hour - 12;
}
}
return hour;
}
public final int getSelectedMinute() {
return minuteWheelView.getCurrentItem();
}
public final int getSelectedSecond() {
if (timeMode == TimeMode.HOUR_12_NO_SECOND
|| timeMode == TimeMode.HOUR_24_NO_SECOND) {
return 0;
}
return secondWheelView.getCurrentItem();
}
public final boolean isAnteMeridiem() {
Object currentItem = meridiemWheelView.getCurrentItem();
if (currentItem == null) {
return isAnteMeridiem;
}
return "AM".equalsIgnoreCase(currentItem.toString());
}
private void changeHour() {
int min = Math.min(startValue.getHour(), endValue.getHour());
int max = Math.max(startValue.getHour(), endValue.getHour());
int minHour = isHour12Mode() ? 1 : 0;
int maxHour = isHour12Mode() ? 12 : 23;
min = Math.max(minHour, min);
max = Math.min(maxHour, max);
if (selectedHour == null) {
selectedHour = min;
} else {
selectedHour = Math.max(selectedHour, min);
selectedHour = Math.min(selectedHour, max);
}
hourWheelView.setRange(min, max, hourStep);
hourWheelView.setDefaultValue(selectedHour);
changeMinute(selectedHour);
}
private void changeMinute(int hour) {
final int min, max;
//开始时及结束时相同情况
if (hour == startValue.getHour() && hour == endValue.getHour()) {
min = startValue.getMinute();
max = endValue.getMinute();
}
//开始时相同情况
else if (hour == startValue.getHour()) {
min = startValue.getMinute();
max = 59;
}
//结束时相同情况
else if (hour == endValue.getHour()) {
min = 0;
max = endValue.getMinute();
} else {
min = 0;
max = 59;
}
if (selectedMinute == null) {
selectedMinute = min;
} else {
selectedMinute = Math.max(selectedMinute, min);
selectedMinute = Math.min(selectedMinute, max);
}
minuteWheelView.setRange(min, max, minuteStep);
minuteWheelView.setDefaultValue(selectedMinute);
changeSecond();
}
private void changeSecond() {
if (selectedSecond == null) {
selectedSecond = 0;
}
secondWheelView.setRange(0, 59, secondStep);
secondWheelView.setDefaultValue(selectedSecond);
}
private void changeAnteMeridiem() {
meridiemWheelView.setDefaultValue(isAnteMeridiem ? "AM" : "PM");
}
}
================================================
FILE: WheelPicker/src/main/res/layout/wheel_picker_date.xml
================================================
================================================
FILE: WheelPicker/src/main/res/layout/wheel_picker_datime.xml
================================================
================================================
FILE: WheelPicker/src/main/res/layout/wheel_picker_linkage.xml
================================================
================================================
FILE: WheelPicker/src/main/res/layout/wheel_picker_number.xml
================================================
================================================
FILE: WheelPicker/src/main/res/layout/wheel_picker_option.xml
================================================
================================================
FILE: WheelPicker/src/main/res/layout/wheel_picker_time.xml
================================================
================================================
FILE: WheelPicker/src/main/res/values/wheel_attrs.xml
================================================
================================================
FILE: WheelView/README.md
================================================
# 滚轮
仿 IOS 风格的滚轮控件。
================================================
FILE: WheelView/build.gradle
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
apply from: "${rootDir}/gradle/library.gradle"
apply from: "${rootDir}/gradle/publish.gradle"
dependencies {
implementation androidxLibrary.annotation
}
================================================
FILE: WheelView/consumer-rules.pro
================================================
# 本库模块专用的混淆规则
================================================
FILE: WheelView/src/main/AndroidManifest.xml
================================================
================================================
FILE: WheelView/src/main/java/com/github/gzuliyujiang/wheelview/annotation/CurtainCorner.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelview.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/10/28 11:27
*/
@Retention(RetentionPolicy.SOURCE)
public @interface CurtainCorner {
int NONE = 0;
int ALL = 1;
int TOP = 2;
int BOTTOM = 3;
int LEFT = 4;
int RIGHT = 5;
}
================================================
FILE: WheelView/src/main/java/com/github/gzuliyujiang/wheelview/annotation/ItemTextAlign.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelview.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* 滚轮条目文本对齐方式
*
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/6/19 12:04
*/
@Retention(RetentionPolicy.SOURCE)
public @interface ItemTextAlign {
int CENTER = 0;
int LEFT = 1;
int RIGHT = 2;
}
================================================
FILE: WheelView/src/main/java/com/github/gzuliyujiang/wheelview/annotation/ScrollState.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelview.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2021/10/28 11:28
*/
@Retention(RetentionPolicy.SOURCE)
public @interface ScrollState {
int IDLE = 0;
int DRAGGING = 1;
int SCROLLING = 2;
}
================================================
FILE: WheelView/src/main/java/com/github/gzuliyujiang/wheelview/contract/OnWheelChangedListener.java
================================================
/*
* Copyright (c) 2016-present 贵州纳雍穿青人李裕江<1032694760@qq.com>
*
* The software is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package com.github.gzuliyujiang.wheelview.contract;
import com.github.gzuliyujiang.wheelview.annotation.ScrollState;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
/**
* 滚轮滑动接口
*
* @author Florent Champigny
* @author 贵州山野羡民(1032694760@qq.com)
* @since 2019/5/14 20:03
*/
public interface OnWheelChangedListener {
/**
* Invoke when scroll stopped
* Will return a distance offset which between current scroll position and
* initial position, this offset is a positive or a negative, positive means
* scrolling from bottom to top, negative means scrolling from top to bottom
*
* @param view wheel view
* @param offset Distance offset which between current scroll position and initial position
*/
void onWheelScrolled(WheelView view, int offset);
/**
* Invoke when scroll stopped
* This method will be called when wheel stop and return current selected item data's
* position in list
*
* @param view wheel view
* @param position Current selected item data's position in list
*/
void onWheelSelected(WheelView view, int position);
/**
* Invoke when scroll state changed
* The state always between idle, dragging, and scrolling, this method will
* be called when they switch
*
* @param view wheel view
* @param state {@link ScrollState#IDLE}
* {@link ScrollState#DRAGGING}
* {@link ScrollState#SCROLLING}
*