自定义View 自定义一个带箭头的圆环详解 加速 减速 暂停 变色
来源:互联网 发布:pc刷mac 编辑:程序博客网 时间:2024/04/29 06:48
今天分享给大家一个带箭头会旋转的自定义view实现起来比较简单,效果是这样的
首先我们新建一个class继承view
public class MyCustomView extends View
重写三个构造方法
public class MyCustomView extends View { public MyCustomView(Context context) { this(context,null); } public MyCustomView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public MyCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }}
这里前两个方法有修改不要忘记
然后在values目录下新建一个attrs.xml文件
自定义属性
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyCustomView"> <attr name="viewSize" format="dimension"></attr> <attr name="viewColor" format="color"></attr> </declare-styleable></resources>
布局中
<com.lx_customviewone.views.MyCustomView android:id="@+id/view_custom" android:layout_width="wrap_content" android:layout_height="wrap_content" app:viewColor="@color/colorAccent" app:viewSize="5dp" />
然后我们回到MyCustomView类中在第三个构造里面自定义属性
private int msize; private int mcolor; private int CurrenColor; private Paint paint; private float pivotX; private float pivotY; //设置半径 private float radius = 150; private float currentDegree = 0;
public MyCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyleAttr, 0); for (int i = 0; i < typedArray.getIndexCount(); i++) { //就是我们自定义的属性的资源id int attr = typedArray.getIndex(i); switch (attr){ case R.styleable.MyCustomView_viewSize: msize = typedArray.getDimensionPixelSize(attr, 10); break; case R.styleable.MyCustomView_viewColor: mcolor = typedArray.getColor(attr, Color.BLACK); break; } } typedArray.recycle(); paint = new Paint(); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //设置抗锯齿 paint.setAntiAlias(true); paint.setColor(mcolor); paint.setStrokeWidth(msize); //设置显示风格为空心圆 paint.setStyle(Paint.Style.STROKE); pivotX = getWidth() / 2; pivotY = getHeight() / 2; canvas.drawCircle(pivotX, pivotY,radius,paint); canvas.save(); //旋转画布 , 如果旋转的的度数大的话,视觉上看着是旋转快的 canvas.rotate(currentDegree,pivotX,pivotY); }
做到这里已经出现圆圈了
然后我们接在刚才onDraw()方法里面继续写
//提供了一些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.BLUE); canvas.drawPath(path, paint); canvas.restore(); //旋转的度数一个一个度数增加, 如果乘以一个速度的话,按一个速度速度增加 currentDegree += 1*currentSpeed; invalidate();
这一段如果看不懂的话可以看看我的图
现在一个带箭头旋转图已经做好了
然后我么再给他实现一些效果
在布局中加几个按钮
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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" tools:context="com.lx_customviewone.MainActivity"> <Button android:id="@+id/set_color_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" 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: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: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:text="暂定/开始" /> <com.lx_customviewone.views.MyCustomView android:id="@+id/view_custom" android:layout_width="wrap_content" android:layout_height="wrap_content" app:viewColor="@color/colorAccent" app:viewSize="5dp" /></RelativeLayout>
然后我们在自定义的view中加上这几个方法
定义一个int值接收颜色
private int CurrenColor;
然后我们让mcolor = CurrenColor
然后重写一个方法
public void setColor(int color){ if(color!=CurrenColor){ CurrenColor = color; }else{ mcolor = CurrenColor; } }
//加速 public void setspeed(){ currentSpeed++; if(currentSpeed>=11){ currentSpeed = 11; Toast.makeText(getContext(), "快如闪电了", Toast.LENGTH_SHORT).show(); } } //减速 public void setslowdown(){ currentSpeed--; if(currentSpeed<=1){ currentSpeed = 1; Toast.makeText(getContext(), "你是要停吗?", Toast.LENGTH_SHORT).show(); } }
我们设置一个flag
private int flag = 1;
把onDeaw方法最后的invalidate();限制起来
if(flag ==1){ invalidate(); }
然后写一个方法实现暂停和开始
public void setpauseOrStart(){ switch (flag){ case 0: flag = 1; invalidate(); break; case 1: flag = 0; break; } }
MainActivity:
package com.lx_customviewone;import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import com.lx_customviewone.views.MyCustomView;public class MainActivity extends AppCompatActivity implements View.OnClickListener { /** * 设置颜色 */ private Button mSetColorBtn; /** * 加速 */ private Button mAdd; /** * 减速 */ private Button mSlow; /** * 暂定/开始 */ private Button mPauseOrStart; private MyCustomView mViewCustom; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mSetColorBtn = (Button) findViewById(R.id.set_color_btn); mSetColorBtn.setOnClickListener(this); mAdd = (Button) findViewById(R.id.add); mAdd.setOnClickListener(this); mSlow = (Button) findViewById(R.id.slow); mSlow.setOnClickListener(this); mPauseOrStart = (Button) findViewById(R.id.pause_or_start); mPauseOrStart.setOnClickListener(this); mViewCustom = (MyCustomView) findViewById(R.id.view_custom); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.set_color_btn: mViewCustom.setColor(Color.GREEN); break; case R.id.add: mViewCustom.setspeed(); break; case R.id.slow: mViewCustom.setslowdown(); break; case R.id.pause_or_start: mViewCustom.setpauseOrStart(); break; } }}
好了大功告成效果如图
阅读全文
1 0
- 自定义View 自定义一个带箭头的圆环详解 加速 减速 暂停 变色
- 自定义View 自定义一个带箭头的圆环详解 加速 减速 暂停 变色
- 自定义View 自定义一个带箭头的圆环详解 加速 减速 暂停 变色
- 自定义View带箭头的圆环,加速 减速 暂停 变色
- 自定义View带箭头的圆环,加速 减速 暂停 变色
- 自定义view自定义一个带箭头的圆形详解 加速 减速 变颜色
- 自定义View 箭头 变色 加速 减速
- 自定义View圆形加载器、变色、加速减速、开始暂停
- 自定义一个带箭头的圆环
- 自定义 圆环加速变色
- 自定义view 圆环带箭头旋转
- 自定义View的画圆加速减速
- 自定义View的画圆加速减速
- 自定义view 圆 加速减速
- 自定义带箭头圆圈,自定义点击事件可以加减速,暂停继续,自动旋转
- 自定义view箭头绕圆环旋转
- 自定义view箭头绕圆环转动
- 自定义view箭头沿圆环持续转动
- 【资讯】东京券商所在地期待金融科技公司助其重振旗鼓
- 【国际】日本积极探索金融科技
- 【国际】直布罗陀金融监管者留意ICO增长并发布警告
- Ionic入门开发
- laydate范围选择,结束时间大于开始时间同时大于当前时间
- 自定义View 自定义一个带箭头的圆环详解 加速 减速 暂停 变色
- series.map() 映射值
- Linux-my_bash(cd)
- 【codevs 1080】线段树练习
- 数据结构学习笔记
- 长度扩展攻击详解
- 【观点】前CFTC委员:监管可以解决比特币波动
- 【资讯】乌拉圭宣布新的数字货币试点
- 【动态】万事达卡暗示计划开发区块链结算系统