/**

* 使头像变灰

* @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);

}


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

*/


1.

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 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右下角。
当然上面的效果也可以像下面一样设置,如下: <corners android:radius="5dp" />






更多相关文章

  1. Android中使用TabHost实现类似标签栏的效果
  2. android 自定义相册 多选
  3. android 多点触摸 放大 缩小 图片
  4. android加载本地图片
  5. Android(安卓)图片转成String保存
  6. android 图片的压缩
  7. 自定义ProgressBar的图片
  8. android 拖拽图片&拖动浮动按钮到处跑
  9. Android(安卓)Bitmap用法大全,以后再也不担心了

随机推荐

  1. android自动更新程序,安装完以后就什么都
  2. Android 扩展屏幕 实现主屏副屏同步或者
  3. Android的网络与通信
  4. Android下pm命令详解
  5. android 单元测试cmd 命令集
  6. Rexsee API介绍:Android屏幕锁定,Keyguard
  7. 在Service中新开线程和直接新开线程的区
  8. ratingbar 的使用
  9. 针对 CoordinatorLayout 及 Behavior 的
  10. Android开机log分析