gpt4 book ai didi

java - 使用 AsyncTask 在一个布局上加载大量图像?

转载 作者:行者123 更新时间:2023-11-29 21:04:21 25 4
gpt4 key购买 nike

我想知道如何使用 AsyncTask 或任何有助于在一个布局中加载大量图像(近 30 张图像)的元素。因为应用程序打不开它总是触发 ANR

这是我的 xml,但不包括所有图像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="90dp"
android:src="@drawable/hk2fm" />

<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000" />

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="10dip"
android:text="Weapons"
android:textSize="20sp"
android:textStyle="bold"
/>

<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="136dip"
android:layout_gravity="center" >

<LinearLayout
android:layout_width="150dip"
android:layout_height="156dip"
android:layout_gravity="center"
android:orientation="horizontal" >

<ImageButton
android:id="@+id/staff"
android:layout_width="90dip"
android:layout_height="90dip"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:src="@drawable/magestaff"
android:background="@android:color/transparent" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />

<ImageButton
android:id="@+id/images1"
android:layout_width="140dip"
android:layout_height="140dip"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:src="@drawable/obok"
android:background="@android:color/transparent" />

<ImageButton
android:id="@+id/shield"
android:layout_width="90dip"
android:layout_height="90dip"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/knight_shield" />

</LinearLayout>
</HorizontalScrollView>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enemies"
android:layout_gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:paddingTop="10dp"
/>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="136dip"
android:layout_gravity="center" >

<LinearLayout
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_gravity="center"
android:orientation="horizontal" >

<ImageButton
android:id="@+id/heart"
android:layout_width="90dip"
android:layout_height="90dip"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/heart" />

<ImageButton
android:id="@+id/xiii"
android:layout_width="140dip"
android:layout_height="140dip"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:src="@drawable/xiii2"
android:background="@android:color/transparent" />

<ImageButton
android:id="@+id/imagesn"
android:layout_width="90dip"
android:layout_height="90dip"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/imagesn" />

</LinearLayout>
</HorizontalScrollView>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accessories"
android:layout_gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:paddingTop="10dip"
/>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="136dip"
android:layout_gravity="center" >

<LinearLayout
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_gravity="center"
android:orientation="horizontal" >

<ImageButton
android:id="@+id/armor"
android:layout_width="90dip"
android:layout_height="90dip"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/buster_band" />

<ImageButton
android:id="@+id/xiii2"
android:layout_width="140dip"
android:layout_height="140dip"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:src="@drawable/xiii2"
android:background="@android:color/transparent" />

<ImageButton
android:id="@+id/accessories"
android:layout_width="90dip"
android:layout_height="90dip"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/cosmic_arts" />

</LinearLayout>
</HorizontalScrollView>

</LinearLayout>
</ScrollView>
</LinearLayout>

这是我的 java:

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

public class hk2fm extends Activity {
private ImageButton imgButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.hk2fm2);
final MediaPlayer buttonSound = MediaPlayer.create(hk2fm.this, R.raw.btnclick);
ImageButton staff = (ImageButton) findViewById(R.id.staff);
ImageButton accessories = (ImageButton) findViewById(R.id.accessories);
ImageButton armor = (ImageButton) findViewById(R.id.armor);
ImageButton heart = (ImageButton) findViewById(R.id.heart);
ImageButton images1 = (ImageButton) findViewById(R.id.images1);
ImageButton imagesn = (ImageButton) findViewById(R.id.imagesn);
ImageButton shield = (ImageButton) findViewById(R.id.shield);
ImageButton xiii = (ImageButton) findViewById(R.id.xiii);

staff.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
v.setBackgroundColor(Color.parseColor("#FFCC33"));
startActivity(new Intent("com.example.hkguide.INFO"));

}
});

accessories.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
v.setBackgroundColor(Color.parseColor("#FFCC33"));
startActivity(new Intent("com.example.hkguide.INFO"));

}
});

armor.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
v.setBackgroundColor(Color.parseColor("#FFCC33"));
startActivity(new Intent("com.example.hkguide.INFO"));

}
});

heart.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
v.setBackgroundColor(Color.parseColor("#FFCC33"));
startActivity(new Intent("com.example.hkguide.INFO"));

}
});

images1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
v.setBackgroundColor(Color.parseColor("#FFCC33"));
startActivity(new Intent("com.example.hkguide.INFO"));

}
});

imagesn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
v.setBackgroundColor(Color.parseColor("#FFCC33"));
startActivity(new Intent("com.example.hkguide.INFO"));

}
});

shield.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
v.setBackgroundColor(Color.parseColor("#FFCC33"));
startActivity(new Intent("com.example.hkguide.INFO"));

}
});

xiii.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
v.setBackgroundColor(Color.parseColor("#FFCC33"));
startActivity(new Intent("com.example.hkguide.INFO"));

}
});
}

}

最佳答案

创建一个扩展 AsyncTask 的类并将您的异步方法放在 doInBackground(...) 中,如下所示:

private classImageUpload extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
//your upload code here
return "Executed";
}

@Override
protected void onPostExecute(String result) {}

@Override
protected void onPreExecute() {}

@Override
protected void onProgressUpdate(Void... values) {}

}然后像这样在 oncreate 方法中调用您的 asyncTask:

new ImageUpload().execute();

关于java - 使用 AsyncTask 在一个布局上加载大量图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25020205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com