Android 滑动图像

发布于 2024-11-28 09:10:29 字数 3677 浏览 0 评论 0原文

我正在尝试找到一种在 Android 应用程序中滑动图像的方法。我找到了如何使用 textview 执行此操作的示例,但我无法使用 ImageView 执行相同的操作。

HorizontalPager realViewSwitcher = new HorizontalPager(getApplicationContext());

// Add some views to it
final int[] backgroundColors =
        { Color.RED, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW };
for (int i = 0; i < 5; i++) {
    TextView textView = new TextView(getApplicationContext());
    textView.setText(Integer.toString(i + 1));
    textView.setTextSize(100);
    textView.setTextColor(Color.BLACK);
    textView.setGravity(Gravity.CENTER);
    textView.setBackgroundColor(backgroundColors[i]);
    realViewSwitcher.addView(textView);
}

// set as content view
setContentView(realViewSwitcher);

这里 是 TextView 的原始代码:是我尝试使用 ImageViews 执行的代码:

   HorizontalPager realViewSwitcher = new HorizontalPager(getApplicationContext());
    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("one", R.drawable.one);
    map.put("two", R.drawable.two);
    map.put("three", R.drawable.three);

    img1.setImageResource(map.get("one"));
    img2.setImageResource(map.get("two"));
    img3.setImageResource(map.get("three"));
    realViewSwitcher.addView(img1);
    realViewSwitcher.addView(img2);
    realViewSwitcher.addView(img3);

    setContentView(realViewSwitcher);

图像一、二、三位于可绘制文件夹中。当我使用代码时,它会抛出异常:

LogCat:

08-09 09:21:24.097: ERROR/AndroidRuntime(743): Uncaught handler: thread main exiting due to uncaught exception
08-09 09:21:24.115: ERROR/AndroidRuntime(743): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stampii.stampii/com.stampii.stampii.cards.Cards}: java.lang.NullPointerException
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.os.Looper.loop(Looper.java:123)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at java.lang.reflect.Method.invokeNative(Native Method)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at java.lang.reflect.Method.invoke(Method.java:521)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at dalvik.system.NativeStart.main(Native Method)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): Caused by: java.lang.NullPointerException
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at com.stampii.stampii.cards.Cards.onCreate(Cards.java:30)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     ... 11 more

有什么想法如何让这些东西正常工作吗?

I'm trying to find a way to swipe images in my android application.I find a example of how to do that with textview,but I can't do the same thing with ImageView.Here is the original code with TextView :

HorizontalPager realViewSwitcher = new HorizontalPager(getApplicationContext());

// Add some views to it
final int[] backgroundColors =
        { Color.RED, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW };
for (int i = 0; i < 5; i++) {
    TextView textView = new TextView(getApplicationContext());
    textView.setText(Integer.toString(i + 1));
    textView.setTextSize(100);
    textView.setTextColor(Color.BLACK);
    textView.setGravity(Gravity.CENTER);
    textView.setBackgroundColor(backgroundColors[i]);
    realViewSwitcher.addView(textView);
}

// set as content view
setContentView(realViewSwitcher);

and here is the code that I'm trying to do with ImageViews :

   HorizontalPager realViewSwitcher = new HorizontalPager(getApplicationContext());
    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("one", R.drawable.one);
    map.put("two", R.drawable.two);
    map.put("three", R.drawable.three);

    img1.setImageResource(map.get("one"));
    img2.setImageResource(map.get("two"));
    img3.setImageResource(map.get("three"));
    realViewSwitcher.addView(img1);
    realViewSwitcher.addView(img2);
    realViewSwitcher.addView(img3);

    setContentView(realViewSwitcher);

The images one,two,three are in drawable folder.When I use my code it's throwing me an Exception :

LogCat:

