Skip to content

Commit

Permalink
新增shine Button
Browse files Browse the repository at this point in the history
  • Loading branch information
pichsy committed Feb 25, 2021
1 parent 45d9b43 commit d902686
Show file tree
Hide file tree
Showing 15 changed files with 1,507 additions and 319 deletions.
1 change: 0 additions & 1 deletion app/src/main/java/com/pichs/app/xwidget/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView tv = findViewById(R.id.tv1);
XCardButton btn = findViewById(R.id.btn1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
Binary file added app/src/main/res/drawable-xxhdpi/ic_heart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/ic_start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 24 additions & 29 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,6 @@
android:padding="10dp"
android:text="普通粗细" />

<com.pichs.common.widget.view.XButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#F49595"
android:minHeight="10dp"
android:padding="10dp"
android:text="我是个混子,瑶瑶" />

<com.pichs.common.widget.view.XTextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="#F7F7C2"
android:text="大河之剑天上来,飞流直下三千尺"
android:textColor="#d00"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn1" />

<com.pichs.common.widget.view.XTextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
Expand All @@ -77,18 +54,36 @@
app:layout_constraintTop_toBottomOf="@+id/btn1"
app:xp_ignoreGlobalTypeface="true" />



<com.pichs.common.widget.switcher.XSwitchButton
android:layout_width="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:textColor="@color/white"
app:xp_swb_backgroundColor_switchOff="#A60EDD"
app:xp_swb_backgroundColor_switchOn="#4ee"
app:xp_swb_backgroundRadius="20dp"
app:xp_swb_text_switchOff=""
app:xp_swb_text_switchOn=""
app:xp_swb_thumbCheckedColor="#F43F0E"
app:xp_swb_thumbColor="#fff"
app:xp_swb_backColor="#00f"
android:layout_height="30dp"/>
app:xp_swb_thumbMargin="3dp"
app:xp_swb_thumbPressedColor="#000" />

<com.pichs.common.widget.shinebutton.ShineButton
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="20dp"
app:xp_shine_big_shine_color="#F10C0C"
app:xp_shine_small_shine_color="#3CC63C"
app:xp_shine_count="10"
app:xp_shine_enable_flashing="true"
app:xp_shine_checked_color="#0195EB"
app:xp_shine_icon_image="@drawable/ic_start"
app:xp_shine_normal_color="#C71FEA" />

<Space
android:layout_width="match_parent"
android:layout_height="300dp"/>
android:layout_height="300dp" />

</LinearLayout>
</com.pichs.common.widget.view.XScrollView>
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);
}
}
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);
}
}
}
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));
}
}
Loading

0 comments on commit d902686

Please sign in to comment.