自定义一个带箭头旋转的圆圈 , 配置服务器后台
来源:互联网 发布:开淘宝网店策划书范文 编辑:程序博客网 时间:2024/05/19 02:42
public class MainActivity extends AppCompatActivity { private MyCustom mycustom; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mycustom = (MyCustom) findViewById(R.id.mycustom); } public void onClick(View view) { mycustom.seColor(Color.BLUE); } public void add(View view) { mycustom.speed(); } public void slow(View view) { mycustom.slowDown(); } public void pauseOrStart(View view) { mycustom.pauseOrStart(); }}
public class MyCustom extends View { //从xml中获取的颜色 private int circleBoundColor; private float circleBoundWidth; //当前画笔画圆的颜色 private int CurrenCircleBoundColor; private Paint paint; public MyCustom(Context context) { super(context); initView(context); } public MyCustom(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCustom); for (int i=0;i<typedArray.getIndexCount();i++){ int index = typedArray.getIndex(i); switch (index){ case R.styleable.MyCustom_custom_color: circleBoundColor= typedArray.getColor(index, Color.RED); CurrenCircleBoundColor=circleBoundColor; break; case R.styleable.MyCustom_custom_width: circleBoundWidth=typedArray.getDimension(index,3); break; } } } private void initView(Context context) { paint = new Paint(); } public void seColor(int color){ if (CurrenCircleBoundColor!=color) { CurrenCircleBoundColor=color; }else { CurrenCircleBoundColor=circleBoundColor; } } //圆心 private float pionX; private float pionY; //圆点 private float yuanxin=130; //圆框迅速转 private float currentDegree = 0; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setAntiAlias(true); paint.setColor(CurrenCircleBoundColor); paint.setStrokeWidth(circleBoundWidth); paint.setStyle(Paint.Style.STROKE); pionX=getWidth()/2; pionY=getHeight()/2; canvas.drawCircle(pionX,pionY,yuanxin,paint); canvas.save(); //旋转画布 , 如果旋转的的度数大的话,视觉上看着是旋转快的 canvas.rotate(currentDegree,pionX,pionY); //提供了一些api可以用来画线(画路径) Path path=new Path(); //从哪开始画 从A开始画 path.moveTo(pionX+yuanxin,pionY); //从A点画一个直线到D点 path.lineTo(pionX + yuanxin - 20, pionY - 20); //从D点画一个直线到B点 path.lineTo(pionX + yuanxin, pionY + 20); //从B点画一个直线到C点 path.lineTo(pionX + yuanxin + 20, pionY - 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(); } } private int currentSpeed = 1; private boolean isPause = false; 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; } }}
XML:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.fanyishuo.daijiantou_view.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.fanyishuo.daijiantou_view.MyCustom android:id="@+id/mycustom" android:layout_width="wrap_content" android:layout_height="wrap_content" app:custom_color="@color/colorAccent" app:custom_width="3dp" android:layout_gravity="center" /></LinearLayout>values下attrs:<resources> <declare-styleable name="MyCustom"> <attr name="custom_color" format="color"></attr> <attr name="custom_width" format="dimension"></attr> </declare-styleable>
阅读全文
0 0
- 自定义一个带箭头旋转的圆圈 , 配置服务器后台
- 自定义带箭头圆圈,自定义点击事件可以加减速,暂停继续,自动旋转
- 自定义View 带箭头的圆旋转
- 自定义一个带箭头的圆环
- 自定义view 圆环带箭头旋转
- 自定义View圆圈箭头_
- 自定义View之绕着圆圈的箭头~~
- 自定义View 自定义一个带箭头的圆环详解 加速 减速 暂停 变色
- 自定义View 自定义一个带箭头的圆环详解 加速 减速 暂停 变色
- 自定义View 自定义一个带箭头的圆环详解 加速 减速 暂停 变色
- 自定义view自定义一个带箭头的圆形详解 加速 减速 变颜色
- Android实现带箭头的自定义Progressbar
- Android : 自定义带箭头的Button
- 自定义带三角形箭头的TextView
- 自定义一个圆圈View
- 带动画的自定义view——做一个移动的箭头
- qt画旋转的带箭头的线段
- 一个三维的带箭头螺旋线
- Pointers on C——8 Arrays.16
- JSTL c:If 标签 test条件后追加一个空格导致判断失败
- Io 异常: The Network Adapter could not establish the connection解决方案
- Lucene学习一:入门级Demo,创建索引和查询高亮显示
- 前端的奇淫巧技
- 自定义一个带箭头旋转的圆圈 , 配置服务器后台
- 按位运算的使用技巧
- python文件操作
- 矢量数据下载【道路路网(公路、铁路)、水系、建筑物轮廓、地名等等】
- div+css 页面设计新样式
- Pointers on C——8 Arrays.17
- Pointers on C——8 Arrays.18
- ubuntu16 安装OpenCV2.4.13 (CUDA8.0)
- mysql安装