如何以编程方式更改 android 动画的颜色?
How to change the color of animated in android programmiticaly?
我的形状在 res/drawable/back.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="85dp"
android:shape="ring"
android:thickness="5dp"
android:useLevel="false">
<solid
android:color="#FF0000">
</solid>
<size
android:height="200dp"
android:width="200dp">
</size>
</shape>
此形状设置在 TextView 的背景中:
<TextView
android:id="@+id/fullscreen_content"
android:layout_width="200dp"
android:layout_height="200dp"
android:keepScreenOn="true"
android:textColor="#33b5e5"
android:textStyle="bold"
android:textSize="50sp"
android:gravity="center"
android:text="@string/dummy_content"
android:layout_gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="105dp"
android:background="@drawable/back" />
我需要无限改变形状动画的颜色(随机颜色 - 已完成)。
最后,我需要环绕文字,它会不断地动画变色。
P.S。对不起我的英语不好。我部分使用翻译器。 谢谢!
Java代码:
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.content.Intent;
import android.view.Display;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.Random;
public class FullscreenActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
findViewById(R.id.button3).setOnClickListener(btn3Click);
}
public View.OnClickListener btn3Click = new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView = (TextView) findViewById(R.id.fullscreen_content);
GradientDrawable drawable = (GradientDrawable) textView.getBackground();
drawable.setColor(randColor());
}
};
public int randColor(){
Random rnd = new Random();
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}
}
这个效果:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
var colours=['#ff3333','#33cc66','#3399cc']; // List of colors
var tempID=0;
var changeInterval=2000; // Change interval in miliseconds
var objectID='#bgDiv'; // Object to change colours.
$(document).ready(function(){
setInterval(function(){
$(objectID).animate({backgroundColor: colours[tempID]},500);
tempID=tempID+1;
if (tempID>colours.length-1) tempID=0;
},changeInterval);
});
</script>
<style>
#bgDiv {
width: 100px;
height: 100px;
background: #3399cc;
border-radius:100px;
}
</style>
</head>
<body>
<div id="bgDiv"></div>
</body>
</html>
您可以使用:
root.setBackgroundColor(Color.parseColor("#rrggbb"));
或
root.setBackgroundColor(0xrrggbb);
选择一个随机数的十六进制6位数字,并将其作为上述任一函数的参数传递。它应该可以工作。
我会按如下方式实现:
public class FullscreenActivity extends Activity {
int i = 0;
ImageView frameanimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameanimation = (ImageView) findViewById(R.id.fullscreen_content);
AnimationDrawable frame_animation = (AnimationDrawable) frameanimation
.getBackground();
frame_animation.setVisible(true, true);
frame_animation.start();
Timer timer = new Timer();
timer.schedule(new RunTimer(), 0, 1500);
}
class RunTimer extends TimerTask {
public void run() {
runOnUiThread(runnn);
}
Runnable runnn = new Runnable() {
@Override
public void run() {
AlphaAnimation alpha = new AlphaAnimation(1, 0.7F);
alpha.setDuration(1000);
frameanimation.startAnimation(alpha);
}
};
}
}
添加了完整的源代码here
输出:
我的形状在 res/drawable/back.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="85dp"
android:shape="ring"
android:thickness="5dp"
android:useLevel="false">
<solid
android:color="#FF0000">
</solid>
<size
android:height="200dp"
android:width="200dp">
</size>
</shape>
此形状设置在 TextView 的背景中:
<TextView
android:id="@+id/fullscreen_content"
android:layout_width="200dp"
android:layout_height="200dp"
android:keepScreenOn="true"
android:textColor="#33b5e5"
android:textStyle="bold"
android:textSize="50sp"
android:gravity="center"
android:text="@string/dummy_content"
android:layout_gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="105dp"
android:background="@drawable/back" />
我需要无限改变形状动画的颜色(随机颜色 - 已完成)。 最后,我需要环绕文字,它会不断地动画变色。
P.S。对不起我的英语不好。我部分使用翻译器。 谢谢!
Java代码:
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.content.Intent;
import android.view.Display;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.Random;
public class FullscreenActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
findViewById(R.id.button3).setOnClickListener(btn3Click);
}
public View.OnClickListener btn3Click = new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView = (TextView) findViewById(R.id.fullscreen_content);
GradientDrawable drawable = (GradientDrawable) textView.getBackground();
drawable.setColor(randColor());
}
};
public int randColor(){
Random rnd = new Random();
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}
}
这个效果:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
var colours=['#ff3333','#33cc66','#3399cc']; // List of colors
var tempID=0;
var changeInterval=2000; // Change interval in miliseconds
var objectID='#bgDiv'; // Object to change colours.
$(document).ready(function(){
setInterval(function(){
$(objectID).animate({backgroundColor: colours[tempID]},500);
tempID=tempID+1;
if (tempID>colours.length-1) tempID=0;
},changeInterval);
});
</script>
<style>
#bgDiv {
width: 100px;
height: 100px;
background: #3399cc;
border-radius:100px;
}
</style>
</head>
<body>
<div id="bgDiv"></div>
</body>
</html>
您可以使用:
root.setBackgroundColor(Color.parseColor("#rrggbb"));
或
root.setBackgroundColor(0xrrggbb);
选择一个随机数的十六进制6位数字,并将其作为上述任一函数的参数传递。它应该可以工作。
我会按如下方式实现:
public class FullscreenActivity extends Activity {
int i = 0;
ImageView frameanimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameanimation = (ImageView) findViewById(R.id.fullscreen_content);
AnimationDrawable frame_animation = (AnimationDrawable) frameanimation
.getBackground();
frame_animation.setVisible(true, true);
frame_animation.start();
Timer timer = new Timer();
timer.schedule(new RunTimer(), 0, 1500);
}
class RunTimer extends TimerTask {
public void run() {
runOnUiThread(runnn);
}
Runnable runnn = new Runnable() {
@Override
public void run() {
AlphaAnimation alpha = new AlphaAnimation(1, 0.7F);
alpha.setDuration(1000);
frameanimation.startAnimation(alpha);
}
};
}
}
添加了完整的源代码here
输出: