編輯:關於Android編程
/** * */ package com.kince.rippleview; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.View; /** * @author kince * @category 波紋 * @since 2014.8.9 * @version v1.0.0 * */ public class RippleView extends View { private int mScreenWidth; private int mScreenHeight; private Bitmap mRippleBitmap; private Paint mRipplePaint = new Paint(); private int mBitmapWidth; private int mBitmapHeight; private boolean isStartRipple; private int heightPaddingTop; private int heightPaddingBottom; private int widthPaddingLeft; private int widthPaddingRight; private RectF mRect = new RectF(); private int rippleFirstRadius = 0; private int rippleSecendRadius = -33; private int rippleThirdRadius = -66; private Paint textPaint = new Paint(); private String mText=點擊我吧; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); invalidate(); if (isStartRipple) { rippleFirstRadius++; if (rippleFirstRadius > 100) { rippleFirstRadius = 0; } rippleSecendRadius++; if (rippleSecendRadius > 100) { rippleSecendRadius = 0; } rippleThirdRadius++; if (rippleThirdRadius > 100) { rippleThirdRadius = 0; } sendEmptyMessageDelayed(0, 20); } } }; /** * @param context */ public RippleView(Context context) { super(context); // TODO Auto-generated constructor stub init(); } /** * @param context * @param attrs */ public RippleView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub init(); } /** * @param context * @param attrs * @param defStyleAttr */ public RippleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub init(); } private void init() { mRipplePaint.setColor(4961729); mRipplePaint.setAntiAlias(true); mRipplePaint.setStyle(Paint.Style.FILL); textPaint.setTextSize(26); textPaint.setAntiAlias(true); textPaint.setStyle(Paint.Style.FILL); textPaint.setColor(Color.WHITE); mRippleBitmap = BitmapFactory.decodeStream(getResources() .openRawResource(R.drawable.easy3d_ic_apply)); mBitmapWidth = mRippleBitmap.getWidth(); mBitmapHeight = mRippleBitmap.getHeight(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub super.onMeasure(widthMeasureSpec, heightMeasureSpec); int mh = getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); int mw = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); if (mBitmapWidth < 2 * mBitmapHeight) { mBitmapWidth = (2 * mBitmapHeight); } setMeasuredDimension(mBitmapWidth, mBitmapHeight); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); if (isStartRipple) { float f1 = 3 * mBitmapHeight / 10; mRipplePaint.setAlpha(255); canvas.drawCircle(mBitmapWidth / 2, mBitmapHeight, 7 * mBitmapHeight / 10, mRipplePaint); int i1 = (int) (220.0F - (220.0F - 0.0F) / 100.0F * rippleFirstRadius); mRipplePaint.setAlpha(i1); canvas.drawCircle(mBitmapWidth / 2, mBitmapHeight, 7 * mBitmapHeight / 10 + f1 * rippleFirstRadius / 100.0F, mRipplePaint); if (rippleSecendRadius >= 0) { int i3 = (int) (220.0F - (220.0F - 0.0F) / 100.0F * rippleSecendRadius); mRipplePaint.setAlpha(i3); canvas.drawCircle(mBitmapWidth / 2, mBitmapHeight, 7 * mBitmapHeight / 10 + f1 * rippleSecendRadius / 100.0F, mRipplePaint); } if (rippleThirdRadius >= 0) { int i2 = (int) (220.0F - (220.0F - 0.0F) / 100.0F * rippleThirdRadius); mRipplePaint.setAlpha(i2); canvas.drawCircle(mBitmapWidth / 2, mBitmapHeight, 7 * mBitmapHeight / 10 + f1 * rippleThirdRadius / 100.0F, mRipplePaint); } } mRipplePaint.setAlpha(30); canvas.drawCircle(mBitmapWidth / 2, mBitmapHeight, mBitmapHeight, mRipplePaint); mRipplePaint.setAlpha(120); canvas.drawCircle(mBitmapWidth / 2, mBitmapHeight, 9 * mBitmapHeight / 10, mRipplePaint); mRipplePaint.setAlpha(180); canvas.drawCircle(mBitmapWidth / 2, mBitmapHeight, 8 * mBitmapHeight / 10, mRipplePaint); mRipplePaint.setAlpha(255); canvas.drawCircle(mBitmapWidth / 2, mBitmapHeight, 7 * mBitmapHeight / 10, mRipplePaint); float length = textPaint.measureText(mText); canvas.drawText(mText, (mBitmapWidth - length) / 2, mBitmapHeight * 3 / 4, textPaint); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { // TODO Auto-generated method stub super.onSizeChanged(w, h, oldw, oldh); mScreenWidth = w; mScreenHeight = h; confirmSize(); invalidate(); } private void confirmSize() { int minScreenSize = Math.min(mScreenWidth, mScreenHeight); int widthOverSize = mScreenWidth - minScreenSize; int heightOverSize = mScreenHeight - minScreenSize; heightPaddingTop = (getPaddingTop() + heightOverSize / 2); heightPaddingBottom = (getPaddingBottom() + heightOverSize / 2); widthPaddingLeft = (getPaddingLeft() + widthOverSize / 2); widthPaddingRight = (getPaddingRight() + widthOverSize / 2); int width = getWidth(); int height = getHeight(); mRect = new RectF(widthPaddingLeft, heightPaddingTop, width - widthPaddingRight, height * 2 - heightPaddingBottom); } public void stratRipple() { isStartRipple = true; handler.sendEmptyMessage(0); } }下圖是某個應用的流量顯示界面,使用的是上面說的第二種形式。 實現上面效果的思路是使用正弦或者余弦曲線,代碼如下:
/** * */ package com.kince.waveview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.os.Handler; import android.os.Parcel; import android.os.Parcelable; import android.util.AttributeSet; import android.view.View; import android.widget.ProgressBar; /** * @author kince * @category View必須是正方形 * */ public class WaterWaveView extends View { private Context mContext; private int mScreenWidth; private int mScreenHeight; private Paint mRingPaint; private Paint mCirclePaint; private Paint mWavePaint; private Paint linePaint; private Paint flowPaint; private Paint leftPaint; private int mRingSTROKEWidth = 15; private int mCircleSTROKEWidth = 2; private int mLineSTROKEWidth = 1; private int mCircleColor = Color.WHITE; private int mRingColor = Color.WHITE; private int mWaveColor = Color.WHITE; private Handler mHandler; private long c = 0L; private boolean mStarted = false; private final float f = 0.033F; private int mAlpha = 50;// 透明度 private float mAmplitude = 10.0F; // 振幅 private float mWateLevel = 0.5F;// 水高(0~1) private Path mPath; private String flowNum = 1024M; private String flowLeft = 還剩余; /** * @param context */ public WaterWaveView(Context context) { super(context); // TODO Auto-generated constructor stub mContext = context; init(mContext); } /** * @param context * @param attrs */ public WaterWaveView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub mContext = context; init(mContext); } /** * @param context * @param attrs * @param defStyleAttr */ public WaterWaveView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub mContext = context; init(mContext); } private void init(Context context) { mRingPaint = new Paint(); mRingPaint.setColor(mRingColor); mRingPaint.setAlpha(50); mRingPaint.setStyle(Paint.Style.STROKE); mRingPaint.setAntiAlias(true); mRingPaint.setStrokeWidth(mRingSTROKEWidth); mCirclePaint = new Paint(); mCirclePaint.setColor(mCircleColor); mCirclePaint.setStyle(Paint.Style.STROKE); mCirclePaint.setAntiAlias(true); mCirclePaint.setStrokeWidth(mCircleSTROKEWidth); linePaint = new Paint(); linePaint.setColor(mCircleColor); linePaint.setStyle(Paint.Style.STROKE); linePaint.setAntiAlias(true); linePaint.setStrokeWidth(mLineSTROKEWidth); flowPaint = new Paint(); flowPaint.setColor(mCircleColor); flowPaint.setStyle(Paint.Style.FILL); flowPaint.setAntiAlias(true); flowPaint.setTextSize(36); leftPaint = new Paint(); leftPaint.setColor(mCircleColor); leftPaint.setStyle(Paint.Style.FILL); leftPaint.setAntiAlias(true); leftPaint.setTextSize(18); mWavePaint = new Paint(); mWavePaint.setStrokeWidth(1.0F); mWavePaint.setColor(mWaveColor); mWavePaint.setAlpha(mAlpha); mPath = new Path(); mHandler = new Handler() { @Override public void handleMessage(android.os.Message msg) { if (msg.what == 0) { invalidate(); if (mStarted) { // 不斷發消息給自己,使自己不斷被重繪 mHandler.sendEmptyMessageDelayed(0, 60L); } } } }; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = measure(widthMeasureSpec, true); int height = measure(heightMeasureSpec, false); if (width < height) { setMeasuredDimension(width, width); } else { setMeasuredDimension(height, height); } } /** * @category 測量 * @param measureSpec * @param isWidth * @return */ private int measure(int measureSpec, boolean isWidth) { int result; int mode = MeasureSpec.getMode(measureSpec); int size = MeasureSpec.getSize(measureSpec); int padding = isWidth ? getPaddingLeft() + getPaddingRight() : getPaddingTop() + getPaddingBottom(); if (mode == MeasureSpec.EXACTLY) { result = size; } else { result = isWidth ? getSuggestedMinimumWidth() : getSuggestedMinimumHeight(); result += padding; if (mode == MeasureSpec.AT_MOST) { if (isWidth) { result = Math.max(result, size); } else { result = Math.min(result, size); } } } return result; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { // TODO Auto-generated method stub super.onSizeChanged(w, h, oldw, oldh); mScreenWidth = w; mScreenHeight = h; } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); // 得到控件的寬高 int width = getWidth(); int height = getHeight(); setBackgroundColor(mContext.getResources().getColor( R.color.holo_purple2)); canvas.drawCircle(mScreenWidth / 2, mScreenHeight / 2, mScreenWidth / 4, mRingPaint); canvas.drawCircle(mScreenWidth / 2, mScreenHeight / 2, mScreenWidth / 4 - mRingSTROKEWidth / 2, mCirclePaint); canvas.drawLine(mScreenWidth * 3 / 8, mScreenHeight * 5 / 8, mScreenWidth * 5 / 8, mScreenHeight * 5 / 8, linePaint); float num = flowPaint.measureText(flowNum); canvas.drawText(flowNum, mScreenWidth * 4 / 8 - num / 2, mScreenHeight * 4 / 8, flowPaint); float left = leftPaint.measureText(flowLeft); canvas.drawText(flowLeft, mScreenWidth * 4 / 8 - left / 2, mScreenHeight * 3 / 8, leftPaint); // 如果未開始(未調用startWave方法),繪制一個扇形 if ((!mStarted) || (mScreenWidth == 0) || (mScreenHeight == 0)) { RectF oval = new RectF(mScreenWidth / 4 + mRingSTROKEWidth / 2, mScreenHeight / 4 + mRingSTROKEWidth / 2, mScreenWidth * 3 / 4 - mRingSTROKEWidth / 2, mScreenHeight * 3 / 4 - mRingSTROKEWidth / 2);// 設置個新的長方形,掃描測量 canvas.drawArc(oval, 0, 180, true, mWavePaint); return; } // 繪制,即水面靜止時的高度 RectF oval = new RectF(mScreenWidth / 4 + mRingSTROKEWidth / 2, mScreenHeight / 4 + mRingSTROKEWidth / 2 + mAmplitude * 2, mScreenWidth * 3 / 4 - mRingSTROKEWidth / 2, mScreenHeight * 3 / 4 - mRingSTROKEWidth / 2);// 設置個新的長方形,掃描測量 canvas.drawArc(oval, 0, 180, true, mWavePaint); if (this.c >= 8388607L) { this.c = 0L; } // 每次onDraw時c都會自增 c = (1L + c); float f1 = mScreenHeight * (1.0F - mWateLevel); int top = (int) (f1 + mAmplitude); mPath.reset(); int startX = mScreenWidth / 2 - mScreenWidth / 4 + mRingSTROKEWidth / 2; // 波浪效果 while (startX < mScreenWidth / 2 + mScreenWidth / 4 - mRingSTROKEWidth / 2) { int startY = (int) (f1 - mAmplitude * Math.sin(Math.PI * (2.0F * (startX + this.c * width * this.f)) / width)); canvas.drawLine(startX, startY, startX, top, mWavePaint); startX++; } canvas.restore(); } @Override public Parcelable onSaveInstanceState() { // Force our ancestor class to save its state Parcelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); ss.progress = (int) c; return ss; } @Override public void onRestoreInstanceState(Parcelable state) { SavedState ss = (SavedState) state; super.onRestoreInstanceState(ss.getSuperState()); c = ss.progress; } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); // 關閉硬件加速,防止異常unsupported operation exception this.setLayerType(View.LAYER_TYPE_SOFTWARE, null); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } /** * @category 開始波動 */ public void startWave() { if (!mStarted) { this.c = 0L; mStarted = true; this.mHandler.sendEmptyMessage(0); } } /** * @category 停止波動 */ public void stopWave() { if (mStarted) { this.c = 0L; mStarted = false; this.mHandler.removeMessages(0); } } /** * @category 保存狀態 */ static class SavedState extends BaseSavedState { int progress; /** * Constructor called from {@link ProgressBar#onSaveInstanceState()} */ SavedState(Parcelable superState) { super(superState); } /** * Constructor called from {@link #CREATOR} */ private SavedState(Parcel in) { super(in); progress = in.readInt(); } @Override public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeInt(progress); } public static final Parcelable.CreatorCREATOR = new Parcelable.Creator () { public SavedState createFromParcel(Parcel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; } }
react-native-easy-toast一款簡單易用的 Toast 組件,支持 Android&iOS。安裝1.在終端運行 npm i react-nati
在這裡談一下墨跡天氣的換膚實現方式,不過首先聲明我只是通過反編譯以及參考了一些網上其他資料的方式推測出的換膚原理, 在這裡只供參考. 若大家有更好的方式, 歡迎交流.墨跡
原因分析用戶使用android 5.0以上的系統在安裝APP時,將消息通知的權限關閉掉了。實際上用戶本意只是想關閉Notification,但是Toast的show方法中
Android Studio安裝更新終極解決方式之前寫過一篇Android SDK無法更新的博文,其實該方式對Android Studio同樣有效,大伙可以下載網盤中分享