- 浏览: 5819400 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (890)
- WindowsPhone (0)
- android (88)
- android快速迭代 (17)
- android基础 (34)
- android进阶 (172)
- android高级 (0)
- android拾遗 (85)
- android动画&效果 (68)
- Material Design (13)
- LUA (5)
- j2me (32)
- jQuery (39)
- spring (26)
- hibernate (20)
- struts (26)
- tomcat (9)
- javascript+css+html (62)
- jsp+servlet+javabean (14)
- java (37)
- velocity+FCKeditor (13)
- linux+批处理 (9)
- mysql (19)
- MyEclipse (9)
- ajax (7)
- wap (8)
- j2ee+apache (24)
- 其他 (13)
- phonegap (35)
最新评论
-
Memories_NC:
本地lua脚本终于执行成功了,虽然不是通过redis
java中调用lua脚本语言1 -
ZHOU452840622:
大神://处理返回的接收状态 这个好像没有监听到 遇 ...
android 发送短信的两种方式 -
PXY:
拦截部分地址,怎么写的for(int i=0;i<lis ...
判断是否登录的拦截器SessionFilter -
maotou1988:
Android控件之带清空按钮(功能)的AutoComplet ...
自定义AutoCompleteTextView -
yangmaolinpl:
希望有表例子更好。。。,不过也看明白了。
浅谈onInterceptTouchEvent、onTouchEvent与onTouch
//压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; while ( baos.toByteArray().length / 1024>100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩 baos.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中 options -= 10;//每次都减少10 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片 return bitmap; }
/** * 将彩色图转换为灰度图 * @param img 位图 * @return 返回转换好的位图 */ public Bitmap convertGreyImg(Bitmap img) { int width = img.getWidth(); //获取位图的宽 int height = img.getHeight(); //获取位图的高 int []pixels = new int[width * height]; //通过位图的大小创建像素点数组 img.getPixels(pixels, 0, width, 0, 0, width, height); int alpha = 0xFF << 24; for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { int grey = pixels[width * i + j]; int red = ((grey & 0x00FF0000 ) >> 16); int green = ((grey & 0x0000FF00) >> 8); int blue = (grey & 0x000000FF); grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11); grey = alpha | (grey << 16) | (grey << 8) | grey; pixels[width * i + j] = grey; } } Bitmap result = Bitmap.createBitmap(width, height, Config.RGB_565); result.setPixels(pixels, 0, width, 0, 0, width, height); return result; }
将一个图片切割成多个图片
有种场景,我们想将一个图片切割成多个图片。比如我们在开发一个拼图的游戏,就首先要对图片进行切割。
以下是封装好的两个类,可以实现图片的切割。仅供参考和学习。
一个是ImagePiece类,此类保存了一个Bitmap对象和一个标识图片的顺序索引的int变量。
import android.graphics.Bitmap; public class ImagePiece { public int index = 0; public Bitmap bitmap = null; }
一个是ImageSplitter类,有一个静态方法split,传入的参数是要切割的Bitmap对象,和横向和竖向的切割片数。比如传入的是3、3,则横竖向都切割成3片,最终会将整个图片切割成3X3=9片。
import java.util.ArrayList; import java.util.List; import android.graphics.Bitmap; public class ImageSplitter { public static List<ImagePiece> split(Bitmap bitmap, int xPiece, int yPiece) { List<ImagePiece> pieces = new ArrayList<ImagePiece>(xPiece * yPiece); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int pieceWidth = width / 3; int pieceHeight = height / 3; for (int i = 0; i < yPiece; i++) { for (int j = 0; j < xPiece; j++) { ImagePiece piece = new ImagePiece(); piece.index = j + i * xPiece; int xValue = j * pieceWidth; int yValue = i * pieceHeight; piece.bitmap = Bitmap.createBitmap(bitmap, xValue, yValue, pieceWidth, pieceHeight); pieces.add(piece); } } return pieces; } }
1、图标加灰色过滤;
2、android的图片资源默认是静态的,单实例;如果两个IM好友的头像一样,最简单的都是用的软件自带头像,有一个在线,一个离线,直接改变头像的灰度,则两个用户的头像都会变灰或者在线,答案是:Drawable.mutate()。
Drawable mDrawable = context.getResources().getDrawable(R.drawable.face_icon); //Make this drawable mutable. //A mutable drawable is guaranteed to not share its state with any other drawable. mDrawable.mutate(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm); mDrawable.setColorFilter(cf);
生成缩略图,抠自android launcher源码:
/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.launcher; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.PaintDrawable; import android.graphics.Bitmap; import android.graphics.PixelFormat; import android.graphics.Canvas; import android.graphics.PaintFlagsDrawFilter; import android.graphics.Paint; import android.graphics.Rect; import android.content.res.Resources; import android.content.Context; /** * Various utilities shared amongst the Launcher's classes. */ final class Utilities { private static int sIconWidth = -1; private static int sIconHeight = -1; private static final Paint sPaint = new Paint(); private static final Rect sBounds = new Rect(); private static final Rect sOldBounds = new Rect(); private static Canvas sCanvas = new Canvas(); static { sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, Paint.FILTER_BITMAP_FLAG)); } /** * Returns a Drawable representing the thumbnail of the specified Drawable. * The size of the thumbnail is defined by the dimension * android.R.dimen.launcher_application_icon_size. * * This method is not thread-safe and should be invoked on the UI thread only. * * @param icon The icon to get a thumbnail of. * @param context The application's context. * * @return A thumbnail for the specified icon or the icon itself if the * thumbnail could not be created. */ static Drawable createIconThumbnail(Drawable icon, Context context) { if (sIconWidth == -1) { final Resources resources = context.getResources(); sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size); } int width = sIconWidth; int height = sIconHeight; float scale = 1.0f; if (icon instanceof PaintDrawable) { PaintDrawable painter = (PaintDrawable) icon; painter.setIntrinsicWidth(width); painter.setIntrinsicHeight(height); } else if (icon instanceof BitmapDrawable) { // Ensure the bitmap has a density. BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; Bitmap bitmap = bitmapDrawable.getBitmap(); if (bitmap.getDensity() == Bitmap.DENSITY_NONE) { bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); } } int iconWidth = icon.getIntrinsicWidth(); int iconHeight = icon.getIntrinsicHeight(); if (width > 0 && height > 0) { if (width < iconWidth || height < iconHeight || scale != 1.0f) { final float ratio = (float) iconWidth / iconHeight; if (iconWidth > iconHeight) { height = (int) (width / ratio); } else if (iconHeight > iconWidth) { width = (int) (height * ratio); } final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); final Canvas canvas = sCanvas; canvas.setBitmap(thumb); // Copy the old bounds to restore them later // If we were to do oldBounds = icon.getBounds(), // the call to setBounds() that follows would // change the same instance and we would lose the // old bounds sOldBounds.set(icon.getBounds()); final int x = (sIconWidth - width) / 2; final int y = (sIconHeight - height) / 2; icon.setBounds(x, y, x + width, y + height); icon.draw(canvas); icon.setBounds(sOldBounds); icon = new FastBitmapDrawable(thumb); } else if (iconWidth < width && iconHeight < height) { final Bitmap.Config c = Bitmap.Config.ARGB_8888; final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); final Canvas canvas = sCanvas; canvas.setBitmap(thumb); sOldBounds.set(icon.getBounds()); final int x = (width - iconWidth) / 2; final int y = (height - iconHeight) / 2; icon.setBounds(x, y, x + iconWidth, y + iconHeight); icon.draw(canvas); icon.setBounds(sOldBounds); icon = new FastBitmapDrawable(thumb); } } return icon; } /** * Returns a Bitmap representing the thumbnail of the specified Bitmap. * The size of the thumbnail is defined by the dimension * android.R.dimen.launcher_application_icon_size. * * This method is not thread-safe and should be invoked on the UI thread only. * * @param bitmap The bitmap to get a thumbnail of. * @param context The application's context. * * @return A thumbnail for the specified bitmap or the bitmap itself if the * thumbnail could not be created. */ static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) { if (sIconWidth == -1) { final Resources resources = context.getResources(); sIconWidth = sIconHeight = (int) resources.getDimension( android.R.dimen.app_icon_size); } int width = sIconWidth; int height = sIconHeight; final int bitmapWidth = bitmap.getWidth(); final int bitmapHeight = bitmap.getHeight(); if (width > 0 && height > 0) { if (width < bitmapWidth || height < bitmapHeight) { final float ratio = (float) bitmapWidth / bitmapHeight; if (bitmapWidth > bitmapHeight) { height = (int) (width / ratio); } else if (bitmapHeight > bitmapWidth) { width = (int) (height * ratio); } final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ? bitmap.getConfig() : Bitmap.Config.ARGB_8888; final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); final Canvas canvas = sCanvas; final Paint paint = sPaint; canvas.setBitmap(thumb); paint.setDither(false); paint.setFilterBitmap(true); sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height); sOldBounds.set(0, 0, bitmapWidth, bitmapHeight); canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint); return thumb; } else if (bitmapWidth < width || bitmapHeight < height) { final Bitmap.Config c = Bitmap.Config.ARGB_8888; final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); final Canvas canvas = sCanvas; final Paint paint = sPaint; canvas.setBitmap(thumb); paint.setDither(false); paint.setFilterBitmap(true); canvas.drawBitmap(bitmap, (sIconWidth - bitmapWidth) / 2, (sIconHeight - bitmapHeight) / 2, paint); return thumb; } } return bitmap; } }
//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.图片加载方法,方便用户加载图片 /*** * 加载本地图片 * @param context:主运行函数实例 * @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轴起点坐标 * @param y * :Y轴起点坐标 * @param w * :单一图片的宽度 * @param h * :单一图片的高度 * @param line * :第几列 * @param row * :第几行 */ 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.图片缩放,对当前图片进行缩放处理 /*** * 图片的缩放方法 * * @param bgimage * :源图片资源 * @param newWidth * :缩放后宽度 * @param newHeight * :缩放后高度 * @return */ 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.绘制带有边框的文字,一般在游戏中起文字的美化作用 /*** * 绘制带有边框的文字 * * @param strMsg * :绘制内容 * @param g * :画布 * @param paint * :画笔 * @param setx * ::X轴起始坐标 * @param sety * :Y轴的起始坐标 * @param fg * :前景色 * @param bg * :背景色 */ 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 图片透明度处理代码 /** * 图片透明度处理 * * @param sourceImg * 原始图片 * @param number * 透明度 * @return */ 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); /*翻转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; /** * * @author superdev * @version 1.0 * */ 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; } /** * 将Drawable转化为Bitmap */ 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 createReflectionImageWithOrigin(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; } }
private byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } private Bitmap Bytes2Bimap(byte[] b){ if(b.length!=0){ return BitmapFactory.decodeByteArray(b, 0, b.length); } else { return null; } } /** * create the bitmap from a byte array *生成水印图片 * @param src the bitmap object you want proecss * @param watermark the water mark above the src * @return return a bitmap object ,if paramter's length is 0,return null */ private Bitmap createBitmap( Bitmap src, Bitmap watermark ) { String tag = "createBitmap"; Log.d( tag, "create a new bitmap" ); if( src == null ) { return null; } int w = src.getWidth(); int h = src.getHeight(); int ww = watermark.getWidth(); int wh = watermark.getHeight(); //create the new blank bitmap Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图 Canvas cv = new Canvas( newb ); //draw src into cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src //draw watermark into cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印 //save all clip cv.save( Canvas.ALL_SAVE_FLAG );//保存 //store cv.restore();//存储 return newb; } /** 重新编码Bitmap * * @param src * 需要重新编码的Bitmap * * @param format * 编码后的格式(目前只支持png和jpeg这两种格式) * * @param quality * 重新生成后的bitmap的质量 * * @return * 返回重新生成后的bitmap */ private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format, int quality) { ByteArrayOutputStream os = new ByteArrayOutputStream(); src.compress(format, quality, os); byte[] array = os.toByteArray(); return BitmapFactory.decodeByteArray(array, 0, array.length); } //Stream转换成Byte static byte[] streamToBytes(InputStream is) { ByteArrayOutputStream os = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int len; try { while ((len = is.read(buffer)) >= 0) { os.write(buffer, 0, len); } } catch (java.io.IOException e) { } return os.toByteArray(); } //把View转换成Bitmap /** * 把一个View的对象转换成bitmap */ static Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); //能画缓存就返回false boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
读取raw资源文件中的mp3文件,然后通过音乐播放器播放: /** * 把mp3文件写入卡 * * @param fileName * 输出的文件名(全路径) * @param context * context对象 */ private void writeMP3ToSDcard(String fileName, Context context) { byte[] buffer = new byte[1024 * 8]; int read; BufferedInputStream bin = new BufferedInputStream(context.getResources().openRawResource(R.raw.ring)); try { BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(fileName)); while ((read = bin.read(buffer)) > -1) { bout.write(buffer, 0, read); } bout.flush(); bout.close(); bin.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(newFile("XXXXmp3的文件全路径")),"audio/*"); startActivity(intent);
绘制图像倒影
private void _Init() { m_paint = new Paint(Paint.ANTI_ALIAS_FLAG); LinearGradient lg = new LinearGradient( 0, 0, 0, m_nShadowH, 0xB0FFFFFF, 0x00000000, Shader.TileMode.CLAMP); m_paint.setShader(lg); m_paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int nX = 0; int nY = 20; _DrawNormalImg(canvas, nX, nY); _DrawMirror(canvas, nX, nY); } private void _DrawNormalImg(Canvas canvas, int nX, int nY) { canvas.save(Canvas.MATRIX_SAVE_FLAG); canvas.translate(nX, nY); m_dw.draw(canvas); canvas.restore(); } private void _DrawMirror(Canvas canvas, int nX, int nY) { int nW = m_dw.getIntrinsicWidth(); int nH = m_dw.getIntrinsicHeight(); /////////////////////////////////// //draw mirror image canvas.save(Canvas.MATRIX_SAVE_FLAG); canvas.scale(1.0f, -1.0f); canvas.translate(nX, -(nY + nH * 2)); canvas.clipRect(0, nH, nW, nH - m_nShadowH); m_dw.draw(canvas); canvas.restore(); ////////////////////////////// //draw mask canvas.save(); canvas.translate(nX, nY + nH); canvas.drawRect(0, 0, nW, m_nShadowH, m_paint); canvas.restore(); }
Android 繪圖座標體系預設的原點在左上角,X 軸往右是越來越大的正值,而 Y 軸往下,則是越來越大的正值。要畫出垂直翻轉的圖片,其實也就是要垂直翻轉整個繪圖座標體系。在 Android 中,要如何做?答案就是 canvas.scale(1.0f, -1.0f)。很簡單吧,沒想到給 scale() 函式一個負值,就可以翻轉相對應的軸。
在 Photoshop 中,做鏡像特效的第二步是要對這翻轉的圖片,加個由灰到黑的漸層 mask。
在 Android 中,要畫漸層色,那就一定得用 LinearGradient 這個類別。至於要對背景圖加上個 mask,就請參考一下 Paint 的 setXfermode() 函式。_Init() 這個函式,就是負責生成一個由灰到黑漸層 mask 的 m_paint 物件。
http://blog.csdn.net/Android_Tutor/archive/2010/11/02/5981753.aspx
http://www.cnblogs.com/TerryBlog/archive/2012/01/08/2316482.html
android常用图片特效处理
http://www.eoeandroid.com/thread-170526-1-1.html
相机(Camera)实时滤镜效果
http://www.eoeandroid.com/thread-171528-1-1.html
Android, WindowsPhone7, IOS ,vc2010平台40多套图片滤镜开源
http://www.cnblogs.com/daizhj/archive/2012/05/15/2501406.html
Android实现获取本机中所有图片
http://www.cnblogs.com/hanyonglu/archive/2012/05/10/2494908.html
Android 图片处理工具类封装
http://www.open-open.com/lib/view/open1386682905126.html
评论
13 楼
zi413293813
2016-03-04
nice!
12 楼
习习xi
2014-12-11
楼主,第一种压缩图片的质量,那么图片的宽高会不会改变
11 楼
syw19901001
2014-12-08
推荐11款最佳Android平台图像处理工具
http://www.ihref.com/read-14611.html
http://www.ihref.com/read-14611.html
10 楼
miquan
2014-12-08
//1.浮点算法:Gray=R*0.3+G*0.59+B*0.11 // grey = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11); //2.整数方法:Gray=(R*30+G*59+B*11)/100 // grey = (red*30+green*59+blue*11)/100; //3.移位方法:Gray =(R*28+G*151+B*77)>>8; // grey = (red*28+green*151+blue*77)>>8; //4.平均值法:Gray=(R+G+B)/3; // grey = (red+green+blue)/3; //5.仅取绿色:Gray=G; grey = green;
第五种应该是最有效率的
9 楼
hanyhys
2014-09-29
支持楼主啊,方便大家
8 楼
lmt1993
2014-09-10
帮大忙了 谢谢分享
7 楼
panwen79
2014-04-23
6 楼
亚当爱上java
2013-06-25
good guy !
5 楼
yye_javaeye
2013-03-26
收了,多谢!
4 楼
gangbener
2012-08-22
生成缩略图功能图片会模糊。
3 楼
yzongjie
2012-03-01
,楼主辛苦了,谢谢分享
2 楼
geek87
2011-12-25
绝对要支持一下,楼主辛苦了
1 楼
JACKDG2010
2011-09-04
发表评论
-
http://www.android-studio.org/
2018-08-06 09:25 0http://www.android-studio.org/ ... -
SlidingDrawer源码
2012-03-14 10:13 3805我把SlidingDrawer源码提了出来,希望对1.5的朋友 ... -
简单拖动效果(带Cache,需要完善)
2011-10-13 15:10 4227如何去实现一个具有幻象的拖拽效果? 所谓”幻象“就是当你按下去 ... -
Android Activity中启动另一应用程序的方法,无需得到类名
2011-08-02 14:46 17256在网上搜索了一会相关的实现代码,发现所有的文章都说是需要包名和 ... -
java-universal-tween-engine,一个动画系统库
2011-06-29 09:21 6742http://code.google.com/p/java-u ... -
网上发现的一个android UI包
2011-05-24 12:21 4093里面有些UI和效果 -
android中使用代码启动其他程序
2011-04-29 23:15 5285你要訪問其他的程序,那麼這個程序要先裝載到模擬器或真機上面,因 ... -
listView背景问题以及限制editText字数以及如果想通知别人已经不能在写
2011-04-29 22:44 32091.在listView设置好背景之后 你如果点击空白出 你会发 ... -
Android键盘和触摸事件处理
2011-04-29 22:32 7003activity和VIEW都能接收触摸和按键,如果响应事件只需 ... -
Android的绘制文本对象FontMetrics的介绍及绘制文本
2011-04-29 22:29 11489一。Android绘制文本对象FontMetrics介绍 ... -
Android View 拖动&插入
2011-04-29 22:20 3546View 拖动&插入 即: 支持 拖动图标 然后 ... -
使TextView文本可以水平和垂直滚动
2011-04-29 21:59 14426在做一个小的电子书程序,要求电子书具有放大缩小的功能,所以肯定 ... -
ArrayAdapter源码
2011-04-29 12:29 6300看看人家怎么写的。 /* * Copyright (C ... -
Android下获取开机时间
2011-04-02 21:51 6226找了一圈没发现能得到开机启动时间资料,于是乎突发奇想,得到了解 ... -
AutoCompleteTextView连接到数据库
2011-03-30 20:49 4727AutoCompleteTextView可以根据输入 ... -
改变屏幕Brightness(亮度)
2011-03-30 12:48 4603http://www.eoeandroid.com/forum ... -
android 拖拽图片&拖动浮动按钮到处跑
2011-02-24 20:55 31771来自老外: import android.app.Acti ... -
拖动一个控件在另一个控件(layout)上,并固定位置在几个位置显示
2011-02-24 20:51 5893实现效果: 鼠标拖动btn SSS,SSS在水平的layo ... -
Handler与Message类,实现n秒后无操作自动消失功能
2011-02-24 20:45 4638实现功能:某控件不操作10秒后,自动消失。如照相机变焦条出现后 ... -
带删除按钮的ListView
2011-02-24 10:33 6146不用说了,上图先: import java.util.A ...
相关推荐
在Android开发中,自定义控件和框架的运用是提升应用独特性和性能的关键。下面将对"android 自定义控件实现demo收集 及 框架收集"这一主题进行深入探讨。 首先,自定义控件在Android应用开发中扮演着重要角色。它们...
以上只是部分Android开发中的核心知识点,实际的压缩包可能还包含更深入的主题,如Material Design设计规范、Android Jetpack组件、Android KTX简化代码库、Android Room数据库框架、Android WorkManager处理后台...
14. **多媒体支持**: 支持音频、视频播放和图片处理,利用MediaStore、ExoPlayer、 Glide或Picasso等库。 15. **Google Play服务**: 提供定位、Google Maps、Google Sign-In等功能,大大丰富了Android应用的功能。 ...
随着用户反馈的收集,不断迭代更新,提升游戏体验。 以上就是开发Android仿微信摇骰子游戏的主要技术点,每个环节都需要精心设计和实现,才能打造出一款既美观又好玩的应用。通过这个项目,开发者不仅可以提升...
本文将深入探讨Android的基本控件及其使用方法,这对初学者来说是非常重要的基础知识。 1. **TextView**:TextView是最基础的文本展示控件,用于显示单行或多行文本。可以设置文本内容、字体样式、颜色、大小等属性...
3. **车牌定位**:在识别之前,系统需先定位图片中的车牌位置。这通常通过边缘检测和连通组件分析来实现。Canny边缘检测和霍夫变换是常用的定位方法。 4. **字符分割**:找到车牌后,需要将车牌上的字符分开。这...
接下来,我们将详细解析在开发Android图片浏览器过程中可能遇到的核心概念、技术细节以及最佳实践。 ### 核心概念 1. **Android SDK(Software Development Kit)**:这是进行Android应用开发的基础工具包,包含了...
在多媒体设计开发中,学习如何播放音频、视频,处理图片,并利用Android的多媒体API。互联网应用开发涉及网络编程接口,如Socket、URL、HttpClient等,用于实现网络通信。GPS定位应用开发则需要熟悉Android的定位API...
在移动开发领域,特别是针对Android平台的应用开发过程中,性能优化是确保应用流畅运行、提升用户体验的关键环节。本篇文章将围绕“Android应用性能优化”这一主题,详细探讨Android应用架构的设计原则、常见性能...
这50个项目的收集,无疑为Android开发者提供了一个丰富的学习库,可以从实际案例中汲取知识。 1. **Android基础知识**:通过分析源码,你可以深入了解Android应用程序的基础架构,包括Activity、Service、...
在Android应用开发中,用户体验是至关重要的,而意见反馈界面则是用户与...在游戏应用中,一个精心设计的反馈界面不仅可以提升玩家的满意度,还能帮助开发者更好地收集和理解用户的需求,从而不断改进和优化产品。
1. **滑动事件处理**:在Android中,滑动事件主要通过MotionEvent和GestureDetector类来捕获和处理。开发者需要监听ACTION_DOWN、ACTION_MOVE和ACTION_UP等触摸事件,根据手指在屏幕上的移动轨迹计算滑动距离和速度...
4. **切换主题的逻辑**:在代码中,开发者需要实现一个方法来处理用户选择皮肤的事件。这可能包括读取用户首选项,确定当前选择的皮肤,然后调用相应的资源加载机制。 5. **布局文件引用**:在布局XML文件中,...
- **JSON序列化与反序列化**:理解如何在Android中处理JSON数据。 5. **网络编程**: - **AsyncTask**:基础的异步处理机制,用于短时间的后台操作。 - **Volley**和**Retrofit**:高效的网络请求库,处理网络...
在Android开发中,实现相册功能是一项常见的任务,它涉及到图片的选择、预览以及上传等操作。本篇文章将深入探讨如何在Android应用中构建一个基本的相册功能,结合源码分析和工具的使用。 首先,我们需要理解...
开发者需要理解Android的生命周期管理,以及如何在服务中正确地处理资源和内存。 6. **性能优化**: 动态壁纸需要在后台持续运行,因此性能优化至关重要。避免不必要的计算,使用高效的绘图方法,以及合理管理内存...
支付宝作为一款国民级的应用,在不断迭代的过程中面临着巨大的挑战。随着移动互联网的发展,无线化的趋势使得支付宝必须快速响应市场变化,推进各项新业务。同时,庞大的用户基数带来了前所未见的技术难题:如何在...
同时,持续收集用户反馈,不断优化和改进应用设计。 综上所述,"ANDROID 设计招式之美"涵盖了从设计原则到用户体验优化的全面内容,旨在帮助开发者和设计师打造出色、高效的Android应用。理解和掌握这些知识点,将...
在Android应用程序开发中,用户反馈系统是至关重要的,它能帮助开发者收集用户的建议和问题,以便不断优化和改进产品。本项目提供了一种利用POST请求实现Android应用内向服务器发送反馈信息的方法,并在服务器端使用...