Android自定義View實(shí)現(xiàn)球形動(dòng)態(tài)加速球
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)球形動(dòng)態(tài)加速球的具體代碼,供大家參考,具體內(nè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);
? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android定時(shí)器實(shí)現(xiàn)的幾種方式整理及removeCallbacks失效問題解決
本文為大家詳細(xì)介紹下Android 定時(shí)器實(shí)現(xiàn)的幾種方式:Handler + Runnable、Timer的方式、Handle與線程的sleep(long )方法和removeCallbacks失效問題如何解決2013-06-06
Android畫板開發(fā)之添加背景和保存畫板內(nèi)容為圖片
這篇文章主要為大家詳細(xì)介紹了Android畫板開發(fā)之添加背景和保存畫板內(nèi)容為圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android使用Notification實(shí)現(xiàn)通知功能
這篇文章主要為大家詳細(xì)介紹了Android使用Notification實(shí)現(xiàn)通知功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Flutter 底部彈窗如何實(shí)現(xiàn)多項(xiàng)選擇
在Flutter中提供了一個(gè)showModelBottomSheet方法用于彈出底部彈窗,本篇基于這個(gè)方法介紹實(shí)現(xiàn)底部彈窗多選的思路和方式。2021-06-06
Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡(jiǎn)單的視頻播放示例
這篇文章主要為大家介紹了Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡(jiǎn)單的視頻播放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
淺談Android性能優(yōu)化之內(nèi)存優(yōu)化
Android的內(nèi)存優(yōu)化是性能優(yōu)化中很重要的一部分,本文將詳細(xì)介紹Android性能優(yōu)化之內(nèi)存優(yōu)化。2021-06-06
Android關(guān)于BottomNavigationView使用指南
本文主要介紹了Android關(guān)于BottomNavigationView使用指南,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01

