Android_CodeWiki_02
1、使用TransitionDrawable实现渐变效果
private void setImageBitmap(ImageView imageView, Bitmap bitmap) {
// Use TransitionDrawable to fade in.
final TransitionDrawable td = new TransitionDrawable(new Drawable[] { new ColorDrawable(android.R.color.transparent), new BitmapDrawable(mContext.getResources(), bitmap) });
//noinspection deprecation
imageView.setBackgroundDrawable(imageView.getDrawable());
imageView.setImageDrawable(td);
td.startTransition(200);
} 比使用AlphaAnimation效果要好,可避免出现闪烁问题。
2、 扫描指定的文件
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); 用途:从本软件新增、修改、删除图片、文件某一个文件(音频、视频)需要更新系统媒体库时使用,不必扫描整个SD卡。
3、Dip转px
public static int dipToPX(final Context ctx, float dip) {
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, ctx.getResources().getDisplayMetrics());
} 用途:难免在Activity代码中设置位置、大小等,本方法就很有用了!
4、获取已经安装APK的路径
PackageManager pm = getPackageManager(); for (ApplicationInfo app : pm.getInstalledApplications()) {
Log.d("PackageList", "package: " + app.packageName + ", sourceDir: " + app.sourceDir);
}
输出如下:
package: com.tmobile.thememanager, sourceDir: /system/app/ThemeManager.apk
package: com.touchtype.swiftkey, sourceDir: /data/app/com.touchtype.swiftkey-1.apk
5、 多进程Preferences数据共享
public static void putStringProcess(Context ctx, String key, String value) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences("preference_mu", Context.MODE_MULTI_PROCESS);
Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
} public static String getStringProcess(Context ctx, String key, String defValue) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences("preference_mu", Context.MODE_MULTI_PROCESS);
return sharedPreferences.getString(key, defValue);
}
相关文章:
http://zengrong.net/post/1687.htm
6、泛型ArrayList转数组
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Class<?> cls, ArrayList<T> items) {
if (items == null || items.size() == 0) {
return (T[]) Array.newInstance(cls, 0);
}
return items.toArray((T[]) Array.newInstance(cls, items.size()));
}
7、 保存恢复ListView当前位置
private void saveCurrentPosition() {
if (mListView != null) {
int position = mListView.getFirstVisiblePosition();
View v = mListView.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
//保存position和top
}
} private void restorePosition() {
if (mFolder != null && mListView != null) {
int position = 0;//取出保存的数据
int top = 0;//取出保存的数据
mListView.setSelectionFromTop(position, top);
}
} 可以保存在Preference中或者是数据库中,数据加载完后再设置。
8、调用 便携式热点和数据共享 设置
public static Intent getHotspotSetting() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
ComponentName com = new ComponentName("com.android.settings", "com.android.settings.TetherSettings");
intent.setComponent(com);
return intent;
}
9、 格式化输出IP地址
public static String getIp(Context ctx) {
return Formatter.formatIpAddress((WifiManager) ctx.getSystemService(Context.WIFI_SERVICE).getConnectionInfo().getIpAddress());
}
10、 文件夹排序(先文件夹排序,后文件排序)
public static void sortFiles(File[] files) {
Arrays.sort(files, new Comparator<File>() { @Override
public int compare(File lhs, File rhs) {
//返回负数表示o1 小于o2,返回0 表示o1和o2相等,返回正数表示o1大于o2。
boolean l1 = lhs.isDirectory();
boolean l2 = rhs.isDirectory();
if (l1 && !l2)
return -1;
else if (!l1 && l2)
return 1;
else {
return lhs.getName().compareTo(rhs.getName());
}
}
});
}
参考:http://www.cnblogs.com/over140/archive/2013/05/20/2948239.html
Android_CodeWiki_02的更多相关文章
随机推荐
- Aandroid Error之 新导入工程报Unable to resolve target 'android-18'和R cannot be resolved
有段时间没有写安卓了,今天导入以前的项目,结果看到控制台打印出了这样一句:Unable to resolve target 'android-18', 解决方法: 项目->属性->Andr ...
- V - stl 的 优先队列 Ⅱ
Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every morni ...
- linux学习笔记之文件类型,及目录介绍
引用A:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/20/3033131.html 引用B:http://www.cnblogs.c ...
- [Effective Modern C++] Item 7. Distinguish between () and {} when creating objects - 辨别使用()与{}创建对象的差别
条款7 辨别使用()与{}创建对象的差别 基础知识 目前已知有如下的初始化方式: ); ; }; }; // the same as above 在以“=”初始化的过程中没有调用赋值运算,如下例所示: ...
- js 函数(function)
<Javascript高级程序设计第三版> 3.7 函数 1. ECMAScript中的函数在定义时,不必指定是否返回值. 2. 位于return语句之后的任何code都永远不会执行.(之 ...
- github proxy
--set github proxy git config --global http.proxy http://user_name:user_pwd@user_ip:port git config ...
- SSO单点登录(转载)
昨天和几位朋友探讨到了这个话题,发现虽然单点登录,或者叫做独立的passport登录虽然已经有了很多实现方法,但是能真正了解并实现的人却并不太多,所以些下此文,希望从原理到实现,能让大家了解的多一些 ...
- wampserver 2.2装好后80端口未被占用,却打不开localhost
在windows server 2003中装好wampserver2.2后打不开localhost,点击服务全部启动(颜色是橙色)也是打不开,我解决的原因是:安装mysql中sevice中的安装测试服 ...
- python 源代码分析之调试设置
首先在官方下载源代码,我下载的是最新版本3.4.3版本:https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz 解压后的目录如下(借用网上的目 ...
- MYSQL 的 6 个返回时间日期函数
方法1. curdate(),curtime(),now() 方法2. utc_date(),utc_time(),utc_datetime(); 可以看到utc时间相比东西八区要小8小时 注意. 返 ...