ViewUtils.java
3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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;
}
}