Menu Home

android应用

Android:关于ViewGroup的ClipChildren

ClipChildren属性对需要做动画的View非常有用,特别是对ScaleAnimation。 需要注意的是ClipChildren = false只是告诉他的子View可以超出他本身的大小,并不是说他自己可以超出他的父View的大小,所以如果你想允许某个View超过其父View的边界,你需要一直找到最顶层的父View并设置ClipChildren = false。 下面需要提一下的是一个关于ClipChildren的坑,在4.2的系统上(4.3未测试,4.4及5.0版本没有这个 BUG),在启用了hardware accelerated的情况下,ScaleAnimation会出现撕裂等不流畅的现象,处理方法如下: if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { parentView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); } 这里的parentView一定要是你启用了ClipChildren = false的那个ViewGroup,否则没有效果,最佳实践是只在需要做ScaleAnimation的时候才关闭硬件加速,做完动画后应该重新开启。 关于这个BUG的更多信息请移步:这里 关于更多Android Hardware Accelerated信息请阅读:这里

Android:监控电量

获取初始电量 Android系统发送的电量广播是一个sticky broadcast,所以可以通过给registerReceiver传递一个null参数来获取上次系统发送的电量广播。 private int getLastBatteryLevel() { Intent batteryIntent = registerReceiver(null, new IntentFilter( Intent.ACTION_BATTERY_CHANGED)); return batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); } 监控电量 mBatteryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final int level = intent.getIntExtra( BatteryManager.EXTRA_LEVEL, -1); Runnable updateBatteryLevel = new Runnable() { @Override public void run() { […]

Android:时间显示

private void initUpdateTimeThread() { Calendar calendar = Calendar.getInstance(); mHour = calendar.get(Calendar.HOUR_OF_DAY); mMinute = calendar.get(Calendar.MINUTE); setTimeString(mHour, mMinute); mUpdateTimeThread = new Runnable() { @Override public void run() { if (++mMinute >= 60) { mMinute = 0; ++mHour; } if (mHour >= 24) { mHour = 0; } setTimeString(mHour, mMinute); mHandle.postDelayed(this, 60000); } […]

重写Android Viewgroup的addView方法

Viewgroup.addView()有5个重载方法: void addView(View child, int index, ViewGroup.LayoutParams params) void addView(View child, ViewGroup.LayoutParams params) void addView(View child, int index) void addView(View child) void addView(View child, int width, int height) 如果需要想重写addView()实际只需要重写addView(View child, int index, ViewGroup.LayoutParams params)即可。 看一下addView的源码: public void addView(View child) { addView(child, -1); } public void addView(View child, int index) { […]

Android LinearGradient

Bitmap mBitmap; Matrix mShadowPaint; Matrix mLinearGradientMatrix; LinearGradient mLinearGradient; mBitmap = BitmapFactory.decodeResource(getResource, R.drawable.jobs); mShadowPaint = new Paint(); mShadowPaint.setStyle(Style.FILL); mShadowPaint.setAlpha((int)0.9 * 255); //渐变范围从(0, 0)到(1.0, 0) //渐变颜色从红色到透明 //TileMode.CLAMP重复边缘色,这里实际是拉伸透明 mLinearGradient = new LinearGradient(0, 0, 1.0f, 0, Color.red, Color.TRANSPARENT, TileMode.CLAMP); mLinearGradientMatrix = new Matrix(); mLinearGradientMatrix.setScale(mBitmap.getWidth(), 1); //把线性渐变的范围扩大到整张图片 mLinearGradient.setLocalMatrix(mLinearGradientMatrix); mShadowPaint.setShader(mLinearGradient); canvas.drawBitmap(mBitmap, 0, 0, null); canvas.drawRect(0, 0, […]

Android install HTTP Cache

// > Android 4.0, 向下兼容 private void enableHttpResponseCache() { try { long cacheSize = 10 * 1024 * 1024; File cacheDir = new File(getCacheDir(), “cache”); Class.forName(“android.net.http.HttpResponseCache”) .getMethod(“install”, File.class, long.class) .invoke(null, httpCacheDir, httpCacheSize); } catch (Exception httpResponseCacheNotAvailable) { } }

Android对大图的处理

Scale big bitmap and set Keyguard Wallpaper private void processBigBitmapAndSetKeyguardWallpaper(Uri _uri) { InputStream inStream = null; //receive from uri InputStream reOpenStream = null; //Raise efficiency and avoid OOM InputStream outStream = null; //send to keyguard Uri uri = _uri; try { inStream = mContext.getContentResolver().openInputStream(uri); //API > 13 WindowManager wm = […]

Android 4.4对Secondary External Storage的访问

Android在KitKat之后对Secondary External Storage(SES)的访问做了很大的修改。普通APP不再可以随意写整个SES,只能在属于自己的目录中进行写操作,比如Android/data/com.lnmcc.testsdcard,最后部分为APP的包名。并且APP在访问自己的私有文件夹时不再需要任何的权限。 在我的系统中,我把sd卡挂载到了/storage/sdcard1。 下面的代码片段将获取APP在SES上的的私有目录路径,之后就可以在这个路径下做读写操作了。 private String SDCard1 = null; File[] files = getExternalFilesDirs(null); for (File file : files) { Log.d(TAG, file.getAbsolutePath()); // sdcard1 is the external storage(SD card) on my platform String path = file.getAbsolutePath(); if (path.contains(“/storage/sdcard1”)) { // found it SDCard1 = path; Log.d(TAG, SDCard1); break; } } […]

Android:一种判断Navigation Bar位置的方法

public boolean isNavigationBottom() { WindowManager wm; Display display; DisplayMetrics realSize; DisplayMetrics windowSize; realSize = new DisplayMetrics(); windowSize = new DisplayMetrics(); wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); display = wm.getDefaultDisplay(); display.getRealMetrics(realSize); display.getMetrics(windowSize); if (windowSize.heightPixels != realSize.heightPixels) return true; else return false; } getRealMetrics()和getRealSize()的功能几乎一样。 see also: get physical screen size

Android:Battery Information BroadcastReceiver

public class MainActivity extends Activity { private static final String TAG = “TestBatteryInfo”; private TextView batteryInfo; private ImageView imageBatteryState; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); batteryInfo = (TextView) findViewById(R.id.textViewBatteryInfo); imageBatteryState = (ImageView) findViewById(R.id.imageViewBatteryState); IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); registerReceiver(batteryInfoReceiver, filter); } @Override protected void onPause() { super.onPause(); unregisterReceiver(batteryInfoReceiver); […]

Android:Preference默认值设置

在XML文件(advanced_preferences.xml)中为Preference设置默认值 <!– default value is a boolean –> <CheckBoxPreference android:defaultValue=”true” … /> <!– default value is a string –> <ListPreference android:defaultValue=”@string/pref_syncConnectionTypes_default” … /> 在代码中还需要调用 PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false); 第三个参数接受一个BOOL值,含义如下: true:每次调用setDefaultValues都会导致Preference的初始化。 false:只初始化一次,重复调用setDefaultValues不会导致重复初始化。 PreferenceManager.setDefaultValues() This method takes three arguments: Your application Context. The resource ID for the preference XML file for which you […]

Android: get physical screen size

public void getRealSize (Point outSize) Added in API level 17 Gets the real size of the display without subtracting any window decor or applying any compatibility scale factors. The size is adjusted based on the current rotation of the display. The real size may be smaller than the physical size […]

Android Framework: 重启代码

private void RebootIntoRecovery() { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); pm.reboot(“recovery”); } <uses-permission android:name=”android.permission.REBOOT” /> <manifest xmlns:android=”http://schemas.android.com/apk/res/android” package=”com.android.settings” coreApp=”true” android:sharedUserId=”android.uid.system”>