浮动操作按钮 [英] Floating Action Button
问题描述
我一直在尝试使用浮动操作按钮。我试图用一些建议在这里的资源和环节是伟大的;但是,我不能使用,因为问题dependicies很多人。我试图修复它,但它得到了更多的混乱。长话短说,我用下面的code,以此来绕过dependicies在我的包。我得到了按钮的工作;不过,我无法弄清楚如何有选择按钮被点击的时候出现。我试了clicklistener等方式,但我总是得到一个错误。
公共类FloatingActionButton扩展视图{
上下文语境;
涂料mButtonPaint;
涂料mDrawablePaint;
位图mBitmap;
布尔mHidden = FALSE;
公共FloatingActionButton(上下文的背景下){
超(上下文);
this.context =背景;
的init(Color.WHITE);
}
公共无效的init(INT颜色){
setWillNotDraw(假);
setLayerType(View.LAYER_TYPE_SOFTWARE,NULL);
mButtonPaint =新的油漆(Paint.ANTI_ALIAS_FLAG);
mButtonPaint.setColor(颜色);
mButtonPaint.setStyle(Paint.Style.FILL);
mButtonPaint.setShadowLayer(10.0f,0.0,3.5F,Color.argb(100,0,0,0));
mDrawablePaint =新的油漆(Paint.ANTI_ALIAS_FLAG);
无效();
}
@覆盖
保护无效的OnDraw(帆布油画){
setClickable(真正的);
canvas.drawCircle(的getWidth()/ 2,的getHeight()/ 2,(浮子)(的getWidth()/ 2.6),mButtonPaint);
canvas.drawBitmap(mBitmap,(的getWidth() - mBitmap.getWidth())/ 2,
(的getHeight() - mBitmap.getHeight())/ 2,mDrawablePaint);
}
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
如果(event.getAction()== MotionEvent.ACTION_UP){
setAlpha(1.0F);
}否则,如果(event.getAction()== MotionEvent.ACTION_DOWN){
setAlpha(0.6f);
}
返回super.onTouchEvent(事件);
}
公共无效setColor(INT颜色){
初始化(彩色);
}
公共无效setDrawable(绘制对象绘制){
mBitmap =((BitmapDrawable)可拉伸).getBitmap();
无效();
}
公共无效隐藏(){
如果(!mHidden){
ObjectAnimator的scaleX = ObjectAnimator.ofFloat(这一点,将scaleX,1,0);
ObjectAnimator的scaleY = ObjectAnimator.ofFloat(这一点,的scaleY,1,0);
AnimatorSet animSetXY =新AnimatorSet();
animSetXY.playTogether(将scaleX,的scaleY);
animSetXY.setInterpolator(新AccelerateInterpolator());
animSetXY.setDuration(100);
animSetXY.start();
mHidden =真;
}
}
公共无效展(){
如果(mHidden){
ObjectAnimator的scaleX = ObjectAnimator.ofFloat(此,将scaleX,0,1);
ObjectAnimator的scaleY = ObjectAnimator.ofFloat(此,的scaleY,0,1);
AnimatorSet animSetXY =新AnimatorSet();
animSetXY.playTogether(将scaleX,的scaleY);
animSetXY.setInterpolator(新OvershootInterpolator());
animSetXY.setDuration(200);
animSetXY.start();
mHidden = FALSE;
}
}
公共布尔isHidden(){
返回mHidden;
}
公共静态类生成器{
私人FrameLayout.LayoutParams PARAMS;
私人最终活动的活动;
INT比重= Gravity.BOTTOM | Gravity.RIGHT; //默认的右下方
绘制对象绘制;
INT颜色= Color.WHITE;
INT大小= 0;
浮规模= 0;
/ **
*构造函数使用上下文为这个生成器和
* {@link com.williammora.openfeed.widgets.FloatingActionButton}它创建
* @参数方面
* /
公共生成器(活动范围内){
规模= context.getResources()getDisplayMetrics()密度。
//计算(值*规模+ 0.5F)是一种广泛使用的转换为DPS像素
基于密度规模//单位
//看< A HREF =http://developer.android.com/guide/practices/screens_support.html>
// developer.android.com(支持多个屏幕大小)LT; / A>
大小=(int)的(72 *标度+ 0.5F); //默认大小为72dp由72dp
PARAMS =新FrameLayout.LayoutParams(尺寸,大小);
params.gravity =严重性;
this.activity =背景;
}
公共生成器withGravity(INT重力){
this.gravity =严重性;
回到这一点;
}
公共生成器withMargins(INT左,INT顶部,诠释权,诠释底部){
params.setMargins((INT)(左*规模+ 0.5F),(INT)(前*规模+ 0.5F)
(INT)(右*规模+ 0.5F),(INT)(下*规模+ 0.5F));
回到这一点;
}
公共生成器抽出(最终绘制对象绘制){
this.drawable =绘制;
回到这一点;
}
公共生成器withColor(最终诠释色){
this.color =颜色;
回到这一点;
}
公共生成器withSize(INT尺寸){
大小=(INT)(尺寸*规模+ 0.5F);
PARAMS =新FrameLayout.LayoutParams(尺寸,大小);
回到这一点;
}
公共FloatingActionButton创建(){
最后FloatingActionButton按钮=新FloatingActionButton(活动);
button.setColor(this.color);
button.setDrawable(this.drawable);
params.gravity = this.gravity;
ViewGroup中的root =(ViewGroup中)activity.findViewById(android.R.id.content);
root.addView(按钮,则params);
返回按钮;
}
}
}
FloatingActionButton mFab =新FloatingActionButton.Builder(本)
.withColor(getResources()的getColor(R.color.primaryColorDark))
.withDrawable(getResources()。getDrawable(R.drawable.ic_launcher))
.withSize(72)
.withMargins(0,0,16,16)
。创建();
MainActivity
FloatingActionButton mFab =新FloatingActionButton.Builder(本)
.withColor(getResources()的getColor(R.color.primaryColorDark))
.withDrawable(getResources()。getDrawable(R.drawable.ic_launcher))
.withSize(72)
.withMargins(0,0,16,16)
。创建();
只要把在模块的应用程序编译com.android.support:design:22.2.1
依赖。
在你的XML:
< android.support.design.widget.FloatingActionButton
风格=@风格/< your_style>中
机器人:SRC =@可绘制/< your_icon_src>中
应用程序:layout_anchor =@ ID /<如果采用与表把你的listID这里>中
应用程序:layout_anchorGravity =底部|右|结束
机器人:ID =@ + ID /晶圆厂
/>
在你的java:
FloatingActionButton FAB =(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
}
});
I have been trying to use Floating Action Button. I tried to use some of the resources suggested on here and the links were great; however, I couldn't use alot of them because of problems with dependicies. I tried to fix it but it got more messed up. Long story short, I use the following code as a way to bypass dependicies in my bundle. I got the button to work; however, I couldn't figure out how to have options to appear when the button is clicked. I tried on clicklistener and other ways but I always got an error.
public class FloatingActionButton extends View {
Context context;
Paint mButtonPaint;
Paint mDrawablePaint;
Bitmap mBitmap;
boolean mHidden = false;
public FloatingActionButton(Context context) {
super(context);
this.context = context;
init(Color.WHITE);
}
public void init(int color) {
setWillNotDraw(false);
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mButtonPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mButtonPaint.setColor(color);
mButtonPaint.setStyle(Paint.Style.FILL);
mButtonPaint.setShadowLayer(10.0f, 0.0f, 3.5f, Color.argb(100, 0, 0, 0));
mDrawablePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
setClickable(true);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, (float) (getWidth() / 2.6), mButtonPaint);
canvas.drawBitmap(mBitmap, (getWidth() - mBitmap.getWidth()) / 2,
(getHeight() - mBitmap.getHeight()) / 2, mDrawablePaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
setAlpha(1.0f);
} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
setAlpha(0.6f);
}
return super.onTouchEvent(event);
}
public void setColor(int color) {
init(color);
}
public void setDrawable(Drawable drawable) {
mBitmap = ((BitmapDrawable) drawable).getBitmap();
invalidate();
}
public void hide() {
if (!mHidden) {
ObjectAnimator scaleX = ObjectAnimator.ofFloat(this, "scaleX", 1, 0);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(this, "scaleY", 1, 0);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(scaleX, scaleY);
animSetXY.setInterpolator(new AccelerateInterpolator());
animSetXY.setDuration(100);
animSetXY.start();
mHidden = true;
}
}
public void show() {
if (mHidden) {
ObjectAnimator scaleX = ObjectAnimator.ofFloat(this, "scaleX", 0, 1);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(this, "scaleY", 0, 1);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(scaleX, scaleY);
animSetXY.setInterpolator(new OvershootInterpolator());
animSetXY.setDuration(200);
animSetXY.start();
mHidden = false;
}
}
public boolean isHidden() {
return mHidden;
}
public static class Builder {
private FrameLayout.LayoutParams params;
private final Activity activity;
int gravity = Gravity.BOTTOM | Gravity.RIGHT; // default bottom right
Drawable drawable;
int color = Color.WHITE;
int size = 0;
float scale = 0;
/**
* Constructor using a context for this builder and the
* {@link com.williammora.openfeed.widgets.FloatingActionButton} it creates
* @param context
*/
public Builder(Activity context) {
scale = context.getResources().getDisplayMetrics().density;
// The calculation (value * scale + 0.5f) is a widely used to convert to dps to pixel
// units based on density scale
// see <a href="http://developer.android.com/guide/practices/screens_support.html">
// developer.android.com (Supporting Multiple Screen Sizes)</a>
size = (int) (72 * scale + 0.5f); // default size is 72dp by 72dp
params = new FrameLayout.LayoutParams(size, size);
params.gravity = gravity;
this.activity = context;
}
public Builder withGravity(int gravity) {
this.gravity = gravity;
return this;
}
public Builder withMargins(int left, int top, int right, int bottom) {
params.setMargins((int) (left * scale + 0.5f), (int) (top * scale + 0.5f),
(int) (right * scale + 0.5f), (int) (bottom * scale + 0.5f));
return this;
}
public Builder withDrawable(final Drawable drawable) {
this.drawable = drawable;
return this;
}
public Builder withColor(final int color) {
this.color = color;
return this;
}
public Builder withSize(int size) {
size = (int) (size * scale + 0.5f);
params = new FrameLayout.LayoutParams(size, size);
return this;
}
public FloatingActionButton create() {
final FloatingActionButton button = new FloatingActionButton(activity);
button.setColor(this.color);
button.setDrawable(this.drawable);
params.gravity = this.gravity;
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
return button;
}
}
}
FloatingActionButton mFab = new FloatingActionButton.Builder(this)
.withColor(getResources().getColor(R.color.primaryColorDark))
.withDrawable(getResources().getDrawable(R.drawable.ic_launcher))
.withSize(72)
.withMargins(0, 0, 16, 16)
.create();
MainActivity
FloatingActionButton mFab = new FloatingActionButton.Builder(this)
.withColor(getResources().getColor(R.color.primaryColorDark))
.withDrawable(getResources().getDrawable(R.drawable.ic_launcher))
.withSize(72)
.withMargins(0, 0, 16, 16)
.create();
Just put compile 'com.android.support:design:22.2.1'
in your module app dependencies.
In your xml:
<android.support.design.widget.FloatingActionButton
style="@style/<your_style>"
android:src="@drawable/<your_icon_src>"
app:layout_anchor="@id/<if using along with list put your listID here>"
app:layout_anchorGravity="bottom|right|end"
android:id="@+id/fab"
/>
In you java:
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
这篇关于浮动操作按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!