【问题标题】:Using AsyncTask to load a lot of images on one layout?使用 AsyncTask 在一个布局上加载大量图像?
【发布时间】:2014-07-29 16:22:35
【问题描述】:

我想知道如何使用 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 的尝试也几乎总是很痛苦。在这里学习你的想法。谢谢。

标签: java android xml eclipse android-asynctask


【解决方案1】:

创建一个扩展 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();

【讨论】:

    【解决方案2】:

    我真的建议你检查Picasso 这是一个图像库,很好地实现了性能和内存管理。哦,使用和学习也是小菜一碟!

    简单地实现一个 AsyncTask 来加载将完成一小部分工作,比如内存管理、调整大小、裁剪和其他与图像相关的东西,这些东西可能会在未来出现!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多