日期:2014-05-20 浏览次数:20715 次
/**
*
* @param containerWidth 容器的宽度
* @param containerHeight
* @param widgetWidth 控件的宽度
* @param widgetHeight
* @return int[] i
* i[0]代表控件应赋予的宽度,i[1]代表控件应赋予的高度
*/
public int[] getImgWidthAndHeight(int containerWidth,int containerHeight,int widgetWidth,int widgetHeight){
double rateWeight=(double)widgetWidth/(double)containerWidth;
double rateHeight=(double)widgetHeight/(double)containerHeight;
double rate=(rateWeight>rateHeight)?rateWeight:rateHeight;
if(rate<=1){
return new int[]{widgetWidth,widgetHeight};
}else{
System.out.println(rate);
return new int[]{widgetWidth/((int)rate+1),widgetHeight/((int)rate+1)};
}
}
// 输入list获取byte[]对象
public byte[] getByte(List<Object> list) {
byte[] buf = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 构造一个字节数组输出流
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(baos);
// 构造一个类输出流
oos.writeObject(list); // 写这个对象
buf = baos.toByteArray(); // 从这个地层字节流中把传输的数组给一个新的数组
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return buf;
}
// 通过byte[]数组获取对象
public List<Object> getObject(byte[] b) {
List<Object> list = null;
// 构建一个类输入流,地层用字节输入流实现
try {
ByteArrayInputStream bais = new ByteArrayInputStream(b);
ObjectInputStream ois;
ois = new ObjectInputStream(bais);
list = (List<Object>) ois.readObject();
// ArrayList str = (ArrayList) ois.readObject(); //读取类
} catch (Exception e) {
e.printStackTrace();
}
return list;
}