自定义View+随机数点击变化
2017-10-08 19:22
113 查看
CustomTitleView
public class CustomTitleView extends View { /** * 文本 */ private String mTitleText; /** * 文本的颜色 */ private int mTitleTextColor; /** * 文本的大小 */ private int mTitleTextSize; /** * 绘制时控制文本绘制的范围 */ private Rect mBound; private Paint mPaint; public CustomTitleView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomTitleView(Context context) { this(context, null); } /** * 获得我自定义的样式属性 * * @param context * @param attrs * @param defStyle */ public CustomTitleView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); /** * 获得我们所定义的自定义样式属性 */ TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyle, 0); int n = a.getIndexCount(); for (int i = 0; i < n; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.CustomTitleView_titleText: mTitleText = a.getString(attr); break; case R.styleable.CustomTitleView_titleTextColor: // 默认颜色设置为黑色 mTitleTextColor = a.getColor(attr, Color.BLACK); break; case R.styleable.CustomTitleView_titleTextSize: // 默认设置为16sp,TypeValue也可以把sp转化为px mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); break; } } a.recycle(); /** * 获得绘制文本的宽和高 */ mPaint = new Paint(); mPaint.setTextSize(mTitleTextSize); // mPaint.setColor(mTitleTextColor); mBound = new Rect(); mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @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); 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(); } } attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="titleText" format="string" /> <attr name="titleTextColor" format="color" /> <attr name="titleTextSize" format="dimension" /> <declare-styleable name="CustomTitleView"> <attr name="titleText" /> <attr name="titleTextColor" /> <attr name="titleTextSize" /> </declare-styleable> </resources>main activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.demo12306.MainActivity">
<com.example.demo12306.CustomTitleViewandroid:id="@+id/tangao"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:titleText="3712"
custom:titleTextColor="#ff0000"
custom:titleTextSize="40sp" />
</RelativeLayout>
相关文章推荐
- 点击哪个,哪个下面的颜色变化
- Discuz!论坛后台无法登陆,点击登陆像刷新-iP变化问题
- android imageButton 点击按钮前中后,按钮颜色的变化
- ListView中item点击的时候颜色变化
- ViewPager+Fragment滑动或者点击导航颜色变化
- android按钮被点击文字颜色变化效果
- 在AndroidStudio中,使用selector完成按钮点击颜色变化功能
- 一行代码监听EditText变化改变Button可否点击
- Android 按钮被点击文字颜色变化效果
- 两个关联的GridView,点击其中一个,另一个的状态发生变化,联动操作
- ListView点击时按下后没有颜色变化原因
- Android小知识——按钮点击的变化
- tableViewCell 内部按钮点击事件 和 cell 高度变化操作
- 网页中点击链接后控制格式变化的代码——Dreamweaver
- Android控件在点击、选择时背景变化(button、listview)
- 点击某个功能 界面变化
- 如何在js中调用append里面点击标签变化的函数
- 让弹出窗口在屏幕中间显示,没有滚动条,并且点击某行,窗口大小不变化.
- 点击按钮,div发生各种样式变化
- jquery 点击查看更多箭头变化,文字变化,超出带滚动条。