日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android入門篇——Button控件+自定義Button控件

首先***步就是往布局文件里拖一個Button控件,當(dāng)然自己碼出來也可以。XML布局如下

創(chuàng)新互聯(lián)一直秉承“誠信做人,踏實做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個客戶多一個朋友!為您提供成都做網(wǎng)站、成都網(wǎng)站制作、成都網(wǎng)頁設(shè)計、微信小程序開發(fā)、成都網(wǎng)站開發(fā)、成都網(wǎng)站制作、成都軟件開發(fā)、重慶APP軟件開發(fā)是成都本地專業(yè)的網(wǎng)站建設(shè)和網(wǎng)站設(shè)計公司,等你一起來見證!

 
 
 
 
  1.     xmlns:tools="http://schemas.android.com/tools"
  2.     android:layout_width="match_parent"
  3.     android:layout_height="match_parent"
  4.     
  5.      >
  6.     
  7.         android:id="@+id/button1"             
  8.         android:layout_width="wrap_content"   
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentTop="true"  
  11.         android:layout_centerHorizontal="true"
  12.         android:layout_marginTop="150dp"      
  13.         android:text="Button" />              

當(dāng)然,一個控件的布局屬性還有很多,這些都是需要我們多用多熟悉才行。

然后再在程序中調(diào)用它

 
 
 
 
  1. public class MainActivity extends Activity {
  2.     
  3.     private Button myButton;
  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.activity_main);
  8.         //通過id尋找控件,記得尋找控件前一定要先設(shè)置好布局文件
  9.         myButton = (Button)findViewById(R.id.button1);
  10.         myButton.setOnClickListener(new OnClickListener() {
  11.             
  12.             @Override
  13.             public void onClick(View v) {
  14.                 // TODO Auto-generated method stub
  15.                 //這里填寫單擊按鈕后要執(zhí)行的事件
  16.             }
  17.             
  18.         });
  19.         myButton.setOnTouchListener(new OnTouchListener(){...});//設(shè)置觸碰到按鈕的監(jiān)聽器
  20.         myButton.setOnLongClickListener(new OnLongClickListener(){...});//設(shè)置長按按鈕的監(jiān)聽器
  21.         myButton.setOnHoverListener(new OnHoverListener(){...});//設(shè)置界面覆蓋按鈕時的監(jiān)聽器
  22.         //還有其它的的監(jiān)聽器,我們可以根據(jù)不同的需求來調(diào)用相應(yīng)的監(jiān)聽器
  23.     }
  24. }

或者這樣設(shè)置監(jiān)聽器

 
 
 
 
  1. public class MainActivity extends Activity implements OnClickListener{
  2.     
  3.     private Button myButton;
  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.activity_main);
  8.         //尋找控件,記得尋找控件前一定要先設(shè)置好布局文件
  9.         myButton = (Button)findViewById(R.id.button1);
  10.         myButton.setOnClickListener(this);
  11.             
  12.     }
  13.     @Override
  14.     public void onClick(View v) {
  15.         // TODO Auto-generated method stub
  16.         //獲取點擊的View
  17.         switch(v.getId()){
  18.         //根據(jù)View的id來進行相關(guān)操作
  19.         case R.id.button1:
  20.             //按鈕點擊時處理相關(guān)的事件
  21.             break;
  22.         }
  23.     }
  24. }

這樣一個基礎(chǔ)功能的button控件就完成了。但當(dāng)然,這不是我們今天要講的重點,重點是我們?nèi)绾巫远x一個按鈕,而不是使用系統(tǒng)給我們的按鈕。

二、自定義按鈕

我們先來看看效果圖吧

這是一個自帶進度條的按鈕,它可以顯示異步任務(wù)的進度,當(dāng)完成后結(jié)束操作。我們來看看具體是怎么實現(xiàn)的吧。

  1. 拆分這個按鈕。仔細觀察上面的效果圖,我們可以把這個按鈕分成3個部分,首先是 最簡單的外面一圈圓,基本上畫出個圓放在那里就行了。接著是中間的三角形,正方形以及完成的勾,這個我們可以使用view里的畫圖類勾勒出來,再使用簡單 的動畫Animation來切換。***的一部分是覆蓋在圓圈上的不斷在表示進度的圓圈,這個我們可以不斷調(diào)用這個view的ondraw來刷新進度。這就 是整個按鈕的設(shè)計思路。我們來看看實際的代碼吧。
  2. 首先是表示進度的圓圈,我們來新建一個CusImage繼承view類,實時的傳入進度參數(shù)。
 
 
 
 
  1. package com.example.mybutton;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.RectF;
  8. import android.util.AttributeSet;
  9. import android.util.DisplayMetrics;
  10. import android.util.Log;
  11. import android.view.View;
  12. @SuppressLint("ViewConstructor") 
  13. public class CusImage extends View {
  14.     private ButtonLayout b;
  15.     private Paint myPaint;
  16.     private float startAngle, sweepAngle;
  17.     private RectF rect;
  18.     // 默認控件大小
  19.     private int pix = 160;
  20.     public CusImage(Context context, ButtonLayout b) {
  21.         super(context);
  22.         this.b = b;
  23.         init();
  24.         // TODO Auto-generated constructor stub
  25.     }
  26.     public CusImage(Context context, AttributeSet attrs, ButtonLayout b) {
  27.         super(context, attrs);
  28.         this.b = b;
  29.         init();
  30.         // TODO Auto-generated constructor stub
  31.     }
  32.     private void init() {
  33.         myPaint = new Paint();
  34.         DisplayMetrics metrics = getContext().getResources()
  35.                 .getDisplayMetrics();
  36.         int width = metrics.widthPixels;
  37.         int height = metrics.heightPixels;
  38.         Log.d("TAG", width + "");
  39.         Log.d("TAG", height + "");
  40.         float scarea = width * height;
  41.         pix = (int) Math.sqrt(scarea * 0.0217);
  42.         //抗鋸齒
  43.         myPaint.setAntiAlias(true);
  44.         //stroke表示空心,F(xiàn)ill表示實心
  45.         myPaint.setStyle(Paint.Style.STROKE);
  46.         //顏色
  47.         myPaint.setColor(Color.rgb(0, 161, 234));
  48.         //設(shè)置線條粗細
  49.         myPaint.setStrokeWidth(7);
  50.         float startx = (float) (pix * 0.05);
  51.         float endx = (float) (pix * 0.95);
  52.         float starty = (float) (pix * 0.05);
  53.         float endy = (float) (pix * 0.95);
  54.         //矩形區(qū)域
  55.         rect = new RectF(startx, starty, endx, endy);
  56.     }
  57.     @Override
  58.     protected void onDraw(Canvas canvas) {
  59.         // 畫弧線
  60.         // 在rect這個區(qū)域內(nèi)畫,開始的角度,掃過的度數(shù)而不是結(jié)束的角度,false表示不與圓心連線,true通常用來畫扇形,畫筆。
  61.         canvas.drawArc(rect, startAngle, sweepAngle, false, myPaint);
  62.         startAngle = -90;
  63.         //小于1圈
  64.         if (sweepAngle < 360 &&b.flg_frmwrk_mode == 2) {
  65.             invalidate();
  66.         }else if(b.flg_frmwrk_mode == 1){
  67.                     
  68.         }else {//掃完一圈,調(diào)用b.finalAnimation()
  69.             sweepAngle = 0;
  70.             startAngle = -90;
  71.             b.finalAnimation();
  72.         }
  73.         super.onDraw(canvas);
  74.     }
  75.     /**
  76.      * 控制控件的大小 http://blog.csdn.net/pi9nc/article/details/18764863
  77.      **/
  78.     @Override
  79.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  80.         int desiredWidth = pix;
  81.         int desiredHeight = pix;
  82.         int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  83.         int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  84.         int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  85.         int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  86.         int width;
  87.         int height;
  88.         // 如果控件寬度是指定大小,寬度為指定的尺寸
  89.         if (widthMode == MeasureSpec.EXACTLY) {
  90.             width = widthSize;
  91.         } else if (widthMode == MeasureSpec.AT_MOST) { // 沒有限制,默認內(nèi)容大小
  92.             width = Math.min(desiredWidth, widthSize);
  93.         } else {
  94.             width = desiredWidth;
  95.         }
  96.         // 如果控件高度是指定大小,高度為指定的尺寸
  97.         if (heightMode == MeasureSpec.EXACTLY) {
  98.             height = heightSize;
  99.         } else if (heightMode == MeasureSpec.AT_MOST) {// 沒有限制,默認內(nèi)容大小
  100.             height = Math.min(desiredHeight, heightSize);
  101.         } else {
  102.             height = desiredHeight;
  103.         }
  104.         // 設(shè)定控件大小
  105.         setMeasuredDimension(width, height);
  106.     }
  107.     // 傳入?yún)?shù)
  108.     public void setupprogress(int progress) {
  109.         sweepAngle = (float) (progress * 3.6);
  110.     }
  111.     public void reset() {
  112.         startAngle = -90;
  113.     }
  114. }

有了表示進度的view之后,我們要在一個viewgroup控件中組裝各個部分來實現(xiàn)整個按鈕,這里我用的是framelayout

這里代碼寫在一起了,我把它們一個一個拎出來講解。

首先是ImageView的初始化

 
 
 
 
  1. /**
  2.      * 創(chuàng)建各個控件
  3.      */
  4.     private void initialise() {
  5.         // 按鈕的進度條
  6.         cusView = new CusImage(getContext(), this);
  7.         // 按鈕中間的形狀
  8.         buttonimage = new ImageView(getContext());
  9.         // 完成進度后顯示的圖像
  10.         fillcircle = new ImageView(getContext());
  11.         //外面一圈圓
  12.         full_circle_image = new ImageView(getContext());
  13.         // 設(shè)置控件不接受點擊事件
  14.         cusView.setClickable(false);
  15.         buttonimage.setClickable(false);
  16.         fillcircle.setClickable(false);
  17.         full_circle_image.setClickable(false);
  18.         setClickable(true);
  19.     }

然后是設(shè)置動畫

 
 
 
 
  1. /**
  2.      * 設(shè)置動畫及動畫監(jiān)聽器
  3.      */
  4.     private void setAnimation() {
  5.         // Setting up and defining view animations.
  6.         // http://blog.csdn.net/congqingbin/article/details/7889778
  7.         // RELATIVE_TO_PARENT:與父控件的的中心為重點;RELATIVE_TO_SELF以自己為中心
  8.         // 左上角 分別為0.0f 0.0f 中心點為0.5f,0.5f 右下角1.0f,1.0f
  9.         /*
  10.          * arcRotation = new RotateAnimation(0.0f, 360.0f,
  11.          * Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  12.          */
  13.         // 持續(xù)時間1000ms
  14.         // arcRotation.setDuration(500);
  15.         in = new AnimationSet(true);
  16.         out = new AnimationSet(true);
  17.         // http://blog.csdn.net/jason0539/article/details/16370405
  18.         out.setInterpolator(new AccelerateDecelerateInterpolator());
  19.         in.setInterpolator(new AccelerateDecelerateInterpolator());
  20.         // http://blog.csdn.net/xsl1990/article/details/17096501
  21.         scale_in = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
  22.                 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
  23.                 0.5f);
  24.         scale_out = new ScaleAnimation(1.0f, 3.0f, 1.0f, 3.0f,
  25.                 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
  26.                 0.5f);
  27.         // 縮放動畫,起始x軸的縮放為0,y軸的縮放為0,動畫后,x,y軸大小與圖像尺寸相同
  28.         // x,y可以把它當(dāng)做寬度和高度
  29.         new_scale_in = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
  30.                 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
  31.                 0.5f);
  32.         new_scale_in.setDuration(200);
  33.         // 透明度的動畫
  34.         fade_in = new AlphaAnimation(0.0f, 1.0f);
  35.         fade_out = new AlphaAnimation(1.0f, 0.0f);
  36.         scale_in.setDuration(150);
  37.         scale_out.setDuration(150);
  38.         fade_in.setDuration(150);
  39.         fade_out.setDuration(150);
  40.         // 進入的動畫集
  41.         in.addAnimation(scale_in);
  42.         in.addAnimation(fade_in);
  43.         // 退出的動畫集
  44.         out.addAnimation(fade_out);
  45.         out.addAnimation(scale_out);
  46.         out.setAnimationListener(new AnimationListener() {
  47.             @Override
  48.             public void onAnimationStart(Animation animation) {
  49.                 // TODO Auto-generated method stub
  50.                 System.out.println("print this");
  51.             }
  52.             @Override
  53.             public void onAnimationRepeat(Animation animation) {
  54.                 // TODO Auto-generated method stub
  55.             }
  56.             @Override
  57.             public void onAnimationEnd(Animation animation) {
  58.                 // TODO Auto-generated method stub
  59.                 buttonimage.setVisibility(View.GONE);
  60.                 buttonimage.setImageBitmap(second_icon_bmp);
  61.                 buttonimage.setVisibility(View.VISIBLE);
  62.                 buttonimage.startAnimation(in);
  63.                 full_circle_image.setVisibility(View.VISIBLE);
  64.                 cusView.setVisibility(View.VISIBLE);
  65.                 flg_frmwrk_mode = 2;
  66.                 System.out.println("flg_frmwrk_mode" + flg_frmwrk_mode);
  67.             }
  68.         });
  69.         new_scale_in.setAnimationListener(new AnimationListener() {
  70.             @Override
  71.             public void onAnimationStart(Animation animation) {
  72.                 // TODO Auto-generated method stub
  73.             }
  74.             @Override
  75.             public void onAnimationRepeat(Animation animation) {
  76.                 // TODO Auto-generated method stub
  77.             }
  78.             @Override
  79.             public void onAnimationEnd(Animation animation) {
  80.                 // TODO Auto-generated method stub
  81.                 cusView.setVisibility(View.GONE);
  82.                 buttonimage.setVisibility(View.VISIBLE);
  83.                 buttonimage.setImageBitmap(third_icon_bmp);
  84.                 flg_frmwrk_mode = 3;
  85.                 buttonimage.startAnimation(in);
  86.             }
  87.         });
  88.     }

再接著是畫出各個形狀

 
 
 
 
  1. * 設(shè)置各個畫面的路徑
  2.    */
  3.   private void iconCreate() {
  4.       // Creating icons using path
  5.       // Create your own icons or feel free to use these
  6.       play = new Path();
  7.       play.moveTo(pix * 40 / 100, pix * 36 / 100);
  8.       play.lineTo(pix * 40 / 100, pix * 63 / 100);
  9.       play.lineTo(pix * 69 / 100, pix * 50 / 100);
  10.       play.close();
  11.       stop = new Path();
  12.       stop.moveTo(pix * 38 / 100, pix * 38 / 100);
  13.       stop.lineTo(pix * 62 / 100, pix * 38 / 100);
  14.       stop.lineTo(pix * 62 / 100, pix * 62 / 100);
  15.       stop.lineTo(pix * 38 / 100, pix * 62 / 100);
  16.       stop.close();
  17.       download_triangle = new Path();
  18.       download_triangle.moveTo(pix * 375 / 1000, (pix / 2)
  19.               + (pix * 625 / 10000) - (pix * 3 / 100));
  20.       download_triangle.lineTo(pix / 2, (pix * 625 / 1000)
  21.               + (pix * 625 / 10000) - (pix * 3 / 100));
  22.       download_triangle.lineTo(pix * 625 / 1000, (pix / 2)
  23.               + (pix * 625 / 10000) - (pix * 3 / 100));
  24.       download_triangle.close();
  25.       download_rectangle = new Path();
  26.       download_rectangle.moveTo(pix * 4375 / 10000, (pix / 2)
  27.               + (pix * 625 / 10000) - (pix * 3 / 100));
  28.       download_rectangle.lineTo(pix * 5625 / 10000, (pix / 2)
  29.               + (pix * 625 / 10000) - (pix * 3 / 100));
  30.       download_rectangle.lineTo(pix * 5625 / 10000, (pix * 375 / 1000)
  31.               + (pix * 625 / 10000) - (pix * 3 / 100));
  32.       download_rectangle.lineTo(pix * 4375 / 10000, (pix * 375 / 1000)
  33.               + (pix * 625 / 10000) - (pix * 3 / 100));
  34.       download_rectangle.close();
  35.       tick = new Path();
  36.       tick.moveTo(pix * 30 / 100, pix * 50 / 100);
  37.       tick.lineTo(pix * 45 / 100, pix * 625 / 1000);
  38.       tick.lineTo(pix * 65 / 100, pix * 350 / 1000);
  39.   }
  40.   /**
  41.    * 創(chuàng)建各個bitmap添加到framelayout中
  42.    */
  43.   public void init() {
  44.       // Defining and drawing bitmaps and assigning views to the layout
  45.       FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
  46.               FrameLayout.LayoutParams.WRAP_CONTENT,
  47.               FrameLayout.LayoutParams.WRAP_CONTENT);
  48.       lp.setMargins(10, 10, 10, 10);
  49.       fillcircle.setVisibility(View.GONE);
  50.       Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
  51.       Bitmap full_circle_bmp = Bitmap.createBitmap(pix, pix, conf);
  52.       Bitmap fill_circle_bmp = Bitmap.createBitmap(pix, pix, conf);
  53.       first_icon_bmp = Bitmap.createBitmap(pix, pix, conf); // Bitmap to draw
  54.                                                               // first icon(
  55.                                                               // Default -
  56.                                                               // Play )
  57.       second_icon_bmp = Bitmap.createBitmap(pix, pix, conf); // Bitmap to draw
  58.                                                               // second icon(
  59.                                                               // Default -
  60.                                                               // Stop )
  61.       third_icon_bmp = Bitmap.createBitmap(pix, pix, conf); // Bitmap to draw
  62.                                                               // third icon(
  63.                                                               // Default -
  64.                                                               // Tick )
  65.       Canvas first_icon_canvas = new Canvas(first_icon_bmp);
  66.       Canvas second_icon_canvas = new Canvas(second_icon_bmp);
  67.     
    文章標(biāo)題:Android入門篇——Button控件+自定義Button控件
    轉(zhuǎn)載源于:http://www.dlmjj.cn/article/djddise.html