У меня есть приложение "цветной экран", над которым я работаю. Основное действие отображает исходные варианты выбора цвета для отображения пользователем.
При выборе цвета действие «Основное» переключается на действие «Лампа», которое отображает выбранный цвет в полноэкранном режиме - ЭТА ЧАСТЬ РАБОТАЕТ ИЗБРАННАЯ.
Когда пользователь касается экрана активности лампы, вызывается операция «Настройки». Здесь я работаю над настройками приложения «Последовательность», в соответствии с которыми пользователь может выбирать последовательность цветов, используя переключатели, которые отображаются в виде анимационной последовательности в действии «Последовательность».
На главной На экране также есть кнопка «Радуга», которая анимирует между предустановленными цветами - это работает и похоже на то, что я хочу сделать, но делает его редактируемым пользователем и может быть сохранено как «предустановка».
Поскольку для этого требуется ряд процедур, я не знаю, как это реализовать - любая помощь очень ценится:
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();
}
}
RainbowActivity
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);
}
});
}
}
НастройкиActivity
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>
action_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>