2012-07-05 75 views
5

我想在我的Android代码的某处创建一个循环,以某种速度连续更改两种颜色之间的可绘制矩形的颜色。我想用两个按钮启动并停止闪烁。我做了很多研究,但似乎无法弄清楚如何去做。我是android新手,并没有run()方法的经验。但我猜我必须用run()方法创建某种类型的矩形类,并将其变为动态变化的颜色。Android中的动画形状颜色

回答

1

我也是相当新的android,但我会给它一个镜头。

既然你说你想让它闪烁,你应该能够通过简单的'for'循环来切换实际图像,比如说,蓝色和红色。当按下按钮时,您可以将布尔值的状态从false更改为true。然后,当'for'语句不再成立时,它跳转到下一组代码,这会停止它。这是我会做的。

你的这两个按钮的XML:

<Button 
    android:id="@+id/start" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Start" 
    android:Clickable="true" 
    android:onClick="start" 
    /> 

<Button 
    android:id="@+id/stop" <!-- Gives the button an ID to use in java code --> 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Stop" <!-- sets the text on the button --> 
    android:Clickable="true" <!-- makes the button clickable --> 
    android:onClick="stop" <!-- The method it calls when it is clicked, removes the necessity of an OnClickListener --> 
    /> 

现在,你将有2个ImageViews:blue_rectanglered_rectangle,无论在布局相同的地方。这里是两个ImageView的XML

<ImageView 
android:id="@+id/blue_rectangle" 
android:layout_width="100dp" 
android:layout_height="75dp" 
android:layout_marginLeft="50dp" 
android:layout_marginTop="5dp" 
android:src="@drawable/blue_rectangle" /> 

<ImageView 
android:id="@+id/red_rectangle" 
android:layout_width="100dp" 
android:layout_height="75dp" 
android:layout_marginLeft="50dp" 
android:layout_marginTop="5dp" 
android:src="@drawable/red_rectangle" /> 

下一部分是棘手的部分。

这是Java。

package your.package.name.thingy.here; 

//everything it tells you to import goes here 

public class BlinkingActivity extends Activity{ 

ImageView blueRectangle; 
ImageView redRectangle; 
Button start; 
Button stop; 

    @Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.yourLayout); 

    blueRectangle = (ImageView) findViewById(R.id.blue_rectangle); 
    redRectangle = (ImageView) findViewById(R.id.red_rectangle); 
    start = (Button) findViewById(R.id.start); 
    stop = (Button) findViewById(R.id.stop); 
    Boolean isBlinking = new Boolean(false); 

    blinkRectangle(whateverVariableThisNeeds); 

} 

public static void blinkRectangle(View view){ 

blueRectangle.setVisibility(View.INVISIBLE); 
redRectangle.setVisibility(View.INVISIBLE); 

for(initialization; isBlinking; update){ 

    blueRectangle.setVisibility(View.VISIBLE); 

    blueRectangle.postDelayed(new Runnable() { 
    @Override 
    public void run() { 
    blueRectangle.setVisibility(View.INVISIBLE); 
    } 
    }, 2000); //the 2000 is the number of milliseconds for how long blue is visible 
    redRectangle.setVisibility(View.VISIBLE); 

    redRectangle.postDelayed(new Runnable() { 
    @Override 
    public void run(){ 
    redRectangle.setVisibility(View.INVISIBLE); 
    } 
    }, 2000); 

    blueRectangle.setVisibility(View.VISIBLE); //This prevents a bug where if the user hits the stop button at just the right time, both rectangles will be invisible. 
} 


public static void start(View view){ //no need to call this anywhere, the XML onClick does it for you 

isBlinking = true; //I think this is how you set a boolean, if not, correct me. 

} 

public static void stop(View view){//same here 

isBlinking = false; //again, correct me if I'm wrong. 

} 

} 

以下是代码的基本功能。

它有一个布尔值,默认为false。虽然它是错误的,矩形不闪烁。尽管确实如此,blinkRectangle()中的for语句仍在运行。该for循环使蓝色可见,等待2秒,使其不可见,使红色可见,等待两秒钟,并重复。 start()stop()方法分别将布尔值切换为true和false。我认为这种类型的for循环在回到顶端时重新检查布尔值。我从来没有用过它。这就是我可以从我看到的网站收集的信息。

嗯,我认为这样做。如果你不明白任何代码的作用,或者它不起作用,或者我有问题,或者我只是彻头彻尾的错误,或者任何事情,只是评论这个答案。我希望这可以工作!

祝你好运!

P.S.下面是我用来参考

http://www.tutorialspoint.com/java/java_loop_control.htm

http://www.java-examples.com/java-boolean-example

哇网站......我才意识到这个问题是2岁。不过,我希望这可以帮助你!

+0

别担心,你[获得徽章](http://stackoverflow.com/questions/10874571/how-does-check-ajax-referer-really-work/18358174#comment27016156_18358174)([Necromancer] (http://stackoverflow.com/help/badges/17/necromancer))和upvotes将证明其有用性;) – brasofilo