Android自定義view仿微信刷新旋轉小風車
更新時間:2018年12月31日 11:15:36 作者:songyan_love
這篇文章主要介紹了Android自定義view仿微信刷新旋轉小風車,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android仿微信刷新旋轉小風車 具體代碼,供大家參考,具體內容如下

不太會錄像,沒辦法,智能截圖了
不多說了,直接上代碼
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 + "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;
}
}
調用方法
//調用的方法 RotationView rotation = (RotationView) view.findViewById(R.id.rotationView); BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(R.drawable.fengche); rotation.setBitMap(drawable.getBitmap()); rotation.initSize();
圖片資源自己切的,本人不會ps,所以有點切的不太好,見諒

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android自定義ListView實現下拉刷新上拉加載更多
- android RecycleView實現下拉刷新和上拉加載
- 解決android viewmodel 數據刷新異常的問題
- Android巧用XListView實現萬能下拉刷新控件
- Android自定義控件ListView下拉刷新的代碼
- Android ExpandableListView實現下拉刷新和加載更多效果
- Android RecyclerView的刷新分頁的實現
- android使用SwipeRefreshLayout實現ListView下拉刷新上拉加載
- android使用PullToRefresh框架實現ListView下拉刷新上拉加載更多
- Android 中RecyclerView頂部刷新實現詳解
- Android四種方式刷新View的操作方法
相關文章
Android學習小結之Activity保存和恢復狀態(tài)
這篇文章主要介紹了Activity狀態(tài)保存和恢復的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
解決Eclipse創(chuàng)建android項目無法正常預覽布局文件問題的方法
這篇文章主要介紹了解決Eclipse創(chuàng)建android項目無法正常預覽布局文件問題的方法,需要的朋友可以參考下2015-12-12
Android 系統(tǒng)相機拍照后相片無法在相冊中顯示解決辦法
這篇文章主要介紹了Android 系統(tǒng)相機拍照后相片無法在相冊中顯示解決辦法的相關資料,需要的朋友可以參考下2016-12-12
Android 中TeaPickerView數據級聯(lián)選擇器功能的實例代碼
這篇文章主要介紹了Android TeaPickerView數據級聯(lián)選擇器 ,需要的朋友可以參考下2019-06-06

