一值有在用网上朋友贴出来的代码,很方便,也能学到很多东西. 这次我也贴点比较简单使用的小东西. 不怎么形容 有图有代码 自己看真想...

主体代码

package com.suncco.taoxie;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.util.Log;import android.view.View;public class MyView extends View {private int count;private float space, radii;private int point_normal_color, point_seleted_color;// 选中private int selected = 0;// background seleted normalpublic MyView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView);count = a.getInteger(R.styleable.MyView_count, 3);space = a.getDimension(R.styleable.MyView_space, 9);radii = a.getDimension(R.styleable.MyView_point_radii, 9);point_normal_color = a.getColor(R.styleable.MyView_point_normal_color,0x000000);point_seleted_color = a.getColor(R.styleable.MyView_point_seleted_color, 0xffff07);int sum = attrs.getAttributeCount();a.recycle();}public void setCount(int count) {this.count = count;invalidate();}public void next() {if (selected < count - 1)selected++;elseselected = 0;invalidate();}public void previous() {if (selected > 0)selected--;elseselected = count - 1;invalidate();}@Overrideprotected void onDraw(Canvas canvas) {Paint paint = new Paint();paint.setAntiAlias(true);// 起始位置,实现整体居中float w = canvas.getWidth() - (count * 2 * radii) - space * (count - 1);for (int i = 0; i < count; i++) {if (i == selected)paint.setColor(point_seleted_color);elsepaint.setColor(point_normal_color);canvas.drawCircle(w / 2.f + radii + i * (space + radii + radii),radii + 1, ((int) radii + 2) / 2, paint);}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);setMeasuredDimension(widthMeasureSpec, (int) (radii * 2) + 2);}public void setSelected(int selectedId) {if (selectedId >= 0 && selectedId <= count)this.selected = selectedId;else if (selectedId < 0)this.selected = 0;else if (selectedId > count)this.selected = count;invalidate();}}

自定义View 的自定义命名空间:

以下自定义属性 count: 总数, space: 每个点的距离大小之类的等等 名字取得都比较通俗了...

这个文件xml放在 value 下的  ***_attrs.xml  比如我这里去  haowuliaoa_attrs.xml
<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="MyView"><attr name="count" format="integer" />  <attr name="space" format="dimension" /><attr name="point_size" format="dimension" /><attr name="point_seleted_color" format="color|reference" /><attr name="point_normal_color" format="color|reference" /><attr name="point_radii" format="dimension" /></declare-styleable></resources>

一切ok了, 可以直接在xml布局上应用咯...

首先要在布局的头 添加上自己的命名空间xmlns:haowuliaoa="http://schemas.android.com/apk/res/自己的包名"
然后就是xml布局咯...
<包名.MyView android:id="@+id/myView" android:layout_width="fill_parent" android:layout_height="10dip" android:background="#00000000" android:gravity="center" android:layout_marginBottom="4dip" haowuliaoa_attrs:count="6" suncco:space="10dip" haowuliaoa_attrs:point_size="4dip" haowuliaoa_attrs:point_seleted_color="#ff0000" haowuliaoa_attrs:point_normal_color="#ffffff" haowuliaoa_attrs:point_radii="5dip" />

so 简单吧....

图片下面的小点就是今天的效果哦 很常用的

功能却是经常能用到的哦 ...

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. Android开发之侧拉栏的使用
  3. Android(安卓)Activity加载Fragment的一般简易方法
  4. PopupWindow软键盘弹出,上移
  5. android4.0下使用multiDexEnabled属性的踩坑日记
  6. 使用Android(安卓)Ant在编译时混淆
  7. Android之googleMap 2
  8. Android(安卓)github开源进度条类框架SmoothProgressBar的使用
  9. Android下载完成更新后,没有打开安装成功界面,出现闪退问题

随机推荐

  1. Android SQLiteOpenHelper Sqlite数据库
  2. Mac下关于The "android" command is depr
  3. android RSA和Java RSA加密不一致的坑
  4. android配置X86虚拟机
  5. Android数字跑动效果NumberRunView 类似
  6. 睡眠唤醒机制简介
  7. 禁止android显示状态栏
  8. Android兼容android7.0、及Android8.0以
  9. android could not find adb.exe 如何解
  10. Android中文API(138) —— RemoteViews