From 1bb8d891a0de6a114af9d407ce5c0e82dd470175 Mon Sep 17 00:00:00 2001 From: Tamsiree Date: Mon, 23 Mar 2020 21:42:53 +0800 Subject: [PATCH] convert to kotlin. --- .../rxdemo/activity/ActivityCreateQRCode.kt | 2 +- .../rxdemo/activity/ActivityRunTextView.kt | 2 +- .../rxdemo/activity/ActivityRxCaptcha.kt | 4 +- .../rxdemo/activity/ActivityRxToast.kt | 12 +- .../activity/ActivitySlidingDrawerSingle.kt | 2 +- .../rxfeature/activity/ActivityCodeTool.kt | 101 +--- .../java/com/tamsiree/rxkit/RxDeviceTool.kt | 14 +- .../java/com/tamsiree/rxkit/RxImageTool.kt | 10 +- .../main/java/com/tamsiree/rxkit/RxNetTool.kt | 2 +- .../main/java/com/tamsiree/rxkit/RxTool.java | 366 ------------ .../main/java/com/tamsiree/rxkit/RxTool.kt | 356 ++++++++++++ .../java/com/tamsiree/rxkit/RxWebViewTool.kt | 2 +- .../rxkit/interfaces/OnWebViewLoad.kt | 2 +- .../java/com/tamsiree/rxkit/view/RxToast.java | 401 ------------- .../java/com/tamsiree/rxkit/view/RxToast.kt | 430 ++++++++++++++ .../tamsiree/rxui/activity/ActivityWebView.kt | 4 +- .../adapter/AdapterCardViewModelPicture.java | 86 --- .../adapter/AdapterCardViewModelPicture.kt | 66 +++ .../rxui/animation/FloatAnimation.java | 48 -- .../tamsiree/rxui/animation/FloatAnimation.kt | 45 ++ .../animation/RxAbstractPathAnimator.java | 107 ---- .../rxui/animation/RxAbstractPathAnimator.kt | 104 ++++ .../rxui/animation/RxPathAnimator.java | 72 --- .../tamsiree/rxui/animation/RxPathAnimator.kt | 55 ++ .../rxui/animation/RxShineAnimator.java | 44 -- .../rxui/animation/RxShineAnimator.kt | 36 ++ .../java/com/tamsiree/rxui/view/RxTitle.java | 548 ------------------ .../java/com/tamsiree/rxui/view/RxTitle.kt | 500 ++++++++++++++++ RxUI/src/main/res/layout/include_rx_title.xml | 4 +- 29 files changed, 1650 insertions(+), 1775 deletions(-) delete mode 100644 RxKit/src/main/java/com/tamsiree/rxkit/RxTool.java create mode 100644 RxKit/src/main/java/com/tamsiree/rxkit/RxTool.kt delete mode 100644 RxKit/src/main/java/com/tamsiree/rxkit/view/RxToast.java create mode 100644 RxKit/src/main/java/com/tamsiree/rxkit/view/RxToast.kt delete mode 100644 RxUI/src/main/java/com/tamsiree/rxui/adapter/AdapterCardViewModelPicture.java create mode 100644 RxUI/src/main/java/com/tamsiree/rxui/adapter/AdapterCardViewModelPicture.kt delete mode 100644 RxUI/src/main/java/com/tamsiree/rxui/animation/FloatAnimation.java create mode 100644 RxUI/src/main/java/com/tamsiree/rxui/animation/FloatAnimation.kt delete mode 100644 RxUI/src/main/java/com/tamsiree/rxui/animation/RxAbstractPathAnimator.java create mode 100644 RxUI/src/main/java/com/tamsiree/rxui/animation/RxAbstractPathAnimator.kt delete mode 100644 RxUI/src/main/java/com/tamsiree/rxui/animation/RxPathAnimator.java create mode 100644 RxUI/src/main/java/com/tamsiree/rxui/animation/RxPathAnimator.kt delete mode 100644 RxUI/src/main/java/com/tamsiree/rxui/animation/RxShineAnimator.java create mode 100644 RxUI/src/main/java/com/tamsiree/rxui/animation/RxShineAnimator.kt delete mode 100644 RxUI/src/main/java/com/tamsiree/rxui/view/RxTitle.java create mode 100644 RxUI/src/main/java/com/tamsiree/rxui/view/RxTitle.kt diff --git a/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityCreateQRCode.kt b/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityCreateQRCode.kt index b1911c59..aa7d8c1a 100644 --- a/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityCreateQRCode.kt +++ b/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityCreateQRCode.kt @@ -42,7 +42,7 @@ class ActivityCreateQRCode : ActivityBase(), View.OnClickListener { override fun initView() { rx_title.setLeftFinish(mContext) - rx_title.title = "动态生成码" + rx_title.setTitle("动态生成码") ll_refresh.setOnClickListener(this) } diff --git a/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityRunTextView.kt b/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityRunTextView.kt index e24376a3..8e4fc334 100644 --- a/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityRunTextView.kt +++ b/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityRunTextView.kt @@ -39,7 +39,7 @@ class ActivityRunTextView : ActivityBase() { text.setText(26f, 5, -0x899eaa) //设置属性 text.setTextStillTime(3000) //设置停留时长间隔 text.setAnimTime(300) //设置进入和退出的时间间隔 - text.setOnItemClickListener { position -> RxToast.success(mContext, "点击了 : " + titleList[position], Toast.LENGTH_SHORT, true).show() } + text.setOnItemClickListener { position -> RxToast.success(mContext, "点击了 : " + titleList[position], Toast.LENGTH_SHORT, true)?.show() } val views: MutableList = ArrayList() setUPMarqueeView(views, 11) upview1.setViews(views) diff --git a/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityRxCaptcha.kt b/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityRxCaptcha.kt index a05da213..1acf9da4 100644 --- a/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityRxCaptcha.kt +++ b/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityRxCaptcha.kt @@ -34,14 +34,14 @@ class ActivityRxCaptcha : ActivityBase() { rx_title.setLeftFinish(mContext) swipeCaptchaView.onCaptchaMatchCallback = object : OnCaptchaMatchCallback { override fun matchSuccess(rxSwipeCaptcha: RxSwipeCaptcha) { - RxToast.success(mContext, "验证通过!", Toast.LENGTH_SHORT).show() + RxToast.success(mContext, "验证通过!", Toast.LENGTH_SHORT)?.show() //swipeCaptcha.createCaptcha(); dragBar.isEnabled = false } override fun matchFailed(rxSwipeCaptcha: RxSwipeCaptcha) { Log.d("zxt", "matchFailed() called with: rxSwipeCaptcha = [$rxSwipeCaptcha]") - RxToast.error(mContext, "验证失败:拖动滑块将悬浮头像正确拼合", Toast.LENGTH_SHORT).show() + RxToast.error(mContext, "验证失败:拖动滑块将悬浮头像正确拼合", Toast.LENGTH_SHORT)?.show() rxSwipeCaptcha.resetCaptcha() dragBar.progress = 0 } diff --git a/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityRxToast.kt b/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityRxToast.kt index a72172d9..0052b776 100644 --- a/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityRxToast.kt +++ b/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivityRxToast.kt @@ -21,14 +21,14 @@ class ActivityRxToast : ActivityBase() { override fun initView() { rx_title.setOnClickListener { finish() } - button_error_toast.setOnClickListener { RxToast.error(mContext, "这是一个提示错误的Toast!", Toast.LENGTH_SHORT, true).show() } - button_success_toast.setOnClickListener { RxToast.success(mContext, "这是一个提示成功的Toast!", Toast.LENGTH_SHORT, true).show() } - button_info_toast.setOnClickListener { RxToast.info(mContext, "这是一个提示信息的Toast.", Toast.LENGTH_SHORT, true).show() } - button_warning_toast.setOnClickListener { RxToast.warning(mContext, "这是一个提示警告的Toast.", Toast.LENGTH_SHORT, true).show() } - button_normal_toast_wo_icon.setOnClickListener { RxToast.normal(mContext, "这是一个普通的没有ICON的Toast").show() } + button_error_toast.setOnClickListener { RxToast.error(mContext, "这是一个提示错误的Toast!", Toast.LENGTH_SHORT, true)?.show() } + button_success_toast.setOnClickListener { RxToast.success(mContext, "这是一个提示成功的Toast!", Toast.LENGTH_SHORT, true)?.show() } + button_info_toast.setOnClickListener { RxToast.info(mContext, "这是一个提示信息的Toast.", Toast.LENGTH_SHORT, true)?.show() } + button_warning_toast.setOnClickListener { RxToast.warning(mContext, "这是一个提示警告的Toast.", Toast.LENGTH_SHORT, true)?.show() } + button_normal_toast_wo_icon.setOnClickListener { RxToast.normal(mContext, "这是一个普通的没有ICON的Toast")?.show() } button_normal_toast_w_icon.setOnClickListener { val icon = resources.getDrawable(R.drawable.clover) - RxToast.normal(mContext, "这是一个普通的包含ICON的Toast", icon).show() + RxToast.normal(mContext, "这是一个普通的包含ICON的Toast", icon)?.show() } } diff --git a/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivitySlidingDrawerSingle.kt b/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivitySlidingDrawerSingle.kt index abe8d93c..b5a9a50a 100644 --- a/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivitySlidingDrawerSingle.kt +++ b/RxDemo/src/main/java/com/tamsiree/rxdemo/activity/ActivitySlidingDrawerSingle.kt @@ -61,7 +61,7 @@ class ActivitySlidingDrawerSingle : ActivityBase() { pb_web_base?.visibility = View.VISIBLE } - override fun onReceivedTitle(title: String?) { + override fun onReceivedTitle(title: String) { // rx_title.title = title } diff --git a/RxFeature/src/main/java/com/tamsiree/rxfeature/activity/ActivityCodeTool.kt b/RxFeature/src/main/java/com/tamsiree/rxfeature/activity/ActivityCodeTool.kt index 09778cf6..46def8ba 100644 --- a/RxFeature/src/main/java/com/tamsiree/rxfeature/activity/ActivityCodeTool.kt +++ b/RxFeature/src/main/java/com/tamsiree/rxfeature/activity/ActivityCodeTool.kt @@ -3,49 +3,26 @@ package com.tamsiree.rxfeature.activity import android.graphics.Color import android.os.Bundle import android.view.View -import android.widget.EditText -import android.widget.ImageView -import android.widget.LinearLayout -import androidx.core.widget.NestedScrollView import com.tamsiree.rxfeature.R import com.tamsiree.rxfeature.tool.RxBarCode import com.tamsiree.rxfeature.tool.RxQRCode import com.tamsiree.rxkit.* import com.tamsiree.rxkit.view.RxToast import com.tamsiree.rxui.activity.ActivityBase -import com.tamsiree.rxui.view.RxTitle import com.tamsiree.rxui.view.ticker.RxTickerUtils -import com.tamsiree.rxui.view.ticker.RxTickerView +import kotlinx.android.synthetic.main.activity_code_tool.* /** * @author tamsiree */ class ActivityCodeTool : ActivityBase() { - private var mRxTitle: RxTitle? = null - private var mEtQrCode: EditText? = null - private var mIvCreateQrCode: ImageView? = null - private var mIvQrCode: ImageView? = null - private var mActivityCodeTool: LinearLayout? = null - private var mLlCode: LinearLayout? = null - private var mLlQrRoot: LinearLayout? = null - private var mEtBarCode: EditText? = null - private var mIvCreateBarCode: ImageView? = null - private var mIvBarCode: ImageView? = null - private var mLlBarCode: LinearLayout? = null - private var mLlBarRoot: LinearLayout? = null - private var mLlScaner: LinearLayout? = null - private var mLlQr: LinearLayout? = null - private var mLlBar: LinearLayout? = null - private var mRxTickerViewMade: RxTickerView? = null - private var mRxTickerViewScan: RxTickerView? = null - private var nestedScrollView: NestedScrollView? = null + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) RxBarTool.noTitle(this) RxBarTool.setTransparentStatusBar(this) setContentView(R.layout.activity_code_tool) RxDeviceTool.setPortrait(this) - } override fun onResume() { @@ -54,26 +31,8 @@ class ActivityCodeTool : ActivityBase() { } override fun initView() { - mRxTitle = findViewById(R.id.rx_title) - mEtQrCode = findViewById(R.id.et_qr_code) - mIvCreateQrCode = findViewById(R.id.iv_create_qr_code) - mIvQrCode = findViewById(R.id.iv_qr_code) - mActivityCodeTool = findViewById(R.id.activity_code_tool) - mLlCode = findViewById(R.id.ll_code) - mLlQrRoot = findViewById(R.id.ll_qr_root) - nestedScrollView = findViewById(R.id.nestedScrollView) - mEtBarCode = findViewById(R.id.et_bar_code) - mIvCreateBarCode = findViewById(R.id.iv_create_bar_code) - mIvBarCode = findViewById(R.id.iv_bar_code) - mLlBarCode = findViewById(R.id.ll_bar_code) - mLlBarRoot = findViewById(R.id.ll_bar_root) - mLlScaner = findViewById(R.id.ll_scaner) - mLlQr = findViewById(R.id.ll_qr) - mLlBar = findViewById(R.id.ll_bar) - mRxTickerViewScan = findViewById(R.id.ticker_scan_count) - mRxTickerViewScan?.setCharacterList(NUMBER_LIST) - mRxTickerViewMade = findViewById(R.id.ticker_made_count) - mRxTickerViewMade?.setCharacterList(NUMBER_LIST) + ticker_scan_count.setCharacterList(NUMBER_LIST) + ticker_made_count.setCharacterList(NUMBER_LIST) updateMadeCodeCount() } @@ -82,64 +41,64 @@ class ActivityCodeTool : ActivityBase() { } private fun updateScanCodeCount() { - mRxTickerViewScan!!.setText(RxDataTool.stringToInt(RxSPTool.getContent(mContext.baseContext!!, RxConstants.SP_SCAN_CODE)).toString() + "", true) + ticker_scan_count!!.setText(RxDataTool.stringToInt(RxSPTool.getContent(mContext.baseContext!!, RxConstants.SP_SCAN_CODE)).toString() + "", true) } private fun updateMadeCodeCount() { - mRxTickerViewMade!!.setText(RxDataTool.stringToInt(RxSPTool.getContent(mContext.baseContext!!, RxConstants.SP_MADE_CODE)).toString() + "", true) + ticker_made_count.setText(RxDataTool.stringToInt(RxSPTool.getContent(mContext.baseContext!!, RxConstants.SP_MADE_CODE)).toString() + "", true) } private fun initEvent() { - mRxTitle!!.isLeftIconVisibility = true - mRxTitle!!.titleColor = Color.WHITE - mRxTitle!!.titleSize = RxImageTool.dp2px(20f) - mRxTitle!!.setLeftFinish(mContext) - mRxTickerViewScan!!.animationDuration = 500 - mIvCreateQrCode!!.setOnClickListener { - val str = mEtQrCode!!.text.toString() + rx_title.setLeftIconVisibility(true) + rx_title.setTitleColor(Color.WHITE) + rx_title.setTitleSize(RxImageTool.dp2px(20f)) + rx_title.setLeftFinish(mContext) + ticker_scan_count!!.animationDuration = 500 + iv_create_qr_code.setOnClickListener { + val str = et_qr_code.text.toString() if (RxDataTool.isNullString(str)) { RxToast.error("二维码文字内容不能为空!") } else { - mLlCode!!.visibility = View.VISIBLE + ll_code!!.visibility = View.VISIBLE //二维码生成方式一 推荐此方法 - RxQRCode.builder(str).backColor(-0x1).codeColor(-0x1000000).codeSide(600).codeLogo(resources.getDrawable(R.drawable.faviconhandsome)).codeBorder(1).into(mIvQrCode) + RxQRCode.builder(str).backColor(-0x1).codeColor(-0x1000000).codeSide(600).codeLogo(resources.getDrawable(R.drawable.faviconhandsome)).codeBorder(1).into(iv_qr_code) //二维码生成方式二 默认宽和高都为800 背景为白色 二维码为黑色 - // RxQRCode.createQRCode(str,mIvQrCode); - mIvQrCode!!.visibility = View.VISIBLE + // RxQRCode.createQRCode(str,iv_qr_code); + iv_qr_code!!.visibility = View.VISIBLE RxToast.success("二维码已生成!") RxSPTool.putContent(mContext.baseContext!!, RxConstants.SP_MADE_CODE, (RxDataTool.stringToInt(RxSPTool.getContent(mContext.baseContext!!, RxConstants.SP_MADE_CODE)) + 1).toString()) updateMadeCodeCount() nestedScrollView!!.computeScroll() } } - mIvCreateBarCode!!.setOnClickListener { - val str1 = mEtBarCode!!.text.toString() + iv_create_bar_code!!.setOnClickListener { + val str1 = et_bar_code!!.text.toString() if (RxDataTool.isNullString(str1)) { RxToast.error("条形码文字内容不能为空!") } else { - mLlBarCode!!.visibility = View.VISIBLE + ll_bar_code!!.visibility = View.VISIBLE //条形码生成方式一 推荐此方法 - RxBarCode.builder(str1).backColor(0x00000000).codeColor(-0x1000000).codeWidth(1000).codeHeight(300).into(mIvBarCode) + RxBarCode.builder(str1).backColor(0x00000000).codeColor(-0x1000000).codeWidth(1000).codeHeight(300).into(iv_bar_code) //条形码生成方式二 默认宽为1000 高为300 背景为白色 二维码为黑色 - //mIvBarCode.setImageBitmap(RxBarCode.createBarCode(str1, 1000, 300)); - mIvBarCode!!.visibility = View.VISIBLE + //iv_bar_code.setImageBitmap(RxBarCode.createBarCode(str1, 1000, 300)); + iv_bar_code!!.visibility = View.VISIBLE RxToast.success("条形码已生成!") RxSPTool.putContent(mContext.baseContext!!, RxConstants.SP_MADE_CODE, (RxDataTool.stringToInt(RxSPTool.getContent(mContext.baseContext!!, RxConstants.SP_MADE_CODE)) + 1).toString()) updateMadeCodeCount() } } - mLlScaner!!.setOnClickListener { RxActivityTool.skipActivity(mContext, ActivityScanerCode::class.java) } - mLlQr!!.setOnClickListener { - mLlQrRoot!!.visibility = View.VISIBLE - mLlBarRoot!!.visibility = View.GONE + ll_scaner!!.setOnClickListener { RxActivityTool.skipActivity(mContext, ActivityScanerCode::class.java) } + ll_qr!!.setOnClickListener { + ll_qr_root!!.visibility = View.VISIBLE + ll_bar_root!!.visibility = View.GONE } - mLlBar!!.setOnClickListener { - mLlBarRoot!!.visibility = View.VISIBLE - mLlQrRoot!!.visibility = View.GONE + ll_bar!!.setOnClickListener { + ll_bar_root!!.visibility = View.VISIBLE + ll_qr_root!!.visibility = View.GONE } } diff --git a/RxKit/src/main/java/com/tamsiree/rxkit/RxDeviceTool.kt b/RxKit/src/main/java/com/tamsiree/rxkit/RxDeviceTool.kt index e739937c..92b0a7c9 100644 --- a/RxKit/src/main/java/com/tamsiree/rxkit/RxDeviceTool.kt +++ b/RxKit/src/main/java/com/tamsiree/rxkit/RxDeviceTool.kt @@ -86,8 +86,7 @@ object RxDeviceTool { @JvmStatic fun getScreenDensity(context: Context): Float { return context.resources.displayMetrics.density - }//品牌类型 例如: Galaxy nexus - //品牌 例如:samsung + } /** * 获取手机唯一标识序列号 @@ -117,13 +116,11 @@ object RxDeviceTool { */ @JvmStatic fun getIMEI(context: Context): String? { - val deviceId: String? - deviceId = if (isPhone(context)) { + return if (isPhone(context)) { getDeviceIdIMEI(context) } else { getAndroidId(context) } - return deviceId } /** @@ -184,6 +181,7 @@ object RxDeviceTool { * @param context * @return */ + @SuppressLint("HardwareIds") @JvmStatic fun getLine1Number(context: Context): String? { val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager @@ -296,6 +294,7 @@ object RxDeviceTool { * @param context * @return */ + @SuppressLint("HardwareIds") @JvmStatic fun getSimSerialNumber(context: Context): String? { val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager @@ -324,6 +323,7 @@ object RxDeviceTool { * @param context * @return */ + @SuppressLint("HardwareIds") @JvmStatic fun getSubscriberId(context: Context): String? { val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager @@ -356,6 +356,7 @@ object RxDeviceTool { * @param context * @return */ + @SuppressLint("HardwareIds") @JvmStatic fun getAndroidId(context: Context): String { return Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID) @@ -392,6 +393,7 @@ object RxDeviceTool { */ @JvmStatic val serialNumber: String? + @SuppressLint("PrivateApi", "HardwareIds") get() { var serial: String? = null try { @@ -455,7 +457,7 @@ object RxDeviceTool { * @return */ @JvmStatic - fun checkPermission(context: Context, permission: String?): Boolean { + fun checkPermission(context: Context, permission: String): Boolean { var result = false if (Build.VERSION.SDK_INT >= 23) { result = try { diff --git a/RxKit/src/main/java/com/tamsiree/rxkit/RxImageTool.kt b/RxKit/src/main/java/com/tamsiree/rxkit/RxImageTool.kt index e41c9ac7..f3297c45 100644 --- a/RxKit/src/main/java/com/tamsiree/rxkit/RxImageTool.kt +++ b/RxKit/src/main/java/com/tamsiree/rxkit/RxImageTool.kt @@ -6,12 +6,12 @@ import android.graphics.* import android.graphics.Bitmap.CompressFormat import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Drawable -import android.media.ExifInterface import android.media.ThumbnailUtils import android.util.DisplayMetrics import android.util.Log import android.view.WindowManager import android.widget.ImageView +import androidx.exifinterface.media.ExifInterface import com.tamsiree.rxkit.RxDataTool.Companion.isNullString import com.tamsiree.rxkit.RxFileTool.Companion.closeIO import com.tamsiree.rxkit.RxFileTool.Companion.createOrExistsFile @@ -507,9 +507,6 @@ object RxImageTool { */ @JvmStatic fun getBitmap(resId: Int): Bitmap? { - if (RxTool.getContext() == null) { - return null - } val `is` = RxTool.getContext().resources.openRawResource(resId) return BitmapFactory.decodeStream(`is`) } @@ -524,9 +521,6 @@ object RxImageTool { */ @JvmStatic fun getBitmap(resId: Int, maxWidth: Int, maxHeight: Int): Bitmap? { - if (RxTool.getContext() == null) { - return null - } val options = BitmapFactory.Options() options.inJustDecodeBounds = true val `is` = RxTool.getContext().resources.openRawResource(resId) @@ -801,7 +795,7 @@ object RxImageTool { * @return 旋转角度 */ @JvmStatic - fun getRotateDegree(filePath: String?): Int { + fun getRotateDegree(filePath: String): Int { var degree = 0 try { val exifInterface = ExifInterface(filePath) diff --git a/RxKit/src/main/java/com/tamsiree/rxkit/RxNetTool.kt b/RxKit/src/main/java/com/tamsiree/rxkit/RxNetTool.kt index 60d735ce..b6b5c8b0 100644 --- a/RxKit/src/main/java/com/tamsiree/rxkit/RxNetTool.kt +++ b/RxKit/src/main/java/com/tamsiree/rxkit/RxNetTool.kt @@ -118,7 +118,7 @@ object RxNetTool { } } else { netType = NETWORK_NO - RxToast.error(context, "当前无网络连接").show() + RxToast.error(context, "当前无网络连接")?.show() } return netType } diff --git a/RxKit/src/main/java/com/tamsiree/rxkit/RxTool.java b/RxKit/src/main/java/com/tamsiree/rxkit/RxTool.java deleted file mode 100644 index 6eb6acad..00000000 --- a/RxKit/src/main/java/com/tamsiree/rxkit/RxTool.java +++ /dev/null @@ -1,366 +0,0 @@ -package com.tamsiree.rxkit; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.os.Handler; -import android.os.HandlerThread; -import android.text.Editable; -import android.text.InputFilter; -import android.text.InputType; -import android.text.Spanned; -import android.text.TextWatcher; -import android.view.View; -import android.view.ViewGroup; -import android.widget.EditText; -import android.widget.ListAdapter; -import android.widget.ListView; -import android.widget.TextView; - -import com.tamsiree.rxkit.interfaces.OnDoListener; -import com.tamsiree.rxkit.interfaces.OnSimpleListener; -import com.tamsiree.rxkit.view.RxToast; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; - -/** - * @author tamsiree - * @date 2016/1/24 - * RxTool的常用工具类 - *

- * For the brave souls who get this far: You are the chosen ones, - * the valiant knights of programming who toil away, without rest, - * fixing our most awful code. To you, true saviors, kings of men, - * I say this: never gonna give you up, never gonna let you down, - * never gonna run around and desert you. Never gonna make you cry, - * never gonna say goodbye. Never gonna tell a lie and hurt you. - *

- * 致终于来到这里的勇敢的人: - * 你是被上帝选中的人,是英勇的、不敌辛苦的、不眠不休的来修改我们这最棘手的代码的编程骑士。 - * 你,我们的救世主,人中之龙,我要对你说:永远不要放弃,永远不要对自己失望,永远不要逃走,辜负了自己, - * 永远不要哭啼,永远不要说再见,永远不要说谎来伤害自己。 - */ -public class RxTool { - - @SuppressLint("StaticFieldLeak") - private static Context context; - private static long lastClickTime; - - /** - * 初始化工具类 - * - * @param context 上下文 - */ - public static void init(Context context) { - RxTool.context = context.getApplicationContext(); -// RxCrashTool.init(context); - RxLogTool.init(context); - } - - /** - * 在某种获取不到 Context 的情况下,即可以使用才方法获取 Context - *

- * 获取ApplicationContext - * - * @return ApplicationContext - */ - public static Context getContext() { - if (context != null) { - return context; - } - throw new NullPointerException("请先调用init()方法"); - } - //==============================================================================================延时任务封装 end - - //----------------------------------------------------------------------------------------------延时任务封装 start - public static void delayToDo(long delayTime, final OnSimpleListener onSimpleListener) { - new Handler().postDelayed(new Runnable() { - @Override - public void run() { - //execute the task - onSimpleListener.doSomething(); - } - }, delayTime); - } - - /** - * 倒计时 - * - * @param textView 控件 - * @param waitTime 倒计时总时长 - * @param interval 倒计时的间隔时间 - * @param hint 倒计时完毕时显示的文字 - */ - public static void countDown(final TextView textView, long waitTime, long interval, final String hint) { - textView.setEnabled(false); - android.os.CountDownTimer timer = new android.os.CountDownTimer(waitTime, interval) { - - @SuppressLint("DefaultLocale") - @Override - public void onTick(long millisUntilFinished) { - textView.setText(String.format("剩下 %d S", millisUntilFinished / 1000)); - } - - @Override - public void onFinish() { - textView.setEnabled(true); - textView.setText(hint); - } - }; - timer.start(); - } - - /** - * 手动计算出listView的高度,但是不再具有滚动效果 - * - * @param listView - */ - public static void fixListViewHeight(ListView listView) { - // 如果没有设置数据适配器,则ListView没有子项,返回。 - ListAdapter listAdapter = listView.getAdapter(); - int totalHeight = 0; - if (listAdapter == null) { - return; - } - for (int index = 0, len = listAdapter.getCount(); index < len; index++) { - View listViewItem = listAdapter.getView(index, null, listView); - // 计算子项View 的宽高 - listViewItem.measure(0, 0); - // 计算所有子项的高度 - totalHeight += listViewItem.getMeasuredHeight(); - } - ViewGroup.LayoutParams params = listView.getLayoutParams(); - // listView.getDividerHeight()获取子项间分隔符的高度 - // params.height设置ListView完全显示需要的高度 - params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); - listView.setLayoutParams(params); - } - - //---------------------------------------------MD5加密------------------------------------------- - - /** - * 生成MD5加密32位字符串 - * - * @param MStr :需要加密的字符串 - * @return - */ - public static String Md5(String MStr) { - try { - final MessageDigest mDigest = MessageDigest.getInstance("MD5"); - mDigest.update(MStr.getBytes()); - return bytesToHexString(mDigest.digest()); - } catch (NoSuchAlgorithmException e) { - return String.valueOf(MStr.hashCode()); - } - } - - // MD5内部算法---------------不能修改! - private static String bytesToHexString(byte[] bytes) { - // http://stackoverflow.com/questions/332079 - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < bytes.length; i++) { - String hex = Integer.toHexString(0xFF & bytes[i]); - if (hex.length() == 1) { - sb.append('0'); - } - sb.append(hex); - } - return sb.toString(); - } - //============================================MD5加密============================================ - - /** - * 根据资源名称获取资源 id - *

- * 不提倡使用这个方法获取资源,比其直接获取ID效率慢 - *

- * 例如 - * getResources().getIdentifier("ic_launcher", "drawable", getPackageName()); - * - * @param context - * @param name - * @param defType - * @return - */ - public final static int getResIdByName(Context context, String name, String defType) { - return context.getResources().getIdentifier(name, defType, context.getPackageName()); - } - - public static boolean isFastClick(int millisecond) { - long curClickTime = System.currentTimeMillis(); - long interval = (curClickTime - lastClickTime); - - if (0 < interval && interval < millisecond) { - // 超过点击间隔后再将lastClickTime重置为当前点击时间 - return true; - } - lastClickTime = curClickTime; - return false; - } - - /** - * Edittext 首位小数点自动加零,最多两位小数 - * - * @param editText - */ - public static void setEdTwoDecimal(EditText editText) { - setEdDecimal(editText, 2); - } - - /** - * 只允许数字和汉字 - * @param editText - */ - public static void setEdType(final EditText editText) { - editText.addTextChangedListener(new TextWatcher() { - @Override - public void - beforeTextChanged(CharSequence s, int start, int count, int after) { - - } - - @Override - public void - onTextChanged(CharSequence s, int start, int before, int count) { - String editable = editText.getText().toString(); - String str = stringFilter(editable); - if (!editable.equals(str)) { - editText.setText(str); - //设置新的光标所在位置 - editText.setSelection(str.length()); - } - } - - @Override - public void - afterTextChanged(Editable s) { - - } - }); - } - - /** - * // 只允许数字和汉字 - * - * @param str - * @return - * @throws PatternSyntaxException - */ - public static String stringFilter(String str) throws PatternSyntaxException { - - String regEx = "[^0-9\u4E00-\u9FA5]";//正则表达式 - Pattern p = Pattern.compile(regEx); - Matcher m = p.matcher(str); - return m.replaceAll("").trim(); - } - - public static void setEdDecimal(EditText editText, int count) { - if (count < 0) { - count = 0; - } - - count += 1; - - editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER); - - //设置字符过滤 - final int finalCount = count; - editText.setFilters(new InputFilter[]{new InputFilter() { - @Override - public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { - if (".".contentEquals(source) && dest.toString().length() == 0) { - return "0."; - } - if (dest.toString().contains(".")) { - int index = dest.toString().indexOf("."); - int mlength = dest.toString().substring(index).length(); - if (mlength == finalCount) { - return ""; - } - } - - if (dest.toString().equals("0") && source.equals("0")) { - return ""; - } - - return null; - } - }}); - } - - /** - * @param editText 输入框控件 - * @param number 位数 - * 1 -> 1 - * 2 -> 01 - * 3 -> 001 - * 4 -> 0001 - * @param isStartForZero 是否从000开始 - * true -> 从 000 开始 - * false -> 从 001 开始 - */ - public static void setEditNumberAuto(final EditText editText, final int number, final boolean isStartForZero) { - editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { - @Override - public void onFocusChange(View v, boolean hasFocus) { - if (!hasFocus) { - setEditNumber(editText, number, isStartForZero); - } - } - }); - } - - /** - * @param editText 输入框控件 - * @param number 位数 - * 1 -> 1 - * 2 -> 01 - * 3 -> 001 - * 4 -> 0001 - * @param isStartForZero 是否从000开始 - * true -> 从 000 开始 - * false -> 从 001 开始 - */ - public static void setEditNumber(EditText editText, int number, boolean isStartForZero) { - StringBuilder s = new StringBuilder(editText.getText().toString()); - StringBuilder temp = new StringBuilder(); - - int i; - for (i = s.length(); i < number; ++i) { - s.insert(0, "0"); - } - if (!isStartForZero) { - for (i = 0; i < number; ++i) { - temp.append("0"); - } - - if (s.toString().equals(temp.toString())) { - s = new StringBuilder(temp.substring(1) + "1"); - } - } - editText.setText(s.toString()); - } - - /** - * 获取 - * @return - */ - public static Handler getBackgroundHandler() { - HandlerThread thread = new HandlerThread("background"); - thread.start(); - return new Handler(thread.getLooper()); - } - - public static void initFastClickAndVibrate(Context mContext, OnDoListener onRxSimple) { - if (RxTool.isFastClick(RxConstants.FAST_CLICK_TIME)) { - RxToast.normal("请不要重复点击"); - return; - } else { - RxVibrateTool.vibrateOnce(mContext, RxConstants.VIBRATE_TIME); - onRxSimple.doSomething(); - } - } -} diff --git a/RxKit/src/main/java/com/tamsiree/rxkit/RxTool.kt b/RxKit/src/main/java/com/tamsiree/rxkit/RxTool.kt new file mode 100644 index 00000000..2aa3f234 --- /dev/null +++ b/RxKit/src/main/java/com/tamsiree/rxkit/RxTool.kt @@ -0,0 +1,356 @@ +package com.tamsiree.rxkit + +import android.annotation.SuppressLint +import android.content.Context +import android.os.CountDownTimer +import android.os.Handler +import android.os.HandlerThread +import android.text.Editable +import android.text.InputFilter +import android.text.InputType +import android.text.TextWatcher +import android.view.View.OnFocusChangeListener +import android.widget.EditText +import android.widget.ListView +import android.widget.TextView +import com.tamsiree.rxkit.RxVibrateTool.vibrateOnce +import com.tamsiree.rxkit.interfaces.OnDoListener +import com.tamsiree.rxkit.interfaces.OnSimpleListener +import com.tamsiree.rxkit.view.RxToast.normal +import java.security.MessageDigest +import java.security.NoSuchAlgorithmException +import java.util.regex.Pattern +import java.util.regex.PatternSyntaxException + +/** + * @author tamsiree + * @date 2016/1/24 + * RxTool的常用工具类 + * + * + * For the brave souls who get this far: You are the chosen ones, + * the valiant knights of programming who toil away, without rest, + * fixing our most awful code. To you, true saviors, kings of men, + * I say this: never gonna give you up, never gonna let you down, + * never gonna run around and desert you. Never gonna make you cry, + * never gonna say goodbye. Never gonna tell a lie and hurt you. + * + * + * 致终于来到这里的勇敢的人: + * 你是被上帝选中的人,是英勇的、不敌辛苦的、不眠不休的来修改我们这最棘手的代码的编程骑士。 + * 你,我们的救世主,人中之龙,我要对你说:永远不要放弃,永远不要对自己失望,永远不要逃走,辜负了自己, + * 永远不要哭啼,永远不要说再见,永远不要说谎来伤害自己。 + */ +object RxTool { + @SuppressLint("StaticFieldLeak") + private var context: Context? = null + private var lastClickTime: Long = 0 + + /** + * 初始化工具类 + * + * @param context 上下文 + */ + @JvmStatic + fun init(context: Context) { + RxTool.context = context.applicationContext + // RxCrashTool.init(context); + RxLogTool.init(context) + } + + /** + * 在某种获取不到 Context 的情况下,即可以使用才方法获取 Context + * + * + * 获取ApplicationContext + * + * @return ApplicationContext + */ + @JvmStatic + fun getContext(): Context { + if (context != null) { + return context as Context + } + throw NullPointerException("请先调用init()方法") + } + + + //----------------------------------------------------------------------------------------------延时任务封装 start + @JvmStatic + fun delayToDo(delayTime: Long, onSimpleListener: OnSimpleListener) { + Handler().postDelayed({ //execute the task + onSimpleListener.doSomething() + }, delayTime) + } + //==============================================================================================延时任务封装 end + + /** + * 倒计时 + * + * @param textView 控件 + * @param waitTime 倒计时总时长 + * @param interval 倒计时的间隔时间 + * @param hint 倒计时完毕时显示的文字 + */ + @JvmStatic + fun countDown(textView: TextView, waitTime: Long, interval: Long, hint: String?) { + textView.isEnabled = false + val timer: CountDownTimer = object : CountDownTimer(waitTime, interval) { + @SuppressLint("DefaultLocale") + override fun onTick(millisUntilFinished: Long) { + textView.text = String.format("剩下 %d S", millisUntilFinished / 1000) + } + + override fun onFinish() { + textView.isEnabled = true + textView.text = hint + } + } + timer.start() + } + + /** + * 手动计算出listView的高度,但是不再具有滚动效果 + * + * @param listView + */ + @JvmStatic + fun fixListViewHeight(listView: ListView) { + // 如果没有设置数据适配器,则ListView没有子项,返回。 + val listAdapter = listView.adapter + var totalHeight = 0 + if (listAdapter == null) { + return + } + var index = 0 + val len = listAdapter.count + while (index < len) { + val listViewItem = listAdapter.getView(index, null, listView) + // 计算子项View 的宽高 + listViewItem.measure(0, 0) + // 计算所有子项的高度 + totalHeight += listViewItem.measuredHeight + index++ + } + val params = listView.layoutParams + // listView.getDividerHeight()获取子项间分隔符的高度 + // params.height设置ListView完全显示需要的高度 + params.height = totalHeight + listView.dividerHeight * (listAdapter.count - 1) + listView.layoutParams = params + } + //---------------------------------------------MD5加密------------------------------------------- + /** + * 生成MD5加密32位字符串 + * + * @param MStr :需要加密的字符串 + * @return + */ + @JvmStatic + fun Md5(MStr: String): String { + return try { + val mDigest = MessageDigest.getInstance("MD5") + mDigest.update(MStr.toByteArray()) + bytesToHexString(mDigest.digest()) + } catch (e: NoSuchAlgorithmException) { + MStr.hashCode().toString() + } + } + + // MD5内部算法---------------不能修改! + private fun bytesToHexString(bytes: ByteArray): String { + // http://stackoverflow.com/questions/332079 + val sb = StringBuilder() + for (i in bytes.indices) { + val hex = Integer.toHexString(0xFF and bytes[i].toInt()) + if (hex.length == 1) { + sb.append('0') + } + sb.append(hex) + } + return sb.toString() + } + //============================================MD5加密============================================ + /** + * 根据资源名称获取资源 id + * + * + * 不提倡使用这个方法获取资源,比其直接获取ID效率慢 + * + * + * 例如 + * getResources().getIdentifier("ic_launcher", "drawable", getPackageName()); + * + * @param context + * @param name + * @param defType + * @return + */ + @JvmStatic + fun getResIdByName(context: Context, name: String?, defType: String?): Int { + return context.resources.getIdentifier(name, defType, context.packageName) + } + + @JvmStatic + fun isFastClick(millisecond: Int): Boolean { + val curClickTime = System.currentTimeMillis() + val interval = curClickTime - lastClickTime + if (0 < interval && interval < millisecond) { + // 超过点击间隔后再将lastClickTime重置为当前点击时间 + return true + } + lastClickTime = curClickTime + return false + } + + /** + * Edittext 首位小数点自动加零,最多两位小数 + * + * @param editText + */ + @JvmStatic + fun setEdTwoDecimal(editText: EditText) { + setEdDecimal(editText, 2) + } + + /** + * 只允许数字和汉字 + * @param editText + */ + @JvmStatic + fun setEdType(editText: EditText) { + editText.addTextChangedListener(object : TextWatcher { + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { + val editable = editText.text.toString() + val str = stringFilter(editable) + if (editable != str) { + editText.setText(str) + //设置新的光标所在位置 + editText.setSelection(str.length) + } + } + + override fun afterTextChanged(s: Editable) {} + }) + } + + /** + * // 只允许数字和汉字 + * + * @param str + * @return + * @throws PatternSyntaxException + */ + @Throws(PatternSyntaxException::class) + @JvmStatic + fun stringFilter(str: String?): String { + val regEx = "[^0-9\u4E00-\u9FA5]" //正则表达式 + val p = Pattern.compile(regEx) + val m = p.matcher(str) + return m.replaceAll("").trim { it <= ' ' } + } + + @JvmStatic + fun setEdDecimal(editText: EditText, count: Int) { + var count = count + if (count < 0) { + count = 0 + } + count += 1 + editText.inputType = InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_CLASS_NUMBER + + //设置字符过滤 + val finalCount = count + editText.filters = arrayOf(InputFilter { source, start, end, dest, dstart, dend -> + if (".".contentEquals(source) && dest.toString().length == 0) { + return@InputFilter "0." + } + if (dest.toString().contains(".")) { + val index = dest.toString().indexOf(".") + val mlength = dest.toString().substring(index).length + if (mlength == finalCount) { + return@InputFilter "" + } + } + if (dest.toString() == "0" && source == "0") { + "" + } else null + }) + } + + /** + * @param editText 输入框控件 + * @param number 位数 + * 1 -> 1 + * 2 -> 01 + * 3 -> 001 + * 4 -> 0001 + * @param isStartForZero 是否从000开始 + * true -> 从 000 开始 + * false -> 从 001 开始 + */ + @JvmStatic + fun setEditNumberAuto(editText: EditText, number: Int, isStartForZero: Boolean) { + editText.onFocusChangeListener = OnFocusChangeListener { v, hasFocus -> + if (!hasFocus) { + setEditNumber(editText, number, isStartForZero) + } + } + } + + /** + * @param editText 输入框控件 + * @param number 位数 + * 1 -> 1 + * 2 -> 01 + * 3 -> 001 + * 4 -> 0001 + * @param isStartForZero 是否从000开始 + * true -> 从 000 开始 + * false -> 从 001 开始 + */ + @JvmStatic + fun setEditNumber(editText: EditText, number: Int, isStartForZero: Boolean) { + var s = StringBuilder(editText.text.toString()) + val temp = StringBuilder() + var i: Int + i = s.length + while (i < number) { + s.insert(0, "0") + ++i + } + if (!isStartForZero) { + i = 0 + while (i < number) { + temp.append("0") + ++i + } + if (s.toString() == temp.toString()) { + s = StringBuilder(temp.substring(1) + "1") + } + } + editText.setText(s.toString()) + } + + /** + * 获取 + * @return + */ + val backgroundHandler: Handler + get() { + val thread = HandlerThread("background") + thread.start() + return Handler(thread.looper) + } + + @JvmStatic + fun initFastClickAndVibrate(mContext: Context?, onRxSimple: OnDoListener) { + if (isFastClick(RxConstants.FAST_CLICK_TIME)) { + normal("请不要重复点击") + return + } else { + vibrateOnce(mContext!!, RxConstants.VIBRATE_TIME) + onRxSimple.doSomething() + } + } +} \ No newline at end of file diff --git a/RxKit/src/main/java/com/tamsiree/rxkit/RxWebViewTool.kt b/RxKit/src/main/java/com/tamsiree/rxkit/RxWebViewTool.kt index f5969e7e..86f5ffff 100644 --- a/RxKit/src/main/java/com/tamsiree/rxkit/RxWebViewTool.kt +++ b/RxKit/src/main/java/com/tamsiree/rxkit/RxWebViewTool.kt @@ -74,7 +74,7 @@ object RxWebViewTool { // 设置setWebChromeClient对象 webBase.webChromeClient = object : WebChromeClient() { - override fun onReceivedTitle(view: WebView?, title: String?) { + override fun onReceivedTitle(view: WebView, title: String) { super.onReceivedTitle(view, title) onWebViewLoad.onReceivedTitle(title) } diff --git a/RxKit/src/main/java/com/tamsiree/rxkit/interfaces/OnWebViewLoad.kt b/RxKit/src/main/java/com/tamsiree/rxkit/interfaces/OnWebViewLoad.kt index 94ab271a..b054ccc2 100644 --- a/RxKit/src/main/java/com/tamsiree/rxkit/interfaces/OnWebViewLoad.kt +++ b/RxKit/src/main/java/com/tamsiree/rxkit/interfaces/OnWebViewLoad.kt @@ -5,7 +5,7 @@ package com.tamsiree.rxkit.interfaces */ interface OnWebViewLoad { fun onPageStarted() - fun onReceivedTitle(title: String?) + fun onReceivedTitle(title: String) fun onProgressChanged(newProgress: Int) fun shouldOverrideUrlLoading() fun onPageFinished() diff --git a/RxKit/src/main/java/com/tamsiree/rxkit/view/RxToast.java b/RxKit/src/main/java/com/tamsiree/rxkit/view/RxToast.java deleted file mode 100644 index d7ee70bb..00000000 --- a/RxKit/src/main/java/com/tamsiree/rxkit/view/RxToast.java +++ /dev/null @@ -1,401 +0,0 @@ -package com.tamsiree.rxkit.view; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.graphics.Color; -import android.graphics.PorterDuff; -import android.graphics.PorterDuffColorFilter; -import android.graphics.Typeface; -import android.graphics.drawable.Drawable; -import android.graphics.drawable.NinePatchDrawable; -import android.os.Build; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.ImageView; -import android.widget.TextView; -import android.widget.Toast; - -import androidx.annotation.CheckResult; -import androidx.annotation.ColorInt; -import androidx.annotation.DrawableRes; -import androidx.annotation.NonNull; - -import com.tamsiree.rxkit.R; -import com.tamsiree.rxkit.RxTool; - -/** - * @author tamsiree - * @date 2018/06/11 14:20:10 - * 在系统的Toast基础上封装 - */ - -@SuppressLint("InflateParams") -public class RxToast { - - @ColorInt - private static final int DEFAULT_TEXT_COLOR = Color.parseColor("#FFFFFF"); - - @ColorInt - private static final int ERROR_COLOR = Color.parseColor("#FD4C5B"); - - @ColorInt - private static final int INFO_COLOR = Color.parseColor("#2196F3"); - - @ColorInt - private static final int SUCCESS_COLOR = Color.parseColor("#52BA97"); - - @ColorInt - private static final int WARNING_COLOR = Color.parseColor("#FFA900"); - - private static final String TOAST_TYPEFACE = "sans-serif-condensed"; - - private static Toast currentToast; - - //*******************************************普通 使用ApplicationContext 方法********************* - /** - * Toast 替代方法 :立即显示无需等待 - */ - private static Toast mToast; - private static long mExitTime; - - public static void normal(@NonNull String message) { - normal(RxTool.getContext(), message, Toast.LENGTH_SHORT, null, false).show(); - } - - public static void normal(@NonNull String message, Drawable icon) { - normal(RxTool.getContext(), message, Toast.LENGTH_SHORT, icon, true).show(); - } - - public static void normal(@NonNull String message, int duration) { - normal(RxTool.getContext(), message, duration, null, false).show(); - } - - public static void normal(@NonNull String message, int duration, Drawable icon) { - normal(RxTool.getContext(), message, duration, icon, true).show(); - } - - public static Toast normal(@NonNull String message, int duration, Drawable icon, boolean withIcon) { - return custom(RxTool.getContext(), message, icon, DEFAULT_TEXT_COLOR, duration, withIcon); - } - - public static void warning(@NonNull String message) { - warning(RxTool.getContext(), message, Toast.LENGTH_SHORT, true).show(); - } - - public static void warning(@NonNull String message, int duration) { - warning(RxTool.getContext(), message, duration, true).show(); - } - - public static Toast warning(@NonNull String message, int duration, boolean withIcon) { - return custom(RxTool.getContext(), message, getDrawable(RxTool.getContext(), R.drawable.ic_error_outline_white_48dp), DEFAULT_TEXT_COLOR, WARNING_COLOR, duration, withIcon, true); - } - - public static void info(@NonNull String message) { - info(RxTool.getContext(), message, Toast.LENGTH_SHORT, true).show(); - } - - public static void info(@NonNull String message, int duration) { - info(RxTool.getContext(), message, duration, true).show(); - } - - public static Toast info(@NonNull String message, int duration, boolean withIcon) { - return custom(RxTool.getContext(), message, getDrawable(RxTool.getContext(), R.drawable.ic_info_outline_white_48dp), DEFAULT_TEXT_COLOR, INFO_COLOR, duration, withIcon, true); - } - - public static void success(@NonNull String message) { - success(RxTool.getContext(), message, Toast.LENGTH_SHORT, true).show(); - } - - public static void success(@NonNull String message, int duration) { - success(RxTool.getContext(), message, duration, true).show(); - } - - public static Toast success(@NonNull String message, int duration, boolean withIcon) { - return custom(RxTool.getContext(), message, getDrawable(RxTool.getContext(), R.drawable.ic_check_white_48dp), DEFAULT_TEXT_COLOR, SUCCESS_COLOR, duration, withIcon, true); - } - - public static void error(@NonNull String message) { - error(RxTool.getContext(), message, Toast.LENGTH_SHORT, true).show(); - } - //===========================================使用ApplicationContext 方法========================= - - //*******************************************常规方法******************************************** - - public static void error(@NonNull String message, int duration) { - error(RxTool.getContext(), message, duration, true).show(); - } - - public static Toast error(@NonNull String message, int duration, boolean withIcon) { - return custom(RxTool.getContext(), message, getDrawable(RxTool.getContext(), R.drawable.ic_clear_white_48dp), DEFAULT_TEXT_COLOR, ERROR_COLOR, duration, withIcon, true); - } - - @CheckResult - public static Toast normal(@NonNull Context context, @NonNull String message) { - return normal(context, message, Toast.LENGTH_SHORT, null, false); - } - - @CheckResult - public static Toast normal(@NonNull Context context, @NonNull String message, Drawable icon) { - return normal(context, message, Toast.LENGTH_SHORT, icon, true); - } - - @CheckResult - public static Toast normal(@NonNull Context context, @NonNull String message, int duration) { - return normal(context, message, duration, null, false); - } - - @CheckResult - public static Toast normal(@NonNull Context context, @NonNull String message, int duration, Drawable icon) { - return normal(context, message, duration, icon, true); - } - - @CheckResult - public static Toast normal(@NonNull Context context, @NonNull String message, int duration, Drawable icon, boolean withIcon) { - return custom(context, message, icon, DEFAULT_TEXT_COLOR, duration, withIcon); - } - - @CheckResult - public static Toast warning(@NonNull Context context, @NonNull String message) { - return warning(context, message, Toast.LENGTH_SHORT, true); - } - - @CheckResult - public static Toast warning(@NonNull Context context, @NonNull String message, int duration) { - return warning(context, message, duration, true); - } - - @CheckResult - public static Toast warning(@NonNull Context context, @NonNull String message, int duration, boolean withIcon) { - return custom(context, message, getDrawable(context, R.drawable.ic_error_outline_white_48dp), DEFAULT_TEXT_COLOR, WARNING_COLOR, duration, withIcon, true); - } - - @CheckResult - public static Toast info(@NonNull Context context, @NonNull String message) { - return info(context, message, Toast.LENGTH_SHORT, true); - } - - @CheckResult - public static Toast info(@NonNull Context context, @NonNull String message, int duration) { - return info(context, message, duration, true); - } - - @CheckResult - public static Toast info(@NonNull Context context, @NonNull String message, int duration, boolean withIcon) { - return custom(context, message, getDrawable(context, R.drawable.ic_info_outline_white_48dp), DEFAULT_TEXT_COLOR, INFO_COLOR, duration, withIcon, true); - } - - @CheckResult - public static Toast success(@NonNull Context context, @NonNull String message) { - return success(context, message, Toast.LENGTH_SHORT, true); - } - - @CheckResult - public static Toast success(@NonNull Context context, @NonNull String message, int duration) { - return success(context, message, duration, true); - } - - @CheckResult - public static Toast success(@NonNull Context context, @NonNull String message, int duration, boolean withIcon) { - return custom(context, message, getDrawable(context, R.drawable.ic_check_white_48dp), DEFAULT_TEXT_COLOR, SUCCESS_COLOR, duration, withIcon, true); - } - - @CheckResult - public static Toast error(@NonNull Context context, @NonNull String message) { - return error(context, message, Toast.LENGTH_SHORT, true); - } - - //===========================================常规方法============================================ - - @CheckResult - public static Toast error(@NonNull Context context, @NonNull String message, int duration) { - return error(context, message, duration, true); - } - - @CheckResult - public static Toast error(@NonNull Context context, @NonNull String message, int duration, boolean withIcon) { - return custom(context, message, getDrawable(context, R.drawable.ic_clear_white_48dp), DEFAULT_TEXT_COLOR, ERROR_COLOR, duration, withIcon, true); - } - - @CheckResult - public static Toast custom(@NonNull Context context, @NonNull String message, Drawable icon, @ColorInt int textColor, int duration, boolean withIcon) { - return custom(context, message, icon, textColor, -1, duration, withIcon, false); - } - - //*******************************************内需方法******************************************** - - @CheckResult - public static Toast custom(@NonNull Context context, @NonNull String message, @DrawableRes int iconRes, @ColorInt int textColor, @ColorInt int tintColor, int duration, boolean withIcon, boolean shouldTint) { - return custom(context, message, getDrawable(context, iconRes), textColor, tintColor, duration, withIcon, shouldTint); - } - - @CheckResult - public static Toast custom(@NonNull Context context, @NonNull String message, Drawable icon, @ColorInt int textColor, @ColorInt int tintColor, int duration, boolean withIcon, boolean shouldTint) { - if (currentToast == null) { - currentToast = new Toast(context); - } else { - currentToast.cancel(); - currentToast = null; - currentToast = new Toast(context); - } - final View toastLayout = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.toast_layout, null); - final ImageView toastIcon = toastLayout.findViewById(R.id.toast_icon); - final TextView toastTextView = toastLayout.findViewById(R.id.toast_text); - Drawable drawableFrame; - - if (shouldTint) { - drawableFrame = tint9PatchDrawableFrame(context, tintColor); - } else { - drawableFrame = getDrawable(context, R.drawable.toast_frame); - } - setBackground(toastLayout, drawableFrame); - - if (withIcon) { - if (icon == null) { - throw new IllegalArgumentException("Avoid passing 'icon' as null if 'withIcon' is set to true"); - } - setBackground(toastIcon, icon); - } else { - toastIcon.setVisibility(View.GONE); - } - - toastTextView.setTextColor(textColor); - toastTextView.setText(message); - toastTextView.setTypeface(Typeface.create(TOAST_TYPEFACE, Typeface.NORMAL)); - - currentToast.setView(toastLayout); - currentToast.setDuration(duration); - return currentToast; - } - - public static final Drawable tint9PatchDrawableFrame(@NonNull Context context, @ColorInt int tintColor) { - final NinePatchDrawable toastDrawable = (NinePatchDrawable) getDrawable(context, R.drawable.toast_frame); - toastDrawable.setColorFilter(new PorterDuffColorFilter(tintColor, PorterDuff.Mode.SRC_IN)); - return toastDrawable; - } - //===========================================内需方法============================================ - - - //******************************************系统 Toast 替代方法*************************************** - - public static final void setBackground(@NonNull View view, Drawable drawable) { - view.setBackground(drawable); - } - - public static final Drawable getDrawable(@NonNull Context context, @DrawableRes int id) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - return context.getDrawable(id); - } else { - return context.getResources().getDrawable(id); - } - } - - /** - * 封装了Toast的方法 :需要等待 - * - * @param context Context - * @param str 要显示的字符串 - * @param isLong Toast.LENGTH_LONG / Toast.LENGTH_SHORT - */ - public static void showToast(Context context, String str, boolean isLong) { - if (isLong) { - Toast.makeText(context, str, Toast.LENGTH_LONG).show(); - } else { - Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); - } - } - - /** - * 封装了Toast的方法 :需要等待 - */ - public static void showToastShort(String str) { - Toast.makeText(RxTool.getContext(), str, Toast.LENGTH_SHORT).show(); - } - - /** - * 封装了Toast的方法 :需要等待 - */ - public static void showToastShort(int resId) { - Toast.makeText(RxTool.getContext(), RxTool.getContext().getString(resId), Toast.LENGTH_SHORT).show(); - } - - /** - * 封装了Toast的方法 :需要等待 - */ - public static void showToastLong(String str) { - Toast.makeText(RxTool.getContext(), str, Toast.LENGTH_LONG).show(); - } - - /** - * 封装了Toast的方法 :需要等待 - */ - public static void showToastLong(int resId) { - Toast.makeText(RxTool.getContext(), RxTool.getContext().getString(resId), Toast.LENGTH_LONG).show(); - } - - /** - * Toast 替代方法 :立即显示无需等待 - * - * @param msg 显示内容 - */ - public static void showToast(String msg) { - if (mToast == null) { - mToast = Toast.makeText(RxTool.getContext(), msg, Toast.LENGTH_LONG); - } else { - mToast.setText(msg); - } - mToast.show(); - } - - /** - * Toast 替代方法 :立即显示无需等待 - * - * @param resId String资源ID - */ - @SuppressLint("ShowToast") - public static void showToast(int resId) { - if (mToast == null) { - mToast = Toast.makeText(RxTool.getContext(), RxTool.getContext().getString(resId), Toast.LENGTH_LONG); - } else { - mToast.setText(RxTool.getContext().getString(resId)); - } - mToast.show(); - } - - /** - * Toast 替代方法 :立即显示无需等待 - * - * @param context 实体 - * @param resId String资源ID - * @param duration 显示时长 - */ - public static void showToast(Context context, int resId, int duration) { - showToast(context, context.getString(resId), duration); - } - //===========================================Toast 替代方法====================================== - - /** - * Toast 替代方法 :立即显示无需等待 - * - * @param context 实体 - * @param msg 要显示的字符串 - * @param duration 显示时长 - */ - @SuppressLint("ShowToast") - public static void showToast(Context context, String msg, int duration) { - if (mToast == null) { - mToast = Toast.makeText(context, msg, duration); - } else { - mToast.setText(msg); - } - mToast.show(); - } - - public static boolean doubleClickExit() { - if ((System.currentTimeMillis() - mExitTime) > 2000) { - RxToast.normal("再按一次退出"); - mExitTime = System.currentTimeMillis(); - return false; - } - return true; - } -} diff --git a/RxKit/src/main/java/com/tamsiree/rxkit/view/RxToast.kt b/RxKit/src/main/java/com/tamsiree/rxkit/view/RxToast.kt new file mode 100644 index 00000000..9bf2f1fe --- /dev/null +++ b/RxKit/src/main/java/com/tamsiree/rxkit/view/RxToast.kt @@ -0,0 +1,430 @@ +package com.tamsiree.rxkit.view + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.graphics.PorterDuff +import android.graphics.PorterDuffColorFilter +import android.graphics.Typeface +import android.graphics.drawable.Drawable +import android.graphics.drawable.NinePatchDrawable +import android.os.Build +import android.view.LayoutInflater +import android.view.View +import android.widget.ImageView +import android.widget.TextView +import android.widget.Toast +import androidx.annotation.CheckResult +import androidx.annotation.ColorInt +import androidx.annotation.DrawableRes +import com.tamsiree.rxkit.R +import com.tamsiree.rxkit.RxTool + +/** + * @author tamsiree + * @date 2018/06/11 14:20:10 + * 在系统的Toast基础上封装 + */ +object RxToast { + @ColorInt + private val DEFAULT_TEXT_COLOR = Color.parseColor("#FFFFFF") + + @ColorInt + private val ERROR_COLOR = Color.parseColor("#FD4C5B") + + @ColorInt + private val INFO_COLOR = Color.parseColor("#2196F3") + + @ColorInt + private val SUCCESS_COLOR = Color.parseColor("#52BA97") + + @ColorInt + private val WARNING_COLOR = Color.parseColor("#FFA900") + private const val TOAST_TYPEFACE = "sans-serif-condensed" + private var currentToast: Toast? = null + //*******************************************普通 使用ApplicationContext 方法********************* + /** + * Toast 替代方法 :立即显示无需等待 + */ + private var mToast: Toast? = null + private var mExitTime: Long = 0 + + @JvmStatic + fun normal(message: String) { + normal(RxTool.getContext(), message, Toast.LENGTH_SHORT, null, false)?.show() + } + + @JvmStatic + fun normal(message: String, icon: Drawable?) { + normal(RxTool.getContext(), message, Toast.LENGTH_SHORT, icon, true)?.show() + } + + @JvmStatic + fun normal(message: String, duration: Int) { + normal(RxTool.getContext(), message, duration, null, false)?.show() + } + + @JvmStatic + fun normal(message: String, duration: Int, icon: Drawable?) { + normal(RxTool.getContext(), message, duration, icon, true)?.show() + } + + @JvmStatic + fun normal(message: String, duration: Int, icon: Drawable?, withIcon: Boolean): Toast? { + return custom(RxTool.getContext(), message, icon, DEFAULT_TEXT_COLOR, duration, withIcon) + } + + @JvmStatic + fun warning(message: String) { + warning(RxTool.getContext(), message, Toast.LENGTH_SHORT, true)?.show() + } + + @JvmStatic + fun warning(message: String, duration: Int) { + warning(RxTool.getContext(), message, duration, true)?.show() + } + + @JvmStatic + fun warning(message: String, duration: Int, withIcon: Boolean): Toast? { + return custom(RxTool.getContext(), message, getDrawable(RxTool.getContext(), R.drawable.ic_error_outline_white_48dp), DEFAULT_TEXT_COLOR, WARNING_COLOR, duration, withIcon, true) + } + + @JvmStatic + fun info(message: String) { + info(RxTool.getContext(), message, Toast.LENGTH_SHORT, true)?.show() + } + + @JvmStatic + fun info(message: String, duration: Int) { + info(RxTool.getContext(), message, duration, true)?.show() + } + + @JvmStatic + fun info(message: String, duration: Int, withIcon: Boolean): Toast? { + return custom(RxTool.getContext(), message, getDrawable(RxTool.getContext(), R.drawable.ic_info_outline_white_48dp), DEFAULT_TEXT_COLOR, INFO_COLOR, duration, withIcon, true) + } + + @JvmStatic + fun success(message: String) { + success(RxTool.getContext(), message, Toast.LENGTH_SHORT, true)?.show() + } + + @JvmStatic + fun success(message: String, duration: Int) { + success(RxTool.getContext(), message, duration, true)?.show() + } + + @JvmStatic + fun success(message: String, duration: Int, withIcon: Boolean): Toast? { + return custom(RxTool.getContext(), message, getDrawable(RxTool.getContext(), R.drawable.ic_check_white_48dp), DEFAULT_TEXT_COLOR, SUCCESS_COLOR, duration, withIcon, true) + } + + @JvmStatic + fun error(message: String) { + error(RxTool.getContext(), message, Toast.LENGTH_SHORT, true)?.show() + } + + //===========================================使用ApplicationContext 方法========================= + //*******************************************常规方法******************************************** + @JvmStatic + fun error(message: String, duration: Int) { + error(RxTool.getContext(), message, duration, true)?.show() + } + + @JvmStatic + fun error(message: String, duration: Int, withIcon: Boolean): Toast? { + return custom(RxTool.getContext(), message, getDrawable(RxTool.getContext(), R.drawable.ic_clear_white_48dp), DEFAULT_TEXT_COLOR, ERROR_COLOR, duration, withIcon, true) + } + + @CheckResult + @JvmStatic + fun normal(context: Context, message: String): Toast? { + return normal(context, message, Toast.LENGTH_SHORT, null, false) + } + + @CheckResult + @JvmStatic + fun normal(context: Context, message: String, icon: Drawable?): Toast? { + return normal(context, message, Toast.LENGTH_SHORT, icon, true) + } + + @CheckResult + @JvmStatic + fun normal(context: Context, message: String, duration: Int): Toast? { + return normal(context, message, duration, null, false) + } + + @CheckResult + @JvmStatic + fun normal(context: Context, message: String, duration: Int, icon: Drawable?): Toast? { + return normal(context, message, duration, icon, true) + } + + @CheckResult + @JvmStatic + fun normal(context: Context, message: String, duration: Int, icon: Drawable?, withIcon: Boolean): Toast? { + return custom(context, message, icon, DEFAULT_TEXT_COLOR, duration, withIcon) + } + + @CheckResult + @JvmStatic + fun warning(context: Context, message: String): Toast? { + return warning(context, message, Toast.LENGTH_SHORT, true) + } + + @CheckResult + @JvmStatic + fun warning(context: Context, message: String, duration: Int): Toast? { + return warning(context, message, duration, true) + } + + @CheckResult + @JvmStatic + fun warning(context: Context, message: String, duration: Int, withIcon: Boolean): Toast? { + return custom(context, message, getDrawable(context, R.drawable.ic_error_outline_white_48dp), DEFAULT_TEXT_COLOR, WARNING_COLOR, duration, withIcon, true) + } + + @CheckResult + @JvmStatic + fun info(context: Context, message: String): Toast? { + return info(context, message, Toast.LENGTH_SHORT, true) + } + + @CheckResult + @JvmStatic + fun info(context: Context, message: String, duration: Int): Toast? { + return info(context, message, duration, true) + } + + @CheckResult + @JvmStatic + fun info(context: Context, message: String, duration: Int, withIcon: Boolean): Toast? { + return custom(context, message, getDrawable(context, R.drawable.ic_info_outline_white_48dp), DEFAULT_TEXT_COLOR, INFO_COLOR, duration, withIcon, true) + } + + @CheckResult + @JvmStatic + fun success(context: Context, message: String): Toast? { + return success(context, message, Toast.LENGTH_SHORT, true) + } + + @CheckResult + @JvmStatic + fun success(context: Context, message: String, duration: Int): Toast? { + return success(context, message, duration, true) + } + + @CheckResult + @JvmStatic + fun success(context: Context, message: String, duration: Int, withIcon: Boolean): Toast? { + return custom(context, message, getDrawable(context, R.drawable.ic_check_white_48dp), DEFAULT_TEXT_COLOR, SUCCESS_COLOR, duration, withIcon, true) + } + + @CheckResult + @JvmStatic + fun error(context: Context, message: String): Toast? { + return error(context, message, Toast.LENGTH_SHORT, true) + } + + //===========================================常规方法============================================ + @CheckResult + @JvmStatic + fun error(context: Context, message: String, duration: Int): Toast? { + return error(context, message, duration, true) + } + + @CheckResult + @JvmStatic + fun error(context: Context, message: String, duration: Int, withIcon: Boolean): Toast? { + return custom(context, message, getDrawable(context, R.drawable.ic_clear_white_48dp), DEFAULT_TEXT_COLOR, ERROR_COLOR, duration, withIcon, true) + } + + @CheckResult + @JvmStatic + fun custom(context: Context, message: String, icon: Drawable?, @ColorInt textColor: Int, duration: Int, withIcon: Boolean): Toast? { + return custom(context, message, icon, textColor, -1, duration, withIcon, false) + } + + //*******************************************内需方法******************************************** + @CheckResult + @JvmStatic + fun custom(context: Context, message: String, @DrawableRes iconRes: Int, @ColorInt textColor: Int, @ColorInt tintColor: Int, duration: Int, withIcon: Boolean, shouldTint: Boolean): Toast? { + return custom(context, message, getDrawable(context, iconRes), textColor, tintColor, duration, withIcon, shouldTint) + } + + @CheckResult + @JvmStatic + fun custom(context: Context, message: String, icon: Drawable?, @ColorInt textColor: Int, @ColorInt tintColor: Int, duration: Int, withIcon: Boolean, shouldTint: Boolean): Toast? { + if (currentToast == null) { + currentToast = Toast(context) + } else { + currentToast?.cancel() + currentToast = null + currentToast = Toast(context) + } + val toastLayout = (context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(R.layout.toast_layout, null) + val toastIcon = toastLayout.findViewById(R.id.toast_icon) + val toastTextView = toastLayout.findViewById(R.id.toast_text) + val drawableFrame: Drawable? + drawableFrame = if (shouldTint) { + tint9PatchDrawableFrame(context, tintColor) + } else { + getDrawable(context, R.drawable.toast_frame) + } + setBackground(toastLayout, drawableFrame) + if (withIcon) { + requireNotNull(icon) { "Avoid passing 'icon' as null if 'withIcon' is set to true" } + setBackground(toastIcon, icon) + } else { + toastIcon.visibility = View.GONE + } + toastTextView.setTextColor(textColor) + toastTextView.text = message + toastTextView.typeface = Typeface.create(TOAST_TYPEFACE, Typeface.NORMAL) + currentToast?.view = toastLayout + currentToast?.duration = duration + return currentToast + } + + @JvmStatic + fun tint9PatchDrawableFrame(context: Context, @ColorInt tintColor: Int): Drawable? { + val toastDrawable = getDrawable(context, R.drawable.toast_frame) as NinePatchDrawable? + toastDrawable?.colorFilter = PorterDuffColorFilter(tintColor, PorterDuff.Mode.SRC_IN) + return toastDrawable + } + + //===========================================内需方法============================================ + //******************************************系统 Toast 替代方法*************************************** + @JvmStatic + fun setBackground(view: View, drawable: Drawable?) { + view.background = drawable + } + + @JvmStatic + fun getDrawable(context: Context, @DrawableRes id: Int): Drawable? { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + context.getDrawable(id) + } else { + context.resources.getDrawable(id) + } + } + + /** + * 封装了Toast的方法 :需要等待 + * + * @param context Context + * @param str 要显示的字符串 + * @param isLong Toast.LENGTH_LONG / Toast.LENGTH_SHORT + */ + @JvmStatic + fun showToast(context: Context?, str: String?, isLong: Boolean) { + if (isLong) { + Toast.makeText(context, str, Toast.LENGTH_LONG).show() + } else { + Toast.makeText(context, str, Toast.LENGTH_SHORT).show() + } + } + + /** + * 封装了Toast的方法 :需要等待 + */ + @JvmStatic + fun showToastShort(str: String?) { + Toast.makeText(RxTool.getContext(), str, Toast.LENGTH_SHORT).show() + } + + /** + * 封装了Toast的方法 :需要等待 + */ + @JvmStatic + fun showToastShort(resId: Int) { + Toast.makeText(RxTool.getContext(), RxTool.getContext().getString(resId), Toast.LENGTH_SHORT).show() + } + + /** + * 封装了Toast的方法 :需要等待 + */ + @JvmStatic + fun showToastLong(str: String?) { + Toast.makeText(RxTool.getContext(), str, Toast.LENGTH_LONG).show() + } + + /** + * 封装了Toast的方法 :需要等待 + */ + @JvmStatic + fun showToastLong(resId: Int) { + Toast.makeText(RxTool.getContext(), RxTool.getContext().getString(resId), Toast.LENGTH_LONG).show() + } + + /** + * Toast 替代方法 :立即显示无需等待 + * + * @param msg 显示内容 + */ + @JvmStatic + fun showToast(msg: String?) { + if (mToast == null) { + mToast = Toast.makeText(RxTool.getContext(), msg, Toast.LENGTH_LONG) + } else { + mToast?.setText(msg) + } + mToast?.show() + } + + /** + * Toast 替代方法 :立即显示无需等待 + * + * @param resId String资源ID + */ + @SuppressLint("ShowToast") + @JvmStatic + fun showToast(resId: Int) { + if (mToast == null) { + mToast = Toast.makeText(RxTool.getContext(), RxTool.getContext().getString(resId), Toast.LENGTH_LONG) + } else { + mToast?.setText(RxTool.getContext().getString(resId)) + } + mToast?.show() + } + + /** + * Toast 替代方法 :立即显示无需等待 + * + * @param context 实体 + * @param resId String资源ID + * @param duration 显示时长 + */ + @JvmStatic + fun showToast(context: Context, resId: Int, duration: Int) { + showToast(context, context.getString(resId), duration) + } + //===========================================Toast 替代方法====================================== + /** + * Toast 替代方法 :立即显示无需等待 + * + * @param context 实体 + * @param msg 要显示的字符串 + * @param duration 显示时长 + */ + @SuppressLint("ShowToast") + @JvmStatic + fun showToast(context: Context?, msg: String?, duration: Int) { + if (mToast == null) { + mToast = Toast.makeText(context, msg, duration) + } else { + mToast?.setText(msg) + } + mToast?.show() + } + + @JvmStatic + fun doubleClickExit(): Boolean { + if (System.currentTimeMillis() - mExitTime > 2000) { + normal("再按一次退出") + mExitTime = System.currentTimeMillis() + return false + } + return true + } +} \ No newline at end of file diff --git a/RxUI/src/main/java/com/tamsiree/rxui/activity/ActivityWebView.kt b/RxUI/src/main/java/com/tamsiree/rxui/activity/ActivityWebView.kt index ace1569a..fb671439 100644 --- a/RxUI/src/main/java/com/tamsiree/rxui/activity/ActivityWebView.kt +++ b/RxUI/src/main/java/com/tamsiree/rxui/activity/ActivityWebView.kt @@ -60,8 +60,8 @@ class ActivityWebView : ActivityBase() { pb_web_base.visibility = View.VISIBLE } - override fun onReceivedTitle(title: String?) { - web_rx_title.title = title + override fun onReceivedTitle(title: String) { + web_rx_title.setTitle(title) } override fun onProgressChanged(newProgress: Int) { diff --git a/RxUI/src/main/java/com/tamsiree/rxui/adapter/AdapterCardViewModelPicture.java b/RxUI/src/main/java/com/tamsiree/rxui/adapter/AdapterCardViewModelPicture.java deleted file mode 100644 index 1c2d157b..00000000 --- a/RxUI/src/main/java/com/tamsiree/rxui/adapter/AdapterCardViewModelPicture.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.tamsiree.rxui.adapter; - -import android.content.Context; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.TextView; - -import com.bumptech.glide.Glide; -import com.tamsiree.rxui.R; -import com.tamsiree.rxui.model.ModelPicture; -import com.tamsiree.rxui.view.RxTextAutoZoom; -import com.tamsiree.rxui.view.cardstack.RxCardStackView; -import com.tamsiree.rxui.view.cardstack.tools.RxAdapterStack; - -import java.io.File; - -public class AdapterCardViewModelPicture extends RxAdapterStack { - - public AdapterCardViewModelPicture(Context context) { - super(context); - } - - @Override - public void bindView(ModelPicture data, int position, RxCardStackView.ViewHolder holder) { - if (holder instanceof ColorItemViewHolder) { - ColorItemViewHolder h = (ColorItemViewHolder) holder; - h.onBind(data, position); - if (getItemCount() < 2) { - h.mRxTextAutoZoom.setVisibility(View.GONE); - } else { - h.mRxTextAutoZoom.setVisibility(View.VISIBLE); - } - } - } - - @Override - protected RxCardStackView.ViewHolder onCreateView(ViewGroup parent, int viewType) { - View view = getLayoutInflater().inflate(R.layout.card_item_picture, parent, false); - return new ColorItemViewHolder(view); - } - - @Override - public int getItemViewType(int position) { - return R.layout.card_item_picture; - } - - static class ColorItemViewHolder extends RxCardStackView.ViewHolder { - View mContainerContent; - ImageView mIvPicture; - TextView mTvPointLo; - TextView mTvPointLa; - TextView mTvCollectDate; - RxTextAutoZoom mRxTextAutoZoom; - - - public ColorItemViewHolder(View view) { - super(view); - mIvPicture = view.findViewById(R.id.iv_picture); - mContainerContent = view.findViewById(R.id.container_list_content); - mTvPointLo = view.findViewById(R.id.tv_point_lo); - mTvPointLa = view.findViewById(R.id.tv_point_la); - mTvCollectDate = view.findViewById(R.id.tv_collect_date); - mRxTextAutoZoom = view.findViewById(R.id.tv_number); - - } - - @Override - public void onItemExpand(boolean b) { - mContainerContent.setVisibility(b ? View.VISIBLE : View.GONE); - } - - public void onBind(ModelPicture data, int position) { - Glide.with(getContext()). - load(new File(data.getPicturePath())). - thumbnail(0.5f). - into(mIvPicture); - mTvCollectDate.setText(data.getDate()); - mTvPointLo.setText(data.getLongitude()); - mTvPointLa.setText(data.getLatitude()); - mRxTextAutoZoom.setText("第 " + (position + 1) + " 张"); - } - - } - -} diff --git a/RxUI/src/main/java/com/tamsiree/rxui/adapter/AdapterCardViewModelPicture.kt b/RxUI/src/main/java/com/tamsiree/rxui/adapter/AdapterCardViewModelPicture.kt new file mode 100644 index 00000000..2c98d7d7 --- /dev/null +++ b/RxUI/src/main/java/com/tamsiree/rxui/adapter/AdapterCardViewModelPicture.kt @@ -0,0 +1,66 @@ +package com.tamsiree.rxui.adapter + +import android.content.Context +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView +import com.bumptech.glide.Glide +import com.tamsiree.rxui.R +import com.tamsiree.rxui.model.ModelPicture +import com.tamsiree.rxui.view.RxTextAutoZoom +import com.tamsiree.rxui.view.cardstack.RxCardStackView +import com.tamsiree.rxui.view.cardstack.tools.RxAdapterStack +import java.io.File + +class AdapterCardViewModelPicture(context: Context?) : RxAdapterStack(context) { + override fun bindView(data: ModelPicture, position: Int, holder: RxCardStackView.ViewHolder) { + if (holder is ColorItemViewHolder) { + val h = holder + h.onBind(data, position) + if (itemCount < 2) { + h.mRxTextAutoZoom.visibility = View.GONE + } else { + h.mRxTextAutoZoom.visibility = View.VISIBLE + } + } + } + + override fun onCreateView(parent: ViewGroup, viewType: Int): RxCardStackView.ViewHolder { + val view = layoutInflater.inflate(R.layout.card_item_picture, parent, false) + return ColorItemViewHolder(view) + } + + override fun getItemViewType(position: Int): Int { + return R.layout.card_item_picture + } + + internal class ColorItemViewHolder(view: View) : RxCardStackView.ViewHolder(view) { + var mContainerContent: View + var mIvPicture: ImageView + var mTvPointLo: TextView + var mTvPointLa: TextView + var mTvCollectDate: TextView + var mRxTextAutoZoom: RxTextAutoZoom + override fun onItemExpand(b: Boolean) { + mContainerContent.visibility = if (b) View.VISIBLE else View.GONE + } + + fun onBind(data: ModelPicture, position: Int) { + Glide.with(context).load(File(data.picturePath)).thumbnail(0.5f).into(mIvPicture) + mTvCollectDate.text = data.date + mTvPointLo.text = data.longitude + mTvPointLa.text = data.latitude + mRxTextAutoZoom.setText("第 " + (position + 1) + " 张") + } + + init { + mIvPicture = view.findViewById(R.id.iv_picture) + mContainerContent = view.findViewById(R.id.container_list_content) + mTvPointLo = view.findViewById(R.id.tv_point_lo) + mTvPointLa = view.findViewById(R.id.tv_point_la) + mTvCollectDate = view.findViewById(R.id.tv_collect_date) + mRxTextAutoZoom = view.findViewById(R.id.tv_number) + } + } +} \ No newline at end of file diff --git a/RxUI/src/main/java/com/tamsiree/rxui/animation/FloatAnimation.java b/RxUI/src/main/java/com/tamsiree/rxui/animation/FloatAnimation.java deleted file mode 100644 index e3fd84d9..00000000 --- a/RxUI/src/main/java/com/tamsiree/rxui/animation/FloatAnimation.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.tamsiree.rxui.animation; - -import android.graphics.Matrix; -import android.graphics.Path; -import android.graphics.PathMeasure; -import android.view.View; -import android.view.animation.Animation; -import android.view.animation.Transformation; - -/** - * @author Tamsiree - * @date 2018/6/19 - */ -public class FloatAnimation extends Animation { - private PathMeasure mPm; - private View mView; - private float mDistance; - private float mRotation; - - public FloatAnimation(Path path, float rotation, View parent, View child) { - mPm = new PathMeasure(path, false); - mDistance = mPm.getLength(); - mView = child; - mRotation = rotation; - parent.setLayerType(View.LAYER_TYPE_HARDWARE, null); - } - - - private static float scale(double a, double b, double c, double d, double e) { - return (float) ((a - b) / (c - b) * (e - d) + d); - } - - @Override - protected void applyTransformation(float factor, Transformation transformation) { - Matrix matrix = transformation.getMatrix(); - mPm.getMatrix(mDistance * factor, matrix, PathMeasure.POSITION_MATRIX_FLAG); - mView.setRotation(mRotation * factor); - float scale = 1F; - if (3000.0F * factor < 200.0F) { - scale = scale(factor, 0.0D, 0.06666667014360428D, 0.20000000298023224D, 1.100000023841858D); - } else if (3000.0F * factor < 300.0F) { - scale = scale(factor, 0.06666667014360428D, 0.10000000149011612D, 1.100000023841858D, 1.0D); - } - mView.setScaleX(scale); - mView.setScaleY(scale); - transformation.setAlpha(1.0F - factor); - } -} \ No newline at end of file diff --git a/RxUI/src/main/java/com/tamsiree/rxui/animation/FloatAnimation.kt b/RxUI/src/main/java/com/tamsiree/rxui/animation/FloatAnimation.kt new file mode 100644 index 00000000..b60e2c4e --- /dev/null +++ b/RxUI/src/main/java/com/tamsiree/rxui/animation/FloatAnimation.kt @@ -0,0 +1,45 @@ +package com.tamsiree.rxui.animation + +import android.graphics.Path +import android.graphics.PathMeasure +import android.view.View +import android.view.animation.Animation +import android.view.animation.Transformation + +/** + * @author Tamsiree + * @date 2018/6/19 + */ +class FloatAnimation(path: Path?, rotation: Float, parent: View, child: View) : Animation() { + private val mPm: PathMeasure = PathMeasure(path, false) + private val mView: View + private val mDistance: Float + private val mRotation: Float + override fun applyTransformation(factor: Float, transformation: Transformation) { + val matrix = transformation.matrix + mPm.getMatrix(mDistance * factor, matrix, PathMeasure.POSITION_MATRIX_FLAG) + mView.rotation = mRotation * factor + var scale = 1f + if (3000.0f * factor < 200.0f) { + scale = scale(factor.toDouble(), 0.0, 0.06666667014360428, 0.20000000298023224, 1.100000023841858) + } else if (3000.0f * factor < 300.0f) { + scale = scale(factor.toDouble(), 0.06666667014360428, 0.10000000149011612, 1.100000023841858, 1.0) + } + mView.scaleX = scale + mView.scaleY = scale + transformation.alpha = 1.0f - factor + } + + companion object { + private fun scale(a: Double, b: Double, c: Double, d: Double, e: Double): Float { + return ((a - b) / (c - b) * (e - d) + d).toFloat() + } + } + + init { + mDistance = mPm.length + mView = child + mRotation = rotation + parent.setLayerType(View.LAYER_TYPE_HARDWARE, null) + } +} \ No newline at end of file diff --git a/RxUI/src/main/java/com/tamsiree/rxui/animation/RxAbstractPathAnimator.java b/RxUI/src/main/java/com/tamsiree/rxui/animation/RxAbstractPathAnimator.java deleted file mode 100644 index a1e14985..00000000 --- a/RxUI/src/main/java/com/tamsiree/rxui/animation/RxAbstractPathAnimator.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (C) 2015 tyrantgit - * - * 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.tamsiree.rxui.animation; - -import android.content.res.Resources; -import android.content.res.TypedArray; -import android.graphics.Path; -import android.view.View; -import android.view.ViewGroup; - -import com.tamsiree.rxui.R; - -import java.util.Random; -import java.util.concurrent.atomic.AtomicInteger; - - -/** - * @author tamsiree - */ -public abstract class RxAbstractPathAnimator { - protected final Config mConfig; - private final Random mRandom; - - - public RxAbstractPathAnimator(Config config) { - mConfig = config; - mRandom = new Random(); - } - - public float randomRotation() { - return mRandom.nextFloat() * 28.6F - 14.3F; - } - - public Path createPath(AtomicInteger counter, View view, int factor) { - Random r = mRandom; - int x = r.nextInt(mConfig.xRand); - int x2 = r.nextInt(mConfig.xRand); - int y = view.getHeight() - mConfig.initY; - int y2 = counter.intValue() * 15 + mConfig.animLength * factor + r.nextInt(mConfig.animLengthRand); - factor = y2 / mConfig.bezierFactor; - x = mConfig.xPointFactor + x; - x2 = mConfig.xPointFactor + x2; - int y3 = y - y2; - y2 = y - y2 / 2; - Path p = new Path(); - p.moveTo(mConfig.initX, y); - p.cubicTo(mConfig.initX, y - factor, x, y2 + factor, x, y2); - p.moveTo(x, y2); - p.cubicTo(x, y2 - factor, x2, y3 + factor, x2, y3); - return p; - } - - public abstract void start(View child, ViewGroup parent); - - public static class Config { - public int initX; - public int initY; - public int xRand; - public int animLengthRand; - public int bezierFactor; - public int xPointFactor; - public int animLength; - public int heartWidth; - public int heartHeight; - public int animDuration; - - public static Config fromTypeArray(TypedArray typedArray) { - Config config = new Config(); - Resources res = typedArray.getResources(); - config.initX = (int) typedArray.getDimension(R.styleable.RxHeartLayout_initX, - res.getDimensionPixelOffset(R.dimen.heart_anim_init_x)); - config.initY = (int) typedArray.getDimension(R.styleable.RxHeartLayout_initY, - res.getDimensionPixelOffset(R.dimen.heart_anim_init_y)); - config.xRand = (int) typedArray.getDimension(R.styleable.RxHeartLayout_xRand, - res.getDimensionPixelOffset(R.dimen.heart_anim_bezier_x_rand)); - config.animLength = (int) typedArray.getDimension(R.styleable.RxHeartLayout_animLength, - res.getDimensionPixelOffset(R.dimen.heart_anim_length)); - config.animLengthRand = (int) typedArray.getDimension(R.styleable.RxHeartLayout_animLengthRand, - res.getDimensionPixelOffset(R.dimen.heart_anim_length_rand)); - config.bezierFactor = typedArray.getInteger(R.styleable.RxHeartLayout_bezierFactor, - res.getInteger(R.integer.heart_anim_bezier_factor)); - config.xPointFactor = (int) typedArray.getDimension(R.styleable.RxHeartLayout_xPointFactor, - res.getDimensionPixelOffset(R.dimen.heart_anim_x_point_factor)); - config.heartWidth = (int) typedArray.getDimension(R.styleable.RxHeartLayout_heart_width, - res.getDimensionPixelOffset(R.dimen.heart_size_width)); - config.heartHeight = (int) typedArray.getDimension(R.styleable.RxHeartLayout_heart_height, - res.getDimensionPixelOffset(R.dimen.heart_size_height)); - config.animDuration = typedArray.getInteger(R.styleable.RxHeartLayout_anim_duration, - res.getInteger(R.integer.anim_duration)); - return config; - } - } -} - diff --git a/RxUI/src/main/java/com/tamsiree/rxui/animation/RxAbstractPathAnimator.kt b/RxUI/src/main/java/com/tamsiree/rxui/animation/RxAbstractPathAnimator.kt new file mode 100644 index 00000000..d6e3c5d9 --- /dev/null +++ b/RxUI/src/main/java/com/tamsiree/rxui/animation/RxAbstractPathAnimator.kt @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2015 tyrantgit + * + * 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.tamsiree.rxui.animation + +import android.content.res.TypedArray +import android.graphics.Path +import android.view.View +import android.view.ViewGroup +import com.tamsiree.rxui.R +import java.util.* +import java.util.concurrent.atomic.AtomicInteger + +/** + * @author tamsiree + */ +abstract class RxAbstractPathAnimator(protected val mConfig: Config) { + private val mRandom: Random = Random() + fun randomRotation(): Float { + return mRandom.nextFloat() * 28.6f - 14.3f + } + + fun createPath(counter: AtomicInteger, view: View, factor: Int): Path { + var factor = factor + val r = mRandom + var x = r.nextInt(mConfig.xRand) + var x2 = r.nextInt(mConfig.xRand) + val y = view.height - mConfig.initY + var y2: Int = counter.toInt() * 15 + mConfig.animLength * factor + r.nextInt(mConfig.animLengthRand) + factor = y2 / mConfig.bezierFactor + x += mConfig.xPointFactor + x2 += mConfig.xPointFactor + val y3 = y - y2 + y2 = y - y2 / 2 + val p = Path() + p.moveTo(mConfig.initX.toFloat(), y.toFloat()) + p.cubicTo(mConfig.initX.toFloat(), y - factor.toFloat(), x.toFloat(), y2 + factor.toFloat(), x.toFloat(), y2.toFloat()) + p.moveTo(x.toFloat(), y2.toFloat()) + p.cubicTo(x.toFloat(), y2 - factor.toFloat(), x2.toFloat(), y3 + factor.toFloat(), x2.toFloat(), y3.toFloat()) + return p + } + + abstract fun start(child: View, parent: ViewGroup?) + class Config { + var initX = 0 + var initY = 0 + var xRand = 0 + var animLengthRand = 0 + var bezierFactor = 0 + var xPointFactor = 0 + var animLength = 0 + + @JvmField + var heartWidth = 0 + + @JvmField + var heartHeight = 0 + + @JvmField + var animDuration = 0 + + companion object { + @JvmStatic + fun fromTypeArray(typedArray: TypedArray): Config { + val config = Config() + val res = typedArray.resources + config.initX = typedArray.getDimension(R.styleable.RxHeartLayout_initX, + res.getDimensionPixelOffset(R.dimen.heart_anim_init_x).toFloat()).toInt() + config.initY = typedArray.getDimension(R.styleable.RxHeartLayout_initY, + res.getDimensionPixelOffset(R.dimen.heart_anim_init_y).toFloat()).toInt() + config.xRand = typedArray.getDimension(R.styleable.RxHeartLayout_xRand, + res.getDimensionPixelOffset(R.dimen.heart_anim_bezier_x_rand).toFloat()).toInt() + config.animLength = typedArray.getDimension(R.styleable.RxHeartLayout_animLength, + res.getDimensionPixelOffset(R.dimen.heart_anim_length).toFloat()).toInt() + config.animLengthRand = typedArray.getDimension(R.styleable.RxHeartLayout_animLengthRand, + res.getDimensionPixelOffset(R.dimen.heart_anim_length_rand).toFloat()).toInt() + config.bezierFactor = typedArray.getInteger(R.styleable.RxHeartLayout_bezierFactor, + res.getInteger(R.integer.heart_anim_bezier_factor)) + config.xPointFactor = typedArray.getDimension(R.styleable.RxHeartLayout_xPointFactor, + res.getDimensionPixelOffset(R.dimen.heart_anim_x_point_factor).toFloat()).toInt() + config.heartWidth = typedArray.getDimension(R.styleable.RxHeartLayout_heart_width, + res.getDimensionPixelOffset(R.dimen.heart_size_width).toFloat()).toInt() + config.heartHeight = typedArray.getDimension(R.styleable.RxHeartLayout_heart_height, + res.getDimensionPixelOffset(R.dimen.heart_size_height).toFloat()).toInt() + config.animDuration = typedArray.getInteger(R.styleable.RxHeartLayout_anim_duration, + res.getInteger(R.integer.anim_duration)) + return config + } + } + } + +} \ No newline at end of file diff --git a/RxUI/src/main/java/com/tamsiree/rxui/animation/RxPathAnimator.java b/RxUI/src/main/java/com/tamsiree/rxui/animation/RxPathAnimator.java deleted file mode 100644 index 419fabad..00000000 --- a/RxUI/src/main/java/com/tamsiree/rxui/animation/RxPathAnimator.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2015 tyrantgit - * - * 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.tamsiree.rxui.animation; - -import android.os.Handler; -import android.os.Looper; -import android.view.View; -import android.view.ViewGroup; -import android.view.animation.Animation; -import android.view.animation.LinearInterpolator; - -import java.util.concurrent.atomic.AtomicInteger; - -/** - * @author tamsiree - */ -public class RxPathAnimator extends RxAbstractPathAnimator { - private final AtomicInteger mCounter = new AtomicInteger(0); - private Handler mHandler; - - public RxPathAnimator(Config config) { - super(config); - mHandler = new Handler(Looper.getMainLooper()); - } - - - @Override - public void start(final View child, final ViewGroup parent) { - parent.addView(child, new ViewGroup.LayoutParams(mConfig.heartWidth, mConfig.heartHeight)); - FloatAnimation anim = new FloatAnimation(createPath(mCounter, parent, 2), randomRotation(), parent, child); - anim.setDuration(mConfig.animDuration); - anim.setInterpolator(new LinearInterpolator()); - anim.setAnimationListener(new Animation.AnimationListener() { - @Override - public void onAnimationEnd(Animation animation) { - mHandler.post(new Runnable() { - @Override - public void run() { - parent.removeView(child); - } - }); - mCounter.decrementAndGet(); - } - - @Override - public void onAnimationRepeat(Animation animation) { - - } - - @Override - public void onAnimationStart(Animation animation) { - mCounter.incrementAndGet(); - } - }); - anim.setInterpolator(new LinearInterpolator()); - child.startAnimation(anim); - } -} - diff --git a/RxUI/src/main/java/com/tamsiree/rxui/animation/RxPathAnimator.kt b/RxUI/src/main/java/com/tamsiree/rxui/animation/RxPathAnimator.kt new file mode 100644 index 00000000..10c921f2 --- /dev/null +++ b/RxUI/src/main/java/com/tamsiree/rxui/animation/RxPathAnimator.kt @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2015 tyrantgit + * + * 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.tamsiree.rxui.animation + +import android.os.Handler +import android.os.Looper +import android.view.View +import android.view.ViewGroup +import android.view.animation.Animation +import android.view.animation.LinearInterpolator +import java.util.concurrent.atomic.AtomicInteger + +/** + * @author tamsiree + */ +class RxPathAnimator(config: Config?) : RxAbstractPathAnimator(config!!) { + private val mCounter = AtomicInteger(0) + private val mHandler: Handler + override fun start(child: View, parent: ViewGroup?) { + parent!!.addView(child, ViewGroup.LayoutParams(mConfig.heartWidth, mConfig.heartHeight)) + val anim = FloatAnimation(createPath(mCounter, parent, 2), randomRotation(), parent, child) + anim.duration = mConfig.animDuration.toLong() + anim.interpolator = LinearInterpolator() + anim.setAnimationListener(object : Animation.AnimationListener { + override fun onAnimationEnd(animation: Animation) { + mHandler.post { parent.removeView(child) } + mCounter.decrementAndGet() + } + + override fun onAnimationRepeat(animation: Animation) {} + override fun onAnimationStart(animation: Animation) { + mCounter.incrementAndGet() + } + }) + anim.interpolator = LinearInterpolator() + child.startAnimation(anim) + } + + init { + mHandler = Handler(Looper.getMainLooper()) + } +} \ No newline at end of file diff --git a/RxUI/src/main/java/com/tamsiree/rxui/animation/RxShineAnimator.java b/RxUI/src/main/java/com/tamsiree/rxui/animation/RxShineAnimator.java deleted file mode 100644 index f86d74f4..00000000 --- a/RxUI/src/main/java/com/tamsiree/rxui/animation/RxShineAnimator.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.tamsiree.rxui.animation; - -import android.animation.ValueAnimator; -import android.graphics.Canvas; - -import com.tamsiree.rxui.view.likeview.tools.RxShineView; -import com.tamsiree.rxui.view.likeview.tools.ei.RxEase; -import com.tamsiree.rxui.view.likeview.tools.ei.RxEasingInterpolator; - - -/** - * @author tamsiree - * @date 2016/7/5 下午5:09 - */ -public class RxShineAnimator extends ValueAnimator { - - float MAX_VALUE = 1.5f; - long ANIM_DURATION = 1500; - Canvas canvas; - - public RxShineAnimator() { - setFloatValues(1f, MAX_VALUE); - setDuration(ANIM_DURATION); - setStartDelay(200); - setInterpolator(new RxEasingInterpolator(RxEase.QUART_OUT)); - } - - public RxShineAnimator(long duration, float maxValue, long delay) { - setFloatValues(1f, maxValue); - setDuration(duration); - setStartDelay(delay); - setInterpolator(new RxEasingInterpolator(RxEase.QUART_OUT)); - } - - public void startAnim(final RxShineView rxShineView, final int centerAnimX, final int centerAnimY) { - - start(); - } - - public void setCanvas(Canvas canvas) { - this.canvas = canvas; - } - -} diff --git a/RxUI/src/main/java/com/tamsiree/rxui/animation/RxShineAnimator.kt b/RxUI/src/main/java/com/tamsiree/rxui/animation/RxShineAnimator.kt new file mode 100644 index 00000000..1d90b4d4 --- /dev/null +++ b/RxUI/src/main/java/com/tamsiree/rxui/animation/RxShineAnimator.kt @@ -0,0 +1,36 @@ +package com.tamsiree.rxui.animation + +import android.animation.ValueAnimator +import android.graphics.Canvas +import com.tamsiree.rxui.view.likeview.tools.RxShineView +import com.tamsiree.rxui.view.likeview.tools.ei.RxEase +import com.tamsiree.rxui.view.likeview.tools.ei.RxEasingInterpolator + +/** + * @author tamsiree + * @date 2016/7/5 下午5:09 + */ +class RxShineAnimator : ValueAnimator { + var MAX_VALUE = 1.5f + var ANIM_DURATION: Long = 1500 + var canvas: Canvas? = null + + constructor() { + setFloatValues(1f, MAX_VALUE) + duration = ANIM_DURATION + startDelay = 200 + interpolator = RxEasingInterpolator(RxEase.QUART_OUT) + } + + constructor(duration: Long, maxValue: Float, delay: Long) { + setFloatValues(1f, maxValue) + setDuration(duration) + startDelay = delay + interpolator = RxEasingInterpolator(RxEase.QUART_OUT) + } + + fun startAnim(rxShineView: RxShineView?, centerAnimX: Int, centerAnimY: Int) { + start() + } + +} \ No newline at end of file diff --git a/RxUI/src/main/java/com/tamsiree/rxui/view/RxTitle.java b/RxUI/src/main/java/com/tamsiree/rxui/view/RxTitle.java deleted file mode 100644 index e01ec004..00000000 --- a/RxUI/src/main/java/com/tamsiree/rxui/view/RxTitle.java +++ /dev/null @@ -1,548 +0,0 @@ -package com.tamsiree.rxui.view; - -import android.annotation.TargetApi; -import android.app.Activity; -import android.content.Context; -import android.content.res.TypedArray; -import android.graphics.Canvas; -import android.util.AttributeSet; -import android.util.TypedValue; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.FrameLayout; -import android.widget.ImageView; -import android.widget.LinearLayout; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import com.tamsiree.rxkit.RxActivityTool; -import com.tamsiree.rxkit.RxDataTool; -import com.tamsiree.rxkit.RxImageTool; -import com.tamsiree.rxkit.RxKeyboardTool; -import com.tamsiree.rxui.R; - -/** - * @author tamsiree - * @date 2017/1/2 - */ - -public class RxTitle extends FrameLayout { - //*******************************************控件start****************************************** - - private View mRootView; - - //根布局 - private LinearLayout mRootLayout; - - //Title的TextView控件 - private RxTextAutoZoom mTvTitle; - - //左边布局 - private LinearLayout mLlLeft; - - //左边ImageView控件的背景 - private LinearLayout mllIconLeftBg; - - //左边ImageView控件 - private ImageView mIvLeft; - - //左边TextView控件 - private TextView mTvLeft; - - //右边布局 - private LinearLayout mLlRight; - - //左边ImageView控件的背景 - private LinearLayout mllIconRightBg; - - //右边ImageView控件 - private ImageView mIvRight; - - //右边TextView控件 - private TextView mTvRight; - //===========================================控件end============================================= - - //*******************************************属性start******************************************* - //Title文字 - private String mTitle; - - //Title字体颜色 - private int mTitleColor; - - //Title字体大小 - private int mTitleSize; - - //Title是否显示 - private boolean mTitleVisibility; - - //------------------------------------------左侧布局--------------------------------------------- - - //左边 ICON 引用的资源ID - private int mLeftIcon; - - //左边 ICON 是否显示 - private boolean mLeftIconVisibility; - - //左边文字 - private String mLeftText; - - //左边字体颜色 - private int mLeftTextColor; - - //左边字体大小 - private int mLeftTextSize; - - //左边文字是否显示 - private boolean mLeftTextVisibility; - - //--------------------------------------------右侧布局------------------------------------------- - - //右边 ICON 引用的资源ID - private int mRightIcon; - - //右边文字 - private String mRightText; - - //右边 ICON 是否显示 - private boolean mRightIconVisibility; - - //右边字体颜色 - private int mRightTextColor; - - //右边字体大小 - private int mRightTextSize; - - //右边文字是否显示 - private boolean mRightTextVisibility; - - //===========================================属性end============================================= - - public RxTitle(Context context) { - super(context); - initView(context, null); - } - - public RxTitle(Context context, AttributeSet attrs) { - super(context, attrs); - //导入布局 - initView(context, attrs); - } - - public RxTitle(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - initView(context, attrs); - } - - @TargetApi(21) - public RxTitle(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { - super(context, attrs, defStyleAttr, defStyleRes); - initView(context, attrs); - } - - private void initView(final Context context, AttributeSet attrs) { - mRootView = LayoutInflater.from(context).inflate(R.layout.include_rx_title, this); - mRootLayout = findViewById(R.id.root_layout); - mTvTitle = findViewById(R.id.tv_rx_title); - mLlLeft = findViewById(R.id.ll_left); - mllIconLeftBg = findViewById(R.id.ll_icon_left_bg); - mllIconRightBg = findViewById(R.id.ll_icon_right_bg); - mIvLeft = findViewById(R.id.iv_left); - mIvRight = findViewById(R.id.iv_right); - mLlRight = findViewById(R.id.ll_right); - mTvLeft = findViewById(R.id.tv_left); - mTvRight = findViewById(R.id.tv_right); - - //获得这个控件对应的属性。 - TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RxTitle); - - try { - //获得属性值 - //getColor(R.styleable.RxTitle_RxBackground, getResources().getColor(R.color.transparent)) - //标题 - mTitle = a.getString(R.styleable.RxTitle_title); - //标题颜色 - mTitleColor = a.getColor(R.styleable.RxTitle_titleColor, getResources().getColor(R.color.white)); - - //标题字体大小 - mTitleSize = a.getDimensionPixelSize(R.styleable.RxTitle_titleSize, RxImageTool.dp2px(context, 20)); - //TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()) - mTitleVisibility = a.getBoolean(R.styleable.RxTitle_titleVisibility, true); - - //左边图标 - mLeftIcon = a.getResourceId(R.styleable.RxTitle_leftIcon, R.drawable.ic_back); - //右边图标 - mRightIcon = a.getResourceId(R.styleable.RxTitle_rightIcon, R.drawable.set); - //左边图标是否显示 - mLeftIconVisibility = a.getBoolean(R.styleable.RxTitle_leftIconVisibility, true); - //右边图标是否显示 - mRightIconVisibility = a.getBoolean(R.styleable.RxTitle_rightIconVisibility, false); - - mLeftText = a.getString(R.styleable.RxTitle_leftText); - //左边字体颜色 - mLeftTextColor = a.getColor(R.styleable.RxTitle_leftTextColor, getResources().getColor(R.color.white)); - //左侧标题字体大小 - mLeftTextSize = a.getDimensionPixelSize(R.styleable.RxTitle_leftTextSize, RxImageTool.dp2px(context, 8)); - mLeftTextVisibility = a.getBoolean(R.styleable.RxTitle_leftTextVisibility, false); - - mRightText = a.getString(R.styleable.RxTitle_rightText); - //右边字体颜色 - mRightTextColor = a.getColor(R.styleable.RxTitle_rightTextColor, getResources().getColor(R.color.white)); - //标题字体大小 - mRightTextSize = a.getDimensionPixelSize(R.styleable.RxTitle_rightTextSize, RxImageTool.dp2px(context, 8)); - mRightTextVisibility = a.getBoolean(R.styleable.RxTitle_rightTextVisibility, false); - - - } finally { - //回收这个对象 - a.recycle(); - } - - //******************************************************************************************以下属性初始化 - if (!RxDataTool.isNullString(mTitle)) { - setTitle(mTitle); - } - - if (mTitleColor != 0) { - setTitleColor(mTitleColor); - } - - if (mTitleSize != 0) { - setTitleSize(mTitleSize); - } - - if (mLeftIcon != 0) { - setLeftIcon(mLeftIcon); - } - - if (mRightIcon != 0) { - setRightIcon(mRightIcon); - } - - setTitleVisibility(mTitleVisibility); - - setLeftText(mLeftText); - - setLeftTextColor(mLeftTextColor); - - setLeftTextSize(mLeftTextSize); - - setLeftTextVisibility(mLeftTextVisibility); - - setRightText(mRightText); - - setRightTextColor(mRightTextColor); - - setRightTextSize(mRightTextSize); - - setRightTextVisibility(mRightTextVisibility); - - setLeftIconVisibility(mLeftIconVisibility); - - setRightIconVisibility(mRightIconVisibility); - - initAutoFitEditText(context); - //==========================================================================================以上为属性初始化 - } - - private void initAutoFitEditText(Context context) { - mTvTitle.clearFocus(); - mTvTitle.setEnabled(false); - mTvTitle.setFocusableInTouchMode(false); - mTvTitle.setFocusable(false); - mTvTitle.setEnableSizeCache(false); - //might cause crash on some devices - mTvTitle.setMovementMethod(null); - // can be added after layout inflation; - mTvTitle.setMaxHeight(RxImageTool.dp2px(context, 55f)); - //don't forget to add min text size programmatically - mTvTitle.setMinTextSize(37f); - try { - RxTextAutoZoom.setNormalization((Activity) getContext(), mRootLayout, mTvTitle); - RxKeyboardTool.hideSoftInput((Activity) getContext()); - } catch (Exception e) { - e.printStackTrace(); - } - } - - //**********************************************************************************************以下为get方法 - - public LinearLayout getRootLayout() { - return mRootLayout; - } - - public RxTextAutoZoom getTitleTextView() { - return mTvTitle; - } - - public LinearLayout getLeftLayout() { - return mLlLeft; - } - - public ImageView getLeftImageView() { - return mIvLeft; - } - - public TextView getLeftTextView() { - return mTvLeft; - } - - public LinearLayout getRightLayout() { - return mLlRight; - } - - public ImageView getRightImageView() { - return mIvRight; - } - - public TextView getRightTextView() { - return mTvRight; - } - - public boolean isTitleVisibility() { - return mTitleVisibility; - } - - public LinearLayout getLeftIconBg() { - return mllIconLeftBg; - } - - public LinearLayout getRightIconBg() { - return mllIconRightBg; - } - - public void setTitleVisibility(boolean titleVisibility) { - mTitleVisibility = titleVisibility; - if (mTitleVisibility) { - mTvTitle.setVisibility(VISIBLE); - } else { - mTvTitle.setVisibility(GONE); - } - } - - public String getLeftText() { - return mLeftText; - } - - //**********************************************************************************************以下为 左边文字 相关方法 - public void setLeftText(String leftText) { - mLeftText = leftText; - mTvLeft.setText(mLeftText); - - } - - public int getLeftTextColor() { - return mLeftTextColor; - } - - public void setLeftTextColor(int leftTextColor) { - mLeftTextColor = leftTextColor; - mTvLeft.setTextColor(mLeftTextColor); - } - - public int getLeftTextSize() { - return mLeftTextSize; - } - - public void setLeftTextSize(int leftTextSize) { - mLeftTextSize = leftTextSize; - mTvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLeftTextSize); - } - - public boolean isLeftTextVisibility() { - return mLeftTextVisibility; - } - - public void setLeftTextVisibility(boolean leftTextVisibility) { - mLeftTextVisibility = leftTextVisibility; - if (mLeftTextVisibility) { - mTvLeft.setVisibility(VISIBLE); - } else { - mTvLeft.setVisibility(GONE); - } - } - - public String getRightText() { - return mRightText; - } - - //**********************************************************************************************以下为 右边文字 相关方法 - public void setRightText(String rightText) { - mRightText = rightText; - mTvRight.setText(mRightText); - - } - - public int getRightTextColor() { - return mRightTextColor; - } - - public void setRightTextColor(int rightTextColor) { - mRightTextColor = rightTextColor; - mTvRight.setTextColor(mRightTextColor); - } - - public int getRightTextSize() { - return mRightTextSize; - } - - public void setRightTextSize(int rightTextSize) { - mRightTextSize = rightTextSize; - mTvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, mRightTextSize); - } - - //==============================================================================================以上为get方法 - - //**********************************************************************************************以下为set方法 - - public boolean isRightTextVisibility() { - return mRightTextVisibility; - } - - public void setRightTextVisibility(boolean rightTextVisibility) { - mRightTextVisibility = rightTextVisibility; - if (mRightTextVisibility) { - mTvRight.setVisibility(VISIBLE); - if (isRightIconVisibility()) { - mTvRight.setPadding(0, 0, 0, 0); - } - } else { - mTvRight.setVisibility(GONE); - } - } - - public String getTitle() { - return mTitle; - } - - //**********************************************************************************************以下为Title相关方法 - public void setTitle(String title) { - mTitle = title; - mTvTitle.setText(mTitle); - } - - public int getTitleColor() { - return mTitleColor; - } - - public void setTitleColor(int titleColor) { - mTitleColor = titleColor; - mTvTitle.setTextColor(mTitleColor); - } - - public int getTitleSize() { - return mTitleSize; - } - - public void setTitleSize(int titleSize) { - mTitleSize = titleSize; - mTvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleSize); - } - - public int getLeftIcon() { - return mLeftIcon; - } - - public void setLeftIcon(int leftIcon) { - mLeftIcon = leftIcon; - mIvLeft.setImageResource(mLeftIcon); - } - - public int getRightIcon() { - return mRightIcon; - } - //==============================================================================================以上为 Title 相关方法 - - public void setRightIcon(int rightIcon) { - mRightIcon = rightIcon; - mIvRight.setImageResource(mRightIcon); - } - - public boolean isLeftIconVisibility() { - return mLeftIconVisibility; - } - - public void setLeftIconVisibility(boolean leftIconVisibility) { - mLeftIconVisibility = leftIconVisibility; - if (mLeftIconVisibility) { - mIvLeft.setVisibility(VISIBLE); - } else { - mIvLeft.setVisibility(GONE); - } - } - - public boolean isRightIconVisibility() { - return mRightIconVisibility; - } - //==============================================================================================以上为 左边文字 相关方法 - - public void setRightIconVisibility(boolean rightIconVisibility) { - mRightIconVisibility = rightIconVisibility; - if (mRightIconVisibility) { - mIvRight.setVisibility(VISIBLE); - } else { - mIvRight.setVisibility(GONE); - } - } - - public void setLeftFinish(final Activity activity) { - mLlLeft.setOnClickListener(v -> activity.finish()); - } - - public void setLeftFinish(final Activity activity, boolean isFade, boolean isTransition) { - mLlLeft.setOnClickListener(v -> { - RxActivityTool.finishActivity(activity, isTransition); - if (isFade) { - RxActivityTool.fadeTransition(activity); - } - }); - } - - public void setLeftOnClickListener(OnClickListener onClickListener) { - mLlLeft.setOnClickListener(onClickListener); - } - - public void setRightOnClickListener(OnClickListener onClickListener) { - mLlRight.setOnClickListener(onClickListener); - } - //==============================================================================================以上为 右边文字 相关方法 - - public void setLeftTextOnClickListener(OnClickListener onClickListener) { - mTvLeft.setOnClickListener(onClickListener); - } - - public void setRightTextOnClickListener(OnClickListener onClickListener) { - mTvRight.setOnClickListener(onClickListener); - } - - public void setLeftIconOnClickListener(OnClickListener onClickListener) { - mIvLeft.setOnClickListener(onClickListener); - } - - public void setRightIconOnClickListener(OnClickListener onClickListener) { - mIvRight.setOnClickListener(onClickListener); - } - //==============================================================================================以上为set方法 - - @Override - protected void onDraw(Canvas canvas) { - super.onDraw(canvas); - } - - @Override - protected void onLayout(boolean changed, int left, int top, int right, int bottom) { - super.onLayout(changed, left, top, right, bottom); - } - - @Override - protected void onSizeChanged(int w, int h, int oldw, int oldh) { - super.onSizeChanged(w, h, oldw, oldh); - } - - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - super.onMeasure(widthMeasureSpec, heightMeasureSpec); - } - -} diff --git a/RxUI/src/main/java/com/tamsiree/rxui/view/RxTitle.kt b/RxUI/src/main/java/com/tamsiree/rxui/view/RxTitle.kt new file mode 100644 index 00000000..6a4d327a --- /dev/null +++ b/RxUI/src/main/java/com/tamsiree/rxui/view/RxTitle.kt @@ -0,0 +1,500 @@ +package com.tamsiree.rxui.view + +import android.annotation.TargetApi +import android.app.Activity +import android.content.Context +import android.util.AttributeSet +import android.util.TypedValue +import android.view.LayoutInflater +import android.view.View +import android.widget.FrameLayout +import android.widget.ImageView +import android.widget.LinearLayout +import android.widget.TextView +import com.tamsiree.rxkit.RxActivityTool +import com.tamsiree.rxkit.RxDataTool.Companion.isNullString +import com.tamsiree.rxkit.RxImageTool.dp2px +import com.tamsiree.rxkit.RxKeyboardTool.hideSoftInput +import com.tamsiree.rxui.R + +/** + * @ClassName RxTitle + * @Description TODO + * @Author tamsiree + * @Date 2017/1/2 下午 5:58 + * @Version 1.0 + */ +class RxTitle : FrameLayout { + + //*******************************************控件start****************************************** + + //*******************************************控件start****************************************** + private var mRootView: View? = null + + //根布局 + private var mRootLayout: LinearLayout? = null + + //Title的TextView控件 + private var mTvTitle: RxTextAutoZoom? = null + + //左边布局 + private var mLlLeft: LinearLayout? = null + + //左边ImageView控件的背景 + private var mllIconLeftBg: LinearLayout? = null + + //左边ImageView控件 + private var mIvLeft: ImageView? = null + + //左边TextView控件 + private var mTvLeft: TextView? = null + + //右边布局 + private var mLlRight: LinearLayout? = null + + //左边ImageView控件的背景 + private var mllIconRightBg: LinearLayout? = null + + //右边ImageView控件 + private var mIvRight: ImageView? = null + + //右边TextView控件 + private var mTvRight: TextView? = null + //===========================================控件end============================================= + + //===========================================控件end============================================= + //*******************************************属性start******************************************* + //Title文字 + private var mTitle: String? = null + + //Title字体颜色 + private var mTitleColor = 0 + + //Title字体大小 + private var mTitleSize = 0 + + //Title是否显示 + private var mTitleVisibility = false + + //------------------------------------------左侧布局--------------------------------------------- + + //------------------------------------------左侧布局--------------------------------------------- + //左边 ICON 引用的资源ID + private var mLeftIcon = 0 + + //左边 ICON 是否显示 + private var mLeftIconVisibility = false + + //左边文字 + private var mLeftText: String? = null + + //左边字体颜色 + private var mLeftTextColor = 0 + + //左边字体大小 + private var mLeftTextSize = 0 + + //左边文字是否显示 + private var mLeftTextVisibility = false + + //--------------------------------------------右侧布局------------------------------------------- + + //--------------------------------------------右侧布局------------------------------------------- + //右边 ICON 引用的资源ID + private var mRightIcon = 0 + + //右边文字 + private var mRightText: String? = null + + //右边 ICON 是否显示 + private var mRightIconVisibility = false + + //右边字体颜色 + private var mRightTextColor = 0 + + //右边字体大小 + private var mRightTextSize = 0 + + //右边文字是否显示 + private var mRightTextVisibility = false + + + //===========================================属性end============================================= + constructor(context: Context) : super(context) { + initView(context, null) + } + + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { + //导入布局 + initView(context, attrs) + } + + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { + initView(context, attrs) + } + + @TargetApi(21) + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) { + initView(context, attrs) + } + + private fun initView(context: Context, attrs: AttributeSet?) { + mRootView = LayoutInflater.from(context).inflate(R.layout.include_rx_title, this) + mRootLayout = findViewById(R.id.root_layout) + mTvTitle = findViewById(R.id.tv_rx_title) + mLlLeft = findViewById(R.id.ll_left) + mllIconLeftBg = findViewById(R.id.ll_icon_left_bg) + mllIconRightBg = findViewById(R.id.ll_icon_right_bg) + mIvLeft = findViewById(R.id.iv_left) + mIvRight = findViewById(R.id.iv_right) + mLlRight = findViewById(R.id.ll_right) + mTvLeft = findViewById(R.id.tv_left) + mTvRight = findViewById(R.id.tv_right) + + //获得这个控件对应的属性。 + val a = getContext().obtainStyledAttributes(attrs, R.styleable.RxTitle) + try { + //获得属性值 + //getColor(R.styleable.RxTitle_RxBackground, getResources().getColor(R.color.transparent)) + //标题 + mTitle = a.getString(R.styleable.RxTitle_title) + //标题颜色 + mTitleColor = a.getColor(R.styleable.RxTitle_titleColor, resources.getColor(R.color.white)) + + //标题字体大小 + mTitleSize = a.getDimensionPixelSize(R.styleable.RxTitle_titleSize, dp2px(context, 20f)) + //TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()) + mTitleVisibility = a.getBoolean(R.styleable.RxTitle_titleVisibility, true) + + //左边图标 + mLeftIcon = a.getResourceId(R.styleable.RxTitle_leftIcon, R.drawable.ic_back) + //右边图标 + mRightIcon = a.getResourceId(R.styleable.RxTitle_rightIcon, R.drawable.set) + //左边图标是否显示 + mLeftIconVisibility = a.getBoolean(R.styleable.RxTitle_leftIconVisibility, true) + //右边图标是否显示 + mRightIconVisibility = a.getBoolean(R.styleable.RxTitle_rightIconVisibility, false) + mLeftText = a.getString(R.styleable.RxTitle_leftText) + //左边字体颜色 + mLeftTextColor = a.getColor(R.styleable.RxTitle_leftTextColor, resources.getColor(R.color.white)) + //左侧标题字体大小 + mLeftTextSize = a.getDimensionPixelSize(R.styleable.RxTitle_leftTextSize, dp2px(context, 8f)) + mLeftTextVisibility = a.getBoolean(R.styleable.RxTitle_leftTextVisibility, false) + mRightText = a.getString(R.styleable.RxTitle_rightText) + //右边字体颜色 + mRightTextColor = a.getColor(R.styleable.RxTitle_rightTextColor, resources.getColor(R.color.white)) + //标题字体大小 + mRightTextSize = a.getDimensionPixelSize(R.styleable.RxTitle_rightTextSize, dp2px(context, 8f)) + mRightTextVisibility = a.getBoolean(R.styleable.RxTitle_rightTextVisibility, false) + } finally { + //回收这个对象 + a.recycle() + } + + //******************************************************************************************以下属性初始化 + if (!isNullString(mTitle)) { + setTitle(mTitle) + } + if (mTitleColor != 0) { + setTitleColor(mTitleColor) + } + if (mTitleSize != 0) { + setTitleSize(mTitleSize) + } + if (mLeftIcon != 0) { + setLeftIcon(mLeftIcon) + } + if (mRightIcon != 0) { + setRightIcon(mRightIcon) + } + setTitleVisibility(mTitleVisibility) + setLeftText(mLeftText) + setLeftTextColor(mLeftTextColor) + setLeftTextSize(mLeftTextSize) + setLeftTextVisibility(mLeftTextVisibility) + setRightText(mRightText) + setRightTextColor(mRightTextColor) + setRightTextSize(mRightTextSize) + setRightTextVisibility(mRightTextVisibility) + setLeftIconVisibility(mLeftIconVisibility) + setRightIconVisibility(mRightIconVisibility) + initAutoFitEditText(context) + //==========================================================================================以上为属性初始化 + } + + private fun initAutoFitEditText(context: Context) { + mTvTitle?.clearFocus() + mTvTitle?.isEnabled = false + mTvTitle?.isFocusableInTouchMode = false + mTvTitle?.isFocusable = false + mTvTitle?.setEnableSizeCache(false) + //might cause crash on some devices + mTvTitle?.movementMethod = null + // can be added after layout inflation; + mTvTitle?.maxHeight = dp2px(context, 55f) + //don't forget to add min text size programmatically + mTvTitle?.minTextSize = 37f + try { + RxTextAutoZoom.setNormalization(getContext() as Activity, mRootLayout, mTvTitle) + hideSoftInput((getContext() as Activity)) + } catch (e: Exception) { + e.printStackTrace() + } + } + + //**********************************************************************************************以下为get方法 + fun getRootLayout(): LinearLayout? { + return mRootLayout + } + + fun getTitleTextView(): RxTextAutoZoom? { + return mTvTitle + } + + fun getLeftLayout(): LinearLayout? { + return mLlLeft + } + + fun getLeftImageView(): ImageView? { + return mIvLeft + } + + fun getLeftTextView(): TextView? { + return mTvLeft + } + + fun getRightLayout(): LinearLayout? { + return mLlRight + } + + fun getRightImageView(): ImageView? { + return mIvRight + } + + fun getRightTextView(): TextView? { + return mTvRight + } + + fun isTitleVisibility(): Boolean { + return mTitleVisibility + } + + fun getLeftIconBg(): LinearLayout? { + return mllIconLeftBg + } + + fun getRightIconBg(): LinearLayout? { + return mllIconRightBg + } + + fun setTitleVisibility(titleVisibility: Boolean) { + mTitleVisibility = titleVisibility + if (mTitleVisibility) { + mTvTitle?.visibility = View.VISIBLE + } else { + mTvTitle?.visibility = View.GONE + } + } + + fun getLeftText(): String? { + return mLeftText + } + + //**********************************************************************************************以下为 左边文字 相关方法 + fun setLeftText(leftText: String?): Unit { + mLeftText = leftText + mTvLeft?.text = mLeftText + } + + fun getLeftTextColor(): Int { + return mLeftTextColor + } + + fun setLeftTextColor(leftTextColor: Int) { + mLeftTextColor = leftTextColor + mTvLeft?.setTextColor(mLeftTextColor) + } + + fun getLeftTextSize(): Int { + return mLeftTextSize + } + + fun setLeftTextSize(leftTextSize: Int) { + mLeftTextSize = leftTextSize + mTvLeft?.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLeftTextSize.toFloat()) + } + + fun isLeftTextVisibility(): Boolean { + return mLeftTextVisibility + } + + fun setLeftTextVisibility(leftTextVisibility: Boolean) { + mLeftTextVisibility = leftTextVisibility + if (mLeftTextVisibility) { + mTvLeft?.visibility = View.VISIBLE + } else { + mTvLeft?.visibility = View.GONE + } + } + + fun getRightText(): String? { + return mRightText + } + + //**********************************************************************************************以下为 右边文字 相关方法 + fun setRightText(rightText: String?) { + mRightText = rightText + mTvRight?.text = mRightText + } + + fun getRightTextColor(): Int { + return mRightTextColor + } + + fun setRightTextColor(rightTextColor: Int) { + mRightTextColor = rightTextColor + mTvRight?.setTextColor(mRightTextColor) + } + + fun getRightTextSize(): Int { + return mRightTextSize + } + + fun setRightTextSize(rightTextSize: Int) { + mRightTextSize = rightTextSize + mTvRight?.setTextSize(TypedValue.COMPLEX_UNIT_PX, mRightTextSize.toFloat()) + } + + //==============================================================================================以上为get方法 + + + //**********************************************************************************************以下为set方法 + fun isRightTextVisibility(): Boolean { + return mRightTextVisibility + } + + fun setRightTextVisibility(rightTextVisibility: Boolean) { + mRightTextVisibility = rightTextVisibility + if (mRightTextVisibility) { + mTvRight?.visibility = View.VISIBLE + if (isRightIconVisibility()) { + mTvRight?.setPadding(0, 0, 0, 0) + } + } else { + mTvRight?.visibility = View.GONE + } + } + + fun getTitle(): String? { + return mTitle + } + + //**********************************************************************************************以下为Title相关方法 + fun setTitle(title: String?) { + mTitle = title + mTvTitle?.setText(mTitle) + } + + fun getTitleColor(): Int { + return mTitleColor + } + + fun setTitleColor(titleColor: Int) { + mTitleColor = titleColor + mTvTitle?.setTextColor(mTitleColor) + } + + fun getTitleSize(): Int { + return mTitleSize + } + + fun setTitleSize(titleSize: Int) { + mTitleSize = titleSize + mTvTitle?.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleSize.toFloat()) + } + + fun getLeftIcon(): Int { + return mLeftIcon + } + + fun setLeftIcon(leftIcon: Int) { + mLeftIcon = leftIcon + mIvLeft?.setImageResource(mLeftIcon) + } + + fun getRightIcon(): Int { + return mRightIcon + } + + //==============================================================================================以上为 Title 相关方法 + fun setRightIcon(rightIcon: Int) { + mRightIcon = rightIcon + mIvRight?.setImageResource(mRightIcon) + } + + fun isLeftIconVisibility(): Boolean { + return mLeftIconVisibility + } + + fun setLeftIconVisibility(leftIconVisibility: Boolean) { + mLeftIconVisibility = leftIconVisibility + if (mLeftIconVisibility) { + mIvLeft?.visibility = View.VISIBLE + } else { + mIvLeft?.visibility = View.GONE + } + } + + fun isRightIconVisibility(): Boolean { + return mRightIconVisibility + } + + //==============================================================================================以上为 左边文字 相关方法 + fun setRightIconVisibility(rightIconVisibility: Boolean) { + mRightIconVisibility = rightIconVisibility + if (mRightIconVisibility) { + mIvRight?.visibility = View.VISIBLE + } else { + mIvRight?.visibility = View.GONE + } + } + + fun setLeftFinish(activity: Activity) { + mLlLeft?.setOnClickListener { v: View? -> activity.finish() } + } + + fun setLeftFinish(activity: Activity, isFade: Boolean, isTransition: Boolean) { + mLlLeft?.setOnClickListener { v: View? -> + RxActivityTool.finishActivity(activity, isTransition) + if (isFade) { + RxActivityTool.fadeTransition(activity) + } + } + } + + fun setLeftOnClickListener(onClickListener: OnClickListener?) { + mLlLeft?.setOnClickListener(onClickListener) + } + + fun setRightOnClickListener(onClickListener: OnClickListener?) { + mLlRight?.setOnClickListener(onClickListener) + } + + //==============================================================================================以上为 右边文字 相关方法 + fun setLeftTextOnClickListener(onClickListener: OnClickListener?) { + mTvLeft?.setOnClickListener(onClickListener) + } + + fun setRightTextOnClickListener(onClickListener: OnClickListener?) { + mTvRight?.setOnClickListener(onClickListener) + } + + fun setLeftIconOnClickListener(onClickListener: OnClickListener?) { + mIvLeft?.setOnClickListener(onClickListener) + } + + fun setRightIconOnClickListener(onClickListener: OnClickListener?) { + mIvRight?.setOnClickListener(onClickListener) + } + +} \ No newline at end of file diff --git a/RxUI/src/main/res/layout/include_rx_title.xml b/RxUI/src/main/res/layout/include_rx_title.xml index a6290ec9..307a993d 100644 --- a/RxUI/src/main/res/layout/include_rx_title.xml +++ b/RxUI/src/main/res/layout/include_rx_title.xml @@ -29,7 +29,7 @@ android:id="@+id/iv_left" android:layout_width="@dimen/dp_55" android:layout_height="match_parent" - android:padding="@dimen/dp_15" + android:padding="@dimen/dp_18" android:src="@drawable/ic_back" /> @@ -97,7 +97,7 @@ android:id="@+id/iv_right" android:layout_width="@dimen/dp_55" android:layout_height="match_parent" - android:padding="@dimen/dp_15" + android:padding="@dimen/dp_18" android:src="@drawable/icon_menu" />