Created
September 23, 2016 07:03
-
-
Save wei-spring/878e77ecd5d4206db2e70d78a791394b to your computer and use it in GitHub Desktop.
锯齿背景+动画效果展示(包含3个文件集合)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Java | |
//------锯齿锯齿锯齿Drawable | |
package com.sample; | |
import android.graphics.Canvas; | |
import android.graphics.ColorFilter; | |
import android.graphics.Paint; | |
import android.graphics.Path; | |
import android.graphics.Rect; | |
import android.graphics.drawable.Drawable; | |
import java.util.Random; | |
public class JagDrawable extends Drawable { | |
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
private Path mPath = new Path(); | |
private int mMaxDeepness = 24; | |
private int mMinDeepness = 4; | |
private Rect mFluCount = new Rect(); | |
private int mColor; | |
private int mAlpha; | |
private boolean mSmooth = true; | |
public JagDrawable() { | |
//设置边框锯齿,不会与形状内部冲突 | |
mPaint.setAntiAlias(true); | |
mPaint.setStyle(Paint.Style.FILL); | |
//设置抖动Flag | |
mPaint.setDither(true); | |
mFluCount.top = 36; | |
mFluCount.left = 25; | |
mFluCount.right = 25; | |
mFluCount.bottom = 36; | |
} | |
public void setFluCount(Rect count) { | |
mFluCount.set(count); | |
} | |
public void setDeepness(int min, int max) { | |
mMinDeepness = min; | |
mMaxDeepness = max; | |
} | |
public void setIsRandom(boolean random) { | |
if (random) { | |
if (mRandom == null) | |
mRandom = new Random(); | |
} else { | |
mRandom = null; | |
} | |
} | |
public int getColor() { | |
return mColor; | |
} | |
public void setColor(int color) { | |
if (mColor == color) | |
return; | |
mColor = color; | |
mPaint.setColor(color); | |
invalidateSelf(); | |
} | |
public void setColorUnInvalidate(int color) { | |
if (mColor == color) | |
return; | |
mColor = color; | |
mPaint.setColor(color); | |
} | |
public Paint getPaint() { | |
return mPaint; | |
} | |
public void setSmooth(boolean smooth) { | |
if (this.mSmooth != smooth) { | |
this.mSmooth = smooth; | |
initPath(getBounds()); | |
} | |
} | |
public boolean isSmooth() { | |
return mSmooth; | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
draw(canvas, mPath, mPaint); | |
} | |
protected void draw(Canvas canvas, Path path, Paint paint) { | |
canvas.drawPath(path, paint); | |
} | |
@Override | |
public int getAlpha() { | |
return mAlpha; | |
} | |
@Override | |
public void setAlpha(int alpha) { | |
mAlpha = alpha; | |
mPaint.setAlpha(alpha); | |
} | |
@Override | |
public void setColorFilter(ColorFilter cf) { | |
mPaint.setColorFilter(cf); | |
} | |
@Override | |
public int getOpacity() { | |
return 0; | |
} | |
@Override | |
protected void onBoundsChange(Rect bounds) { | |
super.onBoundsChange(bounds); | |
initPath(bounds); | |
} | |
protected void initPath(Rect bounds) { | |
if (mSmooth) | |
initPathSmooth(bounds.left, bounds.top, bounds.right, bounds.bottom); | |
else | |
initPath(bounds.left, bounds.top, bounds.right, bounds.bottom); | |
} | |
protected void initPath(int left, int top, int right, int bottom) { | |
mPath.reset(); | |
mPath.moveTo(left, top); | |
float tagSize; | |
int flu = mFluCount.left; | |
if (flu > 0) { | |
tagSize = (bottom - top) / (float) flu; | |
for (int i = 0; i < flu; i++) { | |
if (i % 2 == 0) { | |
mPath.lineTo(left, top + i * tagSize); | |
} else { | |
mPath.lineTo(left + getDeepness(), top + i * tagSize); | |
} | |
} | |
} | |
mPath.lineTo(left, bottom); | |
flu = mFluCount.bottom; | |
if (flu > 0) { | |
tagSize = (right - left) / (float) flu; | |
for (int i = 0; i < flu; i++) { | |
if (i % 2 == 0) { | |
mPath.lineTo(left + i * tagSize, bottom); | |
} else { | |
mPath.lineTo(left + i * tagSize, bottom - getDeepness()); | |
} | |
} | |
} | |
mPath.lineTo(right, bottom); | |
flu = mFluCount.right; | |
if (flu > 0) { | |
tagSize = (bottom - top) / (float) flu; | |
for (int i = 0; i < flu; i++) { | |
if (i % 2 == 0) { | |
mPath.lineTo(right, bottom - (i * tagSize)); | |
} else { | |
mPath.lineTo(right - getDeepness(), bottom - (i * tagSize)); | |
} | |
} | |
} | |
mPath.lineTo(right, top); | |
flu = mFluCount.top; | |
if (flu > 0) { | |
tagSize = (right - left) / (float) flu; | |
for (int i = 0; i < flu; i++) { | |
if (i % 2 == 0) { | |
mPath.lineTo(right - (i * tagSize), top); | |
} else { | |
mPath.lineTo(right - (i * tagSize), top + getDeepness()); | |
} | |
} | |
} | |
mPath.lineTo(left, top); | |
mPath.close(); | |
} | |
protected void initPathSmooth(int left, int top, int right, int bottom) { | |
final int maxDeep = getMaxDeepness(); | |
left += (mFluCount.left > 0 ? maxDeep : 0); | |
top += (mFluCount.top > 0 ? maxDeep : 0); | |
right -= (mFluCount.right > 0 ? maxDeep : 0); | |
bottom -= (mFluCount.bottom > 0 ? maxDeep : 0); | |
float x, y; | |
float deep = 0, tagSize; | |
mPath.reset(); | |
mPath.moveTo(left, top); | |
x = left; | |
y = top; | |
int flu = mFluCount.left; | |
if (flu > 0) { | |
tagSize = (bottom - top) / (float) (flu * 2); | |
flu--; | |
for (int i = 0; i < flu; i++) { | |
if (i % 2 == 0) { | |
deep = -getDeepness(); | |
} else { | |
deep = getDeepness(); | |
} | |
y += tagSize; | |
mPath.quadTo(x + deep, y, x, y += tagSize); | |
} | |
mPath.quadTo(left + deep, bottom, left, bottom); | |
} else { | |
mPath.lineTo(left, bottom); | |
} | |
y = bottom; | |
flu = mFluCount.bottom; | |
if (flu > 0) { | |
tagSize = (right - left) / (float) (flu * 2); | |
flu--; | |
for (int i = 0; i < flu; i++) { | |
if (i % 2 == 0) { | |
deep = getDeepness(); | |
} else { | |
deep = -getDeepness(); | |
} | |
x += tagSize; | |
mPath.quadTo(x, y + deep, x += tagSize, y); | |
} | |
mPath.quadTo(right, bottom + deep, right, bottom); | |
} else { | |
mPath.lineTo(right, bottom); | |
} | |
x = right; | |
flu = mFluCount.right; | |
if (flu > 0) { | |
tagSize = (bottom - top) / (float) (flu * 2); | |
flu--; | |
for (int i = 0; i < flu; i++) { | |
if (i % 2 == 0) { | |
deep = getDeepness(); | |
} else { | |
deep = -getDeepness(); | |
} | |
y -= tagSize; | |
mPath.quadTo(x + deep, y, x, y -= tagSize); | |
} | |
mPath.quadTo(right + deep, top, right, top); | |
} else { | |
mPath.lineTo(right, top); | |
} | |
y = top; | |
flu = mFluCount.top; | |
if (flu > 0) { | |
tagSize = (right - left) / (float) (flu * 2); | |
flu--; | |
for (int i = 0; i < flu; i++) { | |
if (i % 2 == 0) { | |
deep = -getDeepness(); | |
} else { | |
deep = getDeepness(); | |
} | |
x -= tagSize; | |
mPath.quadTo(x, y + deep, x -= tagSize, y); | |
} | |
mPath.quadTo(left, top + deep, left, top); | |
} else { | |
mPath.lineTo(left, top); | |
} | |
mPath.close(); | |
} | |
private Random mRandom = new Random(); | |
private int getDeepness() { | |
if (mRandom != null) { | |
return mMinDeepness + mRandom.nextInt(mMaxDeepness - mMinDeepness); | |
} else { | |
return (mMaxDeepness + mMinDeepness) / 2; | |
} | |
} | |
private int getMaxDeepness() { | |
if (mRandom != null) { | |
return mMaxDeepness; | |
} else { | |
return (mMaxDeepness + mMinDeepness) / 2; | |
} | |
} | |
} | |
//------------jicheng juchi + donghua | |
package com.sample; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.Path; | |
import android.graphics.Point; | |
import android.graphics.Rect; | |
import android.os.SystemClock; | |
import android.view.animation.DecelerateInterpolator; | |
import android.view.animation.Interpolator; | |
/** | |
* Have a circle animation | |
*/ | |
public class AnimJagDrawable extends JagDrawable { | |
protected static final int FRAME_DURATION = 16; | |
// Base Values | |
protected static final Interpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator(1.2f); | |
// Time | |
protected static final int IN_ANIM_DURATION = 560; | |
private Point mPoint = new Point(); | |
private float mRadius; | |
private float mMaxRadius; | |
private Interpolator mInterpolator = DECELERATE_INTERPOLATOR; | |
private long mStartTime; | |
private boolean isRun = false; | |
private boolean isFirst = true; | |
@Override | |
protected void draw(Canvas canvas, Path path, Paint paint) { | |
if (isFirst && isRun) { | |
isFirst = false; | |
startAnim(); | |
} | |
if (mRadius > 0) { | |
int sc = canvas.save(); | |
canvas.clipPath(path); | |
canvas.drawCircle(mPoint.x, mPoint.y, mRadius, paint); | |
canvas.restoreToCount(sc); | |
} | |
} | |
@Override | |
protected void onBoundsChange(Rect bounds) { | |
super.onBoundsChange(bounds); | |
mPoint.set(bounds.left, bounds.top); | |
int x = bounds.right - bounds.left; | |
int y = bounds.bottom - bounds.top; | |
mMaxRadius = (float) Math.sqrt(x * x + y * y); | |
} | |
public void startAnim() { | |
if (isRun) { | |
unscheduleSelf(mAnim); | |
} | |
isRun = true; | |
// Start animation | |
// delay 3*FRAME_DURATION time | |
mStartTime = SystemClock.uptimeMillis() + 3 * FRAME_DURATION; | |
scheduleSelf(mAnim, mStartTime); | |
} | |
protected void onInAnimateUpdate(float factor) { | |
mRadius = mMaxRadius * factor; | |
invalidateSelf(); | |
} | |
private final Runnable mAnim = new Runnable() { | |
@Override | |
public void run() { | |
isFirst = false; | |
long currentTime = SystemClock.uptimeMillis(); | |
long diff = currentTime - mStartTime; | |
int duration = IN_ANIM_DURATION; | |
if (diff <= duration) { | |
float interpolation = mInterpolator.getInterpolation((float) diff / (float) duration); | |
// Notify | |
onInAnimateUpdate(interpolation); | |
// Next | |
scheduleSelf(this, currentTime + FRAME_DURATION); | |
} else { | |
unscheduleSelf(this); | |
// Notify | |
onInAnimateUpdate(1f); | |
isRun = false; | |
} | |
} | |
}; | |
} | |
//-------how to use--- | |
View view = findViewById(R.id.lay_top); | |
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); | |
AnimJagDrawable drawable = new AnimJagDrawable(); | |
drawable.setFluCount(new Rect(0, 0, 0, 36)); | |
drawable.setColor(getResources().getColor(R.color.purple_500)); | |
drawable.setAlpha(164); | |
view.setBackgroundDrawable(drawable); | |
mTopBg = drawable; | |
// onInitValues | |
onInitValues(); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
if (mTopBg != null) { | |
mTopBg.startAnim(); | |
mTopBg = null; | |
} | |
} | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment