Android自定义View实现箭头沿圆转动实例代码

 更新时间:2017年09月28日 15:59:23   作者:zhao__na  
这篇文章主要介绍了Android自定义View实现箭头沿圆转动实例代码,需要的朋友可以参考下

具体代码如下所示:

//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();
 }
}
主页面布局
<RelativeLayout
 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">
 <Button
  android:id="@+id/set_color_btn"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:onClick="onClick"
  android:text="设置颜色" />
 <Button
  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:onClick="add"
  android:text="加速" />
 <Button
  android:id="@+id/slow"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@+id/add"
  android:layout_centerHorizontal="true"
  android:onClick="slow"
  android:text="减速" />
 <Button
  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:onClick="pauseOrStart"
  android:text="暂定/开始" />
  <com.example.lx_20170928.MyCircleView
   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"
   />
</RelativeLayout>
//在values建一个attrs.xml
<resources>
 <declare-styleable name="MyCustomCircleArrowView">
  <attr name="circlr_bound_width" format="dimension"></attr>
  <attr name="circlr_bound_color" format="color"></attr>
 </declare-styleable>
</resources>

效果图如下所示:

总结

以上所述是小编给大家介绍的Android自定义View实现箭头沿圆转动实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • Android实用的Toast工具类封装

    Android实用的Toast工具类封装

    这篇文章主要为大家详细介绍了Android实用Toast工具类的封装,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-08-08
  • Android自定义控件实现万能的对话框

    Android自定义控件实现万能的对话框

    这篇文章主要为大家详细介绍了Android自定义控件实现万能对话框的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Android AIDL通信DeadObjectException解决方法示例

    Android AIDL通信DeadObjectException解决方法示例

    这篇文章主要为大家介绍了Android AIDL通信DeadObjectException解决的方法示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • flutter局部刷新的实现示例

    flutter局部刷新的实现示例

    这篇文章主要介绍了flutter局部刷新的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Android仿京东金融首页头像效果

    Android仿京东金融首页头像效果

    这篇文章主要为大家详细介绍了Android 仿京东金融首页头像效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Android自定义View实现动画效果详解

    Android自定义View实现动画效果详解

    这篇文章主要为大家详细介绍了Android如何通过自定义View实现动画效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-02-02
  • Android调用系统拍照裁剪图片模糊的解决方法

    Android调用系统拍照裁剪图片模糊的解决方法

    这篇文章主要为大家详细介绍了Android调用系统拍照裁剪图片模糊的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android开发人脸识别登录功能

    Android开发人脸识别登录功能

    这篇文章主要介绍了Android开发人脸识别登录功能,这个很多公司都在使用,非常流行,今天小编给大家从头到尾做一个案例分享到脚本之家平台,需要的朋友参考下吧
    2019-11-11
  • Android中监听短信的两种方法

    Android中监听短信的两种方法

    这篇文章主要介绍了Android中监听短信的两种方法,本文讲解了监听广播、采用观察方法,监听短信数据库两种方法,需要的朋友可以参考下
    2015-04-04
  • Adapter实现ListView带多选框等状态的自定义控件的注意事项

    Adapter实现ListView带多选框等状态的自定义控件的注意事项

    Android本身为ListView提供了几个方便的Adapter,比如ArrayAdapter、SimpleCurrentAdapter等等,接下来介绍自定义Adapter实现ListView带多选框等状态控件的注意事项,感兴趣的朋友可以详细了解下,或许对你有所帮助
    2013-01-01

最新评论