andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

[Tut] Stateful CustomImageButton with programatic Draw


 
       anddev.org - Android Development Community | Android Tutorials | Index -> Advanced Tutorials
Author Message
mauri
Junior Developer


Joined: 04 Dec 2007
Posts: 13

PostPosted: Thu Feb 14, 2008 5:40 pm    Post subject: [Tut] Stateful CustomImageButton with programatic Draw Reply with quote

Hi all

since the new SDK i'm not so happy with the buttons (grey instead of black in the Dark Theme). So decided to draw my own Buttons.

The are centenly more then one way to do this.
My goal is to draw the Image programticly, instead of producing bitmaps with a paint-programm.
Here's my way...

Java:

package org.anddev;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.Path.Direction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
/*
 * Sample CustomImageButton
 * Programatic draw a own Button, whit different States
 * default, focused and pressed
 *
 *  14.02.2008 by Mauri for anddev.org
 */

public class CustomImageButton extends Activity {
    private TextView mDebug;

     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
       
        // prepare the Layout
        // two Buttons
        // Debug-TextView Below
       
        LinearLayout linLayoutMain = new LinearLayout(this);
        linLayoutMain.setOrientation(LinearLayout.VERTICAL);
       
        LinearLayout linLayoutButtons = new LinearLayout(this);
        linLayoutButtons.setOrientation(LinearLayout.HORIZONTAL);
       
        // buttons
        MyCustomButton btn1 = new MyCustomButton(this, "btn1");
        MyCustomButton btn2 = new MyCustomButton(this, "btn2");
       
        // a TextView for debugging output
        mDebug = new TextView(this);
       
        // add button to the layout
        linLayoutButtons.addView(btn1,
               new LinearLayout.LayoutParams(100, 100));
        linLayoutButtons.addView(btn2,
               new LinearLayout.LayoutParams(100, 100));
       
        // add buttons layout and Text view to the Main Layout
        linLayoutMain.addView(linLayoutButtons,
               new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                         LayoutParams.WRAP_CONTENT));
        linLayoutMain.addView(mDebug,
               new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                         LayoutParams.WRAP_CONTENT));
       
        setContentView(linLayoutMain);
    }
   
   
    // Custom Button Class must extends Button,
    // because drawableStateChanged() is needed
    public class MyCustomButton extends Button {

     static final int StateDefault = 0;
     static final int StateFocused = 1;
     static final int StatePressed = 2;
     

     private int mState = StateDefault;
          private Bitmap mBitmapDefault;
          private Bitmap mBitmapFocused;
          private Bitmap mBitmapPressed;
          private String mCaption;

          public MyCustomButton(Context context, String caption) {
          super(context);
          mCaption = caption;
          
          setClickable(true);
          // black Background on the View
          setBackgroundColor(0xff000000);
          
          // create for each State a Bitmap
          // white Image
          mBitmapDefault = Bitmap.createBitmap(100, 100, true);
          // Blue Image
          mBitmapFocused = Bitmap.createBitmap(100, 100, true);
          // Green Image
          mBitmapPressed = Bitmap.createBitmap(100, 100, true);
          
          // create the Canvas
          Canvas canvas = new Canvas();
          
          // define on witch Bitmap should the Canvas draw
          // default Bitmap
          canvas.setDevice(mBitmapDefault);
          
          // create the Drawing Tool (Brush)
          Paint paint = new Paint();
          paint.setAntiAlias(true);  // for a nicer paint
          
          // draw a rectangle with rounded edges
          // white Line
          paint.setColor(0xffffffff);
          // 3px line width
          paint.setStrokeWidth(3);
          // just the line, not filled
          paint.setStyle(Style.STROKE);
          
          // create the Path
          Path path = new Path();
          // rectangle with 10 px Radius
          path.addRoundRect(new RectF(10, 10, 90, 90),
                    10, 10, Direction.CCW);
          
          // draw path on Canvas with the defined "brush"
          canvas.drawPath(path, paint);
          
          // prepare the "brush" for the Text
          Paint paintText = new Paint();
          paintText.setAntiAlias(true);
          paintText.setTextSize(20);
          paintText.setColor(0xffffffff);  // white
          
          // draw Text
          canvas.drawText(caption, 30, 55, paintText);

          // do some more drawing stuff here...

          
          // for the Pressed Image
          canvas.setDevice(mBitmapPressed);
          
          // Greed Color
          paint.setColor(0xff00ff00);
          paintText.setColor(0xff00ff00);  // white Line
          canvas.drawPath(path, paint);
          canvas.drawText(caption, 30, 55, paintText);

          // do some more drawing stuff here...
          
          // for the Pressed Image
          canvas.setDevice(mBitmapFocused);
          
          // Blue Color
          paint.setColor(0xff0000ff);
          paintText.setColor(0xff0000ff);  // white Line
          canvas.drawPath(path, paint);
          canvas.drawText(caption, 30, 55, paintText);

          // do some more drawing stuff here...

          
          // define OnClickListener for the Button
          setOnClickListener(onClickListener);
          
     }

          @Override
          protected void onDraw(Canvas canvas) {
               switch (mState) {
               case StateDefault:
                    canvas.drawBitmap(mBitmapDefault, 0, 0, null);
                    mDebug.append(mCaption + ":default\n");
                    break;
               case StateFocused:
                    canvas.drawBitmap(mBitmapFocused, 0, 0, null);
                    mDebug.append(mCaption + ":focused\n");
                    break;
               case StatePressed:
                    canvas.drawBitmap(mBitmapPressed, 0, 0, null);
                    mDebug.append(mCaption + ":pressed\n");
                    break;
               }
          }
          
          @Override
          protected void drawableStateChanged() {
               if (isPressed()) {
                    mState = StatePressed;
               } else if (hasFocus()) {
                    mState = StateFocused;
               } else {
                    mState = StateDefault;
               }
               // force the redraw of the Image
               // onDraw will be called!
               invalidate();
          }
          
          private OnClickListener onClickListener =
               new OnClickListener() {
               @Override
               public void onClick(View arg0) {
                    mDebug.append(mCaption + ":click\n");
               }
          };
    }
}

Back to top
View user's profile Send private message
bluefloyd8
Experienced Developer


Joined: 22 Jan 2008
Posts: 70
Location: Indiana, USA

PostPosted: Fri Feb 15, 2008 9:56 pm    Post subject: Reply with quote

Maybe post a screen shot of what you are doing so we can all see!
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Advanced Tutorials All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.