android自訂控制項系列教程----真正的圓角button來了

來源:互聯網
上載者:User

標籤:android   button   圓角   

前沿:現在網上隨便輸入一句圓角button就會出現很多部落格和文章提示做這樣的一個效果,但是那多半都是xml檔案來做的,這樣做有個很大的弊端,因為每一次都需要重寫xml檔案(就連簡簡單單的修改個按鈕的顏色也需要修改)。~~為什麼呢?因為不修改臣妾做不到啊!!!今天就帶大家做一個真正的圓角button,我們還是來看效果吧。本文乾貨開始:很明顯我們的按鈕的背景就是我們要實現的圓角部分,那麼我們情不自禁的想到了setBackground這個方法,看看裡面的參數,需要的是一個Drawable,而我們的按鈕還需要點擊停用圓角效果,所以我們很自然的想到了StateListDrawable這個類 ,而且還要是圓角效果,那麼就還需要RoundRectShape這個類和ShapeDrawable這個類,如果這些類不熟悉的,那麼我就簡單的介紹一下這些類的作用。StateListDrawable主要用來添加和管理背景每個狀態應該使用那個Drawable的類,需要注意的是的它裡面的每個狀態的管理。其實可以可以理解為一個hashmap每個狀態對應一個值而已。RoundRectShape還記得我們需要一個圓角矩形嗎?對這個類就是我們主要的畫圓角矩形的類。但是要注意它的構造方法,到時候代碼裡面再講。ShapeDrawable狀態管理和形狀都有了,那麼我們的豬腳就要登場了,對就是ShapeDrawble它就是我們setBackground裡面需要的Drawble啊。好了,每個類的作用都介紹了那麼我們就直接上代碼吧。
import android.content.Context;import android.content.res.ColorStateList;import android.graphics.Color;import android.graphics.drawable.ShapeDrawable;import android.graphics.drawable.StateListDrawable;import android.graphics.drawable.shapes.RoundRectShape;import android.view.Gravity;import android.widget.Button;/* * @FileName:StyleButton.java * @Version:V1.0 * @Date: 2014-5-7 Create * @author: edsheng  */public class StyleButton extends Button{public static int[]mNormalState= new int[] {};public static int[]mPressState= new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };public static int[]mDisableState= new int[] { -android.R.attr.state_enabled };public static int[]mSelectedState= new int[] { android.R.attr.state_selected, android.R.attr.state_enabled };private intmRadius= 0;//預設的圓角半徑//預設文字和背景顏色private intmBgNormalColor= Color.RED;private intmBgPressedColor= Color.GREEN;private intmTextNormalColor= Color.WHITE;private intmTextPressedColor= Color.GRAY;public StyleButton(Context context){super(context);initUI();}private void initUI(){setGravity(Gravity.CENTER);buildDraweableState();buildColorDrawableState();}/** * 構建圖片drawble */private void buildColorDrawableState(){ColorStateList colorStateList = new ColorStateList(new int[][] { mPressState, mNormalState },new int[] { mTextPressedColor, mTextNormalColor });setTextColor(colorStateList);}/** * 構建背景Drawble */private void buildDraweableState(){float outRectr[] = new float[] { mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius };//建立狀態管理器StateListDrawable drawable = new StateListDrawable();/** * 注意StateListDrawable的構造方法我們這裡使用的 * 是第一參數它是一個float的數組儲存的是圓角的半徑,它是按照top-left順時針儲存的八個值 *///建立圓弧形狀RoundRectShape rectShape = new RoundRectShape(outRectr, null, null);//建立drawableShapeDrawable pressedDrawable = new ShapeDrawable(rectShape);//設定我們按鈕背景的顏色pressedDrawable.getPaint().setColor(mBgPressedColor);//添加到狀態管理裡面drawable.addState(mPressState, pressedDrawable);//ShapeDrawable disableDrawable = new ShapeDrawable(rectShape);//disableDrawable.getPaint().setColor(prssedClor);//disableDrawable.getPaint().setAlpha(125);//drawable.addState(mDisableState, disableDrawable);ShapeDrawable normalDrawable = new ShapeDrawable(rectShape);normalDrawable.getPaint().setColor(mBgNormalColor);drawable.addState(mNormalState, normalDrawable);//設定我們的背景,就是xml裡面的selectorsetBackgroundDrawable(drawable);}/** * 設定圓角矩形 *  * @param radius */public void setRadius(int radius){this.mRadius = radius;buildDraweableState();}/** * 設定按鈕背景顏色 *  * @param normalColor * @param prssedClor */public void setBgNormalPressedcolor(int normalColor, int prssedClor){mBgNormalColor = normalColor;mBgPressedColor = prssedClor;buildDraweableState();}/** * 設定按鈕文字顏色 *  * @param normalColor * @param pressedColor */public void setTextNormalPressedcolor(int normalColor, int pressedColor){mTextPressedColor = pressedColor;mTextNormalColor = normalColor;buildColorDrawableState();}}

這樣我們的圓角按鈕就建立起來了。看看我們怎麼使用它吧。直接貼出oncrate函數。
requestWindowFeature(Window.FEATURE_NO_TITLE);LinearLayout linearLayout = new LinearLayout(this);linearLayout.setOrientation(LinearLayout.VERTICAL);LinearLayout.LayoutParams layoutParams = new LayoutParams(200, 200);layoutParams.bottomMargin = 20;LayoutParams commomlayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);commomlayoutParams.bottomMargin = 20;StyleButton button = new StyleButton(this);button.setText("按鈕");linearLayout.addView(button, layoutParams);StyleButton button2 = new StyleButton(this);button2.setText("按鈕");button2.setRadius(16);linearLayout.addView(button2, commomlayoutParams);StyleButton button3 = new StyleButton(this);button3.setText("按鈕");button3.setRadius(32);button3.setTextNormalPressedcolor(Color.CYAN, Color.WHITE);button3.setBgNormalPressedcolor(Color.BLUE, Color.CYAN);linearLayout.addView(button3, commomlayoutParams);StyleButton button4 = new StyleButton(this);button4.setText("按鈕");button4.setRadius(80);button4.setBgNormalPressedcolor(Color.GRAY, Color.CYAN);button4.setTextNormalPressedcolor(Color.RED, Color.WHITE);linearLayout.addView(button4, commomlayoutParams);StyleButton button5 = new StyleButton(this);button5 = new StyleButton(this);button5.setText("按鈕");button5.setRadius(50);button5.setTextNormalPressedcolor(Color.BLACK, Color.BLUE);button5.setBgNormalPressedcolor(Color.WHITE, Color.CYAN);linearLayout.addView(button5, commomlayoutParams);StyleButton button6 = new StyleButton(this);button6.setText("按鈕");button6.setRadius(50);button6.setTextNormalPressedcolor(Color.BLACK, Color.CYAN);button6.setBgNormalPressedcolor(Color.CYAN, Color.BLUE);LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);params.leftMargin = 80;params.rightMargin = 80;linearLayout.addView(button6, params);StyleButton button7 = new StyleButton(this);button7.setText("按鈕");button7.setRadius(80);linearLayout.addView(button7, layoutParams);setContentView(linearLayout);
是不是使用起來方便快捷呢?不管是換顏色還是換圓角半徑這樣我們就自訂好了,自己的一個圓角按鈕。好了。拿去愉快的使用吧。

android自訂控制項系列教程----真正的圓角button來了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.