具体代码如下所示:
//mycircleview类
public class mycircleview extends view{
//当前画笔画圆的颜色
private int currencircleboundcolor;
private paint paint;
从xml中获取的颜色
private int circlebundcolor;
private float circleboundwidth;
private float pivotx;
private float pivoty;
private float radius=130;
private float currentdegree=0;
private int currentspeed=1;
private boolean ispause=false;
public mycircleview(context context) {
super(context);
initview(context);
}
public mycircleview(context context, @nullable attributeset attrs) {
super(context, attrs);
initview(context);
typedarray typedarray = context.obtainstyledattributes(attrs, r.styleable.mycircleview);
for (int i = 0; i < typedarray.getindexcount(); i++) {
//就是我们自定义的属性的资源id
int attr = typedarray.getindex(i);
switch (attr){
case r.styleable.mycircleview_circlr_bound_color:
circlebundcolor = typedarray.getcolor(attr, color.red);
currencircleboundcolor=circlebundcolor;
break;
case r.styleable.mycircleview_circlr_bound_width:
circleboundwidth = typedarray.getdimension(attr, 3);
break;
}
}
}
public mycircleview(context context, @nullable attributeset attrs, int defstyleattr) {
super(context, attrs, defstyleattr);
initview(context);
}
private void initview(context context){
paint = new paint();
}
public void setcolor(int color){
if (currencircleboundcolor!=color){
currencircleboundcolor=color;
}else {
currencircleboundcolor=circlebundcolor;
}
}
@override
protected void ondraw(canvas canvas) {
super.ondraw(canvas);
paint.setantialias(true);
paint.setcolor(currencircleboundcolor);
paint.setstrokewidth(circleboundwidth);
paint.setstyle(paint.style.stroke);
pivotx = getwidth() / 2;
pivoty = getheight() / 2;
canvas.drawcircle(pivotx,pivoty,radius,paint);
canvas.save();
//旋转画布 , 如果旋转的的度数大的话,视觉上看着是旋转快的
canvas.rotate(currentdegree,pivotx,pivoty);
//提供了一些api可以用来画线(画路径)
path path = new path();
//从哪开始画 从a开始画
path.moveto(pivotx+radius,pivoty);
//从a点画一个直线到d点
path.lineto(pivotx+radius-20,pivoty-20);
//从d点画一个直线到b点
path.lineto(pivotx+radius,pivoty+20);
//从b点画一个直线到c点
path.lineto(pivotx+radius+20,pivoty-20);
//闭合 -- 从c点画一个直线到a点
path.close();
paint.setstyle(paint.style.fill);
paint.setcolor(color.black);
canvas.drawpath(path,paint);
canvas.restore();
//旋转的度数一个一个度数增加, 如果乘以一个速度的话,按一个速度速度增加
currentdegree+=1*currentspeed;
if (!ispause){
invalidate();
}
}
public void speed(){
++currentspeed;
if (currentspeed>=10){
currentspeed=10;
toast.maketext(getcontext(),"我比闪电还快",toast.length_short).show();
}
}
public void slowdown(){
--currentspeed;
if (currentspeed<=1){
currentspeed=1;
}
}
public void pauseorstart(){
//如果是开始状态的话去重新绘制
if (ispause){
ispause=!ispause;
invalidate();
}else {
ispause=!ispause;
}
}
}
//主页面
public class mainactivity extends appcompatactivity {
//全局变量
private mycircleview my_view;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
//找控件
my_view = (mycircleview) findviewbyid(r.id.my_view);
}
public void onclick(view view){
my_view.setcolor(color.blue);
}
public void add(view view){
my_view.speed();
}
public void slow(view view){
my_view.slowdown();
}
public void pauseorstart(view view){
my_view.pauseorstart();
}
}
主页面布局
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lx_20170928.mainactivity">
android:id="@+id/set_color_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerhorizontal="true"
android:οnclick="onclick"
android:text="设置颜色" />
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/set_color_btn"
android:layout_centerhorizontal="true"
android:οnclick="add"
android:text="加速" />
android:id="@+id/slow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/add"
android:layout_centerhorizontal="true"
android:οnclick="slow"
android:text="减速" />
android:id="@+id/pause_or_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/slow"
android:layout_centerhorizontal="true"
android:οnclick="pauseorstart"
android:text="暂定/开始" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_view"
android:layout_centerinparent="true"
app:circlr_bound_color="@color/coloraccent"
app:circlr_bound_width="3dp"
/>
//在values建一个attrs.xml
效果图如下所示:
总结
以上所述是小编给大家介绍的android自定义view实现箭头沿圆转动实例代码,希望对大家有所帮助