现在的位置: 首页 > 综合 > 正文

android图片处理技巧

2013年10月10日 ⁄ 综合 ⁄ 共 2390字 ⁄ 字号 评论关闭
package com.img.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;

public class ImgUtil {
    
// 放大缩小图片
    public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
        
int width = bitmap.getWidth();
        
int height = bitmap.getHeight();
        Matrix matrix 
= new Matrix();
        
float scaleWidth = ((float) w / width);
        
float scaleHeight = ((float) h / height);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newBmp 
= Bitmap.createBitmap(bitmap, 00, 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(
00, 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(00, bitmap.getWidth(), bitmap.getHeight());
        
final RectF rectF = new RectF(rect);

        paint.setAntiAlias(
true);
        canvas.drawARGB(
0000);
        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 =

抱歉!评论已关闭.