Android性能优化汇总
案例:加载一个gif文件(比较占内存,模拟正常app的运行显示),此时出现下面耗时或者很消耗内存的情况(我们的代码写的不好),(结果)gif文件的显示就会受到影响,出现卡顿
1 正常gif的显示
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.loadUrl("file:///android_asset/shiver_me_timbers.gif");
2 计算斐波那契数列导致卡顿
斐波那契数列:前一个值+当前值 = 下一个值
1) 递归造成卡顿
public int computeFibonacci(int positionInFibSequence) {
//0 1 1 2 3 5 8
if(positionInFibSequence <=2){
return 1;
}else{
return computeFibonacci(positionInFibSequence-1)+computeFibonacci(positionInFibSequence-2);
}
}
2) 使用缓存思想优化后
//优化后的斐波那契数列的非递归算法 caching缓存+批处理思想
public int computeFibonacci(int positionInFibSequence) {
int pre = 0;
int current = 1;
int newValue;
for (int i = 1; i < positionInFibSequence; i++) {
newValue = current + pre;
pre = current;
current = newValue;
}
return current;
}
3 排序并打印二位数组
1) 优化前
public void imPrettySureSortingIsFree() {
int dimension = 300;
int[][] lotsOfInts = new int[dimension][dimension];
Random random = new Random();
for (int i = 0; i < lotsOfInts.length; i++) {
for (int j = 0; j < lotsOfInts[i].length; j++) {
lotsOfInts[i][j] = random.nextInt();
}
}
for (int i = 0; i < lotsOfInts.length; i++) {
String rowAsStr = "";
//排序
int[] sorted = getSorted(lotsOfInts[i]);
//拼接打印
for (int j = 0; j < lotsOfInts[i].length; j++) {
rowAsStr += sorted[j];
if (j < (lotsOfInts[i].length - 1)) {
rowAsStr += ",";
}
}
Log.i("ricky", "Row " + i + ": " + rowAsStr);
}
//优化以后
StringBuilder sb = new StringBuilder();
String rowAsStr = "";
for (int i = 0; i < lotsOfInts.length; i++) {
//清除上一行
sb.delete(0, rowAsStr.length());
//排序
int[] sorted = getSorted(lotsOfInts[i]);
//拼接打印
for (int j = 0; j < lotsOfInts[i].length; j++) {
rowAsStr += sorted[j];
sb.append(sorted[j]);
if(j < (lotsOfInts[i].length - 1)){
rowAsStr += ", ";
sb.append(", ");
}
}
rowAsStr = sb.toString();
Log.i("ricky", "Row " + i + ": " + rowAsStr);
}
}
public int[] getSorted(int[] input) {
int[] clone = input.clone();
Arrays.sort(clone);
return clone;
}
2)优化后
public void imPrettySureSortingIsFree() {
int dimension = 300;
int[][] lotsOfInts = new int[dimension][dimension];
Random randomGenerator = new Random();
for(int i = 0; i < lotsOfInts.length; i++) {
for (int j = 0; j < lotsOfInts[i].length; j++) {
lotsOfInts[i][j] = randomGenerator.nextInt();
}
}
// 使用StringBuilder完成输出,我们只需要创建一个字符串即可,不需要浪费过多的内存
StringBuilder sb = new StringBuilder();
String rowAsStr = "";
for(int i = 0; i < lotsOfInts.length; i++) {
// 清除上一行
sb.delete(0, rowAsStr.length());
//排序
int[] sorted = getSorted(lotsOfInts[i]);
//拼接打印
for (int j = 0; j < lotsOfInts[i].length; j++) {
sb.append(sorted[j]);
if(j < (lotsOfInts[i].length - 1)){
sb.append(", ");
}
}
rowAsStr = sb.toString();
Log.i("jason", "Row " + i + ": " + rowAsStr);
}
}
public int[] getSorted(int[] input){
int[] clone = input.clone();
Arrays.sort(clone);
return clone;
}
4 BusyUIThread:Display an image.(使用异步处理图片RGB色值变换加载)
private class SepiaFilterTask extends AsyncTask<Bitmap, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Bitmap... bitmaps) {
Bitmap loadedBitmap = bitmaps[0];
int width = loadedBitmap.getWidth();
int height = loadedBitmap.getHeight();
Bitmap sepiaBitmap = Bitmap.createBitmap(width, height, loadedBitmap.getConfig());
// Go through every single pixel in the bitmap, apply a sepia filter to it, and apply it
// to the new "output" bitmap.
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
// get pixel color
int pixel = loadedBitmap.getPixel(x, y);
int alpha = Color.alpha(pixel);
// These are the starting values for this pixel.
int inputRed = Color.red(pixel);
int inputGreen = Color.green(pixel);
int inputBlue = Color.blue(pixel);
// These are the sepia-fied values for each pixel.
// Note that if the resulting value is over 255, Math.Min brings it back down.
// So effectively, min establishes a max value! Isn't that weird?
int outRed = (int) Math.min( (inputRed * .393) + (inputGreen *.769) + (inputBlue * .189), 255);
int outGreen = (int) Math.min(
(inputRed * .349) + (inputGreen *.686) + (inputBlue * .168), 255);
int outBlue = (int) Math.min(
(inputRed * .272) + (inputGreen *.534) + (inputBlue * .131), 255);
// apply new pixel color to output bitmap
sepiaBitmap.setPixel(x, y, Color.argb(alpha, outRed, outGreen, outBlue));
}
}
return sepiaBitmap;
}
@Override
protected void onPostExecute(Bitmap sepiaBitmap) {
ImageView imageView = findViewById(R.id.busyui_imageview);
imageView.setImageBitmap(sepiaBitmap);
}
}
图片处理的一段时间里,gif正常显示