Emotion Rating View 开源项目教程
1. 项目的目录结构及介绍
Emotion Rating View 项目的目录结构如下:
emotion-rating-view/
├── app/
│ ├── gradle/
│ │ └── wrapper/
│ ├── src/
│ └── build.gradle
├── gradle/
│ └── wrapper/
├── library/
│ ├── src/
│ └── build.gradle
├── .gitignore
├── LICENSE
├── README.md
├── build.gradle
├── gradle.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
目录结构介绍
-
app/: 包含示例应用程序的代码。
- gradle/wrapper/: Gradle 包装器文件。
- src/: 示例应用程序的源代码。
- build.gradle: 示例应用程序的构建脚本。
-
gradle/: Gradle 包装器文件。
- wrapper/: Gradle 包装器文件。
-
library/: 包含库的代码。
- src/: 库的源代码。
- build.gradle: 库的构建脚本。
-
.gitignore: Git 忽略文件。
-
LICENSE: 项目许可证。
-
README.md: 项目自述文件。
-
build.gradle: 根项目的构建脚本。
-
gradle.properties: Gradle 属性文件。
-
gradlew: Gradle 包装器脚本(Unix)。
-
gradlew.bat: Gradle 包装器脚本(Windows)。
-
settings.gradle: 项目设置文件。
2. 项目的启动文件介绍
项目的启动文件主要是 app/src/main/java/.../MainActivity.java
和 app/src/main/res/layout/activity_main.xml
。
MainActivity.java
这是示例应用程序的主活动文件,负责初始化和设置 Emotion Rating View 库。
package com.dm.emotionrating.app;
import android.os.Bundle;
import android.widget.RatingBar;
import androidx.appcompat.app.AppCompatActivity;
import com.dm.emotionrating.library.EmotionView;
import com.dm.emotionrating.library.GradientBackgroundView;
public class MainActivity extends AppCompatActivity {
private EmotionView emotionView;
private GradientBackgroundView gradientBackgroundView;
private RatingBar ratingView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emotionView = findViewById(R.id.emotionView);
gradientBackgroundView = findViewById(R.id.gradientBackgroundView);
ratingView = findViewById(R.id.ratingView);
ratingView.setOnRatingBarChangeListener((ratingBar, rating, fromUser) -> {
emotionView.setRating(rating);
gradientBackgroundView.changeBackground(rating);
});
}
}
activity_main.xml
这是示例应用程序的主布局文件,定义了 Emotion Rating View 库的视图。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.dm.emotionrating.library.GradientBackgroundView
android:id="@+id/gradientBackgroundView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.dm.emotionrating.library.EmotionView
android:id="@+id/emotionView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="48dp"
android:layout_marginLeft="48