From 65d66ce82a1904931b442700f963dc80e4ca3a5d Mon Sep 17 00:00:00 2001 From: "zftlive@163.com" Date: Mon, 26 Oct 2015 15:06:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8A=A8=E7=94=BB=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E6=96=87=E4=BB=B6=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E5=89=A5=E7=A6=BBR=E6=96=87=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../android/library/base/BaseActivity.java | 8 +- .../android/library/base/BaseView.java | 324 +++++++-------- .../android/library/base/Operation.java | 369 +++++++++--------- .../sample/animation/AnimationInActivity.java | 4 +- 4 files changed, 366 insertions(+), 339 deletions(-) diff --git a/src/com/zftlive/android/library/base/BaseActivity.java b/src/com/zftlive/android/library/base/BaseActivity.java index a6c3456..3520e48 100644 --- a/src/com/zftlive/android/library/base/BaseActivity.java +++ b/src/com/zftlive/android/library/base/BaseActivity.java @@ -17,8 +17,8 @@ import android.view.ViewConfiguration; import android.view.Window; import android.view.WindowManager; -import com.zftlive.android.R; import com.zftlive.android.library.MApplication; +import com.zftlive.android.library.tools.ToolResource; import com.zftlive.android.library.widget.SwipeBackLayout; /** @@ -227,13 +227,13 @@ public abstract class BaseActivity extends FragmentActivity implements IBaseActi super.finish(); switch (mAnimationType) { case IBaseActivity.LEFT_RIGHT: - overridePendingTransition(0, R.anim.base_slide_right_out); + overridePendingTransition(0, BaseView.gainResId(mApplication, BaseView.ANIM, "base_slide_right_out")); break; case IBaseActivity.TOP_BOTTOM: - overridePendingTransition(0, R.anim.base_push_up_out); + overridePendingTransition(0, BaseView.gainResId(mApplication, BaseView.ANIM, "base_push_up_out")); break; case IBaseActivity.FADE_IN_OUT: - overridePendingTransition(0, R.anim.base_fade_out); + overridePendingTransition(0, BaseView.gainResId(mApplication, BaseView.ANIM, "base_fade_out")); break; default: break; diff --git a/src/com/zftlive/android/library/base/BaseView.java b/src/com/zftlive/android/library/base/BaseView.java index 6f0654d..eb80222 100644 --- a/src/com/zftlive/android/library/base/BaseView.java +++ b/src/com/zftlive/android/library/base/BaseView.java @@ -11,172 +11,178 @@ import android.view.WindowManager; /** * 自定义控件基础类 + * * @author 曾繁添 * @version 1.0 - * + * */ public abstract class BaseView extends View { - - /***资源类型-array**/ - public static final String ARRAY = "array"; - - /***资源类型-attr**/ - public static final String ATTR = "attr"; - - /***资源类型-bool**/ - public static final String BOOL = "bool"; - - /***资源类型-color**/ - public static final String COLOR = "color"; - - /***资源类型-dimen**/ - public static final String DIMEN = "dimen"; - - /***资源类型-drawable**/ - public static final String DRAWABLE = "drawable"; - - /***资源类型-id**/ - public static final String ID = "id"; - - /***资源类型-id**/ - public static final String INTEGER = "integer"; - - /***资源类型-layout**/ - public static final String LAYOUT = "layout"; - - /***资源类型-drawable**/ - public static final String STRING = "string"; - - /***资源类型-style**/ - public static final String STYLE = "style"; - - /***资源类型-styleable**/ - public static final String STYLEABLE = "styleable"; - - /**应用的包名称**/ - protected String PACKAGE_NAME = ""; - - /**上下文**/ - protected Context mContext; - - /**日志输出标志**/ - protected final String TAG = this.getClass().getSimpleName(); - - public BaseView(Context context) { - this(context,null); - } - public BaseView(Context context, AttributeSet attrs) { - this(context, attrs,0); - } + /*** 资源类型-array **/ + public static final String ARRAY = "array"; - public BaseView(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - this.mContext = context; - PACKAGE_NAME = mContext.getPackageName(); - } - - /** - * 获取屏幕宽度 - * @param mContext 上下文 - * @return - */ - public int gainScreenWidth(Context mContext){ - // 获取屏幕分辨率 - DisplayMetrics displayMetrics = new DisplayMetrics(); - WindowManager windowManager = (WindowManager) mContext - .getSystemService(Context.WINDOW_SERVICE); - windowManager.getDefaultDisplay().getMetrics(displayMetrics); - - return displayMetrics.widthPixels; - } - - /** - * 根据手机的分辨率从 dip 的单位 转成为 px(像素) - * @param context - * 环境 - * @param dpValue - * 需要转化的dip值 - * @return int 转化后的px值 - */ - public int dipToPx(Context context, float dipValue) { - final float scale = context.getResources().getDisplayMetrics().density; - return (int) (dipValue * scale + 0.5f); - } + /*** 资源类型-attr **/ + public static final String ATTR = "attr"; - /** - * 根据手机的分辨率从 px(像素) 的单位 转成为 dip - * @param context - * 环境 - * @param pxValue - * 需要转换的像素值 - * @return int 转化后的dip值 - */ - public int pxToDip(Context context, float pxValue) { - final float scale = context.getResources().getDisplayMetrics().density; - return (int) (pxValue / scale + 0.5f); - } + /*** 资源类型-anim **/ + public static final String ANIM = "anim"; - /** - * 将px值转换为sp值,保证文字大小不变 - * @param context - * @param pxValue - */ - public int pxTosp(Context context, float pxValue) { - final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; - return (int) (pxValue / fontScale + 0.5f); - } + /*** 资源类型-bool **/ + public static final String BOOL = "bool"; - /** - * 将sp值转换为px值,保证文字大小不变 - * @param spValue - * @param fontScale - * (DisplayMetrics类中属性scaledDensity) - */ - public int spTopx(Context context, float spValue) { - final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; - return (int) (spValue * fontScale + 0.5f); - } - - /** - * 获取当前分辨率下指定单位对应的像素大小(根据设备信息) - * px,dip,sp -> px

- * - * 用法:getRawSize(context,TypedValue.COMPLEX_UNIT_DIP,itemSpaceDp)

- * - * 代码摘自:TextView.setTextSize() - * @param mContext 上下文 - * @param unit TypedValue.COMPLEX_UNIT_* - * @param size - * @return - */ - public int gainRawSize(Context mContext, int unit, float size) { - Resources r; - if (mContext == null) - r = Resources.getSystem(); - else - r = mContext.getResources(); - - return (int)TypedValue.applyDimension(unit, size, r.getDisplayMetrics()); - } - - /** - * 获取资源文件id - * @param mContext 上下文 - * @param resType 资源类型(drawable/string/layout/style/dimen/color/array等) - * @param resName 资源文件名称 - * @return - */ - public int gainResId(Context mContext,String resType,String resName){ - int result = -1; - try { - String packageName = mContext.getPackageName(); - result = mContext.getResources().getIdentifier(resName, resType, packageName); - } catch (Exception e) { - result = -1; - Log.w(TAG, "获取资源文件失败,原因:"+e.getMessage()); - } - - return result; - } + /*** 资源类型-color **/ + public static final String COLOR = "color"; + + /*** 资源类型-dimen **/ + public static final String DIMEN = "dimen"; + + /*** 资源类型-drawable **/ + public static final String DRAWABLE = "drawable"; + + /*** 资源类型-id **/ + public static final String ID = "id"; + + /*** 资源类型-id **/ + public static final String INTEGER = "integer"; + + /*** 资源类型-layout **/ + public static final String LAYOUT = "layout"; + + /*** 资源类型-drawable **/ + public static final String STRING = "string"; + + /*** 资源类型-style **/ + public static final String STYLE = "style"; + + /*** 资源类型-styleable **/ + public static final String STYLEABLE = "styleable"; + + /** 应用的包名称 **/ + protected String PACKAGE_NAME = ""; + + /** 上下文 **/ + protected Context mContext; + + /** 日志输出标志 **/ + protected final static String TAG = BaseView.class.getSimpleName(); + + public BaseView(Context context) { + this(context, null); + } + + public BaseView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public BaseView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + this.mContext = context; + PACKAGE_NAME = mContext.getPackageName(); + } + + /** + * 获取屏幕宽度 + * + * @param mContext 上下文 + * @return + */ + public static int gainScreenWidth(Context mContext) { + // 获取屏幕分辨率 + DisplayMetrics displayMetrics = new DisplayMetrics(); + WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); + windowManager.getDefaultDisplay().getMetrics(displayMetrics); + + return displayMetrics.widthPixels; + } + + /** + * 根据手机的分辨率从 dip 的单位 转成为 px(像素) + * + * @param mContext 环境 + * @param dpValue 需要转化的dip值 + * @return int 转化后的px值 + */ + public static int dipToPx(Context mContext, float dipValue) { + final float scale = mContext.getResources().getDisplayMetrics().density; + return (int) (dipValue * scale + 0.5f); + } + + /** + * 根据手机的分辨率从 px(像素) 的单位 转成为 dip + * + * @param mContext 环境 + * @param pxValue 需要转换的像素值 + * @return int 转化后的dip值 + */ + public static int pxToDip(Context mContext, float pxValue) { + final float scale = mContext.getResources().getDisplayMetrics().density; + return (int) (pxValue / scale + 0.5f); + } + + /** + * 将px值转换为sp值,保证文字大小不变 + * + * @param mContext + * @param pxValue + */ + public static int pxTosp(Context mContext, float pxValue) { + final float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity; + return (int) (pxValue / fontScale + 0.5f); + } + + /** + * 将sp值转换为px值,保证文字大小不变 + * + * @param spValue + * @param fontScale (DisplayMetrics类中属性scaledDensity) + */ + public static int spTopx(Context mContext, float spValue) { + final float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity; + return (int) (spValue * fontScale + 0.5f); + } + + /** + * 获取当前分辨率下指定单位对应的像素大小(根据设备信息) px,dip,sp -> px
+ *
+ * + * 用法:getRawSize(context,TypedValue.COMPLEX_UNIT_DIP,itemSpaceDp)
+ *
+ * + * 代码摘自:TextView.setTextSize() + * + * @param mContext 上下文 + * @param unit TypedValue.COMPLEX_UNIT_* + * @param size + * @return + */ + public static int gainRawSize(Context mContext, int unit, float size) { + Resources r; + if (mContext == null) + r = Resources.getSystem(); + else + r = mContext.getResources(); + + return (int) TypedValue.applyDimension(unit, size, r.getDisplayMetrics()); + } + + /** + * 获取资源文件id + * + * @param mContext 上下文 + * @param resType 资源类型(drawable/string/layout/style/dimen/color/array等) + * @param resName 资源文件名称 + * @return + */ + public static int gainResId(Context mContext, String resType, String resName) { + int result = -1; + try { + String packageName = mContext.getPackageName(); + result = mContext.getResources().getIdentifier(resName, resType, packageName); + } catch (Exception e) { + result = -1; + Log.w(TAG, "获取资源文件失败,原因:" + e.getMessage()); + } + + return result; + } } diff --git a/src/com/zftlive/android/library/base/Operation.java b/src/com/zftlive/android/library/base/Operation.java index 71f390a..b757c3f 100644 --- a/src/com/zftlive/android/library/base/Operation.java +++ b/src/com/zftlive/android/library/base/Operation.java @@ -1,195 +1,216 @@ package com.zftlive.android.library.base; -import java.io.Serializable; - import android.app.Activity; import android.content.Intent; import android.os.Bundle; -import com.zftlive.android.R; import com.zftlive.android.library.MApplication; import com.zftlive.android.library.config.SysEnv; import com.zftlive.android.library.data.DTO; import com.zftlive.android.library.tools.ToolAlert; import com.zftlive.android.library.tools.ToolAlert.ILoadingOnKeyListener; +import java.io.Serializable; + /** * 基本的操作共通抽取 + * * @author 曾繁添 * @version 1.0 - * + * */ public class Operation { - /**激活Activity组件意图**/ - private Intent mIntent = new Intent(); - /***上下文**/ - private Activity mContext = null; - /***整个应用Applicaiton**/ - private MApplication mApplication = null; - - public Operation(Activity mContext) { - this.mContext = mContext; - mApplication = (MApplication) this.mContext.getApplicationContext(); - } + /** 激活Activity组件意图 **/ + private Intent mIntent = new Intent(); + /*** 上下文 **/ + private Activity mContext = null; + /*** 整个应用Applicaiton **/ + private MApplication mApplication = null; + + public Operation(Activity mContext) { + this.mContext = mContext; + mApplication = (MApplication) this.mContext.getApplicationContext(); + } + + /** + * 跳转Activity + * + * @param activity 需要跳转至的Activity + */ + public void forward(Class activity) { + forward(activity.getName()); + } + + /** + * 跳转Activity + * + * @param activity 需要跳转至的Activity + */ + public void forward(String className) { + forward(className, IBaseActivity.NONE); + } + + /** + * 跳转Activity + * + * @param activity 需要跳转至的Activity + * @param animaType 动画类型IBaseActivity.LEFT_RIGHT/TOP_BOTTOM/FADE_IN_OUT + */ + public void forward(String className, int animaType) { + mIntent.setClassName(mContext, className); + mIntent.putExtra(IBaseActivity.ANIMATION_TYPE, animaType); + mContext.startActivity(mIntent); + switch (animaType) { + case IBaseActivity.LEFT_RIGHT: + int inLFAnim = BaseView.gainResId(mApplication, BaseView.ANIM, "base_slide_right_in"); + int outLFAnim = BaseView.gainResId(mApplication, BaseView.ANIM, "base_slide_remain"); + mContext.overridePendingTransition(inLFAnim, outLFAnim); + break; + case IBaseActivity.TOP_BOTTOM: + int inTBAnim = BaseView.gainResId(mApplication, BaseView.ANIM, "base_push_up_in"); + int outTBAnim = BaseView.gainResId(mApplication, BaseView.ANIM, "base_push_up_out"); + mContext.overridePendingTransition(inTBAnim, outTBAnim); + break; + case IBaseActivity.FADE_IN_OUT: + int inFAnim = BaseView.gainResId(mApplication, BaseView.ANIM, "base_fade_in"); + int outFAnim = BaseView.gainResId(mApplication, BaseView.ANIM, "base_fade_out"); + mContext.overridePendingTransition(inFAnim, outFAnim); + break; + default: + break; + } + } + + /** + * 设置传递参数 + * + * @param value 数据传输对象 + */ + public void addParameter(DTO value) { + mIntent.putExtra(SysEnv.ACTIVITY_DTO_KEY, value); + } + + /** + * 设置传递参数 + * + * @param key 参数key + * @param value 数据传输对象 + */ + public void addParameter(String key, DTO value) { + mIntent.putExtra(key, value); + } + + /** + * 设置传递参数 + * + * @param key 参数key + * @param value 数据传输对象 + */ + public void addParameter(String key, Bundle value) { + mIntent.putExtra(key, value); + } + + /** + * 设置传递参数 + * + * @param key 参数key + * @param value 数据传输对象 + */ + public void addParameter(String key, Serializable value) { + mIntent.putExtra(key, value); + } + + /** + * 设置传递参数 + * + * @param key 参数key + * @param value 数据传输对象 + */ + public void addParameter(String key, String value) { + mIntent.putExtra(key, value); + } + + /** + * 获取跳转时设置的参数 + * + * @param key + * @return + */ + public Object getParameters(String key) { + DTO parms = getParameters(); + if (null != parms) { + return parms.get(key); + } else { + parms = new DTO(); + parms.put(key, mContext.getIntent().getExtras().get(key)); + } + return parms; + } + + /** + * 获取跳转参数集合 + * + * @return + */ + public DTO getParameters() { + DTO parms = (DTO) mContext.getIntent().getExtras().getSerializable(SysEnv.ACTIVITY_DTO_KEY); + return parms; + } + + /** + * 设置全局Application传递参数 + * + * @param strKey 参数key + * @param value 数据传输对象 + */ + public void addGloableAttribute(String strKey, Object value) { + mApplication.assignData(strKey, value); + } + + /** + * 获取跳转时设置的参数 + * + * @param strKey + * @return + */ + public Object getGloableAttribute(String strKey) { + return mApplication.gainData(strKey); + } + + /** + * 弹出等待对话框 + * + * @param message 提示信息 + */ + public void showLoading(String message) { + ToolAlert.loading(mContext, message); + } + + /** + * 弹出等待对话框 + * + * @param message 提示信息 + * @param listener 按键监听器 + */ + public void showLoading(String message, ILoadingOnKeyListener listener) { + ToolAlert.loading(mContext, message, listener); + } + + /** + * 更新等待对话框显示文本 + * + * @param message 需要更新的文本内容 + */ + public void updateLoadingText(String message) { + ToolAlert.updateProgressText(message); + } + + /** + * 关闭等待对话框 + */ + public void closeLoading() { + ToolAlert.closeLoading(); + } - /** - * 跳转Activity - * @param activity 需要跳转至的Activity - */ - public void forward(Class activity) { - forward(activity.getName()); - } - - /** - * 跳转Activity - * @param activity 需要跳转至的Activity - */ - public void forward(String className) { - forward(className,IBaseActivity.NONE); - } - - /** - * 跳转Activity - * @param activity 需要跳转至的Activity - * @param animaType 动画类型IBaseActivity.LEFT_RIGHT/TOP_BOTTOM/FADE_IN_OUT - */ - public void forward(String className,int animaType) { - mIntent.setClassName(mContext, className); - mIntent.putExtra(IBaseActivity.ANIMATION_TYPE, animaType); - mContext.startActivity(mIntent); - switch (animaType) { - case IBaseActivity.LEFT_RIGHT: - mContext.overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain); - break; - case IBaseActivity.TOP_BOTTOM: - mContext.overridePendingTransition(R.anim.base_push_up_in,R.anim.base_push_up_out); - break; - case IBaseActivity.FADE_IN_OUT: - mContext.overridePendingTransition(R.anim.base_fade_in, R.anim.base_fade_out); - break; - default: - break; - } - } - - /** - * 设置传递参数 - * @param value 数据传输对象 - */ - public void addParameter(DTO value) { - mIntent.putExtra(SysEnv.ACTIVITY_DTO_KEY, value); - } - - /** - * 设置传递参数 - * @param key 参数key - * @param value 数据传输对象 - */ - public void addParameter(String key,DTO value) { - mIntent.putExtra(key, value); - } - - /** - * 设置传递参数 - * @param key 参数key - * @param value 数据传输对象 - */ - public void addParameter(String key,Bundle value) { - mIntent.putExtra(key, value); - } - - /** - * 设置传递参数 - * @param key 参数key - * @param value 数据传输对象 - */ - public void addParameter(String key,Serializable value) { - mIntent.putExtra(key, value); - } - - /** - * 设置传递参数 - * @param key 参数key - * @param value 数据传输对象 - */ - public void addParameter(String key,String value) { - mIntent.putExtra(key, value); - } - - /** - * 获取跳转时设置的参数 - * @param key - * @return - */ - public Object getParameters(String key) { - DTO parms = getParameters(); - if(null != parms){ - return parms.get(key); - }else{ - parms = new DTO(); - parms.put(key, mContext.getIntent().getExtras().get(key)); - } - return parms; - } - - /** - * 获取跳转参数集合 - * @return - */ - public DTO getParameters() { - DTO parms = (DTO)mContext.getIntent().getExtras().getSerializable(SysEnv.ACTIVITY_DTO_KEY); - return parms; - } - - /** - * 设置全局Application传递参数 - * @param strKey 参数key - * @param value 数据传输对象 - */ - public void addGloableAttribute(String strKey,Object value) { - mApplication.assignData(strKey, value); - } - - /** - * 获取跳转时设置的参数 - * @param strKey - * @return - */ - public Object getGloableAttribute(String strKey) { - return mApplication.gainData(strKey); - } - - /** - * 弹出等待对话框 - * @param message 提示信息 - */ - public void showLoading(String message){ - ToolAlert.loading(mContext, message); - } - - /** - * 弹出等待对话框 - * @param message 提示信息 - * @param listener 按键监听器 - */ - public void showLoading(String message,ILoadingOnKeyListener listener){ - ToolAlert.loading(mContext, message, listener); - } - - /** - * 更新等待对话框显示文本 - * @param message 需要更新的文本内容 - */ - public void updateLoadingText(String message){ - ToolAlert.updateProgressText(message); - } - - /** - * 关闭等待对话框 - */ - public void closeLoading(){ - ToolAlert.closeLoading(); - } - } diff --git a/src/com/zftlive/android/sample/animation/AnimationInActivity.java b/src/com/zftlive/android/sample/animation/AnimationInActivity.java index fd394df..9f32ff5 100644 --- a/src/com/zftlive/android/sample/animation/AnimationInActivity.java +++ b/src/com/zftlive/android/sample/animation/AnimationInActivity.java @@ -104,7 +104,7 @@ public class AnimationInActivity extends BaseActivity implements View.OnClickLis switch (mAnimSp.getSelectedItemPosition()) { case 0: //淡入淡出效果 - overridePendingTransition(R.anim.fade_in, R.anim.fade_out); + overridePendingTransition(R.anim.base_fade_in, R.anim.base_fade_out); //Android内置的 //overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out); break; @@ -136,7 +136,7 @@ public class AnimationInActivity extends BaseActivity implements View.OnClickLis break; case 7: //下往上推出效果 - overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out); + overridePendingTransition(R.anim.base_push_up_in,R.anim.base_push_up_out); break; case 8: //左右交错效果