圆环的颜色切换与速度控制
来源:互联网 发布:重庆大学教育网络 编辑:程序博客网 时间:2024/05/29 03:27
本次来为大家提供一下自定义控件的小知识,其中部分知识也是借鉴网络大神们的财富汇聚而成,
首先我们绘制自定义控件的类来继承view
来看一下代码实现
import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;import android.widget.Toast;import com.example.mycustomcirclearrowviewdemo.R;/** * Created by hasee on 2017/9/4. */public class MyCustomCircleArrowView extends View { //从xml中获取的颜色 private int circleBoundColor; private float circleBoundWidth; //当前画笔画圆的颜色 private int CurrenCircleBoundColor; private Paint paint; public MyCustomCircleArrowView(Context context) { super(context); initVeiw(context); } public MyCustomCircleArrowView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initVeiw(context); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCustomCircleArrowView); for (int i = 0; i < typedArray.getIndexCount(); i++) { //就是我们自定义的属性的资源id int attr = typedArray.getIndex(i); switch (attr) { case R.styleable.MyCustomCircleArrowView_circlr_bound_color: circleBoundColor = typedArray.getColor(attr, Color.RED); CurrenCircleBoundColor = circleBoundColor; break; case R.styleable.MyCustomCircleArrowView_circlr_bound_width: circleBoundWidth = typedArray.getDimension(attr, 3); break; } } } private void initVeiw(Context context) { paint = new Paint(); } public void setColor(int color) { if (CurrenCircleBoundColor != color) { CurrenCircleBoundColor = color; } else { CurrenCircleBoundColor = circleBoundColor; } } //圆心 private float pivotX; private float pivotY; private float radius = 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); 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(); } } 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布局
<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.mycustomcirclearrowviewdemo.view.MyCustomCircleArrowView android:id="@+id/my_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" app:circlr_bound_color="@color/colorAccent" app:circlr_bound_width="3dp" />
最后在主类实现一下控制
import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import com.example.mycustomcirclearrowviewdemo.view.MyCustomCircleArrowView;public class MainActivity extends AppCompatActivity { private MyCustomCircleArrowView myCustomCircleArrowView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myCustomCircleArrowView = (MyCustomCircleArrowView) findViewById(R.id.my_view); } public void onClick(View view) { myCustomCircleArrowView.setColor(Color.BLUE); } public void add(View view) { myCustomCircleArrowView.speed(); } public void slow(View view) { myCustomCircleArrowView.slowDown(); } public void pauseOrStart(View view) { myCustomCircleArrowView.pauseOrStart(); }}完美的一顿CV即可实现非常faction的旋转圆环的控制
阅读全文
0 0
- 圆环的颜色切换与速度控制
- 控制ViewPager的切换速度
- 控制ViewPager的切换速度
- 控制ViewPager的切换速度
- viewpager控制切换速度
- 速度控制与转矩控制的区别
- 速度控制与转矩控制的区别
- 自主导航与远程控制中的速度优化velocity_smoother与多路输入切换cmd_vel_mux的层次关系(ros/ cmd_vel)
- css控制表格之间行之间的颜色切换
- 自定义圆环进度条,可改变外层圆环颜色,重置与开始
- android自定义控件之三角绕圆环旋转 +改变速度+改变颜色
- iOS 颜色渐变圆环
- 我的IOS库-任意颜色渐变的圆环
- 控制游戏的速度
- 帧速度的控制
- 动画速度的控制
- Android画个颜色渐变的圆环玩玩
- android画多种颜色的圆环带动画效果
- idea创建springboot项目图文教程(四)
- python--异常处理
- AJAX第一步:AJAX接收返回类型为text/html的字符串数据
- matlab2c使用c++实现matlab函数系列教程-abs函数
- SSH整合(配置文件)
- 圆环的颜色切换与速度控制
- Codeforces 851A
- 网易2018校招内推--整数数列
- pkg-config命令使用
- <netty权威指南>笔记-分隔符解码器处理半包问题
- JavaScript专业八级测试,你能做对几道?
- Ubuntu系统如何更改用户密码
- HTML常用标签
- easyui Combotree默认选中第一项