Android 仿微信重新整理旋轉小風車 自定義view
阿新 • • 發佈:2019-02-02
不太會錄影,沒辦法,智慧截圖了
不多說了,直接上程式碼
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,所以有點切的不太好,見諒