我有一些试图加载到屏幕上的图像。但是,除了我制作的每个新Image的onload函数不是唯一的之外,它们都位于同一Image上,从而导致相同的绘制。
我尝试将其放置在数组之外,但是循环运行得太快了,并且在加载第一个图像时,图像源已经在最后一个图像上,因此它只打印出了三个相同的图像。这个数组是我最好的主意。
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "package name"
minSdkVersion 21
targetSdkVersion 28
versionCode 2
versionName "2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation'com.android.support.test.espresso:espressocore:3.0.2'
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation "org.jetbrains.anko:anko-commons:$anko_version"
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.facebook.shimmer:shimmer:0.4.0'
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.skyfishjy.ripplebackground:library:1.0.1'
implementation 'com.sasank.roundedhorizontalprogress:roundedhorizontalprogress:1.0.1'
implementation 'com.google.firebase:firebase-auth:16.2.0'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.tomer:fadingtextview:2.5'
implementation 'com.google.firebase:firebase-ads:18.0.0'
implementation 'com.google.android.gms:play-services-ads:18.0.0'
}
apply plugin: 'com.google.gms.google-services'
我希望function drawRoute(holdCollection) {
if (holdCollection != null) {
holdImageArray = [];
holdCollection.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
holdData = doc.data();
console.log(holdData); // prints out different data each time
// put new image on array and get the index of that image
index = holdImageArray.push(new Image()) - 1;
holdImageArray[index].src = holdData.model;
// set function to be called when image is loaded
holdImageArray[index].onload = drawHold(holdImageArray[index]); // draws the same hold each time
});
});
console.log(holdImageArray[0]===holdImageArray[1]); //returns true
}
}
中的每个图像都是对唯一图像对象的引用。这些中的每一个都应使用其各自的图像引用作为参数来调用drawHold函数。抽拉保持按预期方式工作。它只是将其绘制到HTML5画布上。唯一的问题是最后一个console.log在forEach调用中的任何console.logs之前返回holdImageArray
。
此问题被标记为重复。它不是重复的,因为我知道在调用onload函数之后我的代码将继续执行。那不是问题。我将发布问题答案以证明这一点。