1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| public class ParallaxLayerLayout extends FrameLayout { private static final int DEFAULT_BASE_OFFSET_DP = 10; private static final int DEFAULT_OFFSET_INCREMENT_DP = 5;
private Interpolator interpolator = new DecelerateInterpolator(); private int offsetIncrementPx; private int baseOffsetPx; private float scaleX = 1.0f; private float scaleY = 1.0f; private TranslationUpdater translationUpdater; public ParallaxLayerLayout(Context context) { super(context); }
public ParallaxLayerLayout(Context context, AttributeSet attrs) { super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ParallaxLayerLayout); try { baseOffsetPx = a.getDimensionPixelSize(R.styleable.ParallaxLayerLayout_parallaxOffsetBase, -1); if (baseOffsetPx == -1) { baseOffsetPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_BASE_OFFSET_DP, getResources().getDisplayMetrics()); }
offsetIncrementPx = a.getDimensionPixelSize(R.styleable.ParallaxLayerLayout_parallaxOffsetIncrement, -1); if (offsetIncrementPx == -1) { offsetIncrementPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_OFFSET_INCREMENT_DP, getResources().getDisplayMetrics()); }
scaleX = a.getFloat(R.styleable.ParallaxLayerLayout_parallaxScaleHorizontal, 1.0f); scaleY = a.getFloat(R.styleable.ParallaxLayerLayout_parallaxScaleVertical, 1.0f); if (scaleX > 1.0f || scaleX < 0.0f || scaleY > 1.0f || scaleY < 0.0f) { throw new IllegalArgumentException("Parallax scale must be a value between -1.0 and 1.0"); } } finally { a.recycle(); } }
@Override protected void onFinishInflate() { super.onFinishInflate();
computeOffsets();
if (isInEditMode()) { updateTranslations(new float[]{1.0f, 1.0f}); } } private void computeOffsets() { final int childCount = getChildCount(); for (int i = childCount - 1; i >= 0; i--) { View child = getChildAt(i); LayoutParams lp = (LayoutParams) child.getLayoutParams(); int index; if (lp.customIndex == -1) { index = childCount - 1 - i; } else { index = lp.customIndex; } lp.offsetPx = offsetPxForIndex(index, lp.incrementMultiplier); } }
* 这里计算出不同的childView的索引不同从而移动的距离也不同 * * @param index * @param incrementMultiplier * @return */ private float offsetPxForIndex(int index, float incrementMultiplier) { return incrementMultiplier * baseOffsetPx + index * offsetIncrementPx; }
* 移动 * * @param translations X and Y translation percentage, with values from -1.0 to 1.0. */ public void updateTranslations(@Size(2) float[] translations) { if (Math.abs(translations[0]) > 1 || Math.abs(translations[1]) > 1) { throw new IllegalArgumentException("Translation values must be between 1.0 and -1.0"); }
final int childCount = getChildCount(); for (int i = childCount - 1; i >= 0; i--) { View child = getChildAt(i); float[] translationsPx = calculateFinalTranslationPx(child, translations); child.setTranslationX(translationsPx[0]); child.setTranslationY(translationsPx[1]); } }
* 设置TranslationUpdater * * @param translationUpdater */ public void setTranslationUpdater(TranslationUpdater translationUpdater) { if (this.translationUpdater != null) { this.translationUpdater.unSubscribe(); } this.translationUpdater = translationUpdater; this.translationUpdater.subscribe(this); }
* 计算移动距离 * * @param child * @param translations * @return */ @Size(2) private float[] calculateFinalTranslationPx(View child, @Size(2) float[] translations) { LayoutParams lp = (LayoutParams) child.getLayoutParams(); int xSign = translations[0] > 0 ? 1 : -1; int ySign = translations[1] > 0 ? 1 : -1;
float translationX = xSign * lp.offsetPx * interpolator.getInterpolation(Math.abs(translations[0])) * scaleX; float translationY = ySign * lp.offsetPx * interpolator.getInterpolation(Math.abs(translations[1])) * scaleY;
return new float[]{translationX, translationY}; }
@Override protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { return p instanceof LayoutParams; }
@Override protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); }
@Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new LayoutParams(getContext(), attrs); }
@Override protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { return new LayoutParams(p.width, p.height); }
public interface TranslationUpdater {
void subscribe(ParallaxLayerLayout parallaxLayerLayout);
void unSubscribe(); }
public static class LayoutParams extends FrameLayout.LayoutParams {
private float offsetPx; private int customIndex = -1; private float incrementMultiplier = 1.0f;
public LayoutParams(Context c, AttributeSet attrs) { super(c, attrs); gravity = Gravity.CENTER; TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ParallaxLayerLayout_LayoutParams); try { customIndex = a.getInt(R.styleable.ParallaxLayerLayout_LayoutParams_layout_parallaxZIndex, -1); incrementMultiplier = a.getFloat( R.styleable.ParallaxLayerLayout_LayoutParams_layout_parallaxIncrementMultiplier, 1.0f); } finally { a.recycle(); } }
public LayoutParams(int width, int height) { super(width, height); gravity = Gravity.CENTER; } } }
|