我想在我的Android代码的某处创建一个循环,以某种速度连续更改两种颜色之间的可绘制矩形的颜色。我想用两个按钮启动并停止闪烁。我做了很多研究,但似乎无法弄清楚如何去做。我是android新手,并没有run()方法的经验。但我猜我必须用run()方法创建某种类型的矩形类,并将其变为动态变化的颜色。Android中的动画形状颜色
5
A
回答
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_rectangle
和red_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岁。不过,我希望这可以帮助你!
相关问题
- 1. Android形状动画
- 2. android - 动画形状
- 3. Android动画副作用:按钮在动画颜色时更改其形状
- 4. android - 引用形状内的颜色
- 5. Android Opengl 2形状不同的颜色
- 6. Android:动态更改XML中的形状颜色
- 7. Android中的动画颜色过滤器
- 8. WPF - 多边形上的颜色动画
- 9. Android更改形状可绘制颜色
- 10. 改变形状颜色在Android XML
- 11. Powerpoint更改形状轮廓/使用动画填充颜色
- 12. 如何将颜色更改动画赋予自定义形状?
- 13. 谁可以使用JQuery动画以颜色填充此形状?
- 14. 如何动态改变形状颜色?
- 15. 如何动态更改形状颜色?
- 16. 更改HTML5画布中形状的颜色
- 17. 动画矩形颜色变化
- 18. Android的图标动画改变颜色
- 19. 颜色区域形状PHP
- 20. 更改形状颜色
- 21. java图形 - 两种颜色的形状
- 22. HTML5画布颜色应用到图像,其中形状叠加
- 23. 将新笔画颜色注入到一个形状中
- 24. 的Android fingerpaint画布颜色
- 25. Javascript颜色动画
- 26. 动画div颜色
- 27. 绘制2个形状不同的颜色在画布上
- 28. Android按钮动画(颜色变化)
- 29. android将颜色应用于alpha动画
- 30. 动画“fillLinearGradientColorStops”形状
别担心,你[获得徽章](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