よくある、親のFloating Action Button(FAB)を押すと、子のFABが出てきて、親のFABの背景画像が変わるようなものを実装しようとしているんですが、その背景画像を変える処理がうまく行きません。
具体的に言うと、FABをおすと、一瞬縮んで膨らむようなアニメーションをしている間に背景画像を変えるということをやりたいのです。
アニメーション自体はうまく言っているのですが、背景画像を変える処理を入れると、FABを押した際に、FAB自体の色が抜けて(元の色は白です)透明になり、設定している画像は黒のはずなのに、白くなってしまいます。
setBackgraundResource()の前にsetBackgroundcolor()で背景色を設定しても同じ結果になってしまいます。
背景画像を変える際には、他の方法があるのでしょうか?
Java
1package teppei.reminder; 2 3import android.content.Context; 4import android.support.design.widget.FloatingActionButton; 5import android.view.View; 6import android.view.animation.Animation; 7import android.view.animation.AnimationUtils; 8 9/** 10 * Created by local-teppei on 2016/06/25. 11 */ 12public class FabListener implements View.OnClickListener { 13 private boolean isOpen; 14 private Animation animFabExpansion,animFabShrink; 15 private FloatingActionButton fabParent, fabDelete; 16 17 FabListener(Context context){ 18 isOpen = false; 19 animFabExpansion = AnimationUtils.loadAnimation(context, R.anim.anim_fab_expansion); 20 animFabShrink = AnimationUtils.loadAnimation(context, R.anim.anim_fab_shrink); 21 } 22 23 @Override 24 public void onClick(View v) { 25 26 switch (v.getId()) { 27 case R.id.fabParent: 28 if (isOpen) { 29 v.startAnimation(animFabShrink); 30 v.setBackgroundResource(R.mipmap.ic_keyboard_arrow_down_black_18dp); 31 v.startAnimation(animFabExpansion); 32 isOpen = false; 33 } else { 34 v.startAnimation(animFabShrink); 35 v.setBackgroundResource(R.mipmap.ic_delete_black_18dp); 36 v.startAnimation(animFabExpansion); 37 isOpen = true; 38 } 39 break; 40 41 case R.id.fab_delete: 42 break; 43 44 default: 45 break; 46 47 } 48 } 49} 50
xml
1<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 tools:context="teppei.reminder.FragmentListOfTasks"> 7 8 <ListView 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:id="@+id/listView" 12 > 13 </ListView> 14 15 <RelativeLayout 16 android:layout_width="match_parent" 17 android:layout_height="match_parent"> 18 19 <android.support.design.widget.FloatingActionButton 20 android:id="@+id/fab_delete" 21 android:layout_height="wrap_content" 22 android:layout_width="wrap_content" 23 android:clickable="true" 24 android:src="@mipmap/ic_delete_black_18dp" 25 app:backgroundTint="@color/white" 26 android:layout_marginRight="16dp" 27 android:layout_alignParentRight="true" 28 android:layout_above="@+id/fabParent" /> 29 30 31 <android.support.design.widget.FloatingActionButton 32 android:id="@+id/fabParent" 33 android:layout_height="wrap_content" 34 android:layout_width="wrap_content" 35 android:clickable="true" 36 37 app:backgroundTint="@color/white" 38 android:layout_marginBottom="16dp" 39 android:layout_marginRight="16dp" 40 android:layout_alignParentRight="true" 41 android:layout_alignParentBottom="true" /> 42 </RelativeLayout> 43 44</FrameLayout> 45
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。