Android的一些工具方法 - 開發(fā)資源區(qū) - 中國(guó)移動(dòng)開發(fā)者社區(qū)論壇 OMS開發(fā),Android開發(fā),symbian開發(fā),j2me開發(fā),windows phone開發(fā),手機(jī)軟件開發(fā),手機(jī)游戲開發(fā),主題制作
1.圖片加載方法,方便用戶加載圖片
/***
* 加載本地圖片
* @param context:主運(yùn)行函數(shù)實(shí)例
* @param bitAdress:圖片地址,一般指向R下的drawable目錄
* @return
*/
public final Bitmap CreatImage(Context context, int bitAdress) {
Bitmap bitmaptemp = null;
bitmaptemp =BitmapFactory.decodeResource(context.getResources(),
bitAdress);
return bitmaptemp;
}
2.圖片平均分割方法,將大圖平均分割為N行N列,方便用戶使用
/***
* 圖片分割
*
* @param g
* :畫布
* @param paint
* :畫筆
* @param imgBit
* :圖片
* @param x
* :X軸起點(diǎn)坐標(biāo)
* @param y
* :Y軸起點(diǎn)坐標(biāo)
* @param w
* :?jiǎn)我粓D片的寬度
* @param h
* :?jiǎn)我粓D片的高度
* @param line
* :第幾列
* @param row
* :第幾行
*/
public final void cuteImage(Canvas g, Paint paint, BitmapimgBit, int x,
int y, int w, int h, int line, int row) {
g.clipRect(x, y, x + w, h + y);
g.drawBitmap(imgBit, x - line * w, y - row * h, paint);
g.restore();
}
3.圖片縮放,對(duì)當(dāng)前圖片進(jìn)行縮放處理
/***
* 圖片的縮放方法
*
* @param bgimage
* :源圖片資源
* @param newWidth
* :縮放后寬度
* @param newHeight
* :縮放后高度
* @return
*/
public Bitmap zoomImage(Bitmap bgimage, int newWidth, intnewHeight) {
// 獲取這個(gè)圖片的寬和高
int width = bgimage.getWidth();
int height = bgimage.getHeight();
// 創(chuàng)建操作圖片用的matrix對(duì)象
Matrix matrix = new Matrix();
// 計(jì)算縮放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 縮放圖片動(dòng)作
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0,width, height,
matrix, true);
return bitmap;
}
4.繪制帶有邊框的文字,一般在游戲中起文字的美化作用
/***
* 繪制帶有邊框的文字
*
* @param strMsg
* :繪制內(nèi)容
* @param g
* :畫布
* @param paint
* :畫筆
* @param setx
* ::X軸起始坐標(biāo)
* @param sety
* :Y軸的起始坐標(biāo)
* @param fg
* :前景色
* @param bg
* :背景色
*/
public void drawText(String strMsg, Canvas g, Paint paint, intsetx,
int sety, int fg, int bg) {
paint.setColor(bg);
g.drawText(strMsg, setx + 1, sety, paint);
g.drawText(strMsg, setx, sety - 1, paint);
g.drawText(strMsg, setx, sety + 1, paint);
g.drawText(strMsg, setx - 1, sety, paint);
paint.setColor(fg);
g.drawText(strMsg, setx, sety, paint);
g.restore();
}
5.圖片分割的最簡(jiǎn)便方式
public final Bitmap cuteImage(Bitmap _imgBit, int _startX, int width,
int _startY, int height) {
Bitmap tempMap = null;
tempMap = Bitmap.createBitmap(_imgBit, _startX, _startY, width, height);
return tempMap;
}
6.字符串分行顯示
public String[] StringFormat(String text, int maxWidth, int fontSize) {
String[] result = null;
Vector<String> tempR = new Vector<String>();
int lines = 0;
int len = text.length();
int index0 = 0;
int index1 = 0;
boolean wrap;
while (true) {
int widthes = 0;
wrap = false;
for (index0 = index1; index1 < len; index1++) {
if (text.charAt(index1) == '\n') {
index1++;
wrap = true;
break;
}
widthes = fontSize + widthes;
if (widthes > maxWidth) {
break;
}
}
lines++;
if (wrap) {
tempR.addElement(text.substring(index0, index1 - 1));
} else {
tempR.addElement(text.substring(index0, index1));
}
if (index1 >= len) {
break;
}
}
result = new String[lines];
tempR.copyInto(result);
return result;
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。