我想知道如何使用AsyncTask或任何有助于在一个布局中加载大量图像(近30个图像)的eles。 因为应用程序不会打开它总是会触发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"));
}
});
}
}
答案 0 :(得分:1)
创建一个扩展AsyncTask的类,并将async方法放在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();
答案 1 :(得分:1)
我真的建议你查看Picasso 它是一个图像库,很好地实现了性能和内存管理。哦,还有一块蛋糕可供使用和学习!
简单地实现AsyncTask加载将完成一小部分工作,如内存管理,调整大小,裁剪和其他可能出现的图像相关内容!