Android 如何在API中的图标周围绘制圆形涟漪<;21?
当我想要API<21中的涟漪效应时,我通常使用前景视图-下面是我用于文本视图的Android 如何在API中的图标周围绘制圆形涟漪<;21?,android,android-imageview,Android,Android Imageview,当我想要API
foregroundtextview
。如果是图像,则会有一个foregroundimageview
,该代码也可以在互联网上下载:
/*
* Copyright 2014 DogmaLabs
*
* 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.
*/
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.widget.TextView;
import com.bekwaai.activities.R;
public class ForegroundTextView extends TextView {
private Drawable mForegroundSelector;
private Rect mRectPadding;
private boolean mUseBackgroundPadding = false;
public ForegroundTextView (Context context) {
super(context);
}
public ForegroundTextView (Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ForegroundTextView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ForegroundTextViewLayout, defStyle, 0);
final Drawable d = a.getDrawable(R.styleable.ForegroundTextViewLayout_android_foreground);
if (d != null) {
setForeground(d);
}
a.recycle();
if (this.getBackground() instanceof NinePatchDrawable) {
final NinePatchDrawable npd = (NinePatchDrawable) this.getBackground();
mRectPadding = new Rect();
if (npd.getPadding(mRectPadding)) {
mUseBackgroundPadding = true;
}
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mForegroundSelector != null && mForegroundSelector.isStateful()) {
mForegroundSelector.setState(getDrawableState());
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mForegroundSelector != null) {
if (mUseBackgroundPadding) {
mForegroundSelector.setBounds(mRectPadding.left, mRectPadding.top, w - mRectPadding.right, h - mRectPadding.bottom);
} else {
mForegroundSelector.setBounds(0, 0, w, h);
}
}
}
@Override
protected void dispatchDraw(@NonNull Canvas canvas) {
super.dispatchDraw(canvas);
if (mForegroundSelector != null) {
mForegroundSelector.draw(canvas);
}
}
@Override
protected boolean verifyDrawable(Drawable who) {
return super.verifyDrawable(who) || (who == mForegroundSelector);
}
@Override
public void jumpDrawablesToCurrentState() {
super.jumpDrawablesToCurrentState();
if (mForegroundSelector != null) mForegroundSelector.jumpToCurrentState();
}
public void setForeground(Drawable drawable) {
if (mForegroundSelector != drawable) {
if (mForegroundSelector != null) {
mForegroundSelector.setCallback(null);
unscheduleDrawable(mForegroundSelector);
}
mForegroundSelector = drawable;
if (drawable != null) {
setWillNotDraw(false);
drawable.setCallback(this);
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
} else {
setWillNotDraw(true);
}
requestLayout();
invalidate();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void drawableHotspotChanged(float x, float y) {
super.drawableHotspotChanged(x, y);
if (mForegroundSelector != null) {
mForegroundSelector.setHotspot(x, y);
}
}
}
它就像一个符咒,请参见下面的gif:
现在,我在卡片视图中有一个图标:
您可以在android studio visual editor中看到图标的边界是一个正方形。如果我对其应用前景图像视图,涟漪将延伸到正方形的边界,因此最终效果将是相机图标下的白色方形块
我真的不想那样。我希望最终在相机下方显示圆形白色波纹,类似于单击工具栏中的图标时:
API<21有没有办法做到这一点?我在instamaterial应用程序上找到了一个解决方案,但这并不是我想要的,但它必须做到
他们所做的只是将选择器链接为imageButton的背景:
<?xml version="1.0" encoding="utf-8"?>
<!--drawable/btn_feed_action.xml-->
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="200" android:exitFadeDuration="200">
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="@color/white45" />
</shape>
</item>
</selector>
然后,每次按下图像时,45%的透明白色圆圈进入并投射为图像背景。一旦你的手指被移开,圆圈就会消失
解决方案不是“涟漪”效应,更多的是淡入淡出效应。有谁能想出更好的解决办法吗