zoukankan      html  css  js  c++  java
  • 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(0)) {
             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转数组

      

    1  @SuppressWarnings("unchecked")
    2     public static <T> T[] toArray(Class<?> cls, ArrayList<T> items) {
    3         if (items == null || items.size() == 0) {
    4             return (T[]) Array.newInstance(cls, 0);
    5         }
    6         return items.toArray((T[]) Array.newInstance(cls, items.size()));
    7     }

    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

  • 相关阅读:
    我对“错排问题”的理解
    洛谷P1144 最短路计数 题解 无权图的最短路计数(广搜)
    洛谷P1714 切蛋糕 题解 单调队列
    洛谷P6040 「ACOI2020」课后期末考试滑溜滑溜补习班 题解 单调队列优化DP
    POJ2559 Largest Rectangle in a Histogram 题解 单调队列/单调栈 (直方图的最大矩形面积)
    洛谷P2947 向右看齐Look Up 题解 单调栈/单调队列
    洛谷P1725 琪露诺 题解 单调队列优化DP入门题
    洛谷P1886 滑动窗口 题解 单调队列
    洛谷P2952 牛线Cow Line 题解 双端队列deque的使用
    多线程交互,访问数据,如果访问到了就不访问了,怎么 避免重读?
  • 原文地址:https://www.cnblogs.com/spring87/p/4265924.html
Copyright © 2011-2022 走看看