- //Android
Matrix类实现镜像方法 - public
void drawRegion(Image image_src, -
- int
x_src, int y_src, -
- int
width, int height, -
- int
transform, -
- int
x_dest, int y_dest, -
- int
anchor){ -
- if((anchor&VCENTER)
!= 0){ -
- y_dest
-= height/2; -
- }else
if((anchor&BOTTOM) != 0){ -
- y_dest
-= height; -
- }
-
- if((anchor&RIGHT)
!= 0){ -
- x_dest
-= width; -
- }else
if((anchor&HCENTER) != 0){ -
- x_dest
-= width/2; -
- }
-
- Bitmap
newMap = Bitmap.createBitmap(image_src.getBitmap(), x_src, y_src, width, height); -
- Matrix
mMatrix = new Matrix(); -
- Matrix
temp = new Matrix(); -
- Matrix
temp2 = new Matrix(); -
- float[]
mirrorY = { -
- -1,
0, 0, - 0,
1, 0, - 0,
0, 1 -
- };
-
- temp.setValues(mirrorY);
-
- switch(transform){
-
- case
Sprite.TRANS_NONE: -
- break;
-
- case
Sprite.TRANS_ROT90: -
- mMatrix.setRotate(90,width/2,
height/2); -
- break;
-
- case
Sprite.TRANS_ROT180: -
- mMatrix.setRotate(180,width/2,
height/2); -
- break;
-
- case
Sprite.TRANS_ROT270: -
- mMatrix.setRotate(270,width/2,
height/2); -
- break;
-
- case
Sprite.TRANS_MIRROR: -
- mMatrix.postConcat(temp);
-
- break;
-
- case
Sprite.TRANS_MIRROR_ROT90: -
- mMatrix.postConcat(temp);
-
- mMatrix.setRotate(90,width/2,
height/2); -
- break;
-
- case
Sprite.TRANS_MIRROR_ROT180: -
- mMatrix.postConcat(temp);
-
- mMatrix.setRotate(180,width/2,
height/2); -
- break;
-
- case
Sprite.TRANS_MIRROR_ROT270: -
- mMatrix.postConcat(temp);
-
- mMatrix.setRotate(270,width/2,
height/2); -
- break;
-
- }
-
- mMatrix.setTranslate(x_dest,
y_dest); -
- canvas.drawBitmap(newMap,
mMatrix, mPaint); -
- }
- //图片Url保存为位图并进行缩放操作
- //通过传入图片url获取位图方法
- public
Bitmap returnBitMap(String url) { -
URL myFileUrl = null; -
Bitmap bitmap = null; -
try { -
myFileUrl = new URL(url); -
} catch (MalformedURLException e) { -
e.printStackTrace(); -
} -
try { -
HttpURLConnection conn = (HttpURLConnection) myFileUrl -
.openConnection(); -
conn.setDoInput(true); -
conn.connect(); -
InputStream is = conn.getInputStream(); -
bitmap = BitmapFactory.decodeStream(is); -
is.close(); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
Log.v(tag, bitmap.toString()); -
-
return bitmap; -
} - //通过传入位图,新的宽.高比进行位图的缩放操作
- public
static Drawable resizeImage(Bitmap bitmap, int w, int h) { -
-
// load the origial Bitmap -
Bitmap BitmapOrg = bitmap; -
-
int width = BitmapOrg.getWidth(); -
int height = BitmapOrg.getHeight(); -
int newWidth = w; -
int newHeight = h; -
-
Log.v(tag, String.valueOf(width)); -
Log.v(tag, String.valueOf(height)); -
-
Log.v(tag, String.valueOf(newWidth)); -
Log.v(tag, String.valueOf(newHeight)); -
-
// calculate the scale -
float scaleWidth = ((float) newWidth) / width; -
float scaleHeight = ((float) newHeight) / height; -
-
// create a matrix for the manipulation -
Matrix matrix = new Matrix(); -
// resize the Bitmap -
matrix.postScale(scaleWidth, scaleHeight); -
// if you want to rotate the Bitmap -
// matrix.postRotate(45); -
-
// recreate the new Bitmap -
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, -
height, matrix, true); -
-
// make a Drawable from Bitmap to allow to set the Bitmap -
// to the ImageView, ImageButton or what ever -
return new BitmapDrawable(resizedBitmap); -
-
}
- 1.图片加载方法,方便用户加载图片
-
- public
final Bitmap CreatImage(Context context, int bitAdress) { - Bitmap
bitmaptemp = null; - bitmaptemp
= BitmapFactory.decodeResource(context.getResources(), - bitAdress);
- return
bitmaptemp; - }
- 2.图片平均分割方法,将大图平均分割为N行N列,方便用户使用
-
- public
final void cuteImage(Canvas g, Paint paint, Bitmap imgBit, 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.图片缩放,对当前图片进行缩放处理
-
- public
Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) { - //
获取这个图片的宽和高 - int
width = bgimage.getWidth(); - int
height = bgimage.getHeight(); - //
创建操作图片用的matrix对象 - Matrix
matrix = new Matrix(); - //
计算缩放率,新尺寸除原始尺寸 - float
scaleWidth = ((float) newWidth) / width; - float
scaleHeight = ((float) newHeight) / height; - //
缩放图片动作 - matrix.postScale(scaleWidth,
scaleHeight); - Bitmap
bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height, - matrix,
true); - return
bitmap; - }
- 4.绘制带有边框的文字,一般在游戏中起文字的美化作用
-
- public
void drawText(String strMsg, Canvas g, Paint paint, int setx, - 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.Android
图片透明度处理代码 -
- public
static Bitmap setAlpha(Bitmap sourceImg, int number) { - int[]
argb = new int[sourceImg.getWidth() * sourceImg.getHeight()]; - sourceImg.getPixels(argb,
0, sourceImg.getWidth(), 0, 0,sourceImg.getWidth(), sourceImg.getHeight());// 获得图片的ARGB值 - number
= number * 255 / 100; - for
(int i = 0; i < argb.length; i++) { - argb
= (number << 24) | (argb & 0×00FFFFFF);// 修改最高2位的值 - }
- sourceImg
= Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg.getHeight(), Config.ARGB_8888); - return
sourceImg; - }
- 6.图片翻转
- Resources
res = this.getContext().getResources(); - img
= BitmapFactory.decodeResource(res, R.drawable.slogo); - Matrix
matrix = new Matrix(); - matrix.postRotate(90);
- int
width = img.getWidth(); - int
height = img.getHeight(); - r_img
= Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
- import
android.graphics.Bitmap; - import
android.graphics.Canvas; - import
android.graphics.LinearGradient; - import
android.graphics.Matrix; - import
android.graphics.Paint; - import
android.graphics.PixelFormat; - import
android.graphics.PorterDuffXfermode; - import
android.graphics.Rect; - import
android.graphics.RectF; - import
android.graphics.Bitmap.Config; - import
android.graphics.PorterDuff.Mode; - import
android.graphics.Shader.TileMode; - import
android.graphics.drawable.Drawable; -
- public
class ImageUtil { -
-
- public
static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) { -
int width = bitmap.getWidth(); -
int height = bitmap.getHeight(); -
Matrix matrix = new Matrix(); -
float scaleWidht = ((float) w / width); -
float scaleHeight = ((float) h / height); -
matrix.postScale(scaleWidht, scaleHeight); -
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); -
return newbmp; - }
-
-
- public
static Bitmap drawableToBitmap(Drawable drawable) { -
int width = drawable.getIntrinsicWidth(); -
int height = drawable.getIntrinsicHeight(); -
Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); -
Canvas canvas = new Canvas(bitmap); -
drawable.setBounds(0, 0, width, height); -
drawable.draw(canvas); -
return bitmap; -
- }
-
-
- public
static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { -
-
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); -
Canvas canvas = new Canvas(output); -
-
final int color = 0xff424242; -
final Paint paint = new Paint(); -
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); -
final RectF rectF = new RectF(rect); -
-
paint.setAntiAlias(true); -
canvas.drawARGB(0, 0, 0, 0); -
paint.setColor(color); -
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); -
-
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); -
canvas.drawBitmap(bitmap, rect, rect, paint); -
-
return output; - }
-
-
- public
static Bitmap createReflectionImageWit hOrigin(Bitmap bitmap) { -
final int reflectionGap = 4; -
int width = bitmap.getWidth(); -
int height = bitmap.getHeight(); -
-
Matrix matrix = new Matrix(); -
matrix.preScale(1, -1); -
-
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false); -
-
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888); -
-
Canvas canvas = new Canvas(bitmapWithReflection); -
canvas.drawBitmap(bitmap, 0, 0, null); -
Paint deafalutPaint = new Paint(); -
canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint); -
-
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); -
-
Paint paint = new Paint(); -
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); -
paint.setShader(shader); -
// Set the Transfer mode to be porter duff and destination in -
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); -
// Draw a rectangle using the paint with our linear gradient -
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); -
return bitmapWithReflection; - }
- }
Java代码