The first step is to drag a Button control into the layout file, of course you can also code it yourself. The XML layout is as follows - <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
- xmlns:tools= "http://schemas.android.com/tools"
- android:layout_width= "match_parent"
- android:layout_height= "match_parent"
-
- >
-
- <Button
- android:id= "@+id/button1" <!-- The id number of the button. The program uses this id number to find the corresponding control -->
- android:layout_width= "wrap_content" <!-- The width of the button. Currently, it means automatically stretching according to the content. Other options include match_parent, which means adjusting the size according to the parent control. -->
- android:layout_height= "wrap_content" <!-- button length -->
- android:layout_alignParentTop= "true" <!-- In RelativeLayout, align the top edge of the control with the top edge of the parent control -->
- android:layout_centerHorizontal= "true" <!-- In RelativeLayout, horizontal centering means -->
- android:layout_marginTop= "150dp" <!-- Distance from the top of the parent control in RelativeLayout -->
- android:text= "Button" /> <!-- The text information displayed on the button -->
-
- </RelativeLayout>
Of course, there are many layout properties of a control, which we need to use more and become familiar with. Then call it in the program - public class MainActivity extends Activity {
-
- private Button myButton;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- myButton = (Button)findViewById(R.id.button1);
- myButton.setOnClickListener( new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
-
- }
-
- });
- myButton.setOnTouchListener( new OnTouchListener(){...});
- myButton.setOnLongClickListener( new OnLongClickListener(){...});
- myButton.setOnHoverListener( new OnHoverListener(){...});
-
- }
-
-
- }
Or set the listener like this - public class MainActivity extends Activity implements OnClickListener{
-
- private Button myButton;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- myButton = (Button)findViewById(R.id.button1);
- myButton.setOnClickListener( this );
-
-
- }
-
- @Override
- public void onClick(View v) {
-
-
- switch (v.getId()){
-
- case R.id.button1:
-
- break ;
- }
- }
-
-
- }
A button control with basic functions is now complete. But of course, this is not the focus of today's discussion. The focus is on how to customize a button instead of using the button given to us by the system. 2. Custom Buttons Let’s take a look at the effect picture first. This is a button with a built-in progress bar, which can display the progress of the asynchronous task and end the operation when it is completed. Let's see how it is implemented. - Split this button. Looking carefully at the effect diagram above, we can divide this button into 3 parts. The first is the simplest outer circle. Basically, just draw a circle and put it there. Then there are the triangle, square and check mark in the middle. We can use the drawing class in the view to outline them, and then use a simple animation to switch. The last part is the circle covering the circle and constantly indicating the progress. We can constantly call the ondraw of this view to refresh the progress. This is the design idea of the whole button. Let's take a look at the actual code.
- First, let’s create a circle that represents progress. Let’s create a CusImage-derived view class and pass in progress parameters in real time.
- package com.example.mybutton;
-
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.RectF;
- import android.util.AttributeSet;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.view.View;
-
- @SuppressLint ( "ViewConstructor" )
- public class CusImage extends View {
-
- private ButtonLayout b;
- private Paint myPaint;
- private float startAngle, sweepAngle;
- private RectF rect;
-
- private int pix = 160 ;
-
- public CusImage(Context context, ButtonLayout b) {
- super (context);
- this .b = b;
- init();
-
- }
-
- public CusImage(Context context, AttributeSet attrs, ButtonLayout b) {
- super (context, attrs);
- this .b = b;
- init();
-
- }
-
- private void init() {
- myPaint = new Paint();
- DisplayMetrics metrics = getContext().getResources()
- .getDisplayMetrics();
- int width = metrics.widthPixels;
- int height = metrics.heightPixels;
- Log.d( "TAG" , width + "" );
- Log.d( "TAG" , height + "" );
- float scarea = width * height;
- pix = ( int ) Math.sqrt(scarea * 0.0217 );
-
-
- myPaint.setAntiAlias( true );
-
- myPaint.setStyle(Paint.Style.STROKE);
-
- myPaint.setColor(Color.rgb( 0 , 161 , 234 ));
-
- myPaint.setStrokeWidth( 7 );
-
- float startx = ( float ) (pix * 0.05 );
- float endx = ( float ) (pix * 0.95 );
- float starty = ( float ) (pix * 0.05 );
- float endy = ( float ) (pix * 0.95 );
-
- rect = new RectF(startx, starty, endx, endy);
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
-
-
- canvas.drawArc(rect, startAngle, sweepAngle, false , myPaint);
- startAngle = -90 ;
-
-
- if (sweepAngle < 360 &&b.flg_frmwrk_mode == 2 ) {
- invalidate();
- } else if (b.flg_frmwrk_mode == 1 ){
-
- } else {
- sweepAngle = 0 ;
- startAngle = - 90 ;
- b.finalAnimation();
-
- }
- super .onDraw(canvas);
- }
-
-
-
-
- @Override
- protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {
- int desiredWidth = pix;
- int desiredHeight = pix;
- int widthMode = MeasureSpec.getMode(widthMeasureSpec);
- int widthSize = MeasureSpec.getSize(widthMeasureSpec);
- int heightMode = MeasureSpec.getMode(heightMeasureSpec);
- int heightSize = MeasureSpec.getSize(heightMeasureSpec);
-
- int width;
- int height;
-
-
- if (widthMode == MeasureSpec.EXACTLY) {
- width = widthSize;
- } else if (widthMode == MeasureSpec.AT_MOST) {
- width = Math.min(desiredWidth, widthSize);
- } else {
- width = desiredWidth;
- }
-
-
- if (heightMode == MeasureSpec.EXACTLY) {
- height = heightSize;
- } else if (heightMode == MeasureSpec.AT_MOST) {
- height = Math.min(desiredHeight, heightSize);
- } else {
- height = desiredHeight;
- }
-
- setMeasuredDimension(width, height);
- }
-
- public void setupprogress( int progress) {
- sweepAngle = ( float ) (progress * 3.6 );
- }
-
- public void reset() {
- startAngle = -90 ;
- }
-
- }
After we have the view that represents the progress, we need to assemble the various parts in a viewgroup control to implement the entire button. Here I use framelayout The codes are written together here, I will explain them one by one. First is the initialization of ImageView -
-
-
- private void initialise() {
-
- cusView = new CusImage(getContext(), this );
-
- buttonimage = new ImageView(getContext());
-
- fillcircle = new ImageView(getContext());
-
- full_circle_image = new ImageView(getContext());
-
- cusView.setClickable( false );
- buttonimage.setClickable( false );
- fillcircle.setClickable( false );
- full_circle_image.setClickable( false );
-
- setClickable( true );
-
- }
Then set up the animation -
-
-
- private void setAnimation() {
-
-
-
-
-
-
-
-
-
-
-
- in = new AnimationSet( true );
- out = new AnimationSet( true );
-
-
- out.setInterpolator( new AccelerateDecelerateInterpolator());
- in.setInterpolator( new AccelerateDecelerateInterpolator());
-
-
- scale_in = new ScaleAnimation( 0 .0f, 1 .0f, 0 .0f, 1 .0f,
- Animation.RELATIVE_TO_SELF, 0.5f , Animation.RELATIVE_TO_SELF,
- 0.5f );
- scale_out = new ScaleAnimation( 1 .0f, 3 .0f, 1 .0f, 3 .0f,
- Animation.RELATIVE_TO_SELF, 0.5f , Animation.RELATIVE_TO_SELF,
- 0.5f );
-
-
-
- new_scale_in = new ScaleAnimation( 0 .0f, 1 .0f, 0 .0f, 1 .0f,
- Animation.RELATIVE_TO_SELF, 0.5f , Animation.RELATIVE_TO_SELF,
- 0.5f );
-
- new_scale_in.setDuration( 200 );
-
-
- fade_in = new AlphaAnimation( 0 .0f, 1 .0f);
- fade_out = new AlphaAnimation( 1 .0f, 0 .0f);
-
- scale_in.setDuration( 150 );
- scale_out.setDuration( 150 );
- fade_in.setDuration( 150 );
- fade_out.setDuration( 150 );
-
-
- in.addAnimation(scale_in);
- in.addAnimation(fade_in);
-
- out.addAnimation(fade_out);
- out.addAnimation(scale_out);
-
- out.setAnimationListener( new AnimationListener() {
-
- @Override
- public void onAnimationStart(Animation animation) {
-
- System.out.println( "print this" );
- }
-
- @Override
- public void onAnimationRepeat(Animation animation) {
-
-
- }
-
- @Override
- public void onAnimationEnd(Animation animation) {
-
-
- buttonimage.setVisibility(View.GONE);
- buttonimage.setImageBitmap(second_icon_bmp);
- buttonimage.setVisibility(View.VISIBLE);
- buttonimage.startAnimation(in);
- full_circle_image.setVisibility(View.VISIBLE);
- cusView.setVisibility(View.VISIBLE);
-
- flg_frmwrk_mode = 2 ;
-
- System.out.println( "flg_frmwrk_mode" + flg_frmwrk_mode);
-
- }
- });
-
- new_scale_in.setAnimationListener( new AnimationListener() {
-
- @Override
- public void onAnimationStart(Animation animation) {
-
-
- }
-
- @Override
- public void onAnimationRepeat(Animation animation) {
-
-
- }
-
- @Override
- public void onAnimationEnd(Animation animation) {
-
- cusView.setVisibility(View.GONE);
- buttonimage.setVisibility(View.VISIBLE);
- buttonimage.setImageBitmap(third_icon_bmp);
- flg_frmwrk_mode = 3 ;
- buttonimage.startAnimation(in);
-
- }
- });
-
- }
Next, draw the shapes. - * Set the path of each screen
- */
- private void iconCreate() {
-
-
-
-
- play = new Path();
- play.moveTo(pix * 40 / 100 , pix * 36 / 100 );
- play.lineTo(pix * 40 / 100 , pix * 63 / 100 );
- play.lineTo(pix * 69 / 100 , pix * 50 / 100 );
- play.close();
-
- stop = new Path();
- stop.moveTo(pix * 38 / 100 , pix * 38 / 100 );
- stop.lineTo(pix * 62 / 100 , pix * 38 / 100 );
- stop.lineTo(pix * 62 / 100 , pix * 62 / 100 );
- stop.lineTo(pix * 38 / 100 , pix * 62 / 100 );
- stop.close();
-
- download_triangle = new Path();
- download_triangle.moveTo(pix * 375 / 1000 , (pix / 2 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_triangle.lineTo(pix / 2 , (pix * 625 / 1000 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_triangle.lineTo(pix * 625 / 1000 , (pix / 2 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_triangle.close();
-
- download_rectangle = new Path();
- download_rectangle.moveTo(pix * 4375 / 10000 , (pix / 2 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_rectangle.lineTo(pix * 5625 / 10000 , (pix / 2 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_rectangle.lineTo(pix * 5625 / 10000 , (pix * 375 / 1000 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_rectangle.lineTo(pix * 4375 / 10000 , (pix * 375 / 1000 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_rectangle.close();
-
- tick = new Path();
- tick.moveTo(pix * 30 / 100 , pix * 50 / 100 );
- tick.lineTo(pix * 45 / 100 , pix * 625 / 1000 );
- tick.lineTo(pix * 65 / 100 , pix * 350 / 1000 );
-
- }
-
-
-
-
- public void init() {
-
-
-
- FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
- FrameLayout.LayoutParams.WRAP_CONTENT,
- FrameLayout.LayoutParams.WRAP_CONTENT);
-
- lp.setMargins( 10 , 10 , 10 , 10 );
-
- fillcircle.setVisibility(View.GONE);
-
- Bitmap.Config conf = Bitmap.Config.ARGB_8888;
- Bitmap full_circle_bmp = Bitmap.createBitmap(pix, pix, conf);
- Bitmap fill_circle_bmp = Bitmap.createBitmap(pix, pix, conf);
-
- first_icon_bmp = Bitmap.createBitmap(pix, pix, conf);
-
-
-
-
- second_icon_bmp = Bitmap.createBitmap(pix, pix, conf);
-
-
-
-
- third_icon_bmp = Bitmap.createBitmap(pix, pix, conf);
-
-
-
-
- Canvas first_icon_canvas = new Canvas(first_icon_bmp);
- Canvas second_icon_canvas = new Canvas(second_icon_bmp);
- Canvas third_icon_canvas = new Canvas(third_icon_bmp);
- Canvas fill_circle_canvas = new Canvas(fill_circle_bmp);
- Canvas full_circle_canvas = new Canvas(full_circle_bmp);
- float startx = ( float ) (pix * 0.05 );
- float endx = ( float ) (pix * 0.95 );
- System.out.println( "full circle " + full_circle_canvas.getWidth()
- + full_circle_canvas.getHeight());
- float starty = ( float ) (pix * 0.05 );
- float endy = ( float ) (pix * 0.95 );
- rect = new RectF(startx, starty, endx, endy);
-
- first_icon_canvas.drawPath(play, fill_color);
-
-
-
-
-
- second_icon_canvas.drawPath(stop, icon_color);
-
-
-
-
-
- third_icon_canvas.drawPath(tick, final_icon_color);
-
-
-
-
-
-
- full_circle_canvas.drawArc(rect, 0 , 360 , false , stroke_color);
- fill_circle_canvas.drawArc(rect, 0 , 360 , false , fill_color);
-
- buttonimage.setImageBitmap(first_icon_bmp);
- flg_frmwrk_mode = 1 ;
- fillcircle.setImageBitmap(fill_circle_bmp);
- full_circle_image.setImageBitmap(full_circle_bmp);
-
- cusView.setVisibility(View.GONE);
-
- addView(full_circle_image, lp);
- addView(fillcircle, lp);
- addView(buttonimage, lp);
- addView(cusView, lp);
-
- }
***With the logical relationship between the state switching when clicking the button, the layout of this button is complete. Attach the code of the entire class - package com.example.mybutton;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.RectF;
- import android.util.AttributeSet;
- import android.util.DisplayMetrics;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.AccelerateDecelerateInterpolator;
- import android.view.animation.AlphaAnimation;
- import android.view.animation.Animation;
- import android.view.animation.AnimationSet;
- import android.view.animation.ScaleAnimation;
- import android.view.animation.Animation.AnimationListener;
- import android.widget.FrameLayout;
- import android.widget.ImageView;
-
- public class ButtonLayout extends FrameLayout implements OnClickListener {
-
- public CusImage cusView;
- public int pix = 0 ;
- public RectF rect;
-
-
- private ImageView circle_image, buttonimage, fillcircle, full_circle_image;
-
-
- private Path stop, tick, play, download_triangle, download_rectangle;
-
-
- private Bitmap third_icon_bmp, second_icon_bmp, first_icon_bmp;
-
-
- private Paint stroke_color, fill_color, icon_color, final_icon_color;
-
-
- private AnimationSet in, out;
-
-
-
-
-
- private ScaleAnimation new_scale_in, scale_in, scale_out;
-
-
- private AlphaAnimation fade_in, fade_out;
-
- public int flg_frmwrk_mode = 0 ;
- boolean first_click = false ;
-
- public ButtonLayout(Context context, AttributeSet attrs) {
- super (context, attrs);
- setOnClickListener( this );
-
- initialise();
- setpaint();
- setAnimation();
- displayMetrics();
- iconCreate();
- init();
-
- }
-
- public ButtonLayout(Context context) {
- super (context);
- setOnClickListener( this );
- setBackgroundColor(Color.CYAN);
- initialise();
- setpaint();
- setAnimation();
- displayMetrics();
- iconCreate();
- init();
- }
-
-
-
-
- private void initialise() {
-
- cusView = new CusImage(getContext(), this );
-
- buttonimage = new ImageView(getContext());
-
- fillcircle = new ImageView(getContext());
-
- full_circle_image = new ImageView(getContext());
-
- cusView.setClickable( false );
- buttonimage.setClickable( false );
- fillcircle.setClickable( false );
- full_circle_image.setClickable( false );
-
- setClickable( true );
-
- }
-
-
-
-
- private void setpaint() {
-
-
-
- stroke_color = new Paint(Paint.ANTI_ALIAS_FLAG);
- stroke_color.setAntiAlias( true );
- stroke_color.setColor(Color.rgb( 0 , 161 , 234 ));
- stroke_color.setStrokeWidth( 3 );
- stroke_color.setStyle(Paint.Style.STROKE);
-
- icon_color = new Paint(Paint.ANTI_ALIAS_FLAG);
- icon_color.setColor(Color.rgb( 0 , 161 , 234 ));
-
- icon_color.setStyle(Paint.Style.FILL_AND_STROKE);
- icon_color.setAntiAlias( true );
-
- final_icon_color = new Paint(Paint.ANTI_ALIAS_FLAG);
- final_icon_color.setColor(Color.WHITE);
- final_icon_color.setStrokeWidth( 12 );
- final_icon_color.setStyle(Paint.Style.STROKE);
- final_icon_color.setAntiAlias( true );
-
- fill_color = new Paint(Paint.ANTI_ALIAS_FLAG);
- fill_color.setColor(Color.rgb( 0 , 161 , 234 ));
- fill_color.setStyle(Paint.Style.FILL_AND_STROKE);
- fill_color.setAntiAlias( true );
-
- }
-
-
-
-
- private void setAnimation() {
-
-
-
-
-
-
-
-
-
-
-
- in = new AnimationSet( true );
- out = new AnimationSet( true );
-
-
- out.setInterpolator( new AccelerateDecelerateInterpolator());
- in.setInterpolator( new AccelerateDecelerateInterpolator());
-
-
- scale_in = new ScaleAnimation( 0 .0f, 1 .0f, 0 .0f, 1 .0f,
- Animation.RELATIVE_TO_SELF, 0.5f , Animation.RELATIVE_TO_SELF,
- 0.5f );
- scale_out = new ScaleAnimation( 1 .0f, 3 .0f, 1 .0f, 3 .0f,
- Animation.RELATIVE_TO_SELF, 0.5f , Animation.RELATIVE_TO_SELF,
- 0.5f );
-
-
-
- new_scale_in = new ScaleAnimation( 0 .0f, 1 .0f, 0 .0f, 1 .0f,
- Animation.RELATIVE_TO_SELF, 0.5f , Animation.RELATIVE_TO_SELF,
- 0.5f );
-
- new_scale_in.setDuration( 200 );
-
-
- fade_in = new AlphaAnimation( 0 .0f, 1 .0f);
- fade_out = new AlphaAnimation( 1 .0f, 0 .0f);
-
- scale_in.setDuration( 150 );
- scale_out.setDuration( 150 );
- fade_in.setDuration( 150 );
- fade_out.setDuration( 150 );
-
-
- in.addAnimation(scale_in);
- in.addAnimation(fade_in);
-
- out.addAnimation(fade_out);
- out.addAnimation(scale_out);
-
- out.setAnimationListener( new AnimationListener() {
-
- @Override
- public void onAnimationStart(Animation animation) {
-
- System.out.println( "print this" );
- }
-
- @Override
- public void onAnimationRepeat(Animation animation) {
-
-
- }
-
- @Override
- public void onAnimationEnd(Animation animation) {
-
-
- buttonimage.setVisibility(View.GONE);
- buttonimage.setImageBitmap(second_icon_bmp);
- buttonimage.setVisibility(View.VISIBLE);
- buttonimage.startAnimation(in);
- full_circle_image.setVisibility(View.VISIBLE);
- cusView.setVisibility(View.VISIBLE);
-
- flg_frmwrk_mode = 2 ;
-
- System.out.println( "flg_frmwrk_mode" + flg_frmwrk_mode);
-
- }
- });
-
- new_scale_in.setAnimationListener( new AnimationListener() {
-
- @Override
- public void onAnimationStart(Animation animation) {
-
-
- }
-
- @Override
- public void onAnimationRepeat(Animation animation) {
-
-
- }
-
- @Override
- public void onAnimationEnd(Animation animation) {
-
- cusView.setVisibility(View.GONE);
- buttonimage.setVisibility(View.VISIBLE);
- buttonimage.setImageBitmap(third_icon_bmp);
- flg_frmwrk_mode = 3 ;
- buttonimage.startAnimation(in);
-
- }
- });
-
- }
-
-
-
-
- private void displayMetrics() {
-
-
- DisplayMetrics metrics = getContext().getResources()
- .getDisplayMetrics();
- int width = metrics.widthPixels;
- int height = metrics.heightPixels;
- float scarea = width * height;
- pix = ( int ) Math.sqrt(scarea * 0.0217 );
-
- }
-
-
-
-
- private void iconCreate() {
-
-
-
-
- play = new Path();
- play.moveTo(pix * 40 / 100 , pix * 36 / 100 );
- play.lineTo(pix * 40 / 100 , pix * 63 / 100 );
- play.lineTo(pix * 69 / 100 , pix * 50 / 100 );
- play.close();
-
- stop = new Path();
- stop.moveTo(pix * 38 / 100 , pix * 38 / 100 );
- stop.lineTo(pix * 62 / 100 , pix * 38 / 100 );
- stop.lineTo(pix * 62 / 100 , pix * 62 / 100 );
- stop.lineTo(pix * 38 / 100 , pix * 62 / 100 );
- stop.close();
-
- download_triangle = new Path();
- download_triangle.moveTo(pix * 375 / 1000 , (pix / 2 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_triangle.lineTo(pix / 2 , (pix * 625 / 1000 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_triangle.lineTo(pix * 625 / 1000 , (pix / 2 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_triangle.close();
-
- download_rectangle = new Path();
- download_rectangle.moveTo(pix * 4375 / 10000 , (pix / 2 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_rectangle.lineTo(pix * 5625 / 10000 , (pix / 2 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_rectangle.lineTo(pix * 5625 / 10000 , (pix * 375 / 1000 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_rectangle.lineTo(pix * 4375 / 10000 , (pix * 375 / 1000 )
- + (pix * 625 / 10000 ) - (pix * 3 / 100 ));
- download_rectangle.close();
-
- tick = new Path();
- tick.moveTo(pix * 30 / 100 , pix * 50 / 100 );
- tick.lineTo(pix * 45 / 100 , pix * 625 / 1000 );
- tick.lineTo(pix * 65 / 100 , pix * 350 / 1000 );
-
- }
-
-
-
-
- public void init() {
-
-
-
- FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
- FrameLayout.LayoutParams.WRAP_CONTENT,
- FrameLayout.LayoutParams.WRAP_CONTENT);
-
- lp.setMargins( 10 , 10 , 10 , 10 );
-
- fillcircle.setVisibility(View.GONE);
-
- Bitmap.Config conf = Bitmap.Config.ARGB_8888;
- Bitmap full_circle_bmp = Bitmap.createBitmap(pix, pix, conf);
- Bitmap fill_circle_bmp = Bitmap.createBitmap(pix, pix, conf);
-
- first_icon_bmp = Bitmap.createBitmap(pix, pix, conf);
-
-
-
-
- second_icon_bmp = Bitmap.createBitmap(pix, pix, conf);
-
-
-
-
- third_icon_bmp = Bitmap.createBitmap(pix, pix, conf);
-
-
-
-
- Canvas first_icon_canvas = new Canvas(first_icon_bmp);
- Canvas second_icon_canvas = new Canvas(second_icon_bmp);
- Canvas third_icon_canvas = new Canvas(third_icon_bmp);
- Canvas fill_circle_canvas = new Canvas(fill_circle_bmp);
- Canvas full_circle_canvas = new Canvas(full_circle_bmp);
- float startx = ( float ) (pix * 0.05 );
- float endx = ( float ) (pix * 0.95 );
- System.out.println( "full circle " + full_circle_canvas.getWidth()
- + full_circle_canvas.getHeight());
- float starty = ( float ) (pix * 0.05 );
- float endy = ( float ) (pix * 0.95 );
- rect = new RectF(startx, starty, endx, endy);
-
- first_icon_canvas.drawPath(play, fill_color);
-
-
-
-
-
- second_icon_canvas.drawPath(stop, icon_color);
-
-
-
-
-
- third_icon_canvas.drawPath(tick, final_icon_color);
-
-
-
-
-
-
- full_circle_canvas.drawArc(rect, 0 , 360 , false , stroke_color);
- fill_circle_canvas.drawArc(rect, 0 , 360 , false , fill_color);
-
- buttonimage.setImageBitmap(first_icon_bmp);
- flg_frmwrk_mode = 1 ;
- fillcircle.setImageBitmap(fill_circle_bmp);
- full_circle_image.setImageBitmap(full_circle_bmp);
-
- cusView.setVisibility(View.GONE);
-
- addView(full_circle_image, lp);
- addView(fillcircle, lp);
- addView(buttonimage, lp);
- addView(cusView, lp);
-
- }
-
- public void animation() {
-
-
-
- if (flg_frmwrk_mode == 1 ) {
-
- buttonimage.startAnimation(out);
- }
-
- }
-
- public void finalAnimation() {
-
-
-
- buttonimage.setVisibility(View.GONE);
- fillcircle.setVisibility(View.VISIBLE);
- fillcircle.startAnimation(new_scale_in);
-
- }
-
- public void stop() {
-
-
-
- cusView.reset();
- buttonimage.setImageBitmap(first_icon_bmp);
- flg_frmwrk_mode = 1 ;
-
- }
-
- @Override
- public void onClick(View v) {
-
- animation();
- }
-
- }
Now that the button is ready, we can call it in the Activity First, write it to the layout file - <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
- xmlns:tools= "http://schemas.android.com/tools"
- android:layout_width= "match_parent"
- android:layout_height= "match_parent"
-
- >
-
- <com.example.mybutton.ButtonLayout
- android:id= "@+id/ButtonLayout01"
- android:layout_width= "wrap_content"
- android:layout_height= "wrap_content"
- android:layout_centerHorizontal= "true"
- android:layout_centerVertical= "true"
- android:clickable= "true" >
- </com.example.mybutton.ButtonLayout>
-
- </RelativeLayout>
Then set it in the activity - public class MainActivity extends Activity {
- 2
- 3 private static ButtonLayout buttonLayout;
- 4
- 5 @Override
- 6 protected void onCreate(Bundle savedInstanceState) {
- 7 super .onCreate(savedInstanceState);
- 8 setContentView(R.layout.activity_main);
- 9 buttonLayout = (ButtonLayout) findViewById(R.id.ButtonLayout01);
- 10 buttonLayout.setOnClickListener( new OnClickListener() {
- 11
- 12 @Override
- 13 public void onClick(View v) {
- 14
- 15 buttonLayout.animation();
- 16
- 17
- 18 if (buttonLayout.flg_frmwrk_mode == 1 ) {
- 19
- 20
- twenty one
- 22 runOnUiThread( new Runnable() {
- twenty three
- twenty four @Override
- 25 public void run() {
- 26
- 27 Toast.makeText( MainActivity.this ,
- 28 "Starting download" , Toast.LENGTH_SHORT)
- 29.show ();
- 30 }
- 31 });
- 32 new DownLoadSigTask().execute();
- 33 }
- 34 if (buttonLayout.flg_frmwrk_mode == 2 ) {
- 35
- 36
- 37
- 38 new DownLoadSigTask().cancel( true );
- 39 buttonLayout.stop();
- 40 runOnUiThread( new Runnable() {
- 41
- 42 @Override
- 43 public void run() {
- 44
- 45 Toast.makeText( MainActivity.this ,
- 46 "Download stopped" , Toast.LENGTH_SHORT)
- 47.show ();
- 48 }
- 49 });
- 50 }
- 51 if (buttonLayout.flg_frmwrk_mode == 3 ) {
- 52
- 53
- 54
- 55 runOnUiThread( new Runnable() {
- 56
- 57 @Override
- 58 public void run() {
- 59
- 60 Toast.makeText( MainActivity.this ,
- 61 "Download complete" , Toast.LENGTH_SHORT)
- 62.show ();
- 63 }
- 64 });
- 65 }
- 66 }
- 67
- 68 });
- 69 }
- 70
- 71 static class DownLoadSigTask extends AsyncTask<String, Integer, String> {
- 72
- 73 @Override
- 74 protected void onPreExecute() {
- 75
- 76 }
- 77
- 78 @Override
- 79 protected String doInBackground( final String... args) {
- 80
- 81
- 82
- 83 for ( int i = 0 ; i <= 100 ;) {
- 84 try {
- 85 Thread.sleep( 50 );
- 86
- 87 } catch (InterruptedException e) {
- 88
- 89 e.printStackTrace();
- 90 }
- 91 if (buttonLayout.flg_frmwrk_mode == 2 &&i<= 100 ){
- 92 i++;
- 93 publishProgress(i);
- 94 }
- 95 }
- 96
- 97 return null ;
- 98 }
- 99
- 100 @Override
- 101 protected void onProgressUpdate(Integer... progress) {
- 102
- 103
- 104
- 105 buttonLayout.cusView.setupprogress(progress[ 0 ]);
- 106 }
- 107
- 108 }
- 109
- 110 }
3. Conclusion I wrote this button based on an open source project, its address is https://github.com/torryharris/TH-ProgressButton. I have mixed some URLs in the code. These are the materials I consulted when reading this entire open source code. If you don’t understand it, you can also go to these addresses to check the information. To be honest, there are too many things to design custom controls, which is not suitable for learning when you are just getting started. However, it is good to have a concept to start with. In the near future, I will also introduce some of the concepts I encountered today, such as animation, path, and canvas. So today ends here~ |