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

首頁 > 系統 > Android > 正文

Android自定義View實現葉子飄動旋轉效果(四)

2019-10-23 18:33:50
字體:
來源:轉載
供稿:網友

上一篇實現了葉子飄動功能,《Android自定義葉子飄動》 現在實現旋轉效果

Android,View,飄動,旋轉

要實現這個效果,要在之前的功能上添加2個功能

1、通過matrix.postTranslate(int x, int y)在添加在Y軸上滑動

2、通過matrix.postRotate(float degrees, float px, float py)實現葉子旋轉

代碼實現

1、獲取Y坐標

 private float getMatrixY() {    float w = (float) ((float) 2 * Math.PI / width);    int y = (int) (18 * Math.sin(w * getMatrixX())) + (height-mLeafHeight)/2;    return y;  }

math.sin(Math.PI....)的取值范圍貌似是-1~1之間。通過X坐標在整個width的比例,獲取到Y坐標的比例。

這里方法有很多,我這邊葉子Y坐標默認在Y軸中間,然后上下+-18px實現在Y軸的滑動,18滑動浮動比較大了

public LeafView(Context context, AttributeSet attrs) {    super(context, attrs);    mResources = getResources();    bgBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf_kuang, null)).getBitmap();    leafBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf, null))).getBitmap();   mLeafHeight = leafBitmap.getWidht();       bgPaint = new Paint();    bgPaint.setColor(mResources.getColor(R.color.bg_color));  }  @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);    //添加葉子    Matrix matrix = new Matrix();    matrix.postTranslate(getMatriX(), getMatrixY);    canvas.drawBitmap(leafBitmap, new Matrix(), new Paint());    //重復調用onDraw()    postInvalidate();  }  long cycleTime = 5000;  //葉子滑動一周的時間5秒  long startTime = 0;   //葉子滑動開始時間  private float getMatriX() {    float betweenTime = startTime - System.currentTimeMillis();    //周期結束再加一個cycleTime    if(betweenTime < 0) {      startTime = System.currentTimeMillis() + cycleTime;      betweenTime = cycleTime;    }    //通過時間差計算出葉子的坐標    float fraction = (float) betweenTime / cycleTime;    float x = (int)(width * fraction);    return x;  }    private float getMatrixY() {    float w = (float) ((float) 2 * Math.PI / width);    int y = (int) (18 * Math.sin(w * getMatrixX())) + (height-mLeafHeight)/2;    return y;  }

看下效果就這樣,在Y軸上實現上下浮動,如果要浮動小點,可以把18改小

Android,View,飄動,旋轉

2、實現旋轉

主要通過matrix.postRotate(float degrees, float px, float py)

degrees就是角度(0~360),px,py就是圖片的中心點

  private int getRotate() {    float scale = ((startTime - System.currentTimeMillis())%cycleTime)/ (float)cycleTime;    int rotate = (int)(scale * 360);    return rotate;  }

同樣,通過當前葉子在X軸的比例,來計算出旋轉的角度(0~360)

完整代碼:

public class LeafView extends View {  private Resources mResources;  private Bitmap mLeafBitmap, bgBitmap;  private int width, height;  private int mLeafWidth,mLeafHeight;  private Paint bgPaint;  private RectF bgRect;  private Rect bgDestRect;  public LeafView(Context context, AttributeSet attrs) {    super(context, attrs);    mResources = getResources();    mLeafBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf, null)).getBitmap();    mLeafWidth = mLeafBitmap.getWidht();    mLeafHeight = mLeafBitmap.getHeight();    bgBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf_kuang, null)).getBitmap();    bgPaint = new Paint();    bgPaint.setColor(mResources.getColor(R.color.bg_color));  }  @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);    canvas.save();    Matrix matrix = new Matrix();    //添加滑動    matrix.postTranslate(getMatrixX(), getMatrixY());    //添加旋轉    matrix.postRotate(getRotate(), getMatrixX() + mLeafWidth / 2, getMatrixY() + mLeafHeight / 2);    canvas.drawBitmap(mLeafBitmap, matrix, new Paint());    canvas.restore();    postInvalidate();  }  long cycleTime = 5000;  //葉子滑動一周的時間5秒  long startTime = 0;  private float getMatrixX() {    float betweenTime = startTime - System.currentTimeMillis();    //周期結束再加一個cycleTime    if(betweenTime < 0) {      startTime = System.currentTimeMillis() + cycleTime;      betweenTime = cycleTime;    }    //通過時間差計算出葉子的坐標    float fraction = (float) betweenTime / cycleTime;    float x = (int)(width * fraction);    return x;  }  private float getMatrixY() {    float w = (float) ((float) 2 * Math.PI / width);    int y = (int) (18 * Math.sin(w * getMatrixX())) + (height-mLeafHeight)/2;    return y;  }  private int getRotate() {    float scale = ((startTime - System.currentTimeMillis())%cycleTime)/ (float)cycleTime;    int rotate = (int)(scale * 360);    return rotate;  }}

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 正阳县| 闻喜县| 洪洞县| 新兴县| 贺兰县| 司法| 西平县| 海兴县| 纳雍县| 吉安市| 黔东| 江华| 宁津县| 德庆县| 惠州市| 凉城县| 涿鹿县| 黔西县| 垦利县| 巴马| 金坛市| 阿克陶县| 徐闻县| 金平| 宁德市| 夹江县| 根河市| 崇仁县| 乳山市| 西昌市| 临西县| 凉城县| 龙门县| 根河市| 渝中区| 张家川| 新巴尔虎右旗| 五大连池市| 广宗县| 海淀区| 星座|