【问题标题】:Android image gallery is very slowAndroid图片库很慢
【发布时间】:2016-11-17 12:45:31
【问题描述】:

我从 android 开始,并在网上学习了一些教程。我正在尝试创建一个 android 画廊图像并且它可以工作,但前提是我放入可绘制文件夹的图像很小。如果我在其中放入更大尺寸和更好质量的图像,我的画廊会变得非常慢。我该怎么办?

主要活动:

public class MainActivity extends AppCompatActivity {

@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            Toast.makeText(MainActivity.this, "" + position,
                    Toast.LENGTH_SHORT).show();
        }
    });
}

图像适配器:

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(4, 4, 4, 4);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}



// references to our images
private Integer[] mThumbIds = {



        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};

感谢所有帮助我的人。

【问题讨论】:

  • I'm trying to create an android gallery image。你是什​​么意思?没有画廊。只有一个图库应用。
  • 我的意思是我想用一些图像创建一个网格,当我使用放入可绘制文件夹的高质量图像时,我的应用程序运行速度非常慢。
  • 添加到 Manifest android:largeHeap="true"
  • 您是否在同一个线程中加载图像?

标签: java android android-studio android-gallery photo-gallery


【解决方案1】:

欢迎来到安卓世界。

您应该尝试使用ViewHolder 模式。

请看一下这个教程。

good tutorial for viewholder pattern

【讨论】:

    【解决方案2】:

    你只需要加载这些图像的缩略图并将它们加载到网格中,阅读更多关于在互联网和教程上创建和缓存缩略图的内容。当用户单击该缩略图即特定网格位置时,你应该加载原始图像并显示it.give at picasso library,会帮助你的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 2021-07-21
      • 2015-09-06
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多