我创建了一个Gallery
,如下所示:
MainActivity:
public class MainActivity extends Activity {
Gallery gallery;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (Gallery) findViewById(R.id.gallery1);
imageView = (ImageView) findViewById(R.id.imageView1);
gallery.setAdapter(new ImageAdapter(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ImageAdapter.java:
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return thumbsIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = null;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(115, 100));
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(thumbsIds[position]);
return imageView;
}
public Integer[] thumbsIds = {
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d
};
}
和activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
但是当我启动我的模拟器时,我收到了这个例外:
10-18 13:32:28.107: E/AndroidRuntime(715): FATAL EXCEPTION: main
10-18 13:32:28.107: E/AndroidRuntime(715): java.lang.OutOfMemoryError
10-18 13:32:28.107: E/AndroidRuntime(715): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.content.res.Resources.loadDrawable(Resources.java:1937)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.content.res.Resources.getDrawable(Resources.java:664)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.ImageView.resolveUri(ImageView.java:542)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.ImageView.setImageResource(ImageView.java:315)
10-18 13:32:28.107: E/AndroidRuntime(715): at com.example.gallerydemo.ImageAdapter.getView(ImageAdapter.java:46)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.Gallery.makeAndAddView(Gallery.java:844)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.Gallery.fillToGalleryRightLtr(Gallery.java:798)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.Gallery.fillToGalleryRight(Gallery.java:742)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.Gallery.layout(Gallery.java:651)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.Gallery.onLayout(Gallery.java:346)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.view.View.layout(View.java:11180)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.view.ViewGroup.layout(ViewGroup.java:4203)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.view.View.layout(View.java:11180)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.view.ViewGroup.layout(ViewGroup.java:4203)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.view.View.layout(View.java:11180)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.view.ViewGroup.layout(ViewGroup.java:4203)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.view.View.layout(View.java:11180)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.view.ViewGroup.layout(ViewGroup.java:4203)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.view.View.layout(View.java:11180)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.view.ViewGroup.layout(ViewGroup.java:4203)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1468)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.os.Looper.loop(Looper.java:137)
10-18 13:32:28.107: E/AndroidRuntime(715): at android.app.ActivityThread.main(ActivityThread.java:4340)
10-18 13:32:28.107: E/AndroidRuntime(715): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 13:32:28.107: E/AndroidRuntime(715): at java.lang.reflect.Method.invoke(Method.java:511)
10-18 13:32:28.107: E/AndroidRuntime(715): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-18 13:32:28.107: E/AndroidRuntime(715): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-18 13:32:28.107: E/AndroidRuntime(715): at dalvik.system.NativeStart.main(Native Method)
应用不会自行启动。它会立即崩溃。
答案 0 :(得分:0)
此链接可以使用Asynctask帮助并在单独的线程中加载图像以实现平滑滚动。 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html