自定义View圆形加载器、变色、加速减速、开始暂停
来源:互联网 发布:松下fpplc编程软件 编辑:程序博客网 时间:2024/05/01 04:26
效果:
首先在valus下创建attrs.xml文件:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyCustomCircleArrowView"> <attr name="circlr_bound_width" format="dimension"></attr> <attr name="circlr_bound_color" format="color"></attr> </declare-styleable></resources>
继承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;/** * 类的作用和实现思路:自定义view * Created by 王 on 2017/10/9. */public class WxyView extends View{ //从xml中获取的颜色 private int circleBoundColor; private float circleBoundWidth; //当前画笔画圆的颜色 private int CurrenCircleBoundColor; private Paint paint; public WxyView(Context context) { super(context); initVeiw(context); } public WxyView(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 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.example.circulardemo.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.circulardemo.WxyView 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" /></RelativeLayout>
Mainactivity:
import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;public class MainActivity extends AppCompatActivity { private WxyView myCustomCircleArrowView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myCustomCircleArrowView = (WxyView) 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(); }}
完成!
阅读全文
0 0
- 自定义View圆形加载器、变色、加速减速、开始暂停
- 自定义View带箭头的圆环,加速 减速 暂停 变色
- 自定义View带箭头的圆环,加速 减速 暂停 变色
- 自定义View 自定义一个带箭头的圆环详解 加速 减速 暂停 变色
- 自定义View 自定义一个带箭头的圆环详解 加速 减速 暂停 变色
- 自定义View 自定义一个带箭头的圆环详解 加速 减速 暂停 变色
- 自定义View 箭头 变色 加速 减速
- 自定义圆形渐变加载百分比-开始-暂停
- 自定义view 圆 加速减速
- 自定义view自定义一个带箭头的圆形详解 加速 减速 变颜色
- 自定义View的画圆加速减速
- 自定义View的画圆加速减速
- 自定义view之实现圆形进度条加载,颜色渐变,可暂停,可循环加载
- 自定义圆加速减速
- 自定义半圆进度view实现油门加速减速
- java风扇(三个风扇-可开始、暂停、重置、加速、减速)
- 自定义view实现圆形加载
- 自定义View圆形加载显示
- LNMP
- linux中asmlinkage宏的解析
- 润泽智能创业项目“智慧城市用电安全监控云平台” 2017泉州创新创业大赛,勇夺小组第一!
- Android Error:(2, 0) No service of type Factory available in ProjectScopeServices.
- python1
- 自定义View圆形加载器、变色、加速减速、开始暂停
- 1000以内所以素数
- 如何在java程序中调用linux命令或者shell脚本
- RTP/RTSP/RTCP有什么区别
- restful-api开发规范
- 加班越久故障越多,如何跳出程序员的恶性循环?
- Linux下tar gz的含义
- centOS7 安装PHP5.6版本
- 【Python编程:从入门到实践】第三章:列表简介