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

本文实例为大家分享了Android仿微信刷新旋转小风车 具体代码,供大家参考,具体内容如下

eb971adf7b1b06f578e5d47c1b593bc2.png

不太会录像,没办法,智能截图了

不多说了,直接上代码

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,所以有点切的不太好,见谅

826966471176435a70a1c4f3abff738d.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值