需求:点击收藏,屏幕中间弹出收藏提示动画,3秒后消失,且悬浮在app之上(即不会随着viewpager滑动和activity跳转而消失)
解决方案:
布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/layout" android:background="#00ffffff"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/tips_collection_success" /> </LinearLayout>
1.使用dialog
import android.app.Dialog; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.ImageView; import android.widget.LinearLayout; import com.xuehu365.xuehu.R; /** * Created by Administrator on 2016/12/27. */ public class CollectionAnimDialog { private Context context; private Dialog dialog; private View view; private LinearLayout layout; public CollectionAnimDialog(Context context) { this.context = context; } public CollectionAnimDialog builder() { view = LayoutInflater.from(context).inflate(R.layout.collection_anim_dialog, null); dialog = new Dialog(context, R.style.CollectionAnimDialogStyle); layout = (LinearLayout) view.findViewById(R.id.layout); layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); image = (ImageView) view.findViewById(R.id.image); dialog.setContentView(view); return this; } public void show() { dialog.show(); collectionAnim(); } /** * 收藏动画 */ private void collectionAnim() { AlphaAnimation alpha = new AlphaAnimation(1, 0); alpha.setDuration(3000); alpha.setFillAfter(true); alpha.setRepeatCount(0); layout.startAnimation(alpha); alpha.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { dialog.dismiss(); } @Override public void onAnimationRepeat(Animation animation) { } }); } }
dialog样式
<!-- CollectionAnimDialog收藏动画对话框样式 --> <style name="CollectionAnimDialogStyle" parent="@android:style/Theme.Dialog"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowFrame">@null</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsTranslucent">true</item> </style>
2.Toast(推荐使用)
//收藏动画 Toast toast = new Toast(App.getApplication()); View view = LayoutInflater.from(App.getApplication()).inflate(R.layout.collection_anim_dialog, null); toast.setGravity(Gravity.CENTER, 0, 0); toast.setView(view); toast.show();
简单明了,动画系统自带,完美
时间: 2024-10-13 23:36:54