问题描述
我创建一个由帆布画界和画布颜色会每5 seconds.Here年代code我做了什么改变一个Android应用程序,但它并没有改变它的颜色,请告诉我,我做了错误
包com.developer.milandemoapp;进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.widget.RadioButton;
进口android.widget.RadioGroup;公共类Automode_Activity延伸活动{
螺纹定时器;
布尔数= TRUE;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(新MyView的(本));
}
公共类MyView的扩展视图{ 公共MyView的(上下文的背景下){
超级(上下文); }
@覆盖
保护无效的onDraw(最终帆布油画){
super.onDraw(画布);
的getWidth();
的getHeight();
最终诠释半径= 25;
System.currentTimeMillis的(); 最终涂料粉刷=新的油漆();
paint.setStyle(Paint.Style.FILL); canvas.drawPaint(油漆);
paint.setColor(Color.parseColor(#D50000)); 最终油漆paint1 =新的油漆();
paint1.setStyle(Paint.Style.FILL);
canvas.drawPaint(paint1);
paint1.setColor(Color.parseColor(#33691E)); //计数= 0;
最终螺纹delay_thread =新主题(新的Runnable(){ @覆盖
公共无效的run(){
而(真){
尝试{
视频下载(5000);
change_colors(); }
赶上(例外五)
{
Log.e(错误,e.getMessage());
}
}} 私人无效change_colors(){
runOnUiThread(新的Runnable(){ @覆盖
公共无效的run(){ 如果(计数)
{
paint1.setColor(Color.parseColor(#D50000));
}
//paint1.setColor(Color.parseColor(\"#33691E));
其他
{
paint1.setColor(Color.parseColor(#33691E));
}
数=计!;
canvas.drawCircle(300,300,半径,paint1);
canvas.drawCircle(400,300,半径,paint1);
canvas.drawCircle(500,300,半径,paint1);
canvas.drawCircle(600,300,半径,paint1);
canvas.drawCircle(700,300,半径,paint1);
canvas.drawCircle(800,300,半径paint1);
canvas.drawCircle(900,300,半径,paint1); }
});
}
}); canvas.drawCircle(300,300,半径,油漆);
canvas.drawCircle(400,300,半径,油漆);
canvas.drawCircle(500,300,半径,油漆);
canvas.drawCircle(600,300,半径,油漆);
canvas.drawCircle(700,300,半径,油漆);
canvas.drawCircle(800,300,半径,油漆);
canvas.drawCircle(900,300,半径,油漆);
delay_thread.start();
}
}
}
嗯,你们有混乱的:
-
尽量不要初始化
的onDraw任何东西()
- 这种方法是调用每次重绘视图时间 -
您已经乱七八糟的颜色在这里 - 你画圆用
油漆
,但你改变paint1
在你的线程。 -
我不明白你为什么叫这一切
的getWidth()
的getHeight()
和System.currentTimeMillis的()
所以,我已经清理一点点所有code的优化它。这就是它现在应该工作:
公共类MainActivity延伸活动{ 私人MyView的观点; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
鉴于=新MyView的(本);
的setContentView(视图);
} @覆盖
保护无效调用onStart(){
//重新启动线程的观点
view.startChangingColors(5000);
super.onStart();
} @覆盖
保护无效的onStop(){
//停止线程
view.stopRunnning();
super.onStop();
} 公共类MyView的扩展视图{ 私人螺纹colorThread; 私人最终诠释COLOR1 = Color.parseColor(#D50000);
私人最终诠释COLOR2 = Color.parseColor(#33691E); 私人涂料粉刷;
私人涂料clearingPaint; 私人最终诠释半径= 25; 私人布尔切换=真; 公共MyView的(上下文的背景下){
超级(上下文); //初始化到这里
油漆=新的油漆();
paint.setStyle(Paint.Style.FILL);
paint.setColor(颜色1); clearingPaint =新的油漆();
clearingPaint.setStyle(Paint.Style.FILL);
clearingPaint.setColor(Color.WHITE); //线程运行
startChangingColors(5000);
} 公共无效stopRunnning(){
//停止previous线程
如果(colorThread!= NULL){
colorThread.interrupt();
}
} / **
*开始改变每n毫秒颜色的线
*
* @参数延迟
* /
公共无效startChangingColors(最终诠释delayMilis){
//华中科技大学安全检查
stopRunnning(); colorThread =新主题(新的Runnable(){ @覆盖
公共无效的run(){
布尔中断=假;
而(!中断){
尝试{
runOnUiThread(新的Runnable(){ @覆盖
公共无效的run(){
changeColors();
}
}); 视频下载(delayMilis); }赶上(例外五){
如果(E的instanceof InterruptedException的){
//我们只是woked向上关闭这一切
中断=真实的;
}其他{
Log.e(错误,e.getMessage());
}
}
} }
});
colorThread.start();
} 公共无效changeColors(){
如果(切换){
paint.setColor(颜色1);
}其他{
paint.setColor(颜色2);
}
!=切换开关;
//重绘视图
无效();
} @覆盖
保护无效的onDraw(最终帆布油画){
//清除画布
canvas.drawPaint(clearingPaint);
//画圈圈
canvas.drawCircle(300,300,半径,油漆);
canvas.drawCircle(400,300,半径,油漆);
canvas.drawCircle(500,300,半径,油漆);
canvas.drawCircle(600,300,半径,油漆);
canvas.drawCircle(700,300,半径,油漆);
canvas.drawCircle(800,300,半径,油漆);
canvas.drawCircle(900,300,半径,油漆);
}
}
}
I am creating an android application that consists of canvas to draw circles and canvas color will be changed for every 5 seconds.Here's the code what i did but it does not changed its color please tell me where i did the mistake
package com.developer.milandemoapp;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class Automode_Activity extends Activity {
Thread timer;
boolean Count=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View{
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
getWidth();
getHeight();
final int radius = 25;
System.currentTimeMillis();
final Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#D50000"));
final Paint paint1 = new Paint();
paint1.setStyle(Paint.Style.FILL);
canvas.drawPaint(paint1);
paint1.setColor(Color.parseColor("#33691E"));
//Count=0;
final Thread delay_thread= new Thread(new Runnable() {
@Override
public void run() {
while(true){
try{
Thread.sleep(5000);
change_colors();
}
catch(Exception e)
{
Log.e("Error:",e.getMessage());
}
}}
private void change_colors() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(Count)
{
paint1.setColor(Color.parseColor("#D50000"));
}
//paint1.setColor(Color.parseColor("#33691E"));
else
{
paint1.setColor(Color.parseColor("#33691E"));
}
Count=!Count;
canvas.drawCircle(300, 300, radius, paint1);
canvas.drawCircle(400, 300, radius, paint1);
canvas.drawCircle(500, 300, radius, paint1);
canvas.drawCircle(600, 300, radius, paint1);
canvas.drawCircle(700, 300, radius, paint1);
canvas.drawCircle(800, 300, radius, paint1);
canvas.drawCircle(900, 300, radius, paint1);
}
});
}
});
canvas.drawCircle(300, 300, radius, paint);
canvas.drawCircle(400, 300, radius, paint);
canvas.drawCircle(500, 300, radius, paint);
canvas.drawCircle(600, 300, radius, paint);
canvas.drawCircle(700, 300, radius, paint);
canvas.drawCircle(800, 300, radius, paint);
canvas.drawCircle(900, 300, radius, paint);
delay_thread.start();
}
}
}
Well, all you got just messed-up:
Try to not initialize anything in
onDraw()
- this method is calls every time you redraw your view.You've messed-up with colors here - you're drawing circles with
paint
but you're changingpaint1
in your thread.I don't understand why do you call all this
getWidth()
getHeight()
andSystem.currentTimeMillis()
So, I've cleaned a little bit all of your code and optimized it. That's how it should work now:
public class MainActivity extends Activity {
private MyView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new MyView(this);
setContentView(view);
}
@Override
protected void onStart() {
// Restart view thread
view.startChangingColors(5000);
super.onStart();
}
@Override
protected void onStop() {
// Stopping thread
view.stopRunnning();
super.onStop();
}
public class MyView extends View {
private Thread colorThread;
private final int color1 = Color.parseColor("#D50000");
private final int color2 = Color.parseColor("#33691E");
private Paint paint;
private Paint clearingPaint;
private final int radius = 25;
private boolean switcher = true;
public MyView(Context context) {
super(context);
// Init goes here
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(color1);
clearingPaint = new Paint();
clearingPaint.setStyle(Paint.Style.FILL);
clearingPaint.setColor(Color.WHITE);
// Run thread
startChangingColors(5000);
}
public void stopRunnning() {
// Stopping previous thread
if (colorThread != null) {
colorThread.interrupt();
}
}
/**
* Starting a thread that changes colors every n miliseconds
*
* @param delay
*/
public void startChangingColors(final int delayMilis) {
// Hust a safety check
stopRunnning();
colorThread = new Thread(new Runnable() {
@Override
public void run() {
boolean interrupted = false;
while (!interrupted) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
changeColors();
}
});
Thread.sleep(delayMilis);
} catch (Exception e) {
if (e instanceof InterruptedException) {
// We just woked up to close all this
interrupted = true;
} else {
Log.e("Error:", e.getMessage());
}
}
}
}
});
colorThread.start();
}
public void changeColors() {
if (switcher) {
paint.setColor(color1);
} else {
paint.setColor(color2);
}
switcher = !switcher;
// Redraw view
invalidate();
}
@Override
protected void onDraw(final Canvas canvas) {
// Clear the canvas
canvas.drawPaint(clearingPaint);
// Draw circles
canvas.drawCircle(300, 300, radius, paint);
canvas.drawCircle(400, 300, radius, paint);
canvas.drawCircle(500, 300, radius, paint);
canvas.drawCircle(600, 300, radius, paint);
canvas.drawCircle(700, 300, radius, paint);
canvas.drawCircle(800, 300, radius, paint);
canvas.drawCircle(900, 300, radius, paint);
}
}
}
这篇关于如何改变画布颜色android的动态,每5秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!