【问题标题】:Animate Shape Color in AndroidAndroid中的动画形状颜色
【发布时间】:2012-07-05 02:19:43
【问题描述】:

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

【问题讨论】:

    标签: android animation colors shape


    【解决方案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 个 ImageView: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 秒,使其不可见,使红色可见,等待 2 秒,然后重复。 start()stop() 方法将布尔值分别切换为 true 和 false。我认为这种类型的for 循环在返回顶部时会重新检查布尔值。我以前从未使用过它。这就是我可以从我查看的网站收集到的信息。

    嗯,我想差不多就行了。如果您不了解任何代码的作用,或者它不起作用,或者我的问题错了,或者我完全错了,或者任何事情,请评论这个答案。我希望这行得通!

    祝你好运!

    附:以下是我参考的网站

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

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

    哇...我刚刚意识到这个问题已经 2 岁了。不过,我希望这对您有所帮助!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 2015-04-30
    • 1970-01-01
    相关资源
    最近更新 更多