Hello friends
actvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.samset.user.dynamicallychangegradiant.MainActivity">
<LinearLayout
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="200dp">
<TextView
android:layout_gravity="center"
android:gravity="center"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="samset.blogspot.com!" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.samset.user.dynamicallychangegradiant;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
private int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout= (LinearLayout) findViewById(R.id.mainlayout);
final GradientDrawable[] colors = new GradientDrawable[]{getgradiantInit(), getgradiantFirst(), getgradiantSecond(), getgradiantThird()};
new CountDownTimer(2000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
linearLayout.setBackground((colors[i]));
i++;
if (i == colors.length - 1) i = 0;
start();
}
}.start();
}
public GradientDrawable getgradiantInit() {
int[] colors = {Color.parseColor("#008000"), Color.parseColor("#ADFF2F")};
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
gd.setCornerRadius(20f);
return gd;
}
private GradientDrawable getgradiantFirst() {
int[] colors1 = {Color.parseColor("#78CC2E"), Color.parseColor("#901B8E")};
GradientDrawable gd1 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors1);
gd1.setCornerRadius(20f);
return gd1;
}
private GradientDrawable getgradiantSecond() {
int[] colors1 = {Color.parseColor("#e50000"), Color.parseColor("#FFC807")};
GradientDrawable gd1 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors1);
gd1.setCornerRadius(20f);
return gd1;
}
private GradientDrawable getgradiantThird() {
int[] colors1 = {Color.parseColor("#3eb0f7"), Color.parseColor("#FFFFFF")};
GradientDrawable gd1 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors1);
gd1.setCornerRadius(10f);
return gd1;
}
}
actvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.samset.user.dynamicallychangegradiant.MainActivity">
<LinearLayout
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="200dp">
<TextView
android:layout_gravity="center"
android:gravity="center"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="samset.blogspot.com!" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.samset.user.dynamicallychangegradiant;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout;
private int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout= (LinearLayout) findViewById(R.id.mainlayout);
final GradientDrawable[] colors = new GradientDrawable[]{getgradiantInit(), getgradiantFirst(), getgradiantSecond(), getgradiantThird()};
new CountDownTimer(2000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
linearLayout.setBackground((colors[i]));
i++;
if (i == colors.length - 1) i = 0;
start();
}
}.start();
}
public GradientDrawable getgradiantInit() {
int[] colors = {Color.parseColor("#008000"), Color.parseColor("#ADFF2F")};
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
gd.setCornerRadius(20f);
return gd;
}
private GradientDrawable getgradiantFirst() {
int[] colors1 = {Color.parseColor("#78CC2E"), Color.parseColor("#901B8E")};
GradientDrawable gd1 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors1);
gd1.setCornerRadius(20f);
return gd1;
}
private GradientDrawable getgradiantSecond() {
int[] colors1 = {Color.parseColor("#e50000"), Color.parseColor("#FFC807")};
GradientDrawable gd1 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors1);
gd1.setCornerRadius(20f);
return gd1;
}
private GradientDrawable getgradiantThird() {
int[] colors1 = {Color.parseColor("#3eb0f7"), Color.parseColor("#FFFFFF")};
GradientDrawable gd1 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors1);
gd1.setCornerRadius(10f);
return gd1;
}
}
Thanks
Live Sample
No comments:
Post a Comment