@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int width = getMeasuredWidth();
final int height = getMeasuredHeight();
if (getChildCount() == 0) {
return;
}
if (mTarget == null) {
ensureTarget();
}
if (mTarget == null) {
return;
}
final View child = mTarget;
final int childLeft = getPaddingLeft();
final int childTop = getPaddingTop();
final int childWidth = width - getPaddingLeft() - getPaddingRight();
final int childHeight = height - getPaddingTop() - getPaddingBottom();
child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
int circleWidth = mCircleView.getMeasuredWidth();
int circleHeight = mCircleView.getMeasuredHeight();
mCircleView.layout((width / 2 - circleWidth / 2), mCurrentTargetOffsetTop,
(width / 2 + circleWidth / 2), mCurrentTargetOffsetTop + circleHeight);
}
// mCircleView.layout((width / 2 - circleWidth / 2), mCurrentTargetOffsetTop,
// (width / 2 + circleWidth / 2), mCurrentTargetOffsetTop + circleHeight);
// 修改进度圈的X坐标使之位于左边
mCircleView.layout(childLeft, mCurrentTargetOffsetTop,
childLeft+circleWidth, mCurrentTargetOffsetTop + circleHeight);
case MotionEvent.ACTION_MOVE: {
pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
if (pointerIndex < 0) {
Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
return false;
}
final float y = MotionEventCompat.getY(ev, pointerIndex);
// 记录手指移动的距离,mInitialMotionY是初始的位置,DRAG_RATE是拖拽因子,默认为0.5。
final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
// 赋值给mTarget的top使之产生拖动效果
mTarget.setTranslationY(overscrollTop);
if (mIsBeingDragged) {
if (overscrollTop > 0) {
moveSpinner(overscrollTop);
} else {
return false;
}
}
break;
}
case MotionEvent.ACTION_UP: {
// 手指松开时启动动画回到头部
mTarget.animate().translationY(0).setDuration(200).start();
pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
if (pointerIndex < 0) {
Log.e(LOG_TAG, "Got ACTION_UP event but don't have an active pointer id.");
return false;
}
final float y = MotionEventCompat.getY(ev, pointerIndex);
final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
mIsBeingDragged = false;
finishSpinner(overscrollTop);
mActivePointerId = INVALID_POINTER;
return false;
}
public void setProgressView(MaterialProgressDrawable mProgress){
this.mProgress = mProgress;
mCircleView.setImageDrawable(mProgress);
}
private float rotation;
private Bitmap mBitmap;
public void setBitmap(Bitmap mBitmap) {
this.mBitmap = mBitmap;
}
@Override
public void setProgressRotation(float rotation) {
// 取负号是为了和微信保持一致,下拉时逆时针转加载时顺时针转,旋转因子是为了调整转的速度。
this.rotation = -rotation*ROTATION_FACTOR;
invalidateSelf();
}
@Override
public void draw(Canvas c) {
Rect bound = getBounds();
c.rotate(rotation,bound.exactCenterX(),bound.exactCenterY());
Rect src = new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight());
c.drawBitmap(mBitmap,src,bound,paint);
}
private void moveSpinner(float overscrollTop) {
…
// setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop, true /* requires update */);
// 最终刷新的位置
int endTarget;
if (!mUsingCustomStart) {
// 没有修改使用默认的值
endTarget = (int) (mSpinnerFinalOffset - Math.abs(mOriginalOffsetTop));
} else {
// 否则使用定义的值
endTarget = (int) mSpinnerFinalOffset;
}
if(targetY>=endTarget){
// 下移的位置超过最终位置后就不再下移,第一个参数为偏移量
setTargetOffsetTopAndBottom(0, true /* requires update */);
}else{
// 否则继续继续下移
setTargetOffsetTopAndBottom(targetY - mCurrentTargetOffsetTop, true /* requires update */);
}
}
@Override
public void start() {
mAnimation.reset();
mRing.storeOriginals();
// Already showing some part of the ring
if (mRing.getEndTrim() != mRing.getStartTrim()) {
mFinishing = true;
mAnimation.setDuration(ANIMATION_DURATION/2);
// 将转圈圈的动画传入
mParent.startAnimation(mAnimation);
} else {
mRing.setColorIndex(0);
mRing.resetOriginals();
mAnimation.setDuration(ANIMATION_DURATION);
// 将转圈圈的动画传入
mParent.startAnimation(mAnimation);
}
}
private void setupAnimation() {
// 初始化旋转动画
mAnimation = new Animation(){
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
setProgressRotation(-interpolatedTime);
}
};
mAnimation.setDuration(5000);
// 无限重复
mAnimation.setRepeatCount(Animation.INFINITE);
mAnimation.setRepeatMode(Animation.RESTART);
// 均匀转速
mAnimation.setInterpolator(new LinearInterpolator());
}
@Override
public void start() {
mParent.startAnimation(mAnimation);
}
private void startScaleDownAnimation(Animation.AnimationListener listener) {
// mScaleDownAnimation = new Animation() {
// @Override
// public void applyTransformation(float interpolatedTime, Transformation t) {
// setAnimationProgress(1 - interpolatedTime);
// }
// };
// 最终的偏移量就是mCircleView距离顶部的高度
final int deltaY = -mCircleView.getBottom();
mScaleDownAnimation = new TranslateAnimation(0,0,0,deltaY);
// mScaleDownAnimation.setDuration(SCALE_DOWN_DURATION);
mScaleDownAnimation.setDuration(500);
mCircleView.setAnimationListener(listener);
mCircleView.clearAnimation();
mCircleView.startAnimation(mScaleDownAnimation);
}
CustomProgressDrawable drawable = new CustomProgressDrawable(this,mRefreshLayout);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.moments_refresh_icon);
drawable.setBitmap(bitmap);
mRefreshLayout.setProgressView(drawable);
mRefreshLayout.setBackgroundColor(Color.BLACK);
mRefreshLayout.setProgressBackgroundColorSchemeColor(Color.BLACK);
mRefreshLayout.setOnRefreshListener(new CustomSwipeRefreshLayout.OnRefreshListener(){
@Override
public void onRefresh() {
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mRefreshLayout.setRefreshing(false);
}
};
new Thread(new Runnable() {
@Override
public void run() {
try {
// 在子线程睡眠三秒后发送消息停止刷新。
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
}).start();
}
});
public class CustomProgressDrawable extends MaterialProgressDrawable{
// 旋转因子,调整旋转速度
private static final int ROTATION_FACTOR = 5*360;
// 加载时的动画
private Animation mAnimation;
private View mParent;
private Bitmap mBitmap;
// 旋转角度
private float rotation;
private Paint paint;
public CustomProgressDrawable(Context context, View parent) {
super(context, parent);
mParent = parent;
paint = new Paint();
setupAnimation();
}
private void setupAnimation() {
// 初始化旋转动画
mAnimation = new Animation(){
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
setProgressRotation(-interpolatedTime);
}
};
mAnimation.setDuration(5000);
// 无限重复
mAnimation.setRepeatCount(Animation.INFINITE);
mAnimation.setRepeatMode(Animation.RESTART);
// 均匀转速
mAnimation.setInterpolator(new LinearInterpolator());
}
@Override
public void start() {
mParent.startAnimation(mAnimation);
}
public void setBitmap(Bitmap mBitmap) {
this.mBitmap = mBitmap;
}
@Override
public void setProgressRotation(float rotation) {
// 取负号是为了和微信保持一致,下拉时逆时针转加载时顺时针转,旋转因子是为了调整转的速度。
this.rotation = -rotation*ROTATION_FACTOR;
invalidateSelf();
}
@Override
public void draw(Canvas c) {
Rect bound = getBounds();
c.rotate(rotation,bound.exactCenterX(),bound.exactCenterY());
Rect src = new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight());
c.drawBitmap(mBitmap,src,bound,paint);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有