国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義View實現多片葉子旋轉滑動(五)

2019-12-12 03:18:24
字體:
來源:轉載
供稿:網友

上一篇《Android 自定義View(四) 葉子飄動+旋轉效果》實現了單片葉子的滑動及旋轉,下面實現多片葉子的滑動旋轉功能

實現思路比較簡單,就是添加一個葉子Leaf類,儲存每片葉子的信息,

然后隨機產生葉子的坐標及旋轉角度,最后實時獲取每片葉子信息,添加到畫布中

1、Leaf.java 葉子類

 private class Leaf {  // 葉子的坐標  float x, y;  // 旋轉角度  int rotateAngle;  // 起始時間(ms)  long startTime; }

2、初始化每片葉子的信息,然后保存到list中

 //使葉子初始時間有間隔 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; } private List<Leaf> getLeafs(int leafSize) {  List<Leaf> list = new LinkedList<Leaf>();  for (int i=0; i<leafSize; i++) {   list.add(getLeaf());  }  return list; }

3、接下去就是改寫getLocation()及getRotate()方法,使其返回每片葉子的坐標及旋轉角度

 //獲取每片葉子在XY軸上的滑動值 private void getLocation(Leaf leaf) {  float betweenTime = leaf.startTime - System.currentTimeMillis();  //周期結束再加一個cycleTime  if(betweenTime < 0) {   leaf.startTime = System.currentTimeMillis() + cycleTime + new Random().nextInt((int) (cycleTime));   betweenTime = cycleTime;  }  //通過時間差計算出葉子的坐標  float fraction = (float) betweenTime / cycleTime;  float x = (int)(width * fraction);  leaf.x = x;  float w = (float) ((float) 2 * Math.PI / width);  int y = (int) (18 * Math.sin(w * x)) + (height-mLeafHeight)/2;  leaf.y = y; } //獲取每片葉子的旋轉角度 private void getRotate(Leaf leaf) {  float scale = ((leaf.startTime - System.currentTimeMillis())%cycleTime)/ (float)cycleTime;  int rotate = (int)(scale * 360);  leaf.rotateAngle = rotate; }

4、在onDraw()方法中,畫出每片葉子

 @Override protected void onDraw(Canvas canvas) {  super.onDraw(canvas);  //畫葉子  int size = leafList.size();  for (int i=0; i<size; i++) {   Leaf leaf = leafList.get(i);   //獲取葉子坐標   getLocation(leaf);   //獲取葉子旋轉角度   getRotate(leaf);   canvas.save();   Matrix matrix = new Matrix();   //設置滑動   matrix.postTranslate(leaf.x, leaf.y);   //設置旋轉   matrix.postRotate(leaf.rotateAngle, leaf.x + mLeafWidth / 2, leaf.y + mLeafHeight / 2);   //添加葉子到畫布   canvas.drawBitmap(mLeafBitmap, matrix, new Paint());   canvas.restore();  }  //調用onDraw()重復滑動  postInvalidate(); }

完整代碼:

public class LeafView extends View { private String TAG = "--------LeafView"; private Resources mResources; //背景圖、葉子 private Bitmap mLeafBitmap, bgBitmap; //整個控件的寬度和高度 private int width, height; private Paint bgPaint; private RectF bgRect; private Rect bgDestRect; //存放葉子lsit private List<Leaf> leafList; //葉子的寬和高 private int mLeafWidth, mLeafHeight; //葉子滑動一周的時間5秒 private final static long cycleTime = 5000; //葉子數量 private final static int leafNumber = 5; 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()  bgBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf_kuang, null)).getBitmap();  bgPaint = new Paint();  bgPaint.setColor(mResources.getColor(R.color.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;  bgDestRect = new Rect(0, 0 , width, height); } @Override protected void onDraw(Canvas canvas) {  super.onDraw(canvas);  bgRect = new RectF(0, 0 , width, height);  //畫背景顏色到畫布  canvas.drawRect(bgRect, bgPaint);  //畫背景圖片到畫布  canvas.drawBitmap(bgBitmap, null, bgDestRect, null);  //畫葉子  int size = leafList.size();  for (int i=0; i<size; i++) {   Leaf leaf = leafList.get(i);   //獲取葉子坐標   getLocation(leaf);   //獲取葉子旋轉角度   getRotate(leaf);   canvas.save();   Matrix matrix = new Matrix();   //設置滑動   matrix.postTranslate(leaf.x, leaf.y);   //設置旋轉   matrix.postRotate(leaf.rotateAngle, leaf.x + mLeafWidth / 2, leaf.y + mLeafHeight / 2);   //添加葉子到畫布   canvas.drawBitmap(mLeafBitmap, matrix, new Paint());   canvas.restore();  }  //調用onDraw()重復滑動  postInvalidate(); } //獲取每片葉子在XY軸上的滑動值 private void getLocation(Leaf leaf) {  float betweenTime = leaf.startTime - System.currentTimeMillis();  //周期結束再加一個cycleTime  if(betweenTime < 0) {   leaf.startTime = System.currentTimeMillis() + cycleTime + new Random().nextInt((int) (cycleTime));   betweenTime = cycleTime;  }  //通過時間差計算出葉子的坐標  float fraction = (float) betweenTime / cycleTime;  float x = (int)(width * fraction);  leaf.x = x;  float w = (float) ((float) 2 * Math.PI / width);  int y = (int) (18 * Math.sin(w * x)) + (height-mLeafHeight)/2;  leaf.y = y; } //獲取每片葉子的旋轉角度 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 {  // 葉子的坐標  float x, y;  // 旋轉角度  int rotateAngle;  // 起始時間(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; } //使葉子初始時間有間隔 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; }}

這里還有很多瑕疵,比如葉子的滑動范圍覆蓋了邊框等等

需要圖片等信息的可以從下面的Github地址下載,不過原文比較復雜

參考 https://github.com/Ajian-studio/GALeafLoading

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄冈市| 延寿县| 辽中县| 扎赉特旗| 嘉禾县| 剑阁县| 雷波县| 荆门市| 连城县| 剑阁县| 保山市| 榆树市| 肃宁县| 乌海市| 长乐市| 隆尧县| 塔城市| 苍溪县| 秦皇岛市| 普兰县| 汕尾市| 杭锦旗| 隆德县| 友谊县| 台山市| 壤塘县| 策勒县| 宣恩县| 铜川市| 巫溪县| 揭东县| 永泰县| 比如县| 阳新县| 汤原县| 西安市| 中江县| 松桃| 平阳县| 郧西县| 烟台市|