篇首语:本文由编程笔记#小编为大家整理,主要介绍了BannerView Android 只一个方向相关的知识,希望对你有一定的参考价值。
package com.example.bannerdemo;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
public class BannerView extends ViewGroup implements Runnable
private final float SCALE_0 = 0.833333333333f;
private final float SCALE_1 = 0.916666667f;
private final float SCALE_2 = 1f;
private final float ALPHA_0 = 0.4f;
private final float ALPHA_1 = 0.8f;
private final float ALPHA_2 = 1.0f;
private final int DEFAULT_BASELINE = 30;
// index=0 child baseline
private int BASELINE_0 = 0;
// index=1 child baseline
private int BASELINE_1 = 0;
// index=2 child baseline
private int BASELINE_2 = 0;
//动画间隔时间
private int waitTime=1000;
// 只能向左滑动
private final int SCROLL_ORIENTATION_LEFT = 0;
// 只能向右滑动
private final int SCROLL_ORIENTATION_RIGHT = 1;
// 双向滑动
private final int SCROLL_ORIENTATION_ALL = 2;
private final int LAYOUT_MODE_TOP = 0;
private final int LAYOUT_MODE_CENTER = 1;
private final int LAYOUT_MODE_BOTTOM = 2;
private final int LAYOUT_LEFT_TO_RIGHT = 3;
private final int LAYOUT_RIGHT_TO_LEFT = 4;
// 布局模式
int layoutMode = LAYOUT_MODE_CENTER;
// 布局方向
int layoutOrientation = LAYOUT_LEFT_TO_RIGHT;
// 滑动方向
int scrollOrientation = SCROLL_ORIENTATION_LEFT;
// baseLine offset 子view偏移量
int baseLineOffset = DEFAULT_BASELINE;
// 最上面的左边距
int topLeftBaseLine = 0;
//item 点击事件
private OnItemClickListener onItemClickListener;
public BannerView(Context context)
this(context, null);
public BannerView(Context context, AttributeSet attrs)
this(context, attrs, 0);
public BannerView(Context context, AttributeSet attrs, int defStyleAttr)
super(context, attrs, defStyleAttr);
init();
private void init()
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
measureChildren(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
for (int i &#61; 0; i < getChildCount(); i&#43;&#43;)
View child &#61; getChildAt(i);
int baseLeftLine &#61; getBaseLeftLine(child);
getBaseTopLine(i);
layoutChild(child, baseLeftLine);
if (i &#61;&#61; 2)
child.setOnClickListener(new OnClickListener()
&#64;Override
public void onClick(View v)
if (onItemClickListener !&#61; null)
LayoutParams lp &#61; (LayoutParams) child.getLayoutParams();
onItemClickListener.onItemClick(lp.dataPosition);
);
else
child.setOnClickListener(null);
private int getBaseLeftLine(View child)
LayoutParams lp &#61; (LayoutParams) child.getLayoutParams();
int baseLine &#61; 0;
switch (lp.fromIndex)
case 0:
if (mOffsetPercent > 0)
baseLine &#61; (int) (BASELINE_0 - mOffsetPercent * BASELINE_1);
if (baseLine < BASELINE_1)
baseLine &#61; BASELINE_1;
lp.scale &#61; (float) (SCALE_0 &#43; ((SCALE_1 - SCALE_0) * mOffsetPercent));
if (lp.scale > SCALE_1)
lp.scale &#61; SCALE_1;
else
baseLine &#61; (int) (BASELINE_0 &#43; mOffsetPercent * BASELINE_1);
if (baseLine < BASELINE_1)
baseLine &#61; BASELINE_1;
lp.scale &#61; (float) (SCALE_0 - ((SCALE_1 - SCALE_0) * mOffsetPercent));
if (lp.scale > SCALE_1)
lp.scale &#61; SCALE_1;
break;
case 1:
// toIndex&#61;2
if (mOffsetPercent > 0)
baseLine &#61; (int) (BASELINE_1 - mOffsetPercent * BASELINE_1);
if (baseLine < BASELINE_2)
baseLine &#61; BASELINE_2;
lp.scale &#61; (float) (SCALE_1 &#43; ((SCALE_2 - SCALE_1) * mOffsetPercent));
if (lp.scale > SCALE_2)
lp.scale &#61; SCALE_2;
lp.alpha &#61; (float) (ALPHA_1 &#43; ((ALPHA_2 - ALPHA_1) * mOffsetPercent));
if (lp.alpha > ALPHA_2)
lp.alpha &#61; ALPHA_2;
else
baseLine &#61; (int) (BASELINE_1 &#43; mOffsetPercent * BASELINE_1);
if (baseLine < BASELINE_2)
baseLine &#61; BASELINE_2;
lp.scale &#61; (float) (SCALE_1 - ((SCALE_2 - SCALE_1) * mOffsetPercent));
if (lp.scale > SCALE_2)
lp.scale &#61; SCALE_2;
lp.alpha &#61; (float) (ALPHA_1 - ((ALPHA_2 - ALPHA_1) * mOffsetPercent));
if (lp.alpha > ALPHA_2)
lp.alpha &#61; ALPHA_2;
break;
case 2:
// toIndex&#61; 0
if (mOffsetPercent > 0)
baseLine &#61; (int) (BASELINE_2 &#43; mOffsetPercent * BASELINE_0);
if (baseLine >&#61; BASELINE_0)
baseLine &#61; BASELINE_0;
else
baseLine &#61; (int) (BASELINE_2 &#43; mOffsetPercent * child.getWidth());
lp.scale &#61; (float) (SCALE_2 - ((SCALE_2 - SCALE_0) * mOffsetPercent));
if (lp.scale <&#61; SCALE_0)
lp.scale &#61; SCALE_0;
else
lp.scale &#61; SCALE_2;
lp.alpha &#61; (float) (ALPHA_2 - ((ALPHA_2 - ALPHA_0) * mOffsetPercent));
if (lp.alpha >&#61; ALPHA_0)
lp.alpha &#61; ALPHA_0;
else
lp.alpha &#61; ALPHA_2;
else
baseLine &#61; (int) (BASELINE_2 &#43; mOffsetPercent * BASELINE_0);
if (baseLine >&#61; BASELINE_0)
baseLine &#61; BASELINE_0;
else
baseLine &#61; (int) (BASELINE_2 &#43; mOffsetPercent * child.getWidth());
lp.scale &#61; (float) (SCALE_2 &#43; ((SCALE_2 - SCALE_0) * mOffsetPercent));
if (lp.scale <&#61; SCALE_0)
lp.scale &#61; SCALE_0;
else
lp.scale &#61; SCALE_2;
lp.alpha &#61; (float) (ALPHA_2 &#43; ((ALPHA_2 - ALPHA_0) * mOffsetPercent));
if (lp.alpha <&#61; ALPHA_0)
lp.alpha &#61; ALPHA_0;
else
lp.alpha &#61; ALPHA_2;
break;
return baseLine;
private int getBaseTopLine(int index)
int baseLine &#61; 0;
switch (index)
case 0:
baseLine &#61; 40;
break;
case 1:
baseLine &#61; 20;
break;
case 2:
baseLine &#61; 0;
break;
return baseLine;
private void layoutChild(View child, int baseLeftLine)
LayoutParams lp &#61; (LayoutParams) child.getLayoutParams();
child.setAlpha(lp.alpha);
int childWidth &#61; child.getMeasuredWidth();
int childHeight &#61; (int) (child.getMeasuredHeight() * lp.scale);
int top &#61; 0;
int bottom &#61; 0;
if (layoutMode &#61;&#61; LAYOUT_MODE_CENTER)
top &#61; (int) (getHeight() / 2.0f - childHeight / 2.0f);
bottom &#61; (int) (getHeight() / 2.0f &#43; childHeight / 2.0f);
else if (layoutMode &#61;&#61; LAYOUT_MODE_BOTTOM)
top &#61; getHeight() - childHeight;
bottom &#61; getHeight();
else if (layoutMode &#61;&#61; LAYOUT_MODE_TOP)
top &#61; 0;
bottom &#61; childHeight;
int left &#61; 0;
int right &#61; 0;
if (layoutOrientation &#61;&#61; LAYOUT_LEFT_TO_RIGHT)
left &#61; baseLeftLine;
right &#61; left &#43; childWidth;
else if (layoutOrientation &#61;&#61; LAYOUT_RIGHT_TO_LEFT)
right &#61; getWidth() - baseLeftLine;
left &#61; right - childWidth;
child.layout(left &#43; lp.leftMargin, top &#43; lp.topMargin, right &#43; lp.rightMargin, bottom &#43; lp.bottomMargin);
private int measureWidth(int widthMeasureSpec)
int width &#61; 0;
int mode &#61; MeasureSpec.getMode(widthMeasureSpec);
int size &#61; MeasureSpec.getSize(widthMeasureSpec);
if (mode &#61;&#61; MeasureSpec.EXACTLY)
width &#61; size;
else
int maxChildWidth &#61; 0;
for (int i &#61; 0; i < getChildCount(); i&#43;&#43;)
View child &#61; getChildAt(i);
LayoutParams layoutParams &#61; (LayoutParams) child.getLayoutParams();
maxChildWidth &#61; Math.max(maxChildWidth, child.getMeasuredWidth() &#43; layoutParams.leftMargin &#43; layoutParams.rightMargin);
width &#61; maxChildWidth &#43; 60;
return width;
private int measureHeight(int heightMeasureSpec)
int height &#61; 0;
int mode &#61; MeasureSpec.getMode(heightMeasureSpec);
int size &#61; MeasureSpec.getSize(heightMeasureSpec);
if (mode &#61;&#61; MeasureSpec.EXACTLY)
height &#61; size;
else
int maxChildHeight &#61; 0;
for (int i &#61; 0; i < getChildCount(); i&#43;&#43;)
View child &#61; getChildAt(i);
LayoutParams layoutParams &#61; (LayoutParams) child.getLayoutParams();
maxChildHeight &#61; Math.max(maxChildHeight, child.getMeasuredHeight() &#43; layoutParams.topMargin &#43; layoutParams.bottomMargin);
height &#61; maxChildHeight;
return height;
&#64;Override
public void addView(View child, int index, ViewGroup.LayoutParams params)
if (index > 2)
return;
LayoutParams lp &#61; params instanceof LayoutParams ? (LayoutParams) params : new LayoutParams(params);
lp.fromIndex &#61; index;
switch (index)
case 0:
lp.scale &#61; SCALE_0;
lp.alpha &#61; ALPHA_0;
BASELINE_0 &#61; baseLineOffset * 2;
break;
case 1:
lp.scale &#61; SCALE_1;
lp.alpha &#61; ALPHA_1;
BASELINE_1 &#61; bas