View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   * This image view for attached image in compose message activity
39   * 
40   * @since Jan 16, 2012 
41   * @author Created by The eXo Platform SAS 
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      // Draw in the original image with transparent
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      // Get the image bitmap frame
95      Bitmap maskBm = BitmapFactory.decodeResource(getResources(), R.drawable.social_attached_image_border);
96      // Create nine patch drawable from image bitmap frame
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       // get the original bitmap drawable
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       // Draw this bitmap to the image view
120       canvas.drawBitmap(fullSizeBitmap, 0, 0, null);
121 
122       NinePatchDrawable nicepatchDrawable = getCachedNinePatchDrawable(width, height);
123 
124       // Draw the NinePatchDrawable to a new output_bitmap
125       Bitmap output_bitmap = getCachedBitmap(width, height);
126 
127       canvasT = getCachedCanvas(width, height);
128 
129       nicepatchDrawable.draw(canvasT);
130       // Draw the output_bitmap to image view
131       canvas.drawBitmap(output_bitmap, 0, 0, null);
132     } catch (OutOfMemoryError e) {
133       // if (greendroid.util.Config.GD_ERROR_LOGS_ENABLED)
134       Log.e("RetangleImageView", e.getMessage());
135     }
136   }
137 
138 }