完善基本框架提供了一个简单的例子

This commit is contained in:
muqing 2024-07-07 00:04:40 +08:00
parent 0e951214de
commit a020247a3b
14 changed files with 309 additions and 57 deletions

View File

@ -0,0 +1,3 @@
<component name="ProjectDictionaryState">
<dictionary name="19669" />
</component>

View File

@ -26,6 +26,9 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding true
}
}
dependencies {

View File

@ -1,26 +0,0 @@
package com.example.androiddemo;
import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.example.androiddemo", appContext.getPackageName());
}
}

View File

@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools" >
<application
android:name=".App"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
@ -11,10 +12,13 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidDemo"
tools:targetApi="31">
tools:targetApi="31" >
<activity
android:name=".activity.Toast"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -0,0 +1,15 @@
package com.example.androiddemo;
import android.app.Application;
/**
* 启动入口
* 不可改写
*/
public class App extends Application {
public static App instance;
public App() {
instance = this;
Function.init();
}
}

View File

@ -0,0 +1,27 @@
package com.example.androiddemo;
import com.example.androiddemo.activity.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* 添加功能的地方
**/
public class Function {
//功能名称
public String name;
//界面的指向
public Class<?> Class;
public static List<Function> list = new ArrayList<>();
public Function(String name, Class<?> Class) {
this.name = name;
this.Class = Class;
}
public static void init() {
list.add(new Function("多样化提示", Toast.class));
}
}

View File

@ -1,24 +1,75 @@
package com.example.androiddemo;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.androiddemo.databinding.ActivityMainBinding;
import com.example.androiddemo.databinding.ItemMainBinding;
/**
* 界面入口
* 功能选择入口
* 不建议改写除了UI布局
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
setSupportActionBar(binding.toolbar);
binding.recycler.setLayoutManager(new LinearLayoutManager(this));
binding.recycler.setAdapter(new MyAdapter());
}
private class MyAdapter extends RecyclerView.Adapter<VH> {
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new VH(getLayoutInflater().inflate(R.layout.item_main, parent, false));
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
holder.binding.button.setText(Function.list.get(position).name);
holder.binding.button.setOnClickListener(v -> {
Function function = Function.list.get(holder.getAdapterPosition());
Intent intent = new Intent(MainActivity.this, function.Class);
startActivity(intent);
});
}
@Override
public int getItemCount() {
return Function.list.size();
}
}
private static class VH extends RecyclerView.ViewHolder {
public ItemMainBinding binding;
public VH(@NonNull View itemView) {
super(itemView);
binding = ItemMainBinding.bind(itemView);
}
}
}

View File

@ -0,0 +1,113 @@
package com.example.androiddemo.activity;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.example.androiddemo.R;
import com.example.androiddemo.databinding.ViewToastBinding;
import com.google.android.material.card.MaterialCardView;
/**
* 多样化提示界面
*/
public class Toast extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_toast);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
View viewById = findViewById(R.id.TopToast);
LinearLayout linearLayout = findViewById(R.id.line1);
// 屏幕的宽度/2
int width = getResources().getDisplayMetrics().widthPixels / 2;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 50, 0, 50);
layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
viewById.setOnClickListener(v -> linearLayout.addView(NewToast("测试"), 0, layoutParams));
}
private View NewToast(String string) {
// 获取当前主题中的样式
ViewToastBinding binding = ViewToastBinding.inflate(getLayoutInflater());
binding.textView.setText(string);
MaterialCardView materialCardView = binding.getRoot();
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(materialCardView, "scaleY", 0.5f, 1f);
scaleYAnimator.setDuration(300); // 动画持续时间1秒
// 创建透明度渐变动画
ObjectAnimator fadeInAnimator = ObjectAnimator.ofFloat(materialCardView, "alpha", 0f, 1f);
fadeInAnimator.setDuration(300); // 动画持续时间1秒
// 同时播放两个动画
AnimatorSet set = new AnimatorSet();
set.playTogether(scaleYAnimator, fadeInAnimator);
set.start();
// 设置动画结束后的回调
set.addListener(new Animator.AnimatorListener() {
@Override
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();
fadeOutAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(@NonNull Animator animation) {
}
@Override
public void onAnimationEnd(@NonNull Animator animation) {
((LinearLayout) findViewById(R.id.line1)).removeView(materialCardView);
}
@Override
public void onAnimationCancel(@NonNull Animator animation) {
}
@Override
public void onAnimationRepeat(@NonNull Animator animation) {
}
});
}
@Override
public void onAnimationCancel(@NonNull Animator animation) {}
@Override
public void onAnimationRepeat(@NonNull Animator animation) {}
});
return materialCardView;
}
}

View File

@ -1,19 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="@string/app_name"/>
</com.google.android.material.appbar.AppBarLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.Toast">
<Button
android:id="@+id/TopToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Toast"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.Material3.Button.ElevatedButton"
android:text="Button"
android:layout_margin="6dp"/>
</LinearLayout>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/Widget.Material3.CardView.Filled"
app:cardUseCompatPadding="true">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:gravity="center"
android:text="Hello World!"
android:textStyle="bold"/>
</com.google.android.material.card.MaterialCardView>

View File

@ -1,17 +0,0 @@
package com.example.androiddemo;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}