倒影效果,简化版
package com.nico;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.AvoidXfermode.Mode;
import android.graphics.Bitmap.Config;
import android.graphics.Shader.TileMode;
import android.os.Bundle;
import android.widget.ImageView;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取图片的bitmap对象
Bitmap bmp = BitmapFactory
.decodeResource(getResources(), R.drawable.qm);
// 调用生成带倒影的bitmap
Bitmap b = getFBitmap(bmp);
ImageView img = (ImageView) findViewById(R.id.img);
// 生成imageview
img.setImageBitmap(b);
}
public Bitmap getFBitmap(Bitmap bmp) {
// 画布
Canvas canvas = new Canvas();
// 矩阵
Matrix matrix = canvas.getMatrix();
// 缩放
matrix.preScale(1, -1);
// 生成带倒影的bitmap打底图
Bitmap allbitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight()
+ bmp.getHeight() / 2, Config.ARGB_8888);
// 设置为画布背景
canvas.setBitmap(allbitmap);
Paint dp = new Paint();
// 将初始图像画上去
canvas.drawBitmap(bmp, 0, 0, dp);
Paint p = new Paint();
// 绘制下半部分的图像
canvas.drawRect(0, bmp.getHeight(), bmp.getWidth(),
bmp.getHeight() / 2, p);
// 绘制下边部分的图像
Bitmap flenbmp = Bitmap.createBitmap(bmp, 0, bmp.getHeight() / 2, bmp
.getWidth(), bmp.getHeight() / 2, matrix, false);
//将btmap 绘制到画布上
canvas.drawBitmap(flenbmp, 0, bmp.getHeight(), null);
Paint paint = new Paint();
//渐变的效果,不是很清楚
LinearGradient shader = new LinearGradient(0, bmp.getHeight(), 0,
allbitmap.getHeight(), 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode((PorterDuff.Mode.DST_IN)));
//绘制下面的图片的框,并使用到设置好参数的paint
canvas.drawRect(0, bmp.getHeight(), bmp.getWidth(), allbitmap.getHeight() , paint);
return allbitmap;
}
}
外国人真具有共产主义精神,Foursquare都拿出开源了,不像国内某些公司。Foursquare下载地址主页地址http://code.google.com/p/foursquared/ 。下载方式hg clone https://foursquared.googlecode.com/hg/ foursquared ,在linux下用hg命令可以直接下载。Foursquare在代码组织方面当然是相当不错的,层次逻辑规划的相当好,在解决网络读写时的界面阻塞,以及图片加载等耗时操作方面采用的方式值得借鉴。下面以传输协议,图片加载,网络读取方面介绍。
一.协议层
Foursquare采用http协议传输数据。目前如Foursquare,twitter都采用如手机+浏览器+iPad等诸多设备,为了服务器的统一,采用http协议。
Foursquare中的基类HttpApi定义了Http协议的一些接口doHttpRequest,及doHttpPost,AbstractHttpApi则实现了上述方法。Foursquare与服务器交换数据的格式采用XML格式。Foursquare客户端要获取数据时首先构造好http请求,通过http层的doHttpRequest,及doHttpPost层发送http请求,服务器解析http请求,把结果保存为XML格式返回给客户端。以City类来解释XML解析过程,过程中涉及四个类,AbstractParser解析基类,主要用户构造解析器基类,提供解析方法,CityParser继承自AbstractParser,用于解析一条协议,FoursqureType接口无函数定义,用于表示是一个Foursqure类型,City继承于FoursqureType表示一个解析结果。
解析方式中采用了设计模式中的模板模式—定义一个操作中的算法骨架,而将进一步实现延迟到子类中,子类不改变一个算法的结构即可中定义改算法的某些特定步骤,达到复用代码的目的。
在AbstractParser类中定义了解析算法的模板,并且定义了抽象方法abstract protected T parseInner()用于解析一个具体的协议。我们来看模板方法:
public final T parse(XmlPullParser parser) throws FoursquareParseException, FoursquareError {
///算法模板
try {
if (parser.getEventType() == XmlPullParser.START_DOCUMENT) {
parser.nextTag();
if (parser.getName().equals("error")) {
throw new FoursquareError(parser.nextText());
}
}
return parseInner(parser); //调用子类具体实现
} catch (IOException e) {
if (DEBUG) LOG.log(Level.FINE, "IOException", e);
throw new FoursquareParseException(e.getMessage());
} catch (XmlPullParserException e) {
if (DEBUG) LOG.log(Level.FINE, "XmlPullParserException", e);
throw new FoursquareParseException(e.getMessage());
}
}
再看下CityParser 子类具体解析的过程:
服务器返回的结果
<?xml version=”1.0”?>
<city>
< geolat ></ geolat >
< geolong ></ geolong >
< id ></ id >
< name ></ name >
< shortname ></ shortname >
< timezone ></ timezone >
< cityid ></ cityid >
</city>
CityParser中的parseInner解析过程
parser.require(XmlPullParser.START_TAG, null, null);
City city = new City(); //解析结果
while (parser.nextTag() == XmlPullParser.START_TAG) {
String name = parser.getName();
if ("geolat".equals(name)) {
city.setGeolat(parser.nextText());
} else if ("geolong".equals(name)) {
city.setGeolong(parser.nextText());
} else if ("id".equals(name)) {
city.setId(parser.nextText());
} else if ("name".equals(name)) {
city.setName(parser.nextText());
} else if ("shortname".equals(name)) {
city.setShortname(parser.nextText());
} else if ("timezone".equals(name)) {
city.setTimezone(parser.nextText());
} else if ("cityid".equals(name)) {
city.setId(parser.nextText());
} else {
skipSubTree(parser);
}
}
return city
Foursquare中要新添加一条协议的时候只要继承AbstractParser并实现其中的parseInner方法,实现FoursqureType定义一个新的类型就可以了。采用模板方法,无疑提高了系统的可扩展性,以及清晰地代码结构,容易维护,这就是采用面向对象思想带来的好处。
二.图标读取优化 延迟加载+缓存+多线程读取+线程池技术
考虑到手机带宽的限制,以及提升性能,缓存是必不可少的组件。在手机端缓存,主要用户缓存一些常用的不易改变的图片,如:地点,用户,朋友头像等。来分析下缓存的具体实现。缓存实现主要在BaseDiskCache类中。
//用于存放一个图片到缓存中
public void store(String key, InputStream is) {
if (DEBUG) Log.d(TAG, "store: " + key);
is = new BufferedInputStream(is);
try {
OutputStream os = new BufferedOutputStream(new FileOutputStream(getFile(key)));//获取存放路径
byte[] b = new byte[2048];
int count;
int total = 0;
while ((count = is.read(b)) > 0) {
os.write(b, 0, count);
total += count;
}
os.close();
if (DEBUG) Log.d(TAG, "store complete: " + key);
} catch (IOException e) {
if (DEBUG) Log.d(TAG, "store failed to store: " + key, e);
return;
}
}
//获取路径
public File getFile(String hash) {
return new File(mStorageDirectory.toString() + File.separator + hash); //存放路径
}
#!/bin/sh
ip="10.85.184.111"
conResult=`adb connect $ip|sed -n '/[Uu]nable/'p`
count=0
while [ \( "$conResult" != "" \) -a \( $count -lt 5 \) ]
do
sleep 1
conResult=`adb connect $ip|sed -n '/[Uu]nable/'p`
echo "try connect again......"
echo $conResult
count=`expr $count + 1`
done
if [ "$conResult" != "" ]
then
echo "connect fail! $conResult"
else
echo "connect to board $ip"
echo "try uninstall......"
#adb shell ls /data/data|sed -n '/test\./p'>uninstallCases.conf
appNum=`adb shell ls /data/data|sed -n '/test\./p'|sed -n '1,$='`
echo $appNum
for appRow in $appNum
do
appName=`adb shell ls /data/data|sed -n '/test\./p'|sed -n '1p'`
#appName=`sed -n "$appRow p" uninstallCases.conf|awk '{printf "%s",$0}'`
echo "@$appName@"
strLen=`echo "$appName"|wc -L`
echo $strLen
str=`echo $appName|cut -c 1-$strLen`
echo "@$str@"
adb uninstall $str
done
echo "try install all cases in dir test_case......"
caseNum=`ls ../test_case/app|sed -n '1,$='`
echo $caseNum
for caseRow in $caseNum
do
caseName=`ls ../test_case/app|sed -n "$caseRow p"`
echo $caseName
#adb shell rm /data/app
adb install ../test_case/app/$caseName
done
fi
exit