我正在使用一个“彩色屏幕”应用程序。主要活动显示初始颜色选择,以供用户显示。
[选择颜色后,“主”(Main)活动切换到“灯”(Lamp)活动,该活动以全屏显示选定的颜色-“此零件为精细”。
当用户点击“灯泡活动”屏幕时,将调用“设置”活动。在这里,我正在研究应用程序的“序列”设置,通过该按钮,用户可以使用切换按钮在“序列”活动中以动画序列显示的颜色来选择颜色序列。
在主屏幕上,我还具有一个名为“彩虹”的按钮,该按钮可在预设颜色之间进行动画处理-可以正常工作,并且与我要执行的操作类似,但可以由用户编辑并可以将其保存为“预设” 。“
由于这似乎涉及许多程序,所以我对如何实现这一点感到迷惑-非常感谢您的帮助:
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=findViewById(R.id.button1);
//Same as for buttons 2 - 10
Button button11=findViewById(R.id.button11);
Button button12=findViewById(R.id.button12);
button1.setOnClickListener(this);
//Same as for buttons 2 - 10
button11.setOnClickListener(this);
button12.setOnClickListener(this);
//Rainbow ***Button*** Animation (from Drawable rainbowList.xml)
AnimationDrawable rainbowBtn = (AnimationDrawable)button12.getBackground();
rainbowBtn.setEnterFadeDuration(500);
rainbowBtn.setExitFadeDuration(500);
rainbowBtn.start();
//Sequence_Activity ***Button***
button11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
Toast.makeText(MainActivity.this,"Settings Clicked",Toast.LENGTH_SHORT).show();
startActivity(intent);
}
});
//Rainbow ***Button***
button12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), RainbowActivity.class);
Toast.makeText(MainActivity.this,"Rainbow Clicked",Toast.LENGTH_SHORT).show();
startActivity(intent);
}
});
}
@Override
protected void onResume() {
//Remove Notification and Title Bars:
View overlay = findViewById(R.id.activity_main);
overlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
//Max Brightness
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1;
getWindow().setAttributes(layout);
//Keep Screen On
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onResume();
}
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), LampActivity.class);
switch (v.getId()) {
case R.id.button1:
Toast.makeText(getApplicationContext(),"Yellow Clicked",Toast.LENGTH_SHORT).show();
intent.putExtra("bgColor", R.color.yellow);
startActivity(intent);
break;
// Same code for buttons 2 - 10
case R.id.button11:
Toast.makeText(MainActivity.this, "Sequence_Activity Clicked", Toast.LENGTH_SHORT).show();
intent.putExtra("bgColor", R.color.white);
startActivity(intent);
break;
case R.id.button12:
Toast.makeText(MainActivity.this, "Rainbow Clicked", Toast.LENGTH_SHORT).show();
intent.putExtra("bgColor", R.drawable.rainbow_list);
startActivity(intent);
break;
}
}
}
LampActivity
package com.example.lightbox;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
public class LampActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lamp);
ConstraintLayout lamp = findViewById(R.id.lampScreen);
Bundle bundle = getIntent().getExtras();
int bgColor = bundle.getInt("bgColor", -1);
lamp.setBackgroundColor(getResources().getColor(bgColor));
//Settings ***Button***
lamp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
Toast.makeText(LampActivity.this,"Settings Clicked",Toast.LENGTH_SHORT).show();
startActivity(intent);
}
});
}
@Override
protected void onResume() {
// Toast.makeText(LampActivity.this,"LampResume called",Toast.LENGTH_SHORT).show();
//Remove Notification and Title Bars:
View overlay = findViewById(R.id.lampScreen);
overlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
//Max Brightness
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1;
getWindow().setAttributes(layout);
//Keep Screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
super.onResume();
}
}
彩虹活动
public class RainbowActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rainbow);
ConstraintLayout constraintLayout = findViewById(R.id.activity_rainbow);
AnimationDrawable rainbowAnim = (AnimationDrawable)constraintLayout.getBackground();
rainbowAnim.setEnterFadeDuration(1000);
rainbowAnim.setExitFadeDuration(1000);
rainbowAnim.start();
constraintLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
Toast.makeText(RainbowActivity.this,"Settings Clicked",Toast.LENGTH_SHORT).show();
startActivity(intent);
}
});
}
@Override
protected void onResume() {
//Remove Notification and Title Bars:
View overlay = findViewById(R.id.activity_rainbow);
overlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
//Max Brightness
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1;
getWindow().setAttributes(layout);
//Keep Screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
super.onResume();
}
}
SequenceActivity
public class Sequence_Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sequence);
ConstraintLayout constraintLayout = findViewById(R.id.activity_sequence);
constraintLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
Toast.makeText(Sequence_Activity.this,"Settings Clicked",Toast.LENGTH_SHORT).show();
startActivity(intent);
}
});
}
}
SettingsActivity
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Button sequence_btn = findViewById(R.id.sequence_btn);
//Fade In / Out Transition
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
//Rainbow ***Button*** Animation (from Drawable rainbowList.xml)
AnimationDrawable rainbowBtn = (AnimationDrawable) sequence_btn.getBackground();
rainbowBtn.setEnterFadeDuration(500);
rainbowBtn.setExitFadeDuration(500);
rainbowBtn.start();
sequence_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Sequence_Activity.class);
intent.putExtra("bgColor", R.color.aqua);
Toast.makeText(SettingsActivity.this, "To Sequence_Activity", Toast.LENGTH_SHORT).show();
startActivity(intent);
}
});
}
@Override
protected void onResume() {
// Toast.makeText(SettingsActivity.this,"SettingsonResume called",Toast.LENGTH_SHORT).show();
//Remove Notification and Title Bars:
View overlay = findViewById(R.id.activity_settings);
overlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
//Max Brightness
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1;
getWindow().setAttributes(layout);
//Keep Screen On
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onResume();
}
}
activity_main.xml
<?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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:layout_margin="0dp"
android:background="#F2000000"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="32dp"
android:background="@color/yellow"
android:text="@string/yellow"
android:textSize="12sp"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@color/orange"
android:text="@string/orange"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/button1"
app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button1"
app:layout_constraintTop_toTopOf="@+id/button1" />
<Button
android:id="@+id/button3"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@color/red"
android:text="@string/red"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/button2"
app:layout_constraintEnd_toStartOf="@+id/button4"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toTopOf="@+id/button2" />
<Button
android:id="@+id/button4"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@color/pink"
android:text="@string/pink"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button3"
app:layout_constraintTop_toTopOf="@+id/button3" />
<Button
android:id="@+id/button5"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="32dp"
android:background="@color/purple"
android:text="@string/purple"
android:textSize="12sp"
app:layout_constraintEnd_toStartOf="@+id/button6"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button1" />
<Button
android:id="@+id/button6"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@color/blue"
android:text="@string/blue"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/button5"
app:layout_constraintEnd_toStartOf="@+id/button7"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button5"
app:layout_constraintTop_toTopOf="@+id/button5" />
<Button
android:id="@+id/button7"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@color/aqua"
android:text="@string/aqua"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/button6"
app:layout_constraintEnd_toStartOf="@+id/button8"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button6"
app:layout_constraintTop_toTopOf="@+id/button6" />
<Button
android:id="@+id/button8"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@color/lime"
android:text="@string/lime"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/button7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button7"
app:layout_constraintTop_toTopOf="@+id/button7" />
<Button
android:id="@+id/button9"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="32dp"
android:background="@color/green"
android:text="@string/green"
android:textSize="12sp"
app:layout_constraintEnd_toStartOf="@+id/button10"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button5" />
<Button
android:id="@+id/button10"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@color/white"
android:text="@string/white"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/button9"
app:layout_constraintEnd_toStartOf="@+id/button11"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button9"
app:layout_constraintTop_toTopOf="@+id/button9" />
<Button
android:id="@+id/button11"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@color/white"
android:textSize="12sp"
android:text="@string/sequence"
app:layout_constraintBottom_toBottomOf="@+id/button10"
app:layout_constraintEnd_toStartOf="@+id/button12"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button10"
app:layout_constraintTop_toTopOf="@+id/button10" />
<Button
android:id="@+id/button12"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/rainbow_list_btn"
android:contentDescription="@string/rainbow_button"
android:textSize="12sp"
android:text="@string/rainbow"
app:layout_constraintBottom_toBottomOf="@+id/button11"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button11"
app:layout_constraintTop_toTopOf="@+id/button11" />
<TextView
android:id="@+id/blurb"
android:layout_width="match_parent"
android:layout_height="30dp"
android:width="40dp"
android:height="40dp"
android:gravity="center"
android:text="@string/blurb"
android:textAlignment="center"
android:textColor="#222222"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_lamp.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme"
android:clickable="true"
android:focusable="true"
android:padding="0dp"
android:background="@color/black"
tools:context=".LampActivity"
android:id="@+id/lampScreen">
</androidx.constraintlayout.widget.ConstraintLayout>
activity_rainbow.xml
<?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/activity_rainbow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rainbow_list"
android:clickable="true"
android:focusable="true"
tools:context=".RainbowActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
activity_sequence.xml
<?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/activity_sequence"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/lime"
android:clickable="true"
android:focusable="true"
tools:context=".Sequence_Activity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sequence"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_settings
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey85"
android:orientation="vertical"
android:padding="0dp"
android:layout_margin="0dp"
tools:context=".SettingsActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_marginBottom="5dp"
android:background="@color/black"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="@string/settings"
android:textColor="@color/white"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@color/grey90"
android:gravity="center" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="0"
android:layout_marginStart="15dp"
android:layout_marginBottom="10dp"
android:gravity="center_horizontal"
android:text="@string/strobe"
android:textAlignment="center"
android:textColor="@color/white"
android:textStyle="bold" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:text="@string/strobe_frequency"
android:textColor="@color/white"
android:textStyle="bold" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="1"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:layout_marginEnd="65dp"/>
</GridLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"
android:background="@color/black"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="@string/color_sequence"
android:textColor="@color/white"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:text="@string/result"
android:textAlignment="center"
android:textColor="@color/white" />
<Button
android:id="@+id/sequence_btn"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginStart="15dp"
android:layout_marginTop="5dp"
android:background="@drawable/rainbow_list_btn"
android:text="@string/click_to_go" />
</LinearLayout>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ToggleButton
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_row="0"
android:layout_column="0"
android:layout_margin="5dp"
android:background="@color/white"
android:textOff=""
android:textStyle="bold"
android:textOn="@string/checked"/>
//Same code for all other toggle buttons
</GridLayout>
</LinearLayout>
</LinearLayout>
不确定我是否完全理解您的问题,但是也许您应该将所有有关颜色序列的详细信息保存为Class对象?
我的意思是,创建一个与此类似的类:
class ColorSequence{
private startColor startColor;
private endColor endColor;
public createSequence(startColor startColor, endColor endColor){
this.startColor = startColor;
this.endColor = endColor;
}
// add more properties and functions as you need...
这里,您可以保存所有需要的数据,并在创建新的活动实例时在布局/活动之间传递此ColorSequcene对象。