diff --git a/app/build.gradle b/app/build.gradle index 9287ed9..8160bba 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -40,4 +40,5 @@ dependencies { testImplementation libs.junit androidTestImplementation libs.ext.junit androidTestImplementation libs.espresso.core + implementation(libs.glide) } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cbd2187..6f45e8f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,10 @@ + + + + diff --git a/app/src/main/java/com/example/androiddemo/MainActivity.java b/app/src/main/java/com/example/androiddemo/MainActivity.java index 94f331b..9ffa28b 100644 --- a/app/src/main/java/com/example/androiddemo/MainActivity.java +++ b/app/src/main/java/com/example/androiddemo/MainActivity.java @@ -27,6 +27,7 @@ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); +// setTheme(R.style.Theme_AndroidDemo); EdgeToEdge.enable(this); ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); @@ -51,7 +52,7 @@ public class MainActivity extends AppCompatActivity { @Override public void onBindViewHolder(@NonNull VH holder, int position) { holder.binding.button.setText(Function.list.get(position).name); - holder.binding.button.setOnClickListener(v -> { + holder.binding.getRoot().setOnClickListener(v -> { Function function = Function.list.get(holder.getAdapterPosition()); Intent intent = new Intent(MainActivity.this, function.Class); startActivity(intent); diff --git a/app/src/main/java/com/example/androiddemo/activity/Toast.java b/app/src/main/java/com/example/androiddemo/activity/Toast.java index 12405e1..d25e8d7 100644 --- a/app/src/main/java/com/example/androiddemo/activity/Toast.java +++ b/app/src/main/java/com/example/androiddemo/activity/Toast.java @@ -3,10 +3,19 @@ package com.example.androiddemo.activity; import android.animation.Animator; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; +import android.app.Dialog; +import android.graphics.Bitmap; import android.os.Bundle; +import android.os.Handler; +import android.renderscript.Allocation; +import android.renderscript.Element; +import android.renderscript.RenderScript; +import android.renderscript.ScriptIntrinsicBlur; +import android.util.DisplayMetrics; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; import android.widget.LinearLayout; import androidx.activity.EdgeToEdge; @@ -16,6 +25,7 @@ import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import com.bumptech.glide.Glide; import com.example.androiddemo.R; import com.example.androiddemo.databinding.ViewToastBinding; import com.google.android.material.card.MaterialCardView; @@ -40,13 +50,24 @@ public class Toast extends AppCompatActivity { // 屏幕的宽度/2 int width = getResources().getDisplayMetrics().widthPixels / 2; - LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT); - layoutParams.setMargins(0, 50, 0, 50); +// 50dp转px + DisplayMetrics metrics = getResources().getDisplayMetrics(); + int v1 = (int) (50 * metrics.density + 0.5f); + LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, v1); + layoutParams.setMargins(0, 20, 0, 0); layoutParams.gravity = Gravity.CENTER_HORIZONTAL; + viewById.setOnClickListener(v -> linearLayout.addView(NewToast("测试"), 0, layoutParams)); + + Glide.with(this).load("https://pic2.zhimg.com/v2-febc9228d18886a29c68a66536279dfb_r.jpg?source=1940ef5c") + .into((ImageView) findViewById(R.id.imageView)); + } + /** + * @noinspection SameParameterValue + */ private View NewToast(String string) { // 获取当前主题中的样式 ViewToastBinding binding = ViewToastBinding.inflate(getLayoutInflater()); @@ -54,7 +75,7 @@ public class Toast extends AppCompatActivity { MaterialCardView materialCardView = binding.getRoot(); - ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(materialCardView, "scaleY", 0.5f, 1f); + ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(materialCardView, "scaleY", 0.8f, 1f); scaleYAnimator.setDuration(300); // 动画持续时间1秒 // 创建透明度渐变动画 @@ -69,15 +90,17 @@ public class Toast extends AppCompatActivity { // 设置动画结束后的回调 set.addListener(new Animator.AnimatorListener() { @Override - public void onAnimationStart(@NonNull Animator animation) {} + public void onAnimationStart(@NonNull Animator animation) { + } @Override public void onAnimationEnd(@NonNull Animator animation) { // 缩小和淡出动画 // 创建透明度淡出动画 - ObjectAnimator fadeOutAnimator = ObjectAnimator.ofFloat(materialCardView, "alpha", 1f, 0f); - fadeOutAnimator.setDuration(3000); // 动画持续时间1秒 - fadeOutAnimator.start(); + ObjectAnimator fadeOutAnimator = ObjectAnimator.ofFloat(materialCardView, "alpha", 1f, 0.1f); + fadeOutAnimator.setDuration(300); // 动画持续时间1秒 + + new Handler().postDelayed(fadeOutAnimator::start, 2000); // 2000 milliseconds delay fadeOutAnimator.addListener(new Animator.AnimatorListener() { @Override @@ -103,10 +126,12 @@ public class Toast extends AppCompatActivity { } @Override - public void onAnimationCancel(@NonNull Animator animation) {} + public void onAnimationCancel(@NonNull Animator animation) { + } @Override - public void onAnimationRepeat(@NonNull Animator animation) {} + public void onAnimationRepeat(@NonNull Animator animation) { + } }); return materialCardView; } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 93ecbce..9694781 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -21,6 +21,7 @@ android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent" + android:layout_marginHorizontal="9dp" app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/> \ No newline at end of file diff --git a/app/src/main/res/layout/activity_toast.xml b/app/src/main/res/layout/activity_toast.xml index 4461032..d6fcec0 100644 --- a/app/src/main/res/layout/activity_toast.xml +++ b/app/src/main/res/layout/activity_toast.xml @@ -6,7 +6,11 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activity.Toast"> - +