android图形处理相关

/**

*缩放图片

*/

public static Bitmap scaleIcon(Bitmap bitmap, double width,

double height) {
if (bitmap != null) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) width / w);
float scaleHeight = ((float) height / h);
matrix.postScale(scaleWidth, scaleHeight);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
}
return bitmap;

}

/**

*得到某个像素的色值

*/

bitmap.getPixel(x, h3) == 0x00000000

/**
* 按rect r剪切图片
* @param mBitmap
* @param r
* @param config
* @return
*/
public static Bitmap cutBitmap(Bitmap mBitmap, Rect r, Bitmap.Config config) {
       int width = r.width();
       int height = r.height();
       Bitmap croppedImage = Bitmap.createBitmap(width, height, config);
       Canvas cvs = new Canvas(croppedImage);
       Rect dr = new Rect(0, 0, width, height);
       cvs.drawBitmap(mBitmap, r, dr, null);
       return croppedImage;
   }

/**
* 划圆角矩形
* @param source
* @param bg
* @return
*/
public static Bitmap createRoundImg(Bitmap source,Bitmap bg,int roundDegree)  
{  
    final Paint paint = new Paint();  
    paint.setAntiAlias(true);  
    Bitmap target = Bitmap.createBitmap(bg.getWidth(), bg.getHeight(), Config.ARGB_8888);  
    Canvas canvas = new Canvas(target);  
    RectF rect = new RectF(0, 0, bg.getWidth(), bg.getHeight());  
    canvas.drawRoundRect(rect, roundDegree, roundDegree, paint);  
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));  
    canvas.drawBitmap(source, 0-Math.abs(source.getWidth()-bg.getWidth())/2, 0-Math.abs(source.getHeight()-bg.getHeight())/2, paint);  
    return target;  


/**
* 创建渐变图片
* @param w
* @param h
* @param colorV
* @return
*/
public static Bitmap createLinearGradientImg(int w ,int h,int colorV){
Bitmap background = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas backCv = new Canvas(background);
/*设置渐变色*/
          Shader mShader=new LinearGradient(0,0,w,h,
                  new int[]{(colorV<=0xffffffff&&colorV>=0xffeeeeee)?0x22dddddd:0x22ffffff,Integer.parseInt(String.valueOf(colorV).replace("0xff", "0x22"))},
                  null,Shader.TileMode.CLAMP);
          Paint paint=new Paint();
          paint.setShader(mShader);
          backCv.drawRect(0, 0, w, h, paint);
          return background;
}

/**
 * 获取屏幕密度
 */

public static float getDensity(Context context) {
if (context instanceof Activity) {
DisplayMetrics metric = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay()
.getMetrics(metric);
return metric.density;
} else
throw new IllegalArgumentException(
"context can not be cast to Activity");
}

/**
 * 获取屏幕分辨率
 */

public static int[] getDisplayMetrics(Context context) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
return new int[] { dm.heightPixels, dm.widthPixels };
}


猜你喜欢

转载自blog.csdn.net/hualizide/article/details/44200141