亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android自定義view仿微信刷新旋轉(zhuǎn)小風車

 更新時間:2018年12月31日 11:15:36   作者:songyan_love  
這篇文章主要介紹了Android自定義view仿微信刷新旋轉(zhuǎn)小風車,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

不多說了,直接上代碼

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 {

 /**
  * 要轉(zhuǎn)動的圖片
  **/
 private Bitmap bitMap;
 /**
  * 風車每次轉(zhuǎn)動的弧度
  **/
 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;
 }


 //一圖片的寬和高來設(shè)定自定義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();
  }
 };

 /***
  * 實現(xiàn)onDraw方法把風車圖片繪制出來,同時繪制出來風車的旋轉(zhuǎn)效果,通過Matrix來控制
  */
 @Override
 protected void onDraw(Canvas canvas) {

  Matrix matrix = new Matrix();
  // 設(shè)置轉(zhuǎn)軸位置 
  matrix.setTranslate((float) width / 2, (float) height / 2);
//  rad -=15;//每次旋轉(zhuǎn)的弧度增量為3當然,數(shù)字越大轉(zhuǎn)動越快
  // 開始轉(zhuǎn) 
  matrix.preRotate(rad);
  // 開始平移
  matrix.postTranslate(0, excursion);
  // 轉(zhuǎn)軸還原 
  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而不斷進行重繪,進而讓風車轉(zhuǎn)動起來 
    postInvalidate();//調(diào)用方法進行重繪 
    downY = (int) event.getY();
    c.cancel();
    break;

   case MotionEvent.ACTION_MOVE://隨著手指的move而不斷進行重繪,進而讓風車轉(zhuǎn)動起來 
    //調(diào)用方法進行重繪 
    int movey2 = moveY;

    rad = (int) -event.getY() * 6;//旋轉(zhuǎn)的速度
    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 + "http://moveY:" + moveY + "http://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;
 }


} 

調(diào)用方法

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

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論