用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

android翻页效果实现

2013-08-01 作者: 云代码会员举报

[android]代码库

package com.astuetz.viewpager.extensions;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.View;


public class IndicatorLineView extends View implements OnPageChangeListener {
	
	@SuppressWarnings("unused")
	private static final String TAG = "com.astuetz.viewpager.extensions";
	
	private ViewPager mPager;
	
	private int mLineColor = 0xFF34B5E8;
	
	private float mLineLeft = 0.0f;
	private float mLineWidth = 0.0f;
	
	private int mFadeOutTime = 500;
	private int mFadingDuration = 200;
	
	private int mAlpha = 0xFF;
	
	private FadeTimer mTimer;
	
	
	public IndicatorLineView(Context context) {
		this(context, null);
	}
	
	public IndicatorLineView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public IndicatorLineView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ViewPagerExtensions, defStyle, 0);
		
		mLineColor = a.getColor(R.styleable.ViewPagerExtensions_lineColor, mLineColor);
		
		mFadeOutTime = a.getInt(R.styleable.ViewPagerExtensions_fadeOutDelay, mFadeOutTime);
		
		mFadingDuration = a.getInt(R.styleable.ViewPagerExtensions_fadeOutDuration, mFadingDuration);
		
		a.recycle();
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		if (mPager != null) {
			mLineWidth = w / mPager.getAdapter().getCount();
			mLineLeft = mLineWidth * mPager.getCurrentItem();
			invalidate();
			resetTimer();
		}
	}
	
	
	
	public void setViewPager(ViewPager pager) {
		this.mPager = pager;
		mPager.setOnPageChangeListener(this);
	}
	
	
	private Paint mLinePaint = new Paint();
	
	protected synchronized void onDraw(Canvas canvas) {
		
		super.onDraw(canvas);
		
		final Paint linePaint = mLinePaint;
		
		final int color = Color.argb(mAlpha, Color.red(mLineColor), Color.green(mLineColor), Color.blue(mLineColor));
		
		linePaint.setColor(color);
		
		// draw the line
		canvas.drawRect(mLineLeft, 0, mLineLeft + mLineWidth, getMeasuredHeight(), linePaint);
		
	}
	
	
	

	public void onPageScrollStateChanged(int state) {}
	

	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
		
		final float currentX = mPager.getScrollX();
		final float fullX = (mPager.getWidth() + mPager.getPageMargin()) * (mPager.getAdapter().getCount());
		
		mLineLeft = getMeasuredWidth() * currentX / fullX;
		
		mLineWidth = getMeasuredWidth() / mPager.getAdapter().getCount();
		
		resetTimer();
		
		invalidate();
		
	}
	

	public void onPageSelected(int position) {}
	
	
	
	public void setLineColor(int lineColor) {
		this.mLineColor = lineColor;
		invalidate();
	}
	
	public int getLineColor() {
		return this.mLineColor;
	}
	
	public void setFadeOutDelay(int milliseconds) {
		this.mFadeOutTime = milliseconds;
		invalidate();
	}
	
	public int getFadeOutDelay() {
		return this.mFadeOutTime;
	}
	
	public void setFadeOutDuration(int milliseconds) {
		this.mFadingDuration = milliseconds;
		invalidate();
	}
	
	public int getFadeOutDuration() {
		return this.mFadingDuration;
	}
	
	
	
	private void setAlpha(int alpha) {
		this.mAlpha = alpha;
		invalidate();
	}
	
	private void resetTimer() {
		
		if (mFadeOutTime > 0) {
			
			if (mTimer == null || mTimer.isRunning == false) {
				mTimer = new FadeTimer();
				mTimer.execute();
			} else {
				mTimer.reset();
			}
			
			mAlpha = 0xFF;
			
		}
		
	}
	
	private class FadeTimer extends AsyncTask<Void, Integer, Void> {
		
		private int elapsed = 0;
		private boolean isRunning = true;
		
		public void reset() {
			elapsed = 0;
		}
		
		@Override
		protected Void doInBackground(Void... args) {
			while (isRunning) {
				try {
					Thread.sleep(1);
					elapsed++;
					
					if (elapsed >= mFadeOutTime && elapsed < mFadeOutTime + mFadingDuration) {
						
						int x0 = mFadeOutTime;
						int x1 = mFadeOutTime + mFadingDuration;
						int x = elapsed;
						int y0 = 0xFF;
						int y1 = 0x00;
						
						int a = y0 + ((x - x0) * y1 - (x - x0) * y0) / (x1 - x0);
						publishProgress(a);
					}
					else if (elapsed >= mFadeOutTime + mFadingDuration) {
						isRunning = false;
					}
					
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			return null;
		}
		
		protected void onProgressUpdate(Integer... alpha) {
			setAlpha(alpha[0]);
		}
		
		@Override
		protected void onPostExecute(Void result) {
			setAlpha(0x00);
		}
	}
	
}

[源代码打包下载]




网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...