不太会录像,没办法,智能截图了
不多说了,直接上代码
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,所以有点切的不太好,见谅