Android:从2个池中随机选取图像
我有一个应用程序,我计划测量屏幕上显示的一些设计。程序如下:Android:从2个池中随机选取图像,android,image,performance,android-animation,Android,Image,Performance,Android Animation,我有一个应用程序,我计划测量屏幕上显示的一些设计。程序如下: 显示从池1随机拾取的图像,持续1秒 显示黑屏(1秒) 显示从池2中随机拾取的图像,持续1秒 显示红色屏幕(等待按钮按下 重复1-4多次。 问题1:实现这种行为的最佳实践是什么?我尝试使用“动画可绘制”,但根据我的经验,您不能从xml中随机选取图像 问题2:效率如何?如问题1所述尝试后,它显示:“I/Choreographer:跳过59帧!应用程序可能在其主线程上做了太多工作。”->这里需要多线程吗 最好的, 虎码有趣的是: 我的课程(
public class GameActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
/* ImageView */
ImageView myAnimation = (ImageView) findViewById(R.id.spin_animation_left);
final AnimationDrawable myAnimationDrawable
= (AnimationDrawable) myAnimation.getDrawable();
myAnimation.post(
new Runnable() {
@Override
public void run() {
myAnimationDrawable.start();
}
});
}
/*Show Gameactivity always on fullscreen */
@Override
public void onWindowFocusChanged(boolean hasFocas) {
super.onWindowFocusChanged(hasFocas);
View decorView = getWindow().getDecorView();
if (hasFocas) {
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
}
@Override
public boolean dispatchKeyEvent(KeyEvent b) {
if (b.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
startActivity(new Intent(this,StartActivity.class));
}
return super.dispatchKeyEvent(b);
};
}
这里是我的XML(其他持续时间,仅显示:“图像池1”-“图像池2”,等等):
对于任何感兴趣的人,这里是我的解决方案:-)
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
>
<item
android:drawable="@drawable/p1"
android:duration="150"/>
<item
android:drawable="@drawable/t1"
android:duration="200"/>
<item
android:drawable="@drawable/p36"
android:duration="150"/>
<item
android:drawable="@drawable/t2"
android:duration="200"/>
<item
android:drawable="@drawable/p28"
android:duration="150"/>
<item
android:drawable="@drawable/t3"
android:duration="200"/>
<item
android:drawable="@drawable/p52"
android:duration="150"/>
<item
android:drawable="@drawable/t4"
android:duration="200"/>
<item
android:drawable="@drawable/p49"
android:duration="150"/>
<item
android:drawable="@drawable/t5"
android:duration="200"/>
<item
android:drawable="@drawable/p30"
android:duration="150"/>
<item
android:drawable="@drawable/t6"
android:duration="200"/>
<item
android:drawable="@drawable/p5"
android:duration="150"/>
<item
android:drawable="@drawable/t1"
android:duration="200"/>
<item
android:drawable="@drawable/p24"
android:duration="150"/>
<item
android:drawable="@drawable/t9"
android:duration="200"/>
<item
android:drawable="@drawable/p19"
android:duration="150"/>
<item
android:drawable="@drawable/t3"
android:duration="200"/>
<item
android:drawable="@drawable/p28"
android:duration="150"/>
<item
android:drawable="@drawable/t10"
android:duration="200"/>
<item
android:drawable="@drawable/p31"
android:duration="150"/>
<item
android:drawable="@drawable/t7"
android:duration="200"/>
<item
android:drawable="@drawable/p23"
android:duration="150"/>
<item
android:drawable="@drawable/t4"
android:duration="200"/>
<item
android:drawable="@drawable/p35"
android:duration="150"/>
<item
android:drawable="@drawable/t8"
android:duration="200"/>
<item
android:drawable="@drawable/p6"
android:duration="150"/>
<item
android:drawable="@drawable/t2"
android:duration="200"/>
<item
android:drawable="@drawable/p24"
android:duration="150"/>
<item
android:drawable="@drawable/t9"
android:duration="200"/>
<item
android:drawable="@drawable/p35"
android:duration="150"/>
<item
android:drawable="@drawable/t4"
android:duration="200"/>
<item
android:drawable="@drawable/p5"
android:duration="150"/>
<item
android:drawable="@drawable/t6"
android:duration="200"/>
<item
android:drawable="@drawable/p6"
android:duration="150"/>
<item
android:drawable="@drawable/t3"
android:duration="200"/>
<item
android:drawable="@drawable/p21"
android:duration="150"/>
<item
android:drawable="@drawable/t10"
android:duration="200"/>
<item
android:drawable="@drawable/p32"
android:duration="150"/>
<item
android:drawable="@drawable/t5"
android:duration="200"/>
</animation-list>
final ImageView imageViewMeasurement = (ImageView) findViewById(measurement_image_view);
imageViewMeasurement.postDelayed(new Runnable(){
@Override
public void run() {
imageViewMeasurement.setImageResource(R.color.colorGreyMeasuerementScreen);
Log.d("QuantifyDesign", "1. MeasurementScreen Default");
}}
,0);
imageViewMeasurement.postDelayed(new Runnable(){
@Override
public void run() {
TypedArray images = getResources().obtainTypedArray(R.array.images_primes_1);
int chosenImageNumber = (int) (Math.random() * images.length());
// setImageResource to the random chosenImageNumber
imageViewMeasurement.setImageResource(images.getResourceId(chosenImageNumber, R.color.colorGreyMeasuerementScreen));
images.recycle();
// Confirmation if the random generator picked a Number from the array
String chosenImageNumberTest = String.valueOf(chosenImageNumber);
Log.d("QuantifyDesignPrime", chosenImageNumberTest);
Log.d("QuantifyDesign", "2. MeasurementScreen Prime 16");
}}
,5000);
imageViewMeasurement.postDelayed(new Runnable(){
@Override
public void run() {
imageViewMeasurement.setImageResource(R.color.colorBlackMeasurementScreen);
Log.d("QuantifyDesign", "3. MeasurementScreen Black Screen");
}}
,5750);
imageViewMeasurement.postDelayed(new Runnable(){
@Override
public void run() {
TypedArray images = getResources().obtainTypedArray(R.array.images_target);
int chosenImageNumber = (int) (Math.random() * images.length());
// setImageResource to the random chosenImageNumber
imageViewMeasurement.setImageResource(images.getResourceId(chosenImageNumber, R.color.colorGreyMeasuerementScreen));
images.recycle();
// Confirmation if the random generator picked a Number from the array
String chosenImageNumberTest = String.valueOf(chosenImageNumber);
Log.d("QuantifyDesignTarget", chosenImageNumberTest);
Log.d("QuantifyDesign", "4. MeasurementScreen Target 1");
}}
,6000);
imageViewMeasurement.postDelayed(new Runnable(){
@Override
public void run() {
imageViewMeasurement.setImageResource(R.drawable.buttonklickmeasurement);
Button resetButtonLike=(Button)findViewById(R.id.button_like);
resetButtonLike.setVisibility(View.VISIBLE); //To set visible
Button resetButtonDislike=(Button)findViewById(R.id.button_dislike);
resetButtonDislike.setVisibility(View.VISIBLE); //To set visible
Log.d("QuantifyDesign", "5. MeasurementScreen Apell");
}}
,7000);