ViewUtils.java 3.76 KB
package com.gimi.common.cinema.utils;

import android.content.Context;
import android.graphics.*;
import android.util.Log;
import android.view.View;
import com.xgimi.gimicinema.R;

/**
 * Created by 李攀 on 2015/10/8.
 */
public class ViewUtils {
    public static Bitmap getViewBitmap(View v, Matrix matrix,Context context) {
//        v.clearFocus();
        v.setPressed(false);

        boolean willNotCache = v.willNotCacheDrawing();
        v.setWillNotCacheDrawing(false);

        // Reset the drawing cache background color to fully transparent
        // for the duration of this operation
        int color = v.getDrawingCacheBackgroundColor();
        v.setDrawingCacheBackgroundColor(0);

        if (color != 0) {
            v.destroyDrawingCache();
        }
        v.buildDrawingCache();
        Bitmap cacheBitmap = v.getDrawingCache();
        if (cacheBitmap == null) {
            Log.e("Folder", "failed getViewBitmap(" + v + ")", new RuntimeException());
            return null;
        }
        Bitmap bitmap;

        if (matrix != null) {
            bitmap = Bitmap.createBitmap(cacheBitmap, 0, 0, cacheBitmap.getWidth(), cacheBitmap.getHeight(), matrix, false);
        } else {
            bitmap = Bitmap.createBitmap(cacheBitmap);
        }
//        Matrix matrix = new Matrix();
//        int width =bitmap.getWidth();
//        int height = bitmap.getHeight();
//        matrix.setScale(0.5f, 0.5f);
//        mNewBitmap=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);

        // Restore the view
        v.destroyDrawingCache();
        v.setWillNotCacheDrawing(willNotCache);
        v.setDrawingCacheBackgroundColor(color);
        return /*drawImageDropShadow(bitmap,context);
//        */ bitmap;
    }

    /**

     * add shadow to bitmap
     *
     * @param originalBitmap
     * @return
     */
    private static Bitmap drawImageDropShadow(Bitmap originalBitmap, Context context) {

        BlurMaskFilter blurFilter = new BlurMaskFilter(1,
                BlurMaskFilter.Blur.NORMAL);
        Paint shadowPaint = new Paint();
        shadowPaint.setAlpha(50);
        shadowPaint.setColor(context.getResources().getColor(R.color.dark_background));
        shadowPaint.setMaskFilter(blurFilter);
        Shader mShader = new LinearGradient(0,0,40,60,
                new int[] {Color.RED,Color.GREEN,Color.BLUE},null,Shader.TileMode.REPEAT);

        int[] offsetXY = new int[2];
        Bitmap shadowBitmap = originalBitmap
                .extractAlpha(shadowPaint, offsetXY);

        Bitmap shadowImage32 = shadowBitmap.copy(Bitmap.Config.ARGB_8888, true);
        Canvas c = new Canvas(shadowImage32);
        c.drawBitmap(originalBitmap, offsetXY[0], offsetXY[1], null);

        return shadowImage32;
    }

    public static Bitmap getScaleBitmapDrawable(Context context,int id,int dstWidth,int dstHeight){
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id);

        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int newWidth = dstWidth;
        int newHeight = dstHeight;

        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        // create the new Bitmap object
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,
                height, matrix, true);
        return  resizedBitmap;
    }

    public static int[] getSize(View view) {
        int[] size = new int[2];
        size[0] = view.getMeasuredWidth();
        size[1] = view.getMeasuredHeight();
        return size;
    }

    public static int[] getPosition(View view) {
        int[] size = new int[2];
        view.getLocationOnScreen(size);
        return size;
    }
}