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

這篇文章主要介紹了Android自定義view仿微信刷新旋轉小風車,具有一定的參考價值,感興趣的小夥伴們可以參考一下

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

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

不多說了,直接上代碼

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,所以有點切的不太好,見諒

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章