内存(ram)
android的总内存大小信息存放在系统的/proc/meminfo文件里面,可以通过读取这个文件来获取这些信息:
String str1 = "/proc/meminfo";
String str2="";
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
while ((str2 = localBufferedReader.readLine()) != null) {
Log.i(TAG, "---" + str2);
}
} catch (IOException e) {
}
}
运行信息如下:
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---MemFree: 4596 kB
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Buffers: 16020 kB
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Cached: 82508 kB
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---SwapCached: 64 kB
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Active: 137104 kB
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---Inactive: 41056 kB
05-30 08:05:14.807: INFO/-SystemInfo-(1519): ---SwapTotal: 65528 kB
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---SwapFree: 65368 kB
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Dirty: 88 kB
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Writeback: 0 kB
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---AnonPages: 79672 kB
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Mapped: 38296 kB
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---Slab: 5768 kB
05-30 08:05:14.817: INFO/-SystemInfo-(1519): ---SReclaimable: 1856 kB
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---SUnreclaim: 3912 kB
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---PageTables: 8184 kB
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---NFS_Unstable: 0 kB
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---Bounce: 0 kB
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---CommitLimit: 167964 kB
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---Committed_AS: 11771920 kB
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocTotal: 761856 kB
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocUsed: 83656 kB
05-30 08:05:14.827: INFO/-SystemInfo-(1519): ---VmallocChunk: 674820 kB
第一行是总内存大小(即用户可以使用的ram的大小)!
获取当前剩余内存(ram)大小的方法:
ActivityManager am = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return mi.availMem;
}
Rom大小
long[] romInfo = new long[2];
//Total rom memory
romInfo[0] = getTotalInternalMemorySize();
//Available rom memory
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
romInfo[1] = blockSize * availableBlocks;
getVersion();
return romInfo;
}
public long getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockSize;
}
注意类型,不然相乘之后会有溢出。可用内部存储的大小不能通过getRootDirectory();取得,之前网上传的很多都是用getRootDirectory()取得的,我测试之后发现取得的数值不对。要根据getDataDirectory();取得。
SDcard大小
long[] sdCardInfo=new long[2];
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File sdcardDir = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(sdcardDir.getPath());
long bSize = sf.getBlockSize();
long bCount = sf.getBlockCount();
long availBlocks = sf.getAvailableBlocks();
sdCardInfo[0] = bSize * bCount;//总大小
sdCardInfo[1] = bSize * availBlocks;//可用大小
}
TwoColorLabel -- 可以显示两种颜色的Label
你可能知道在Swing中通过为一个JLabel设置HTML文本,即可显示复杂多样的文字效果。但是很遗憾,在JavaFX2+中,Label不具备这个特性。在JavaFX2版本中,HTMLEditor和WebView可以解析HTML文本,但是使用这两个组件来显示很小的文本,是个累赘。在JavaFX8中,新增了一个TextFlow容器,可以向其中添加不同效果的Text,该容器会自动的对Text进行换行布局等,以及显示不同效果的文本。
本将介绍一个为在JavaFX2版本中使用的显示两种颜色文本控件(局限性很大,只能分前后显示两种颜色,可以自动换行)
代码如下:
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
/**
* 可以显示两种颜色的Label
*
* @since 2014-7-23 10:08:28
* @author Alan
*/
public class TwoColorLabel extends TextArea {
private static final Color DEFAULT_FIRST_COLOR = Color.RED;
private static final Color DEFAULT_SECOND_COLOR = Color.rgb(76, 120, 174);
public TwoColorLabel(final String firstText, final String secondText) {
this(firstText, secondText, DEFAULT_FIRST_COLOR, DEFAULT_SECOND_COLOR);
}
public TwoColorLabel(final String firstText, final String secondText, final Color firstColor, final Color secondColor) {
setText(firstText + secondText);
setEditable(false);
addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
t.consume();
}
});
setWrapText(true);
setStyle("-fx-text-fill:rgb(" + (int) (firstColor.getRed() * 255) + "," + (int) (firstColor.getGreen() * 255) + "," + (int) (firstColor.getBlue() * 255) + ");"
+ "-fx-highlight-fill: white;\n"
+ "-fx-highlight-text-fill: rgb(" + (int) (secondColor.getRed() * 255) + "," + (int) (secondColor.getGreen() * 255) + "," + (int) (secondColor.getBlue() * 255) + ");"
+ "-fx-background-color: transparent;");
selectRange(firstText.length(), getText().length());
}
}
本文链接:JavaFX2 -- 控件 -- TwoColorLabel -- 可以显示两种颜色的Label,转载请注明。
没有评论:
发表评论