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

首頁 > 系統 > Android > 正文

Android自定義view仿微信刷新旋轉小風車

2019-10-21 21:31:20
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Android仿微信刷新旋轉小風車 具體代碼,供大家參考,具體內容如下

Android,view,微信,旋轉小風車

不太會錄像,沒辦法,智能截圖了

不多說了,直接上代碼

package com.shipneg.demoysp.demo;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Paint;import android.os.CountDownTimer;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.ImageView;/** * Created by dell on 2017/4/7. */public class RotationView extends ImageView { /**  * 要轉動的圖片  **/ private Bitmap bitMap; /**  * 風車每次轉動的弧度  **/ private int rad = 0; /**  * 風車移動的軌跡  **/ private int excursion = -100; /**  * 圖片的寬度:在這里提供的是正方形的圖片,所以寬度和高度是一樣的  **/ private int width = 0; /***  * 圖片的高度:在這里提供的是正方形的圖片,所以寬度和高度是一樣的  **/ private int height = 0; /**  * 定義一個畫筆  **/ private Paint paint = new Paint(); public RotationView(Context context, AttributeSet attrs) {  super(context, attrs); } public RotationView(Context context, AttributeSet attrs, int defStyleAttr) {  super(context, attrs, defStyleAttr); } public RotationView(Context context) {  super(context); } /**  * 獲取圖片的寬和高  */ public void initSize() {  width = bitMap.getWidth();  height = bitMap.getHeight();  postInvalidate(); } public void setBitMap(Bitmap bitMap) {  this.bitMap = bitMap; } //一圖片的寬和高來設定自定義View的寬和高,由于是正方形寬和高是一樣的  @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  // TODO Auto-generated method stub   super.onMeasure(widthMeasureSpec, heightMeasureSpec);  setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); } CountDownTimer c = new CountDownTimer(5000, 10) {  @Override  public void onTick(long millisUntilFinished) {   postInvalidate();   rad = rad + 7;  }  @Override  public void onFinish() {   downY = 0;   excursion = -100;   postInvalidate();  } }; /***  * 實現onDraw方法把風車圖片繪制出來,同時繪制出來風車的旋轉效果,通過Matrix來控制  */ @Override protected void onDraw(Canvas canvas) {  Matrix matrix = new Matrix();  // 設置轉軸位置   matrix.setTranslate((float) width / 2, (float) height / 2);//  rad -=15;//每次旋轉的弧度增量為3當然,數字越大轉動越快  // 開始轉   matrix.preRotate(rad);  // 開始平移  matrix.postTranslate(0, excursion);  // 轉軸還原   matrix.preTranslate(-(float) width / 2, -(float) height / 2);  //繪制風車圖片   canvas.drawBitmap(bitMap, matrix, paint);  super.onDraw(canvas); } private int downY = 0; private int moveY = 0; private int abc = 0; @Override public boolean onTouchEvent(MotionEvent event) {  int action = event.getAction();  switch (action) {   case MotionEvent.ACTION_DOWN://隨著手指的move而不斷進行重繪,進而讓風車轉動起來     postInvalidate();//調用方法進行重繪     downY = (int) event.getY();    c.cancel();    break;   case MotionEvent.ACTION_MOVE://隨著手指的move而不斷進行重繪,進而讓風車轉動起來     //調用方法進行重繪     int movey2 = moveY;    rad = (int) -event.getY() * 6;//旋轉的速度    moveY = (int) (event.getY() - downY);//手指移動的距離    int chz = moveY - movey2;    if (chz > 10) {     chz = chz / 10;    } else if (chz < -10) {     chz = chz / 10;    }    Log.e("TAG:" + excursion, "chz: " + chz + "//moveY:" + moveY + "//movey2:" + movey2);    //100是向下滑動的最大距離    if (excursion >= 100) {     abc = abc + chz;     if (chz < 0 && abc - chz < 0) {      excursion = excursion + chz;     }    } else {     //開始向下運動     excursion += chz;    }    postInvalidate();    c.cancel();    break;   case MotionEvent.ACTION_UP:    c.start();    break;  }  return true; }} 

調用方法

//調用的方法 RotationView rotation = (RotationView) view.findViewById(R.id.rotationView);  BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(R.drawable.fengche);  rotation.setBitMap(drawable.getBitmap());  rotation.initSize();

圖片資源自己切的,本人不會ps,所以有點切的不太好,見諒

Android,view,微信,旋轉小風車

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大方县| 华亭县| 长子县| 虎林市| 和平区| 新干县| 盐池县| 神池县| 西贡区| 穆棱市| 乳源| 遵化市| 兴安盟| 炎陵县| 鹿泉市| 鸡西市| 勃利县| 汶川县| 嘉兴市| 贺兰县| 济宁市| 滦南县| 巴彦淖尔市| 青岛市| 望都县| 利津县| 祁阳县| 大洼县| 平舆县| 白沙| 永兴县| 华安县| 常州市| 苍南县| 绥阳县| 舟山市| 罗平县| 高台县| 饶平县| 托克逊县| 如东县|