您好,登錄后才能下訂單哦!
這篇文章主要講解了“Android怎么自定義View實(shí)現(xiàn)球形動(dòng)態(tài)加速球”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Android怎么自定義View實(shí)現(xiàn)球形動(dòng)態(tài)加速球”吧!
利用貝塞爾曲線畫波浪線封閉黃色矩形,使用PorterDuffXfermode的SRC_ATOP只顯示圓和交疊部分,利用Handler發(fā)送消息模擬進(jìn)度形成動(dòng)態(tài)效果。
代碼:
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;//貝塞爾曲線周期循環(huán) 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); } @Override protected 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的canvas mPaintline.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));//顯示圓和交疊的部分 } @Override protected 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++){//循環(huán)形成10個(gè)周期的波浪封矩形上邊框 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(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case START: count++; progress-=4.5;//圓直徑450,按比例count 1=progress 4.5 if (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; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); circleView= (CircleView) findViewById(R.id.circleview); handler.sendEmptyMessage(START); } }
感謝各位的閱讀,以上就是“Android怎么自定義View實(shí)現(xiàn)球形動(dòng)態(tài)加速球”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Android怎么自定義View實(shí)現(xiàn)球形動(dòng)態(tài)加速球這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。