Android怎么自定义View实现球形动态加速球
短信预约 -IT技能 免费直播动态提醒
这篇文章主要讲解了“Android怎么自定义View实现球形动态加速球”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android怎么自定义View实现球形动态加速球”吧!
利用贝塞尔曲线画波浪线封闭黄色矩形,使用PorterDuffXfermode的class="lazy" data-src_ATOP只显示圆和交叠部分,利用Handler发送消息模拟进度形成动态效果。
代码:
public class CircleView extends View{private int width;private int height;private Bitmap mBitmap;private Canvas canvasBit;private Paint mPaintCircle;private Paint mPaintline;private int count;private Path path;private int progress;private int Maxprogress;public void setMaxprogress(int maxprogress) {Maxprogress = maxprogress; }public void setProgress(int progress) {this.progress = progress; }public int getCount() {return count; }public void setCount(int count) {this.count = count;//贝塞尔曲线周期循环 invalidate(); }public CircleView(Context context) {super(context); }public CircleView(Context context, AttributeSet attrs) {super(context, attrs);mPaintCircle=new Paint();mPaintCircle.setColor(Color.BLUE);mPaintCircle.setAntiAlias(true);mPaintCircle.setStyle(Paint.Style.FILL);path=new Path();mPaintline=new Paint();mPaintline.setAntiAlias(true);mPaintline.setStyle(Paint.Style.FILL);mPaintline.setColor(Color.YELLOW); }@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);width= getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);height =getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec); setMeasuredDimension(width,height);mBitmap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);canvasBit=new Canvas(mBitmap);//新建bitmap的canvasmPaintline.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.class="lazy" data-src_ATOP));//显示圆和交叠的部分 }@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas); canvas.drawColor(Color.BLACK);//背景色canvasBit.drawCircle(300, 300, 150, mPaintCircle);path.reset();path.moveTo(500, progress);//矩形右边框到圆底部path.lineTo(500, 500);//右边框path.lineTo(count, 500);//下边框path.lineTo(count, progress);//左边框for (int i=0;i<10;i++){//循环形成10个周期的波浪封矩形上边框path.rQuadTo(20,5,50,0);path.rQuadTo(20,-5,50,0); }canvasBit.drawPath(path, mPaintline); canvas.drawBitmap(mBitmap,0,0,null); }}//Handler代码如下public class MainActivity extends Activity {private CircleView circleView;private int count;private int progress=450;private static final int START=0x23;private Handler handler=new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what){case START:count++;progress-=4.5;//圆直径450,按比例count 1=progress 4.5if (count<=100){circleView.setCount(count);circleView.setProgress(progress);handler.sendEmptyMessageDelayed(START,20); }else {count=0;progress=450;circleView.setProgress(progress);circleView.setCount(count);handler.sendEmptyMessageDelayed(START,20); }break; } } };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);circleView= (CircleView) findViewById(R.id.circleview);handler.sendEmptyMessage(START); }}
感谢各位的阅读,以上就是“Android怎么自定义View实现球形动态加速球”的内容了,经过本文的学习后,相信大家对Android怎么自定义View实现球形动态加速球这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341