本文实例为大家分享了Android仿微信刷新旋转小风车 具体代码,供大家参考,具体内容如下
不太会录像,没办法,智能截图了
不多说了,直接上代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172package com.shipneg.demoysp.demo; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.os.CountDownTimer; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.widget.ImageView; /** * Created by dell on 2017/4/7. */ public class RotationView extends ImageView { /** * 要转动的图片 **/ private Bitmap bitMap; /** * 风车每次转动的弧度 **/ private int rad = 0; /** * 风车移动的轨迹 **/ private int excursion = -100; /** * 图片的宽度:在这里提供的是正方形的图片,所以宽度和高度是一样的 **/ private int width = 0; /*** * 图片的高度:在这里提供的是正方形的图片,所以宽度和高度是一样的 **/ private int height = 0; /** * 定义一个画笔 **/ private Paint paint = new Paint(); public RotationView(Context context, AttributeSet attrs) { super(context, attrs); } public RotationView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public RotationView(Context context) { super(context); } /** * 获取图片的宽和高 */ public void initSize() { width = bitMap.getWidth(); height = bitMap.getHeight(); postInvalidate(); } public void setBitMap(Bitmap bitMap) { this.bitMap = bitMap; } //一图片的宽和高来设定自定义View的宽和高,由于是正方形宽和高是一样的 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); } CountDownTimer c = new CountDownTimer(5000, 10) { @Override public void onTick(long millisUntilFinished) { postInvalidate(); rad = rad + 7; } @Override public void onFinish() { downY = 0; excursion = -100; postInvalidate(); } }; /*** * 实现onDraw方法把风车图片绘制出来,同时绘制出来风车的旋转效果,通过Matrix来控制 */ @Override protected void onDraw(Canvas canvas) { Matrix matrix = new Matrix(); // 设置转轴位置 matrix.setTranslate((float) width / 2, (float) height / 2); // rad -=15;//每次旋转的弧度增量为3当然,数字越大转动越快 // 开始转 matrix.preRotate(rad); // 开始平移 matrix.postTranslate(0, excursion); // 转轴还原 matrix.preTranslate(-(float) width / 2, -(float) height / 2); //绘制风车图片 canvas.drawBitmap(bitMap, matrix, paint); super.onDraw(canvas); } private int downY = 0; private int moveY = 0; private int abc = 0; @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN://随着手指的move而不断进行重绘,进而让风车转动起来 postInvalidate();//调用方法进行重绘 downY = (int) event.getY(); c.cancel(); break; case MotionEvent.ACTION_MOVE://随着手指的move而不断进行重绘,进而让风车转动起来 //调用方法进行重绘 int movey2 = moveY; rad = (int) -event.getY() * 6;//旋转的速度 moveY = (int) (event.getY() - downY);//手指移动的距离 int chz = moveY - movey2; if (chz > 10) { chz = chz / 10; } else if (chz < -10) { chz = chz / 10; } Log.e("TAG:" + excursion, "chz: " + chz + "//moveY:" + moveY + "//movey2:" + movey2); //100是向下滑动的最大距离 if (excursion >= 100) { abc = abc + chz; if (chz < 0 && abc - chz < 0) { excursion = excursion + chz; } } else { //开始向下运动 excursion += chz; } postInvalidate(); c.cancel(); break; case MotionEvent.ACTION_UP: c.start(); break; } return true; } }
调用方法
复制代码
1
2
3
4
5//调用的方法 RotationView rotation = (RotationView) view.findViewById(R.id.rotationView); BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(R.drawable.fengche); rotation.setBitMap(drawable.getBitmap()); rotation.initSize();
图片资源自己切的,本人不会ps,所以有点切的不太好,见谅
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是动听刺猬最近收集整理的关于Android自定义view仿微信刷新旋转小风车的全部内容,更多相关Android自定义view仿微信刷新旋转小风车内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复