2012-07-05 6 views
5

Androidのコードのどこかにループを作成して、2つの色の間の描画可能な矩形の色を一定の割合で連続的に変更したいと考えています。 2つのボタンを使って点滅を開始して停止したいと思います。私は多くの研究をしましたが、それをどうやって行うのか分かりません。私はアンドロイドには新しく、run()メソッドの経験はありません。しかし、私は色を変化させてアニメーション化するrun()メソッドを使って、ある種の矩形クラスを作らなければならないと思います。Androidの形状のアニメーション

答えて

1

私はまた、アンドロイドにはかなり新しいですが、私はそれを撃つでしょう。

あなたは点滅したいと言うので、単純な 'for'ループで青と赤の間で実際の画像を切り替えることができるはずです。ボタンを押すと、ブール値の状態をfalseからtrueに変更できます。次に、 'for'ステートメントがもう真でないときは、次のコードセットにジャンプし、コードセットを停止します。ここに私がすることがあります。

二つのボタンのためのあなたのXML:両方のレイアウトで同じ場所で、blue_rectanglered_rectangle

<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を持っているでしょう。ここに2つの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ループは、それが先頭に戻るときにブール値を再チェックすると思います。私はこれまでにこれまでに働いたことがない。それは私が見たウェブサイトから集めることができるものです。

まあ、それについてはそうだと思います。コードが何をしているのか理解できない、あるいはうまくいかない、あるいは間違った質問がある、あるいはまったく間違っている、あるいは何か、この答えにコメントするだけです。私はこの作品が欲しい!

Good Luck!

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

関連する問題