Last active
December 17, 2015 11:58
-
-
Save dmitry-zaitsev/5605839 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package robotograph; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.PorterDuff; | |
import android.graphics.PorterDuffXfermode; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
public class CountImageView extends ImageView { | |
private Paint mCirclePaint; | |
private Paint mTextPaint; | |
private int mRadius = 0; | |
private Bitmap mXferBitmap; | |
private Canvas mXferCanvas; | |
private String mText = "150"; | |
public CountImageView(Context context) { | |
super(context); | |
initView(); | |
} | |
public CountImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
initView(); | |
} | |
public CountImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
initView(); | |
} | |
private void initView() { | |
/* | |
* Here we are initializing paintings for further use in | |
* onDraw method. | |
*/ | |
/* | |
* We are using anti-alias (AA) to draw smooth shapes. Without | |
* AA there will be noises on edges of text or circle. | |
*/ | |
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
mCirclePaint.setStyle(Paint.Style.FILL); | |
mCirclePaint.setColor(0x88ffffff); //hardcoded - move this to XML | |
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
mTextPaint.setColor(Color.BLACK); | |
mTextPaint.setTextAlign(Paint.Align.CENTER); | |
/* | |
* We are using Porter-Duff Xfer mode CLEAR, to "erase" | |
* area under text on our translucent-white circle. | |
*/ | |
mTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); | |
} | |
public void setText(String text) { | |
mText = text; | |
} | |
public String getText() { | |
return mText; | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
final int width = getMeasuredWidth(); | |
final int height = getMeasuredHeight(); | |
/* | |
* Create bitmap for working with Xfer paintings | |
*/ | |
if(mXferBitmap == null || mXferBitmap.getWidth() != width | |
|| mXferBitmap.getHeight() != height) { | |
if(mXferBitmap != null) { | |
/* | |
* Don't forget to cleanup, if we don't need | |
* bitmap anymore | |
*/ | |
mXferBitmap.recycle(); | |
} | |
mXferBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); | |
mXferCanvas = new Canvas(mXferBitmap); | |
} | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
mRadius = w > h ? h/2 : w/2; | |
mRadius *= 0.95; | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
final int width = getWidth(); | |
final int height = getHeight(); | |
mTextPaint.setTextSize(mRadius); | |
mXferCanvas.drawCircle(width/2, height/2, mRadius, mCirclePaint); | |
mXferCanvas.drawText(mText, width/2, height/2 - (mTextPaint.descent() + mTextPaint.ascent())/2, mTextPaint); | |
canvas.drawBitmap(mXferBitmap, 0, 0, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment