您好,登錄后才能下訂單哦!
怎么在Android中通過自定義View繪制一個四位數隨機碼?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
首先在res/values文件夾下建利attrs.xml文件,由于這次我們功能決定我們要提供三個自定義屬性,分別是textTitle String類型的,textColor是color類型的,textSize是dimetion類型,代碼如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyTextView"> <attr name="titleText" format="string"/> <attr name="titleTextColor" format="color"/> <attr name="titleTextSize" format="dimension"/> </declare-styleable> </resources>
再來看看我們怎么在布局文件中的自定義控件中去使用我們自定義的屬性
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.qianmo.VerificationCode" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <com.qianmo.VerificationCode.view.MyTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="100dp" custom:titleText="3712" custom:titleTextColor="#ff0000" android:layout_centerInParent="true" custom:titleTextSize="40sp"/> </RelativeLayout>
關鍵的兩句代碼:
xmlns:custom=”http://schemas.android.com/apk/res/com.qianmo.VerificationCode” 添加自定義的空間名,com.qianmo.VerificationCode使我們的包名,使用是以custom:開頭 ,例如:custom:titleTextSize
現在自定義的屬性搞定了,開始我們的自定義View吧,首先選擇,我們繼承的是View還是ViewGroup,很明顯,這次我們是一個簡單的View,所以選擇繼承View,下面直接貼出來代碼了,每一步代碼里面都很詳細,就不多給大家解釋了
package com.qianmo.VerificationCode.view; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.view.View; import com.qianmo.VerificationCode.R; import java.util.HashSet; import java.util.Random; import java.util.Set; /** * Created by wangjitao on 2016/10/13 0013. * 用于實現獲取隨機碼 */ public class MyTextView extends View { /** * 由于是自定義的View,首先我們要確定那些屬性是用戶可以自己定義的 * 1,View里面顯示的字 * 2,顯示字的大小 * 3,顯示字的顏色 */ private String mTitleText; private int mTitleTextColor; private int mTitleTextSize; /** * 畫筆 */ private Paint mPaint; /** * view的矩形背景 */ private Rect mBound; public MyTextView(Context context) { this(context, null); } public MyTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } /** * 獲得自定義的屬性 * * @param context * @param attrs * @param defStyleAttr */ public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); /** * 獲得我們自定義的一些屬性 */ TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyTextView, defStyleAttr, 0); mTitleText = randomText(); //初始化顯示的數字 for (int i = 0; i < a.getIndexCount(); i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.MyTextView_titleText: mTitleText = a.getString(attr); break; case R.styleable.MyTextView_titleTextColor: mTitleTextColor = a.getColor(attr, Color.BLACK); break; case R.styleable.MyTextView_titleTextSize: //設置默認大小為16 mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); break; } } //將TypedArray對象回收 a.recycle(); /** * 初始化畫筆 */ mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setTextSize(mTitleTextSize); mPaint.setColor(mTitleTextColor); mBound = new Rect(); mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound); /** * 模仿點擊換驗證碼 */ this.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { mTitleText = randomText(); postInvalidate(); } }); } /** * 獲取四位隨機數驗證碼 * * @return */ private String randomText() { Random random = new Random(); Set<Integer> set = new HashSet<Integer>(); while (set.size() < 4) { int randomInt = random.nextInt(10); set.add(randomInt); } StringBuffer sb = new StringBuffer(); for (Integer i : set) { sb.append("" + i); } return sb.toString(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { /** * 處理當寬高都是wrap_content的情況 */ int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int width = 0; int height = 0; if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } else { mPaint.setTextSize(mTitleTextSize); mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound); float textWidth = mBound.width(); int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight()); width = desired; } if (heightMode == MeasureSpec.EXACTLY) { height = heightSize; } else { mPaint.setTextSize(mTitleTextSize); mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound); float textWidth = mBound.height(); int desired = (int) (getPaddingTop() + textWidth + getPaddingBottom()); height = desired; } setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { /** * 繪制文字和矩形 */ mPaint.setColor(Color.YELLOW); canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint); mPaint.setColor(mTitleTextColor); canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint); } }
看完上述內容,你們掌握怎么在Android中通過自定義View繪制一個四位數隨機碼的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。