您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在A(yíng)ndroid應(yīng)用中利用View實(shí)現(xiàn)一個(gè)旋轉(zhuǎn)功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
1、添加右側(cè)旋轉(zhuǎn)
Bitmap turnBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.fengshan, null)).getBitmap(); int turnLeafAngle = 0; private void setTurnLeaf(Canvas canvas) { Matrix matrix = new Matrix(); turnLeafAngle = turnLeafAngle + 3; matrix.postTranslate((width - rightCircleWidth/2 - turnBitmap.getWidth()/2), (height - rightCircleWidth/2 - turnBitmap.getHeight()/2)); matrix.postRotate(turnLeafAngle, width - rightCircleWidth/2 - turnBitmap.getWidth()/2 + turnBitmap.getWidth()/2, height - rightCircleWidth/2 - turnBitmap.getHeight()/2 + turnBitmap.getHeight()/2); canvas.drawBitmap(turnBitmap, matrix, new Paint()); }
代碼很明確,首先通過(guò)Matrix.postTranslate(float dx, float dy)把turnBitMap定位到最右側(cè)圓圈
再通過(guò)Matrix.postRotate(float degress, float dx, float dy);設(shè)置旋轉(zhuǎn)角度,每次角度+3°
其中degress為旋轉(zhuǎn)角度,(dx,dy)為旋轉(zhuǎn)中心點(diǎn)坐標(biāo)
2、添加滑動(dòng)效果
a、定義一個(gè)圓形Rectf(為什么不是半圓?因?yàn)楫?huà)圓弧的其實(shí)角度從水平線(xiàn)右側(cè)開(kāi)始)
progressArcRectf = new RectF(0, 0, height, height);
b、定義一個(gè)長(zhǎng)方形Rectf,長(zhǎng)方形x坐標(biāo)起點(diǎn)即時(shí)圓形半徑
progressRectf = new RectF(height/2, 0, width, height);
c、畫(huà)出圓弧Canvas.drawArc(Rectf rectf, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
startAngle:起始角度,默認(rèn)從右側(cè)水平線(xiàn)開(kāi)始
sweepAngle:為旋轉(zhuǎn)的角度,順時(shí)針旋轉(zhuǎn)
useCenter:true只畫(huà)出弧線(xiàn),false則畫(huà)出圓心到弧線(xiàn)的區(qū)域
//畫(huà)滑動(dòng)后的背景條 int currentProgressWidht = currentProgress * (width - borderWidth)/100; if(currentProgressWidht < leftCircleWidth/2) { //angle取值范圍0~90 int angle = 90 * currentProgressWidht / (leftCircleWidth/2); // 起始的位置 int startAngle = 180 - angle; // 掃過(guò)的角度 int sweepAngle = 2 * angle; canvas.drawArc(progressArcRectf, startAngle, sweepAngle, false, progressBgPaint); }else { //畫(huà)左邊半圓形滑過(guò)部分 canvas.drawArc(progressArcRectf, 90, 180, false, progressBgPaint); progressRectf.left = borderWidth + leftCircleWidth/2; progressRectf.right = borderWidth + currentProgressWidht; //畫(huà)中間滑過(guò)部分 canvas.drawRect(progressRectf, progressBgPaint); }
給LeafView.java添加一個(gè)
public void setCurrentProgress(int currentProgress) { this.currentProgress = currentProgress; }
3、修復(fù)葉子飄動(dòng)范圍
這個(gè)簡(jiǎn)單,就是設(shè)置葉子的rect坐標(biāo)起點(diǎn)+邊框距離
賦上所有代碼
1、activity_leaf.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content_leaf" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="226dp" android:layout_height="45dp"> <com.zjcpo.t170313_countdowntimer.LeafView android:id="@+id/leafView" android:layout_width="226dp" android:layout_height="45dp" android:layout_centerHorizontal="true" /> </RelativeLayout> </RelativeLayout>
2、LeafView.java
import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.util.AttributeSet; import android.util.Log; import android.view.View; import java.util.LinkedList; import java.util.List; import java.util.Random; import java.util.jar.Attributes; /** * Created by jiemiao.zhang on 2017-3-15. */ public class LeafView extends View { private String TAG = "--------LeafView"; private Resources mResources; //背景圖、葉子 private Bitmap mLeafBitmap, bgBitmap, turnBitmap; //整個(gè)控件的寬度和高度 private int width, height; //最外層邊框?qū)挾? private int borderWidth; //右側(cè)圓形直徑 private int rightCircleWidth; //左側(cè)圓形直徑 private int leftCircleWidth; private Paint bgPaint; private RectF bgRect; private Rect bgDestRect; //進(jìn)度條實(shí)時(shí)背景 private Paint progressBgPaint; //進(jìn)度條左側(cè)半圓,進(jìn)度條中間長(zhǎng)方形部分Rect private RectF progressArcRectf, progressRectf; //當(dāng)前百分比0~100 private int currentProgress = 0; //存放葉子lsit private List<Leaf> leafList; //葉子的寬和高 private int mLeafWidth, mLeafHeight; //葉子滑動(dòng)一周的時(shí)間5秒 private final static long cycleTime = 5000; //葉子數(shù)量 private final static int leafNumber = 6; public LeafView(Context context, AttributeSet attrs) { super(context, attrs); mResources = getResources(); mLeafBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf, null)).getBitmap(); mLeafWidth = mLeafBitmap.getWidth(); mLeafHeight = mLeafBitmap.getHeight(); turnBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.fengshan, null)).getBitmap(); bgBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf_kuang, null)).getBitmap(); bgPaint = new Paint(); bgPaint.setColor(mResources.getColor(R.color.bg_color)); //進(jìn)度條實(shí)時(shí)背景 progressBgPaint = new Paint(); progressBgPaint.setColor(mResources.getColor(R.color.progress_bg_color)); //獲取所有葉子的信息,放入list leafList = getLeafs(leafNumber); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); width = w; height = h; borderWidth = height * 10/64; rightCircleWidth = width * 62/303; leftCircleWidth = height - 2 * borderWidth; bgDestRect = new Rect(0, 0 , width, height); bgRect = new RectF(0, 0 , width, height); progressArcRectf = new RectF(borderWidth, borderWidth, height - borderWidth, height - borderWidth); progressRectf = new RectF(borderWidth+(height-2*borderWidth)/2, borderWidth, width-rightCircleWidth/2, height-borderWidth); Log.i("leftMarginWidth", (borderWidth + leftCircleWidth/2) + ""); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //畫(huà)背景顏色到畫(huà)布 canvas.drawRect(bgRect, bgPaint); if(currentProgress <= 100) { //畫(huà)葉子 int size = leafList.size(); for (int i=0; i<size; i++) { Leaf leaf = leafList.get(i); //獲取葉子坐標(biāo) getLocation(leaf); //獲取葉子旋轉(zhuǎn)角度 getRotate(leaf); canvas.save(); Matrix matrix = new Matrix(); //設(shè)置滑動(dòng) matrix.postTranslate(leaf.x, leaf.y); //設(shè)置旋轉(zhuǎn) matrix.postRotate(leaf.rotateAngle, leaf.x + mLeafWidth / 2, leaf.y + mLeafHeight / 2); //添加葉子到畫(huà)布 canvas.drawBitmap(mLeafBitmap, matrix, new Paint()); canvas.restore(); //畫(huà)滑動(dòng)后的背景條 int currentProgressWidht = currentProgress * (width - borderWidth - rightCircleWidth/2)/100; if(currentProgressWidht < leftCircleWidth/2) { //angle取值范圍0~90 int angle = 90 * currentProgressWidht / (leftCircleWidth/2); Log.i(TAG, "angle :" + angle); // 起始的位置 int startAngle = 180 - angle; // 掃過(guò)的角度 int sweepAngle = 2 * angle; canvas.drawArc(progressArcRectf, startAngle, sweepAngle, false, progressBgPaint); }else { //畫(huà)左邊半圓形滑過(guò)部分 canvas.drawArc(progressArcRectf, 90, 180, false, progressBgPaint); progressRectf.left = borderWidth + leftCircleWidth/2; progressRectf.right = borderWidth + currentProgressWidht; //畫(huà)中間滑過(guò)部分 canvas.drawRect(progressRectf, progressBgPaint); } } //調(diào)用onDraw()重復(fù)滑動(dòng) if(currentProgress < 100) { postInvalidate(); } } //畫(huà)背景圖片到畫(huà)布 canvas.drawBitmap(bgBitmap, null, bgDestRect, null); //畫(huà)右邊選擇風(fēng)葉 setTurnLeaf(canvas); //畫(huà)百分比 setText(canvas); } int turnLeafAngle = 0; private void setTurnLeaf(Canvas canvas) { Matrix matrix = new Matrix(); turnLeafAngle = turnLeafAngle + 3; matrix.postTranslate((width - rightCircleWidth/2 - turnBitmap.getWidth()/2), (height - rightCircleWidth/2 - turnBitmap.getHeight()/2)); matrix.postRotate(turnLeafAngle, width - rightCircleWidth/2 - turnBitmap.getWidth()/2 + turnBitmap.getWidth()/2, height - rightCircleWidth/2 - turnBitmap.getHeight()/2 + turnBitmap.getHeight()/2); canvas.drawBitmap(turnBitmap, matrix, new Paint()); } //顯示百分比數(shù)字,大于3%開(kāi)始顯示,到50%停止滑動(dòng) private void setText(Canvas canvas) { Paint paintText = new Paint(); paintText.setColor(Color.WHITE); paintText.setTextSize(30); int textX = currentProgress * width / 100; textX = currentProgress < 50 ? (currentProgress * width / 100) : (width/2); if(currentProgress > 3) { canvas.drawText(currentProgress + "%", textX, height/2 + 10,paintText); } } //獲取每片葉子在XY軸上的滑動(dòng)值 private void getLocation(Leaf leaf) { float betweenTime = leaf.startTime - System.currentTimeMillis(); //周期結(jié)束再加一個(gè)cycleTime if(betweenTime < 0) { leaf.startTime = System.currentTimeMillis() + cycleTime + new Random().nextInt((int) (cycleTime)); betweenTime = cycleTime; } //通過(guò)時(shí)間差計(jì)算出葉子的坐標(biāo) float fraction = (float) betweenTime / cycleTime; float x = (int)(width * fraction); //防止葉子飄出邊框 leaf.x = x < borderWidth ? borderWidth : x; float w = (float) ((float) 2 * Math.PI / width); int y = (int) (18 * Math.sin(w * x)) + (height-mLeafHeight)/2; //防止葉子飄出邊框 y = y > (height - borderWidth) ? (height - borderWidth) : y; y = y < borderWidth ? borderWidth : y; leaf.y = y; } //獲取每片葉子的旋轉(zhuǎn)角度 private void getRotate(Leaf leaf) { float scale = ((leaf.startTime - System.currentTimeMillis())%cycleTime)/ (float)cycleTime; int rotate = (int)(scale * 360); leaf.rotateAngle = rotate; } private class Leaf { // 葉子的坐標(biāo) float x, y; // 旋轉(zhuǎn)角度 int rotateAngle; // 起始時(shí)間(ms) long startTime; } private List<Leaf> getLeafs(int leafSize) { List<Leaf> list = new LinkedList<Leaf>(); for (int i=0; i<leafSize; i++) { list.add(getLeaf()); } return list; } //使葉子初始時(shí)間有間隔 int addTime; private Leaf getLeaf() { Random random = new Random(); Leaf leaf = new Leaf(); leaf.rotateAngle = random.nextInt(360); addTime += random.nextInt((int) (cycleTime)); leaf.startTime = System.currentTimeMillis() + cycleTime + addTime; return leaf; } public void setCurrentProgress(int currentProgress) { this.currentProgress = currentProgress; } } 3、LeafActivity.java public class LeafActivity extends Activity { private LeafView leafView; private int mProgress = 0; Handler mHandler = new Handler() { public void handleMessage(Message msg) { if (mProgress < 40) { mProgress += 1; // 隨機(jī)800ms以?xún)?nèi)刷新一次 mHandler.sendEmptyMessageDelayed(1, new Random().nextInt(800)); leafView.setCurrentProgress(mProgress); } else { mProgress += 1; // 隨機(jī)1200ms以?xún)?nèi)刷新一次 mHandler.sendEmptyMessageDelayed(1, new Random().nextInt(100)); leafView.setCurrentProgress(mProgress); } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_leaf); leafView = (LeafView) findViewById(R.id.leafView); mHandler.sendEmptyMessageDelayed(1, 3000); } }
上述內(nèi)容就是怎么在A(yíng)ndroid應(yīng)用中利用View實(shí)現(xiàn)一個(gè)旋轉(zhuǎn)功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guā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)容。