App
SimpleBitmapView.java
Go to the documentation of this file.
1 package mhr.appandroid.views;
2 
3 import android.content.Context;
4 import android.graphics.Bitmap;
5 import android.graphics.Canvas;
6 import android.graphics.Paint;
7 import android.graphics.Rect;
8 import android.util.AttributeSet;
9 import android.view.View;
10 
14 public class SimpleBitmapView extends View {
15  //===== INTERFACES, CLASSES, ENUMS ==========================================================================================================//
16  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
17  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
18 
19  //===== FIELDS ==============================================================================================================================//
20  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
21  protected Paint background;
22  protected Paint foreground;
23  protected Bitmap bitmap = null;
24  protected Rect dstRect = null;
25 
26  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
27 
28  //===== CONSTRUCTORS, DESTRUCTORS, RELATED METHODS ==========================================================================================//
29  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
33  protected void init() {
34  background = new Paint();
35  background.setColor(0xFFFFFFFF);
36  foreground = new Paint();
37  foreground.setColor(0xFF000000);
38  }
39 
40  @Override
41  protected void onDetachedFromWindow() {
42  // ignorovano kvuli male velikosti bitmapy a drag&drop, navic nebude vlastnikem bitmapy, ta bude sdilena
43  }
44 
45  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
46  public SimpleBitmapView(Context context) {
47  super(context);
48  init();
49  }
50 
51  public SimpleBitmapView(Context context, AttributeSet attrs) {
52  super(context, attrs);
53  init();
54  }
55 
56  public SimpleBitmapView(Context context, AttributeSet attrs, int defStyle) {
57  super(context, attrs, defStyle);
58  init();
59  }
60 
61  //===== METHODS =============================================================================================================================//
62  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
63  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
64 
69  public Paint getBitmapBackground() {
70  return background;
71  }
72 
77  public Paint getBitmapForeground() {
78  return foreground;
79  }
80 
85  public void setBitmapForeground(Paint paint) {
86  foreground = paint;
87  }
88 
93  public void setBitmap(Bitmap bitmap) {
94  this.bitmap = bitmap;
95  invalidate();
96  }
97 
102  public Rect getDstRect() {
103  return new Rect(dstRect);
104  }
105 
106  @Override
107  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
108  super.onSizeChanged(w, h, oldw, oldh);
109  dstRect = new Rect(0 + getPaddingLeft(), 0 + getPaddingRight(), w - getPaddingRight(), h - getPaddingBottom());
110  }
111 
112  @Override
113  protected void onDraw(Canvas canvas) {
114  canvas.drawRect(dstRect, background);
115  if (bitmap != null) {
116  canvas.drawBitmap(bitmap, null, dstRect, foreground);
117  }
118  }
119 
120 }