怎么在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;public class MyTextView extends View { private String mTitleText; private int mTitleTextColor; private int mTitleTextSize; private Paint mPaint; private Rect mBound; public MyTextView(Context context) { this(context, null); } public MyTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } 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(); } }); } 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) { 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绘制一个四位数随机码的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网行业资讯频道,感谢各位的阅读!