Android自定义view仿微信刷新旋转小风车

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android自定义view仿微信刷新旋转小风车脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例为大家分享了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仿微信刷新旋转小风车

android教程
脚本网站
android studio

脚本宝典总结

以上是脚本宝典为你收集整理的Android自定义view仿微信刷新旋转小风车全部内容,希望文章能够帮你解决Android自定义view仿微信刷新旋转小风车所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。