-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,507 additions
and
319 deletions.
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
167 changes: 167 additions & 0 deletions
167
widget/src/main/java/com/pichs/common/widget/shinebutton/PorterImageView.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* | ||
* Copyright (C) 2020 xuexiangjys([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.pichs.common.widget.shinebutton; | ||
|
||
import android.content.Context; | ||
import android.graphics.Bitmap; | ||
import android.graphics.Canvas; | ||
import android.graphics.Color; | ||
import android.graphics.Matrix; | ||
import android.graphics.Paint; | ||
import android.graphics.PorterDuff; | ||
import android.graphics.PorterDuffXfermode; | ||
import android.graphics.drawable.ColorDrawable; | ||
import android.graphics.drawable.Drawable; | ||
import android.util.AttributeSet; | ||
|
||
import androidx.appcompat.widget.AppCompatImageView; | ||
|
||
/** | ||
* @author xuexiang | ||
* @since 2020-01-06 11:36 | ||
*/ | ||
public abstract class PorterImageView extends AppCompatImageView { | ||
|
||
private static final PorterDuffXfermode PORTER_DUFF_XFERMODE = new PorterDuffXfermode(PorterDuff.Mode.DST_IN); | ||
|
||
private Canvas mMaskCanvas; | ||
private Bitmap mMaskBitmap; | ||
private Paint mMaskPaint; | ||
|
||
private Canvas mDrawableCanvas; | ||
private Bitmap mDrawableBitmap; | ||
private Paint mDrawablePaint; | ||
|
||
private int mPaintColor = Color.GRAY; | ||
|
||
private boolean mInvalidated = true; | ||
|
||
public PorterImageView(Context context) { | ||
super(context); | ||
init(); | ||
} | ||
|
||
public PorterImageView(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
init(); | ||
} | ||
|
||
public PorterImageView(Context context, AttributeSet attrs, int defStyle) { | ||
super(context, attrs, defStyle); | ||
init(); | ||
} | ||
|
||
private void init() { | ||
if (getScaleType() == ScaleType.FIT_CENTER) { | ||
setScaleType(ScaleType.CENTER_CROP); | ||
} | ||
|
||
mMaskPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | ||
mMaskPaint.setColor(Color.BLACK); | ||
} | ||
|
||
public void setTintColor(int color) { | ||
mPaintColor = color; | ||
setImageDrawable(new ColorDrawable(color)); | ||
if (mDrawablePaint != null) { | ||
mDrawablePaint.setColor(color); | ||
invalidate(); | ||
} | ||
} | ||
|
||
@Override | ||
public void invalidate() { | ||
mInvalidated = true; | ||
super.invalidate(); | ||
} | ||
|
||
@Override | ||
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | ||
super.onSizeChanged(w, h, oldw, oldh); | ||
createMaskCanvas(w, h, oldw, oldh); | ||
} | ||
|
||
private void createMaskCanvas(int width, int height, int oldw, int oldh) { | ||
boolean sizeChanged = width != oldw || height != oldh; | ||
if (width > 0 && height > 0 && (mMaskCanvas == null || sizeChanged)) { | ||
mMaskCanvas = new Canvas(); | ||
mMaskBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); | ||
mMaskCanvas.setBitmap(mMaskBitmap); | ||
|
||
mMaskPaint.reset(); | ||
paintMaskCanvas(mMaskCanvas, mMaskPaint, width, height); | ||
|
||
mDrawableCanvas = new Canvas(); | ||
mDrawableBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); | ||
mDrawableCanvas.setBitmap(mDrawableBitmap); | ||
mDrawablePaint = new Paint(Paint.ANTI_ALIAS_FLAG); | ||
mDrawablePaint.setColor(mPaintColor); | ||
mInvalidated = true; | ||
} | ||
} | ||
|
||
protected abstract void paintMaskCanvas(Canvas maskCanvas, Paint maskPaint, int width, int height); | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
int saveCount = canvas.saveLayer(0.0f, 0.0f, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG); | ||
try { | ||
if (mInvalidated) { | ||
Drawable drawable = getDrawable(); | ||
if (drawable != null) { | ||
mInvalidated = false; | ||
Matrix imageMatrix = getImageMatrix(); | ||
if (imageMatrix == null) { | ||
drawable.draw(mDrawableCanvas); | ||
} else { | ||
int drawableSaveCount = mDrawableCanvas.getSaveCount(); | ||
mDrawableCanvas.save(); | ||
mDrawableCanvas.concat(imageMatrix); | ||
drawable.draw(mDrawableCanvas); | ||
mDrawableCanvas.restoreToCount(drawableSaveCount); | ||
} | ||
|
||
mDrawablePaint.reset(); | ||
mDrawablePaint.setFilterBitmap(false); | ||
mDrawablePaint.setXfermode(PORTER_DUFF_XFERMODE); | ||
mDrawableCanvas.drawBitmap(mMaskBitmap, 0.0f, 0.0f, mDrawablePaint); | ||
} | ||
} | ||
|
||
if (!mInvalidated) { | ||
mDrawablePaint.setXfermode(null); | ||
canvas.drawBitmap(mDrawableBitmap, 0.0f, 0.0f, mDrawablePaint); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
canvas.restoreToCount(saveCount); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | ||
if (widthMeasureSpec == 0) { | ||
widthMeasureSpec = 50; | ||
} | ||
if (heightMeasureSpec == 0) { | ||
heightMeasureSpec = 50; | ||
} | ||
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
widget/src/main/java/com/pichs/common/widget/shinebutton/PorterShapeImageView.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright (C) 2020 xuexiangjys([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.pichs.common.widget.shinebutton; | ||
|
||
import android.content.Context; | ||
import android.content.res.TypedArray; | ||
import android.graphics.Canvas; | ||
import android.graphics.Matrix; | ||
import android.graphics.Paint; | ||
import android.graphics.drawable.BitmapDrawable; | ||
import android.graphics.drawable.Drawable; | ||
import android.util.AttributeSet; | ||
|
||
import com.pichs.common.widget.R; | ||
|
||
/** | ||
* 图标资源控件 | ||
* | ||
* @author xuexiang | ||
* @since 2020-01-06 20:42 | ||
*/ | ||
public class PorterShapeImageView extends PorterImageView { | ||
|
||
private Drawable mIconDrawable; | ||
private Matrix mMatrix; | ||
private Matrix mDrawMatrix; | ||
|
||
public PorterShapeImageView(Context context) { | ||
this(context, null); | ||
} | ||
|
||
public PorterShapeImageView(Context context, AttributeSet attrs) { | ||
this(context, attrs, 0); | ||
} | ||
|
||
public PorterShapeImageView(Context context, AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
initAttrs(context, attrs, defStyleAttr); | ||
} | ||
|
||
private void initAttrs(Context context, AttributeSet attrs, int defStyleAttr) { | ||
if (attrs != null) { | ||
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PorterShapeImageView, defStyleAttr, 0); | ||
mIconDrawable = array.getDrawable(R.styleable.PorterShapeImageView_xp_shine_icon_image); | ||
array.recycle(); | ||
} | ||
mMatrix = new Matrix(); | ||
} | ||
|
||
/** | ||
* 设置图标资源 | ||
* | ||
* @param drawable | ||
*/ | ||
public void setIconDrawable(Drawable drawable) { | ||
mIconDrawable = drawable; | ||
invalidate(); | ||
} | ||
|
||
@Override | ||
protected void paintMaskCanvas(Canvas maskCanvas, Paint maskPaint, int width, int height) { | ||
if (mIconDrawable != null) { | ||
if (mIconDrawable instanceof BitmapDrawable) { | ||
configureBitmapBounds(getWidth(), getHeight()); | ||
if (mDrawMatrix != null) { | ||
int drawableSaveCount = maskCanvas.getSaveCount(); | ||
maskCanvas.save(); | ||
maskCanvas.concat(mMatrix); | ||
mIconDrawable.draw(maskCanvas); | ||
maskCanvas.restoreToCount(drawableSaveCount); | ||
return; | ||
} | ||
} | ||
|
||
mIconDrawable.setBounds(0, 0, getWidth(), getHeight()); | ||
mIconDrawable.draw(maskCanvas); | ||
} | ||
} | ||
|
||
private void configureBitmapBounds(int viewWidth, int viewHeight) { | ||
mDrawMatrix = null; | ||
int drawableWidth = mIconDrawable.getIntrinsicWidth(); | ||
int drawableHeight = mIconDrawable.getIntrinsicHeight(); | ||
boolean fits = viewWidth == drawableWidth && viewHeight == drawableHeight; | ||
|
||
if (drawableWidth > 0 && drawableHeight > 0 && !fits) { | ||
mIconDrawable.setBounds(0, 0, drawableWidth, drawableHeight); | ||
float widthRatio = (float) viewWidth / (float) drawableWidth; | ||
float heightRatio = (float) viewHeight / (float) drawableHeight; | ||
float scale = Math.min(widthRatio, heightRatio); | ||
float dx = (int) ((viewWidth - drawableWidth * scale) * 0.5f + 0.5f); | ||
float dy = (int) ((viewHeight - drawableHeight * scale) * 0.5f + 0.5f); | ||
|
||
mMatrix.setScale(scale, scale); | ||
mMatrix.postTranslate(dx, dy); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
widget/src/main/java/com/pichs/common/widget/shinebutton/ShineAnimator.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
|
||
package com.pichs.common.widget.shinebutton; | ||
|
||
import android.animation.ValueAnimator; | ||
|
||
import com.pichs.common.widget.shinebutton.interpolator.Ease; | ||
import com.pichs.common.widget.shinebutton.interpolator.EasingInterpolator; | ||
|
||
/** | ||
* @author xuexiang | ||
* @since 2020-01-06 11:55 | ||
*/ | ||
public class ShineAnimator extends ValueAnimator { | ||
|
||
private static final float DEFAULT_MAX_VALUE = 1.5f; | ||
private static final long DEFAULT_ANIM_DURATION = 1500; | ||
|
||
public ShineAnimator() { | ||
setFloatValues(1f, DEFAULT_MAX_VALUE); | ||
setDuration(DEFAULT_ANIM_DURATION); | ||
setStartDelay(200); | ||
setInterpolator(new EasingInterpolator(Ease.QUART_OUT)); | ||
} | ||
|
||
public ShineAnimator(long duration, float maxValue, long delay) { | ||
setFloatValues(1f, maxValue); | ||
setDuration(duration); | ||
setStartDelay(delay); | ||
setInterpolator(new EasingInterpolator(Ease.QUART_OUT)); | ||
} | ||
} |
Oops, something went wrong.