android 图片处理大全
16lz
2021-01-24
http://06peng.com/read.php/52.htm
原帖地址~~非常好~
原帖地址~~非常好~
Android 图片处理方法大全
Android编程 , 评论(3) , 引用(0) , 阅读(1577) 大 | 中 | 小整理了一下目前Android开发中图片的各种处理方法:
Java代码- /**
- * 使头像变灰
- * @param drawable
- */
- public static void porBecomeGrey(ImageView imageView, Drawable drawable) {
- drawable.mutate();
- ColorMatrix cm = new ColorMatrix();
- cm.setSaturation(0);
- ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);
- drawable.setColorFilter(cf);
- imageView.setImageDrawable(drawable);
- }
Java 代码 复制内容到剪贴板
- Drawable drawable = new FastBitmapDrawable(bitmap);
- public byte[] getBitmapByte(Bitmap bitmap){
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
- try {
- out.flush();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return out.toByteArray();
- }
- public Bitmap getBitmapFromByte(byte[] temp){
- if(temp != null){
- Bitmap bitmap = BitmapFactory.decodeByteArray(temp, 0, temp.length);
- return bitmap;
- }else{
- return null;
- }
- }
- /**
- * 将Drawable转化为Bitmap
- * @param drawable
- * @return
- */
- 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;
- }
- /**
- * 获取图片的倒影
- * @param bitmap
- * @return
- */
- 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);
- paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
- canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
- + reflectionGap, paint);
- return bitmapWithReflection;
- }
- /**
- * 把图片变成圆角
- * @param bitmap 需要修改的图片
- * @param pixels 圆角的弧度
- * @return 圆角图片
- */
- public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
- 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);
- final float roundPx = pixels;
- 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;
- }
- /**
- * 缩放图片
- * @param bmp
- * @param width
- * @param height
- * @return
- */
- public static Bitmap PicZoom(Bitmap bmp, int width, int height) {
- int bmpWidth = bmp.getWidth();
- int bmpHeght = bmp.getHeight();
- Matrix matrix = new Matrix();
- matrix.postScale((float) width / bmpWidth, (float) height / bmpHeght);
- return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeght, matrix, true);
- }
- /**
- * @param photoPath --原图路经
- * @param aFile --保存缩图
- * @param newWidth --缩图宽度
- * @param newHeight --缩图高度
- */
- public static boolean bitmapToFile(String photoPath, File aFile, int newWidth, int newHeight) {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- // 获取这个图片的宽和高
- Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
- options.inJustDecodeBounds = false;
- //计算缩放比
- options.inSampleSize = reckonThumbnail(options.outWidth, options.outHeight, newWidth, newHeight);
- bitmap = BitmapFactory.decodeFile(photoPath, options);
- try {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- byte[] photoBytes = baos.toByteArray();
- if (aFile.exists()) {
- aFile.delete();
- }
- aFile.createNewFile();
- FileOutputStream fos = new FileOutputStream(aFile);
- fos.write(photoBytes);
- fos.flush();
- fos.close();
- return true;
- } catch (Exception e1) {
- e1.printStackTrace();
- if (aFile.exists()) {
- aFile.delete();
- }
- Log.e("Bitmap To File Fail", e1.toString());
- return false;
- }
- }
- /**
- * 计算缩放比
- * @param oldWidth
- * @param oldHeight
- * @param newWidth
- * @param newHeight
- * @return
- */
- public static int reckonThumbnail(int oldWidth, int oldHeight, int newWidth, int newHeight) {
- if ((oldHeight > newHeight && oldWidth > newWidth)
- || (oldHeight <= newHeight && oldWidth > newWidth)) {
- int be = (int) (oldWidth / (float) newWidth);
- if (be <= 1)
- be = 1;
- return be;
- } else if (oldHeight > newHeight && oldWidth <= newWidth) {
- int be = (int) (oldHeight / (float) newHeight);
- if (be <= 1)
- be = 1;
- return be;
- }
- return 1;
- }
Android边框圆角
XML/HTML 代码 复制内容到剪贴板- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schema...android">
- <solid android:color="#000000" />
- <corners android:topLeftRadius="10dp"
- android:topRightRadius="10dp"
- android:bottomRightRadius="10dp"
- android:bottomLeftRadius="10dp"/>
- shape>
解释:solid的表示填充颜色,为了简单,这里用的是黑色。
而corners则是表示圆角,注意的是这里bottomRightRadius是左下角而不是右下角,bottomLeftRadius右下角。
当然上面的效果也可以像下面一样设置,如下:
源码下载:
下载文件 点击这里下载文件
更多相关文章
- Eclipse,到了说再见的时候了——Android(安卓)Studio最全解析
- windows环境下 android 源码阅读
- Android中的HTTP通信
- 【代码】android通过criteria选择合适的地理位置服务
- android2.2更新为android2.3
- android 调用系统计算器 实例
- android 自定义viewR.styleable找不到或者是报错!
- 安卓在代码中设置TextView的drawableLeft、drawableRight、drawa
- Android注解——Butter Knife的使用
随机推荐
- Android(安卓)Parcelable代码自动生成插
- (Android学习之路)Android中listView结合
- Android(安卓)ConstraintLayout完全解析
- 启动Activity时的方法调用(应用层)(MVC模式)
- Android:BaseAdapter的优化方案一览
- android ndk返回String(字符串)
- 【Android】java.lang.IllegalArgumentEx
- Android(安卓)Studio 4.0 gradle-6.1.1-a
- android把odex转成dex文件
- Android(安卓)List Background and item