08-09 09:21:24.097: ERROR/AndroidRuntime(743): Uncaught handler: thread main exiting due to uncaught exception
08-09 09:21:24.115: ERROR/AndroidRuntime(743): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stampii.stampii/com.stampii.stampii.cards.Cards}: java.lang.NullPointerException
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.os.Looper.loop(Looper.java:123)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at java.lang.reflect.Method.invokeNative(Native Method)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at java.lang.reflect.Method.invoke(Method.java:521)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at dalvik.system.NativeStart.main(Native Method)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): Caused by: java.lang.NullPointerException
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at com.stampii.stampii.cards.Cards.onCreate(Cards.java:30)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-09 09:21:24.115: ERROR/AndroidRuntime(743):     ... 11 more

Any ideas how to get the things to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉默的熊 2024-12-05 09:10:29

尝试一下并报告回来

HorizontalPager realViewSwitcher = new HorizontalPager(getApplicationContext());

ImageView img1 = new ImageView(getApplicationContext());
ImageView img2 = new ImageView(getApplicationContext());
ImageView img3 = new ImageView(getApplicationContext());

img1.setImageResource(R.drawable.one);
img2.setImageResource(R.drawable.two);
img3.setImageResource(R.drawable.three);

realViewSwitcher.addView(img1);
realViewSwitcher.addView(img2);
realViewSwitcher.addView(img3);

setContentView(realViewSwitcher); 

Try this and report back please

HorizontalPager realViewSwitcher = new HorizontalPager(getApplicationContext());

ImageView img1 = new ImageView(getApplicationContext());
ImageView img2 = new ImageView(getApplicationContext());
ImageView img3 = new ImageView(getApplicationContext());

img1.setImageResource(R.drawable.one);
img2.setImageResource(R.drawable.two);
img3.setImageResource(R.drawable.three);

realViewSwitcher.addView(img1);
realViewSwitcher.addView(img2);
realViewSwitcher.addView(img3);

setContentView(realViewSwitcher); 
风苍溪 2024-12-05 09:10:29

对于滑动图像,我们可以简单地使用 ViewPager 概念。

1.在布局中设置viewpager

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>

public class MyMain extends Activity {

         @Override public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); setContentView(R.layout.main);

             ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
              ImagePagerAdapter adapter = new ImagePagerAdapter();
              viewPager.setAdapter(adapter);
           }

        private class ImagePagerAdapter extends PagerAdapter {
             private int[] mImages = new int[] {
                   R.drawable.one,
                   R.drawable.two,
                   R.drawable.hree,
                   R.drawable.four
                       };

                     @Override
                   public int getCount() {
                   return mImages.length;
                     }

         @Override
           public boolean isViewFromObject(View view, Object object) {
              return view == ((ImageView) object);
                 }

           @Override
           public Object instantiateItem(ViewGroup container, int position) {
                Context context = MyMain.this;
              ImageView imageView = new ImageView(context);
           int padding =context.getResources().  
                getDimensionPixelSize(R.dimen.padding_medium);
             imageView.setPadding(padding, padding, padding, padding);
             imageView.setScaleType(ImageView.ScaleType.CENTER);
  imageView.setImageResource(mImages[position]);
  ((ViewPager) container).addView(imageView, 0); 

For Swiping images we can simply use ViewPager concept.

1.set the viewpager in your layout

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>

public class MyMain extends Activity {

         @Override public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); setContentView(R.layout.main);

             ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
              ImagePagerAdapter adapter = new ImagePagerAdapter();
              viewPager.setAdapter(adapter);
           }

        private class ImagePagerAdapter extends PagerAdapter {
             private int[] mImages = new int[] {
                   R.drawable.one,
                   R.drawable.two,
                   R.drawable.hree,
                   R.drawable.four
                       };

                     @Override
                   public int getCount() {
                   return mImages.length;
                     }

         @Override
           public boolean isViewFromObject(View view, Object object) {
              return view == ((ImageView) object);
                 }

           @Override
           public Object instantiateItem(ViewGroup container, int position) {
                Context context = MyMain.this;
              ImageView imageView = new ImageView(context);
           int padding =context.getResources().  
                getDimensionPixelSize(R.dimen.padding_medium);
             imageView.setPadding(padding, padding, padding, padding);
             imageView.setScaleType(ImageView.ScaleType.CENTER);
  imageView.setImageResource(mImages[position]);
  ((ViewPager) container).addView(imageView, 0); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文