具体代码如下所示:
//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=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(); } } 主页面布局 //在values建一个attrs.xml
效果图如下所示:
总结
以上所述是小编给大家介绍的Android自定义View实现箭头沿圆转动实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!