Android自定義view之太極圖的實(shí)現(xiàn)教程
太極圖
周四課余時(shí)間比較多,正好前幾天為了給小學(xué)弟解決問題,回顧了一些Android的知識(shí),(上學(xué)還是不能把以前上班學(xué)到的東西丟掉)于是寫一篇關(guān)于自定義view的文章。
最后完成的樣子(可旋轉(zhuǎn))
這篇文章主要內(nèi)容為使用Canvas畫簡(jiǎn)單圖案,自定義屬性,以及屬性動(dòng)畫ObjectAnimator中的旋轉(zhuǎn)動(dòng)畫
提示:以下是本篇文章正文內(nèi)容
一、先畫一個(gè)太極
先介紹一下定義的東西:
private int useWidth; //最后測(cè)量得到的值 private int leftcolor; //左太極的顏色(黑) private int rightcolor;//右太極的顏色(白)
1.第一步,畫一個(gè)半邊黑半邊白的圓
mPaint.setColor(leftcolor); canvas.drawArc(new RectF(0, 0, useWidth, useWidth), 270, -180, true, mPaint); mPaint.setColor(rightcolor); canvas.drawArc(new RectF(0, 0, useWidth, useWidth), 270, 180, true, mPaint);
效果:
2.第二步,畫黑白兩個(gè)小圓
mPaint.setColor(leftcolor); canvas.drawArc(new RectF(useWidth / 4, 0, useWidth / 2 +useWidth / 4, useWidth / 2), 270, 360, true, mPaint); mPaint.setColor(rightcolor); canvas.drawArc(new RectF(useWidth / 4, useWidth / 2, useWidth / 2 + useWidth / 4,useWidth), 270, 360, true, mPaint);
效果:
3.第三步,畫黑白兩個(gè)更小的圓
mPaint.setColor(leftcolor); canvas.drawCircle(useWidth/ 2, useWidth * 3 / 4, useWidth/16, mPaint); mPaint.setColor(rightcolor); canvas.drawCircle(useWidth / 2, useWidth / 4, useWidth/16, mPaint);
效果
二、讓太極旋轉(zhuǎn)
先簡(jiǎn)單介紹一下定義的東西
private ObjectAnimator objectAnimator;//屬性動(dòng)畫 private int animaltime;//動(dòng)畫的旋轉(zhuǎn)時(shí)間(速度)
1.旋轉(zhuǎn)的開始方法
//旋轉(zhuǎn)動(dòng)畫開始的方法 public void createAnimation() { if (objectAnimator==null){ objectAnimator = ObjectAnimator.ofFloat(this, "rotation", 0f, 360f);//添加旋轉(zhuǎn)動(dòng)畫,旋轉(zhuǎn)中心默認(rèn)為控件中點(diǎn) objectAnimator.setDuration(animaltime);//設(shè)置動(dòng)畫時(shí)間 objectAnimator.setInterpolator(new LinearInterpolator());//動(dòng)畫時(shí)間線性漸變 objectAnimator.setRepeatCount(ObjectAnimator.INFINITE); objectAnimator.setRepeatMode(ObjectAnimator.RESTART); objectAnimator.start();//動(dòng)畫開始 }else{ objectAnimator.resume();//動(dòng)畫繼續(xù)開始 } }
2.旋轉(zhuǎn)的暫停方法(可繼續(xù)旋轉(zhuǎn))
public void stopAnimation(){ if (objectAnimator!=null){ objectAnimator.pause();//動(dòng)畫暫停 .end()結(jié)束動(dòng)畫 } }
3.旋轉(zhuǎn)的停止方法(不可繼續(xù)旋轉(zhuǎn))
public void cleanAnimation(){ if (objectAnimator!=null){ objectAnimator.end(); //結(jié)束動(dòng)畫 } }
三、自定義屬性(顏色,動(dòng)畫速度)
1.新建attrs文件
2.定義屬性
<declare-styleable name="TaiJiView"> <attr name="leftcolor" format="reference|color"/> <attr name="rightcolor" format="reference|color"/> <attr name="animaltime" format="integer"/> </declare-styleable>
3.布局中使用
app:leftcolor="@color/colorAccent" app:rightcolor="@color/colorPrimaryDark" app:animaltime="3000"
4.java文件中獲取在布局中定義的值
/** 獲取自定義屬性 */ private void initCustomAttrs(Context context, AttributeSet attrs) { //獲取自定義屬性。 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TaiJiView); //獲取太極顏色 leftcolor = ta.getColor(R.styleable.TaiJiView_leftcolor, Color.BLACK); rightcolor=ta.getColor(R.styleable.TaiJiView_rightcolor, Color.WHITE); //時(shí)間 animaltime=ta.getInt(R.styleable.TaiJiView_animaltime,1000); //回收 ta.recycle(); }
最后運(yùn)行一下看一下效果
四、源碼
1.TaiJiView.java
public class TaiJiView extends View{ private Paint mPaint; private int mWidth; private int mHeight; private int useWidth; private int leftcolor; private int rightcolor; private ObjectAnimator objectAnimator; private int animaltime; @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public TaiJiView(Context context) { this(context,null); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public TaiJiView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public TaiJiView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr,0); initCustomAttrs(context,attrs); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public TaiJiView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(); } private void init() { initPaint(); } /** 獲取自定義屬性 */ private void initCustomAttrs(Context context, AttributeSet attrs) { //獲取自定義屬性。 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TaiJiView); //獲取太極顏色 leftcolor = ta.getColor(R.styleable.TaiJiView_leftcolor, Color.BLACK); rightcolor=ta.getColor(R.styleable.TaiJiView_rightcolor, Color.WHITE); animaltime=ta.getInt(R.styleable.TaiJiView_animaltime,1000); //回收 ta.recycle(); } /** * 初始化畫筆 */ private void initPaint() { mPaint = new Paint(); //創(chuàng)建畫筆對(duì)象 mPaint.setColor(Color.BLACK); //設(shè)置畫筆顏色 mPaint.setStyle(Paint.Style.FILL); //設(shè)置畫筆模式為填充 mPaint.setStrokeWidth(10f); //設(shè)置畫筆寬度為10px mPaint.setAntiAlias(true); //設(shè)置抗鋸齒 mPaint.setAlpha(255); //設(shè)置畫筆透明度 } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = w; mHeight = h; useWidth=mWidth; if (mWidth>mHeight){ useWidth=mHeight; } } @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(leftcolor); canvas.drawArc(new RectF(0, 0, useWidth, useWidth), 270, -180, true, mPaint); mPaint.setColor(rightcolor); canvas.drawArc(new RectF(0, 0, useWidth, useWidth), 270, 180, true, mPaint); mPaint.setColor(leftcolor); canvas.drawArc(new RectF(useWidth / 4, 0, useWidth / 2 +useWidth / 4, useWidth / 2), 270, 360, true, mPaint); mPaint.setColor(rightcolor); canvas.drawArc(new RectF(useWidth / 4, useWidth / 2, useWidth / 2 + useWidth / 4,useWidth), 270, 360, true, mPaint); mPaint.setColor(leftcolor); canvas.drawCircle(useWidth/ 2, useWidth * 3 / 4, useWidth/16, mPaint); mPaint.setColor(rightcolor); canvas.drawCircle(useWidth / 2, useWidth / 4, useWidth/16, mPaint); } @RequiresApi(api = Build.VERSION_CODES.KITKAT) public void createAnimation() { if (objectAnimator==null){ objectAnimator = ObjectAnimator.ofFloat(this, "rotation", 0f, 360f);//添加旋轉(zhuǎn)動(dòng)畫,旋轉(zhuǎn)中心默認(rèn)為控件中點(diǎn) objectAnimator.setDuration(animaltime);//設(shè)置動(dòng)畫時(shí)間 objectAnimator.setInterpolator(new LinearInterpolator());//動(dòng)畫時(shí)間線性漸變 objectAnimator.setRepeatCount(ObjectAnimator.INFINITE); objectAnimator.setRepeatMode(ObjectAnimator.RESTART); objectAnimator.start();//動(dòng)畫開始 }else{ objectAnimator.resume();//動(dòng)畫繼續(xù)開始 } } @RequiresApi(api = Build.VERSION_CODES.KITKAT) public void stopAnimation(){ if (objectAnimator!=null){ objectAnimator.pause();//動(dòng)畫暫停 .end()結(jié)束動(dòng)畫 } } public void cleanAnimation(){ if (objectAnimator!=null){ objectAnimator.end(); //結(jié)束動(dòng)畫 } } }
2.attrs文件
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TaiJiView"> <attr name="leftcolor" format="reference|color"/> <attr name="rightcolor" format="reference|color"/> <attr name="animaltime" format="integer"/> </declare-styleable> </resources>
總結(jié)
希望能夠?qū)δ兴鶐椭?。如有疑問可留言。歡迎各位前輩點(diǎn)評(píng)
到此這篇關(guān)于Android自定義view之太極圖的文章就介紹到這了,更多相關(guān)Android自定義view之太極圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- canvas雪花效果核心代碼分享
- Canvas實(shí)現(xiàn)動(dòng)態(tài)的雪花效果
- jquery實(shí)現(xiàn)漫天雪花飛舞的圣誕祝福雪花效果代碼分享
- android自定義view實(shí)現(xiàn)圓周運(yùn)動(dòng)
- Android自定義view實(shí)現(xiàn)輸入框效果
- Android自定義View實(shí)現(xiàn)雪花特效
- Android自定義View實(shí)現(xiàn)分段選擇按鈕的實(shí)現(xiàn)代碼
- Android自定義View圓形圖片控件代碼詳解
- Android自定義View實(shí)現(xiàn)跟隨手指移動(dòng)的小兔子
- Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件
- Android如何用自定義View實(shí)現(xiàn)雪花效果
相關(guān)文章
android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode
這篇文章主要介紹了android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode,有需要的可以了解一下。2016-11-11flutter InkWell實(shí)現(xiàn)水波紋點(diǎn)擊效果
這篇文章主要為大家詳細(xì)介紹了flutter InkWell實(shí)現(xiàn)水波紋點(diǎn)擊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07淺談Android輕量級(jí)的數(shù)據(jù)緩存框架RxCache
本篇文章主要介紹了淺談Android輕量級(jí)的數(shù)據(jù)緩存框架RxCache,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android實(shí)現(xiàn)將View轉(zhuǎn)化為圖片并保存到本地
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)將View轉(zhuǎn)化為圖片并保存到本地,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02