android - create a button programatically with sharp edge containing image -


i wanted create custom button or view progrmatically simple image , text shown in image, edge of button not of image.

please don't use xml.

any appreciated. wanted learn , create custom view canvas since new 1 canvas, not able create it.

enter image description here

import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.rect;  import android.util.attributeset; import android.view.view;  public class containerbox extends view {     private paint textpaint;     private string maintext="vikram singh";     private string backgroundcolour = "#ff8514";     private string textcolour = "#1896bb";  private bitmap lefticon; private paint paintbackground; private rect recbackground;  private paint paintimage ; private rect recimage;  public containerbox(context context) {     super(context);     initializepaints(context); }  public containerbox(context context, attributeset attrs) {     super(context, attrs);     initializepaints(context); }  public containerbox(context context, attributeset attrs, int defstyleattr) {     super(context, attrs, defstyleattr);     initializepaints(context); }  private void initializepaints(context context) {     lefticon = bitmapfactory.decoderesource(getresources(), r.drawable.icon_done);      paintimage = new paint();     paintimage.setcolor(color.parsecolor(backgroundcolour));     paintimage.setstrokewidth(3);     paintimage.setantialias(true);     paintimage.setstyle(paint.style.fill);      paintbackground = new paint();     paintbackground.setcolor(color.parsecolor(backgroundcolour));     paintbackground.setstrokewidth(3);     paintbackground.setantialias(true);     paintbackground.setstyle(paint.style.fill);      textpaint = new paint();     textpaint.setcolor(color.parsecolor(textcolour));     textpaint.setantialias(true);     textpaint.settextsize(4);     textpaint.setstyle(paint.style.stroke); }  @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {     super.onmeasure(widthmeasurespec, heightmeasurespec);     setmeasureddimension(getmeasuredwidth(),lefticon.getheight()+40); }  @override protected void ondraw(canvas canvas) {     int width = getwidth();     int height = lefticon.getheight()+40;      int differenceheight=height-25;     int differencewidth=width-lefticon.getwidth()+15;     recbackground=new rect(0,25,differencewidth,differenceheight);      canvas.drawrect(recbackground,paintbackground);      textpaint.settextsize(15f);     float textwidth = textpaint.measuretext(maintext);     int x = (int) ((recbackground.width() - textwidth) / 2);     int y = (int) ((recbackground.centery() - (textpaint.descent() + textpaint.ascent())/2));     // draw text     canvas.drawtext(maintext, x, y, textpaint);       recimage=new rect(recbackground.right,0,width,height);     canvas.drawrect(recimage,paintimage);      int left=recimage.width()/2-lefticon.getwidth()/2;     int top=recimage.height()/2-lefticon.getheight()/2;     canvas.drawbitmap(lefticon,recimage.left,top,paintimage);     super.ondraw(canvas); } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -