首页 > 移动平台 > 详细

《Android特效》AndroidScrollingImageView实现滚动轮播特效

时间:2015-09-06 02:11:44      阅读:402      评论:0      收藏:0      [点我收藏+]

AndroidScrollingImageView实现滚动轮播特效,MainActivity继承AppCompatActivity,
自定义一个ScrollingImageView设置scrolling_image_view:speed="1dp",ScrollingImageView
获取图片后对图片做一个循环播放动画postInvalidateOnAnimation方法来实现的。

运行效果:

?

bubuko.com,布布扣

?

?特效完整源码下载地址:http://www.itlanbao.com/code/20150905/10000/100504.html

?

package com.q42.android.scrollingimageview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import static java.lang.Math.abs;
import static java.lang.Math.floor;

/**
?* Created by thijs on 08-06-15.
?*/
public class ScrollingImageView extends View {
??? private final int speed;
??? private final Bitmap bitmap;

??? private Rect clipBounds = new Rect();
??? private float offset = 0;

??? private boolean isStarted;

??? public ScrollingImageView(Context context, AttributeSet attrs) {
??????? super(context, attrs);
??????? TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ParallaxView, 0, 0);
??????? try {
??????????? speed = ta.getDimensionPixelSize(R.styleable.ParallaxView_speed, 10);
??????????? bitmap = BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0));
??????? } finally {
??????????? ta.recycle();
??????? }
??????? start();
??? }

??? @Override
??? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
??????? super.onMeasure(widthMeasureSpec, heightMeasureSpec);
??????? setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), bitmap.getHeight());
??? }

??? @Override
??? public void onDraw(Canvas canvas) {
??????? super.onDraw(canvas);
??????? if (canvas == null) {
??????????? return;
??????? }

??????? canvas.getClipBounds(clipBounds);

??????? float layerWidth = bitmap.getWidth();
??????? if (offset < -layerWidth) {
??????????? offset += (floor(abs(offset) / layerWidth) * layerWidth);
??????? }

??????? float left = offset;
??????? while (left < clipBounds.width()) {
??????????? canvas.drawBitmap(bitmap, getBitmapLeft(layerWidth, left), 0, null);
??????????? left += layerWidth;
??????? }

??????? if (isStarted) {
??????????? offset -= abs(speed);
??????????? postInvalidateOnAnimation();
??????? }
??? }

??? private float getBitmapLeft(float layerWidth, float left) {
??????? if (speed < 0) {
??????????? return clipBounds.width() - layerWidth - left;
??????? } else {
??????????? return left;
??????? }
??? }

??? /**
???? * Start the animation
???? */
??? public void start() {
??????? if (!isStarted) {
??????????? isStarted = true;
??????????? postInvalidateOnAnimation();
??????? }
??? }

??? /**
???? * Stop the animation
???? */
??? public void stop() {
??????? if (isStarted) {
??????????? isStarted = false;
??????????? invalidate();
??????? }
??? }
}

特效完整源码下载地址:http://www.itlanbao.com/code/20150905/10000/100504.html

Android技术交流群:468184825

《Android特效》AndroidScrollingImageView实现滚动轮播特效

原文:http://wuchengyijt.iteye.com/blog/2240696

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!