1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.exoplatform.widget;
20
21 import org.exoplatform.R;
22
23 import android.content.Context;
24 import android.graphics.Bitmap;
25 import android.graphics.Bitmap.Config;
26 import android.graphics.BitmapFactory;
27 import android.graphics.Canvas;
28 import android.graphics.Color;
29 import android.graphics.Paint;
30 import android.graphics.Rect;
31 import android.graphics.drawable.BitmapDrawable;
32 import android.graphics.drawable.NinePatchDrawable;
33 import android.util.AttributeSet;
34 import android.util.Log;
35 import android.widget.ImageView;
36
37
38
39
40
41
42
43 public class RectangleImageView extends ImageView {
44
45 public RectangleImageView(Context context, AttributeSet attrs, int defStyle) {
46 super(context, attrs, defStyle);
47 }
48
49 public RectangleImageView(Context context, AttributeSet attrs) {
50 super(context, attrs);
51 }
52
53 public RectangleImageView(Context context) {
54 super(context);
55 }
56
57 private Bitmap _bitmap = null;
58
59 private Bitmap getCachedBitmap(int width, int height) {
60 if ((_bitmap != null) && (_bitmap.getWidth() == width) && (_bitmap.getHeight() == height))
61 return _bitmap;
62
63 _bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
64 return _bitmap;
65 }
66
67 private Canvas _canvasT = null;
68
69 private Canvas getCachedCanvas(int width, int height) {
70 if (_canvasT != null)
71 return _canvasT;
72 Log.i("RetangleImageView", "create new canvas");
73
74 _canvasT = new Canvas(getCachedBitmap(width, height));
75 return _canvasT;
76 }
77
78 private Canvas getDrawnCanvas(int width, int height, Bitmap fullSizeBitmap) {
79 Canvas canvas = getCachedCanvas(width, height);
80 Paint p = new Paint();
81 p.setColor(Color.TRANSPARENT);
82
83 canvas.drawBitmap(fullSizeBitmap, 0, 0, p);
84 return canvas;
85 }
86
87 private NinePatchDrawable _nicepatchDrawable = null;
88
89 private NinePatchDrawable getCachedNinePatchDrawable(int width, int height) {
90 if ((_nicepatchDrawable != null) && (_nicepatchDrawable.getBounds().height() == height)
91 && (_nicepatchDrawable.getBounds().width() == width))
92 return _nicepatchDrawable;
93
94
95 Bitmap maskBm = BitmapFactory.decodeResource(getResources(), R.drawable.social_attached_image_border);
96
97 byte[] chunk = maskBm.getNinePatchChunk();
98 _nicepatchDrawable = new NinePatchDrawable(getResources(), maskBm, chunk, new Rect(), null);
99 _nicepatchDrawable.setBounds(0, 0, width, height);
100 return _nicepatchDrawable;
101 }
102
103 @Override
104 protected void onDraw(Canvas canvas) {
105 try {
106
107 BitmapDrawable drawable = (BitmapDrawable) getDrawable();
108
109 if (drawable == null || getWidth() == 0 || getHeight() == 0) {
110 return;
111 }
112 Bitmap fullSizeBitmap = drawable.getBitmap();
113
114 int height = fullSizeBitmap.getHeight();
115 int width = fullSizeBitmap.getWidth();
116
117 Canvas canvasT = getDrawnCanvas(width, height, fullSizeBitmap);
118
119
120 canvas.drawBitmap(fullSizeBitmap, 0, 0, null);
121
122 NinePatchDrawable nicepatchDrawable = getCachedNinePatchDrawable(width, height);
123
124
125 Bitmap output_bitmap = getCachedBitmap(width, height);
126
127 canvasT = getCachedCanvas(width, height);
128
129 nicepatchDrawable.draw(canvasT);
130
131 canvas.drawBitmap(output_bitmap, 0, 0, null);
132 } catch (OutOfMemoryError e) {
133
134 Log.e("RetangleImageView", e.getMessage());
135 }
136 }
137
138 }