2016-4-6 安桌框架分析 http://blog.csdn.net/yanbober/article/details/45967639 一个Activity就有一个Context,而且生命周期和Activity类相同(记住这句话,写应用就可以避免一些低级的内存泄漏问题)。 一个Service就有一个Context,而且生命周期和Service类相同(记住这句话,写应用就可以避免一些低级的内存泄漏问题)。 当我们写好一个APP以后每次重新启动时都会首先创建Application对象(每个APP都有一个唯一的全局Application对象,与整个APP的生命周期相同)。 一个Application就有一个Context,而且生命周期和Application类相同(然而一个App只有一个Application,而且与应用生命周期相同)。 App时通过context.getResources得到资源是不是就不是同一份呢? 我们通过不同的Context实例得到的Resources是同一套资源。 Application的Context生命周期与应用程序完全相同。 对于Context使用不当会引起内存泄漏。譬如一个单例模式的自定义数据库管理工具类需要传入一个Context,而这个数据库管理对象又需要在Activity中使用,如果我们传递Activity的Context就可能造成内存泄漏,所以需要传递Application的Context。 就是说单例类的生命周期是整个APP存活期,传入Activity的Context就导致单例类持有Activity的引用,导致Activity不能及时回收,也就是内存泄漏,所以使用Application的Context,他的生命周期与单例一样长。
private class BalanceOnTouchListener implements View.OnTouchListener { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() ==
MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { } //return false; return true; } } 这个方法必须返回ture,否则MotionEvent.ACTION_MOVE 跟MotionEvent.ACTION_UP的消息没起作用。
byte[] data = {55, 1, 8, 62, 0, 0, 2, 3, 8, b, c, 30}; for(byte byteChar : data) { Log.d(TAG, String.format("assembleData: %d", byteChar)); } 相当于把data 赋值给 byteChar ,然后逐个打印出来
/** * 把byte的取值范围从(-128)到127改成0到255 -86可以变成0xaa(170)以及86还是变成86 * 返回值不能是byte类型不然数据的范围转换没用 * * @param a * @return */ public static int changeByte(byte a) { return (a & 0x000000FF); }
// 将byte数组byteArray转为一个整数,字节数组的低位是整型的低字节位, 参数byteArray为4个字节 public static int byte2int(byte[] byteArray) { int iResult = 0; byte bLoop; for ( int i =0; i<4 ; i++) {
bLoop = byteArray[i]; iResult+= (bLoop & 0xFF) << (8 * i); } return iResult; }
在工作线程中处理UI线程报错:Only the original thread that created a view hierarchy can touch its views.
http://blog.csdn.net/shenyuemei/article/details/11030679
//靠右对齐:android:layout_gravity="end", <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:layout_marginTop="5dp" android:orientation="horizontal" android:padding="0dp"
>
<TextView android:id="@+id/low_temp" style="@style/customfontStyle02" android:layout_gravity="end" android:layout_marginTop="6dp"
/>
//N是遍历所有回调接口的个数,就是说有多少个retCarSpeedInfo()接口 synchronized public void retCarSpeedInfo(int nCarSpeed) { synchronized(mCanboxCallbacks) { int N = mCanboxCallbacks.beginBroadcast(); while (N > 0) { N--; try { mCanboxCallbacks.getBroadcastItem(N).retCarSpeedInfo(nCarSpeed); } catch (RemoteException e) { e.printStackTrace(); } } mCanboxCallbacks.finishBroadcast(); } }
//判断一个activity是否是在顶层 public static boolean isTopActivity(String strClassName) { boolean isTop = false; ActivityManager am = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> listtaskinfo =am. if (listtaskinfo.size()>0) { ComponentName cn = listtaskinfo.get(0).topActivity; if (cn.getClassName().equals(strClassName)) { isTop = true; } } return isTop; }
2016-6-23 //获取资源文件中的值: Resources resources = context.getResources(); mCellWidth = resources.getDimensionPixelSize(R.dimen.apps_customize_cell_width);
sIsScreenLarge = getResources().getBoolean(R.bool.is_large_screen); sScreenDensity = getResources().getDisplayMetrics().density;
//在Fragment中,这样获得Context的对象: Context context = this.getActivity();
数学与逻辑学中,singleton定义为“有且仅有一个元素的集合”。 单例模式最初的定义出现于《设计模式》(艾迪生维斯理, 1994):“保证一个类仅有一个实例,并提供一个访问它的全局访问点。” Java中单例模式定义:“一个类有且仅有一个实例,并且自行实例化向整个系统提供。”
Java单例模式例子: public class SingletonClass { private static SingletonClass instance=null; public static SingletonClass getInstance() { if(instance==null) { synchronized(SingletonClass.class) { if(instance==null) { instance=new SingletonClass(); } } } return instance; } private SingletonClass(){} }
Application: Application和Activity,Service一样是android框架的一个系统组件,当android程序启动时系统会创建一个
application对象,用来存储系统的一些信息。通常我们是不需要指定一个Application的,这时系统会自动帮我们创建,如果需要创建自己 的Application,也很简单创建一个类继承
Application并在manifest的application标签中进行注册(只需要给Application标签增加个name属性把自己的
Application的名字定入即可)。 android系统会为每个程序运行时创建一个Application类的对象且仅创建一个,所以Application可以说是单例
(singleton)模式的一个类.且application对象的生命周期是整个程序中最长的,它的生命周期就等于这个程序的生命周期。因为它是全局 的单例的,所以在不同的Activity,Service中获得的对象都是同一个对象。所以通过Application来进行一些,数据传递,数据共享 等,数据缓存等操作。
Android 除了提供/res目录存放资源文件外,在/assets目录也可以存放资源文件,而且/assets目录下的资源文件不会在R.java自动生成ID,所以读取/assets目录下的文件必须指定文件的路径。我们可以通过AssetManager 类来访问这些文件。 比如我要读取/assets/background.png Bitmap bgImg =
getImageFromAssetFile( "background.png" );
Bitmap bgImg =
getImageFromAssetFile("background.png"); private
Bitmap getImageFromAssetFile(String fileName){ Bitmap image = null
; try { AssetManager am = context.getAssets(); InputStream is =
am.open(fileName); image =
BitmapFactory.decodeStream(is); is.close(); }catch (Exception e){ } return
image; }
http://blog.csdn.net/stevenhu_223/article/details/10051279 E:\work\android\RK_Common_UI\Code\Px4_Native_Launcher\src\com\oklcom4\launcher3 \PagedView.java
public PagedView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0); setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0)); mPageLayoutPaddingTop = a.getDimensionPixelSize( R.styleable.PagedView_pageLayoutPaddingTop, 0); mPageLayoutPaddingBottom = a.getDimensionPixelSize( R.styleable.PagedView_pageLayoutPaddingBottom, 0); mPageLayoutPaddingLeft = a.getDimensionPixelSize( R.styleable.PagedView_pageLayoutPaddingLeft, 0); mPageLayoutPaddingRight = a.getDimensionPixelSize( R.styleable.PagedView_pageLayoutPaddingRight, 0); mPageLayoutWidthGap = a.getDimensionPixelSize( R.styleable.PagedView_pageLayoutWidthGap, 0); mPageLayoutHeightGap = a.getDimensionPixelSize( R.styleable.PagedView_pageLayoutHeightGap, 0); mScrollIndicatorPaddingLeft = a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0); mScrollIndicatorPaddingRight = a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0); a.recycle();
setHapticFeedbackEnabled(false); init(); }
E:\work\android\RK_Common_UI\Code\Px4_Native_Launcher\res\values\attrs.xml <!-- PagedView specific
attributes. These attributes are used to customize a PagedView view in XML files. --> <declare-styleable name="PagedView"> <!-- A spacing override for the icons within a page --> <attr name="pageLayoutWidthGap" format="dimension" /> <attr name="pageLayoutHeightGap" format="dimension" /> <!-- The padding of the pages that are dynamically created per page
--> <attr name="pageLayoutPaddingTop" format="dimension" /> <attr name="pageLayoutPaddingBottom" format="dimension" /> <attr name="pageLayoutPaddingLeft" format="dimension" /> <attr name="pageLayoutPaddingRight" format="dimension" /> <!-- The space between adjacent pages of the PagedView. --> <attr name="pageSpacing" format="dimension" /> <!-- The padding for the scroll indicator area --> <attr name="scrollIndicatorPaddingLeft" format="dimension" /> <attr name="scrollIndicatorPaddingRight" format="dimension" /> </declare-styleable>
说明:R.styleable.PagedView_scrollIndicatorPaddingRight ,是由PagedView加_再加scrollIndicatorPaddingRight组成的。
2016-7-23 android:paddingEnd="22dp" |
|
来自: lhzstudio > 《android笔记》