Tôi muốn tạo vòng lặp ở đâu đó trong mã Android của mình để thay đổi màu của hình chữ nhật có thể vẽ giữa hai màu liên tục ở một mức nào đó. Tôi muốn bắt đầu và ngừng nhấp nháy bằng hai nút. Tôi đã thực hiện rất nhiều nghiên cứu, nhưng dường như không thể tìm ra cách để làm điều đó. Tôi mới dùng Android và không có kinh nghiệm với các phương thức run(). Nhưng tôi đoán tôi phải thực hiện một số loại hình chữ nhật lớp với một phương pháp run() sẽ animate nó vào thay đổi màu sắc.Animate Shape Color trong Android
Trả lời
Tôi cũng khá mới đối với android, nhưng tôi sẽ cung cấp cho nó một shot.
Vì bạn nói bạn muốn nó nhấp nháy, bạn sẽ có thể chuyển đổi hình ảnh thực giữa, cho phép nói, màu xanh và đỏ, với vòng lặp 'for' đơn giản. Khi nhấn nút, bạn có thể thay đổi trạng thái của boolean từ false thành true. Sau đó, khi câu lệnh 'for' không còn đúng nữa, nó sẽ nhảy tới bộ mã tiếp theo, nó dừng nó lại. Đây là những gì tôi sẽ làm.
XML của bạn cho hai nút:
<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 -->
/>
Bây giờ, bạn sẽ có 2 ImageViews: blue_rectangle
và red_rectangle
, cả hai trong cùng một vị trí trong cách bố trí. Đây là XML cho hai ImageViews
<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" />
Phần tiếp theo này là phần khó khăn.
Đây là 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.
}
}
Đây là những gì mã cơ bản làm.
Nó có boolean được mặc định là false. Trong khi nó là sai, hình chữ nhật không nhấp nháy. Trong khi đó là sự thật, câu lệnh for
trong số blinkRectangle()
chạy. Đó là vòng lặp for
làm cho màu xanh hiển thị, chờ 2 giây, làm cho nó ẩn đi, làm cho màu đỏ hiển thị, chờ hai giây và lặp lại. Phương thức start()
và stop()
chuyển boolean thành true và false, tương ứng. Tôi nghĩ rằng loại vòng lặp for
kiểm tra lại boolean khi nó được trở lại đầu trang. Tôi chưa bao giờ làm việc với nó trước đây. Đó là những gì tôi có thể thu thập từ trang web mà tôi đã xem.
Vâng, tôi nghĩ về điều đó. Nếu bạn không hiểu bất kỳ mã nào, hoặc nó không hoạt động, hoặc tôi có câu hỏi sai, hoặc tôi hoàn toàn sai, hoặc bất cứ điều gì, chỉ cần bình luận về câu trả lời này. Tôi mong cái này sẽ thành công!
Chúc may mắn!
P.S. Dưới đây là những trang web tôi đã sử dụng để tham khảo
http://www.tutorialspoint.com/java/java_loop_control.htm
http://www.java-examples.com/java-boolean-example
Wow ... Tôi chỉ nhận ra câu hỏi này là 2 tuổi. Tuy nhiên, tôi hy vọng điều này sẽ giúp bạn!
- 1. Android ImageButton background color
- 2. Cách thêm đệm vào gradient <shape> trong Android?
- 3. Shape Kích drawable không làm việc
- 4. Animate Text Sử dụng TextSwitcher Trong Android
- 5. CSS "color" so với "font-color"
- 6. PHP GD - Crop Shape
- 7. Android: animate một hình chữ nhật
- 8. Màu Danh sách nhà nước không được công nhận trong Shape drawable
- 9. Sự khác biệt giữa "-webkit-text-fill-color" và "color"?
- 10. jQuery: màu chữ animate
- 11. Làm thế nào để nút animate trong android?
- 12. Animate chuyển selector/tiểu bang
- 13. UIProgressView unprogressed color
- 14. PreferenceFragment background color
- 15. jQuery: Color fade
- 16. UITextField clearButtonMode color
- 17. Color "trong suốt" không làm việc
- 18. Animate Expander trong WPF
- 19. Đệm không ảnh hưởng đến <shape> trong Bố cục XML
- 20. Core-Plot PieChart Slice color
- 21. PHPExcel: Cách lấy CELL COLOR
- 22. Xác định Kiểu cha mẹ XML của Gradient/Shape/Corners
- 23. Thay đổi tr background-color
- 24. Thay đổi Polyline Color Dynamically
- 25. Nút animate trên MouseOver và MouseDown
- 26. Moz-use-text-color là gì?
- 27. "body background-color: transparent" không ảnh hưởng đến chế độ xem web của android 4.0.3
- 28. NSIS visual studio color/font/intellisense
- 29. Đối tượng AndroidAnimator animate backgroundColor of Layout
- 30. jQuery animate scrollTop
Đừng lo lắng, bạn [nhận huy hiệu cho điều đó] (http://stackoverflow.com/questions/10874571/how-does-check-ajax-referer-really-work/18358174#comment27016156_18358174) ([Necromancer] (http://stackoverflow.com/help/badges/17/necromancer)) và upvotes sẽ chứng minh tính hữu ích của nó;) – brasofilo